|
| 1 | +package org.archive.io.arc; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.RandomAccessFile; |
| 8 | + |
| 9 | +import org.archive.io.ArchiveReader; |
| 10 | +import org.archive.io.ArchiveRecord; |
| 11 | + |
| 12 | +import junit.framework.TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * |
| 16 | + * Based on https://github.com/iipc/openwayback/pull/104/files |
| 17 | + * |
| 18 | + * @author csr@statsbiblioteket.dk (Colin Rosenthal) |
| 19 | + * |
| 20 | + */ |
| 21 | +public class ARCReaderFactoryTest extends TestCase { |
| 22 | + |
| 23 | + private File testfile1 = new File("src/test/resources/org/archive/format/arc/IAH-20080430204825-00000-blackbook-truncated.arc"); |
| 24 | + |
| 25 | + /** |
| 26 | + * Test reading uncompressed arcfile for issue |
| 27 | + * https://github.com/iipc/openwayback/issues/101 |
| 28 | + * @throws Exception |
| 29 | + */ |
| 30 | + public void testGetResource() throws Exception { |
| 31 | + this.offsetResourceTest(testfile1, 1515, "archive.org/robots.txt" ); |
| 32 | + this.offsetResourceTest(testfile1, 36420, "archive.org/services/collection-rss.php" ); |
| 33 | + } |
| 34 | + |
| 35 | + private void offsetResourceTest( File testfile, long offset, String uri ) throws Exception { |
| 36 | + RandomAccessFile raf = new RandomAccessFile(testfile, "r"); |
| 37 | + raf.seek(offset); |
| 38 | + InputStream is = new FileInputStream(raf.getFD()); |
| 39 | + String fPath = testfile.getAbsolutePath(); |
| 40 | + ArchiveReader reader = ARCReaderFactory.get(fPath, is, false); |
| 41 | + // This one works: |
| 42 | + //ArchiveReader reader = ARCReaderFactory.get(testfile, offset); |
| 43 | + ArchiveRecord record = reader.get(); |
| 44 | + System.out.println("Position:"+record.getPosition()); |
| 45 | + |
| 46 | + final String url = record.getHeader().getUrl(); |
| 47 | + System.out.println("Got URL: "+url); |
| 48 | + assertEquals("URL of record is not as expected.", uri, url); |
| 49 | + |
| 50 | + final long position = record.getPosition(); |
| 51 | + final long recordLength = record.getHeader().getLength(); |
| 52 | + System.out.println("Position:"+position); |
| 53 | + System.out.println("Length:"+recordLength); |
| 54 | + assertTrue("Position " + position + " is after end of record " + recordLength, position <= recordLength); |
| 55 | + |
| 56 | + // Clean up: |
| 57 | + if( raf != null ) |
| 58 | + raf.close(); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments