View Javadoc

1   /* ARCReaderFactoryTest.java
2    *
3    * $Id: ARCReaderFactoryTest.java 4512 2006-08-19 00:22:10Z stack-sf $
4    *
5    * Created Jul 15, 2005
6    *
7    * Copyright (C) 2005 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  package org.archive.io.arc;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  import java.util.Iterator;
32  
33  import org.archive.util.TmpDirTestCase;
34  
35  public class ARCReaderFactoryTest extends TmpDirTestCase {
36  //    public void testGetHttpURL() throws MalformedURLException, IOException {
37  //        ARCReader reader = null;
38  //        try {
39  //            // TODO: I can get a single ARCRecord but trying to iterate from
40  //            // a certain point is getting an EOR when I go to read GZIP header.
41  //            reader = ARCReaderFactory.
42  //                get(new URL("http://localhost/test.arc.gz"), 0);
43  //            for (final Iterator i = reader.iterator(); i.hasNext();) {
44  //                ARCRecord ar = (ARCRecord)i.next();
45  //                System.out.println(ar.getMetaData().getUrl());
46  //            }
47  //        } finally {
48  //            if (reader != null) {
49  //                reader.close();
50  //            }
51  //        }
52  //    }
53      
54      /***
55       * Test File URL.
56       * If a file url, we just use the pointed to file.  There is no
57       * copying down to a file in tmp that gets cleaned up after close.
58       * @throws MalformedURLException
59       * @throws IOException
60       */
61      public void testGetFileURL() throws MalformedURLException, IOException {
62          File arc = ARCWriterTest.createARCFile(getTmpDir(), true);
63          doGetFileUrl(arc);
64      }
65      
66      protected void doGetFileUrl(File arc)
67      throws MalformedURLException, IOException {
68          ARCReader reader = null;
69          File tmpFile = null;
70          try {
71              reader = ARCReaderFactory.
72                  get(new URL("file:////" + arc.getAbsolutePath()));
73              tmpFile = null;
74              for (Iterator i = reader.iterator(); i.hasNext();) {
75                  ARCRecord r = (ARCRecord)i.next();
76                  if (tmpFile == null) {
77                      tmpFile = new File(r.getMetaData().getArc());
78                  }
79              }
80              assertTrue(tmpFile.exists());
81          } finally {
82              if (reader != null) {
83                  reader.close();
84              }
85          }
86          assertTrue(tmpFile.exists());
87      }
88      
89      /***
90       * Test path or url.
91       * @throws MalformedURLException 
92       * @throws IOException 
93       */
94      public void testGetPathOrURL() throws MalformedURLException, IOException {
95          File arc = ARCWriterTest.createARCFile(getTmpDir(), true);
96          ARCReader reader = ARCReaderFactory.get(arc.getAbsoluteFile());
97          assertNotNull(reader);
98          reader.close();
99          doGetFileUrl(arc);
100     }   
101 }