-
Notifications
You must be signed in to change notification settings - Fork 23
/
WarcRecordClientTester.java
285 lines (255 loc) · 12.4 KB
/
WarcRecordClientTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package dk.netarkivet.common.utils.warc;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.io.IOUtils;
import org.archive.io.ArchiveReader;
import org.archive.io.ArchiveRecord;
import org.archive.io.arc.ARCReaderFactory;
import org.archive.io.arc.ARCRecord;
import org.archive.io.warc.WARCReader;
import org.archive.io.warc.WARCReaderFactory;
import org.archive.io.warc.WARCRecord;
import org.junit.Test;
import dk.netarkivet.common.distribute.arcrepository.BitarchiveRecord;
import static org.junit.Assert.*;
public class WarcRecordClientTester {
/**
* Tests that basic data retrieval function by issuing a get request.
* @throws Exception
*/
// **************** Positive tests ***************
@Test
public void testGet() throws Exception {
for (int i=0; i < 40; i++) {
final URI baseUri = new URI("http://localhost:8883/cgi-bin2/py1.cgi");
WarcRecordClient warcRecordClient = new WarcRecordClient(baseUri);
BitarchiveRecord bitarchiveRecord = warcRecordClient
.get("10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc.gz", 3442L);
assertNotNull("Should have non null BitarchiveRecord", bitarchiveRecord);
assertTrue("Expect a non-zero length bitarchiveRecord",
IOUtils.toByteArray(bitarchiveRecord.getData()).length > 100);
System.out.println(i);
}
}
// test read first existing warc record from file thisisa.warc
@Test
public void testBuildingBitarchiveRecord() throws IOException {
String filename = "thisisa.warc";
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
ArchiveReader archiveReader = WARCReaderFactory.get(filename, fileInputStream, true);
ArchiveRecord archiveRecord = archiveReader.get();
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
}
// test read existing warc record from offset 0 of file thisisa.warc
@Test
public void testBuildingBitarchiveRecord2() throws IOException {
String filename = "thisisa.warc";
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
ArchiveReader archiveReader = WARCReaderFactory.get(filename, fileInputStream, true);
ArchiveRecord archiveRecord = archiveReader.get();
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
}
/*
@Test
public void testBuildingBitarchiveRecord3() throws Exception {
String filename = "10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
long offset = 3442L;
URI test_uri = SAMPLE_HOST;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
ArchiveReader archiveReader = WARCReaderFactory.get(filename, fileInputStream, true);
ArchiveRecord archiveRecord = archiveReader.get();
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
}
*/
@Test
public void testBuildingBitarchiveRecord5() throws Exception {
String filename = "10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 3442L;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
Boolean fail = false;
try {
BitarchiveRecord bitarchiveRecord = warcRecordClient.get(filename, offset);
bitarchiveRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
fail = true;
}
assertFalse("Exception", fail);
}
//@Test
public void testPosBuildingBitarchiveRecord6() throws Exception {
String filename = "2-2-20060731110420-00000-sb-test-har-001.statsbiblioteket.dk.arc";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 3442L;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
Boolean fail = false;
try {
BitarchiveRecord bitarchiveRecord = warcRecordClient.get(filename, offset);
bitarchiveRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
fail = true;
}
assertFalse("Exception", fail);
}
// ************** Negative tests ****************
// test read existing arc record from offset 0 -Server Service Not Yet Implemented
@Test
public void testBuildingBitarchiveRecord7() throws IOException {
String filename = "2-2-20060731110420-00000-sb-test-har-001.statsbiblioteket.dk.arc";
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
Boolean fail = false;
try {
ARCRecord archiveRecord = new ARCRecord(fileInputStream, filename, 0L, false, false, true);
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
} catch (Exception e) {
System.out.println(("Error in reading arc record"));
fail = true;
}
assertTrue("Exception", fail);
}
// Test for offset not atFirstRecord OK
// test read existing arc record from offset 5000
@Test
public void testBuildingBitarchiveRecord001() throws Exception {
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc");
URI test_uri = SAMPLE_HOST;
long offset = 5000L; // 3442L; //5000L;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
Boolean fail = false;
try {
BitarchiveRecord bitarchiveRecord = warcRecordClient.get("10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc.gz", offset);
bitarchiveRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
fail = true;
}
assertTrue("Exception", fail);
}
@Test
public void testBuildingBitarchiveRecord002() throws IOException {
String filename = "2-2-20060731110420-00000-sb-test-har-001.statsbiblioteket.dk.arc";
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
Boolean fail = false;
try {
ARCRecord archiveRecord = new ARCRecord(fileInputStream, filename, 4000L, false, false, true);
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
} catch (Exception e) {
System.out.println(("Error in reading arc record"));
fail = true;
}
assertTrue("Exception", fail);
}
// Testing for invalid offset
@Test
public void testFailInBuildingBitarchiveRecord3() throws IOException, URISyntaxException {
String filename = "10-4-20161218234343407-00000-kb-test-har-003.kb.dk.warc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 4000L;
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
Boolean fail = false;
try {
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
}
catch (Exception e) {
System.out.println("Expect NullPointerException: " + e.getMessage());
fail = true; //Boolean.parseBoolean("Expect IOException" + e.getMessage());
}
assertTrue("Exception: ", fail);
}
@Test
public void testFailInBuildingBitarchiveRecord4() throws IOException, URISyntaxException {
String filename = "10-4-20161218234349999-00000-kb-test-har-003.kb.dk.warc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 2L;
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
Boolean fail = false;
try {
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
fail = true; //Boolean.parseBoolean("Expect IOException" + e.getMessage());
}
assertTrue("Exception: ", fail);
}
// Test for file not found
// Testing for file that is not in our environment
@Test
public void testBuildingBitarchiveRecord4() throws Exception {
String filename = "netarkivet-20081105135926-00001.warc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 3442L;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
BitarchiveRecord warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
Boolean fail = false;
try {
BitarchiveRecord bitarchiveRecord = warcRecordClient.get(filename, offset);
bitarchiveRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by File Not Found Error: " + e.getMessage());
fail = true;
}
assertTrue("Exception", fail);
}
// Test using the wrong reader for .arc files
@Test
public void testFailInBuildingBitarchiveRecord1() throws IOException {
String filename = "91-7-20100212214140-00000-sb-test-har-001.statsbiblioteket.dk.arc.gz";
File inputFile = new File("src/test/java/data.txt");
System.out.println(inputFile.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(inputFile);
Boolean fail = false;
try {
ArchiveReader archiveReader = WARCReaderFactory.get(filename, fileInputStream, true);
ArchiveRecord archiveRecord = archiveReader.get();
BitarchiveRecord bitarchiveRecord = new BitarchiveRecord(archiveRecord, filename);
bitarchiveRecord.getData(System.out);
throw new Exception();
}
catch (Exception e) {
fail = true;
System.out.println(Boolean.parseBoolean("Expect IOException: " + e.getMessage()));
}
assertTrue("Exception: ", fail);
}
}