/
WarcRecordClientTester.java
executable file
·289 lines (231 loc) · 10.5 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
286
287
288
289
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 {
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);
}
@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 reading .warc record from offset 3442L
@Test
public void testBuildingBitarchiveRecord1() 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;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
}
assertNotNull(warcRecord);
}
// Test reading .warc record from offset 0L
@Test
public void testBuildingBitarchiveRecord2() 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 = 0L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
}
assertNotNull(warcRecord);
}
// 1.st Test reading .arc record, offfset 0L
@Test
public void testBuildingBitarchiveRecord3() throws Exception {
String filename = "42-23-20060726143926-00000-udvikling.kb.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 0L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
}
assertNotNull(warcRecord);
}
// 2.nd Test for .arc records, offset 0L
@Test
public void testPosBuildingBitarchiveRecord4() throws Exception {
String filename = "91-7-20100212214140-00000-sb-test-har-001.statsbiblioteket.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 0L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (NullPointerException e) {
System.out.println("Nullpointer Exception caused by offset errror");
}
assertNotNull(warcRecord);
}
@Test
public void testPosBuildingBitarchiveRecord5() throws Exception {
String filename = "42-23-20060726143926-00000-udvikling.kb.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 682L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNotNull(warcRecord);
}
// 3.rd test for .arc record at correct positive offset, but no body content
@Test
public void testPosBuildingBitarchiveRecord6() throws Exception {
String filename = "42-23-20060726143926-00000-udvikling.kb.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 789L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNotNull(warcRecord);
}
// ************** Negative tests ****************
// test read non-existing .arc record from offset 0L
@Test
public void testBuildingBitarchiveRecord7() throws Exception {
String filename = "2-2-20060731110420-00000-sb-test-har-001.statsbiblioteket.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 0L;
BitarchiveRecord arcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
arcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
arcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNull(arcRecord);
}
// test reading .arc record from positive invalid offset 4000L
@Test
public void testBuildingBitarchiveRecord8() throws Exception {
String filename = "42-23-20060726143926-00000-udvikling.kb.dk.arc.gz";
URI SAMPLE_HOST = new URI("http://localhost:8883/cgi-bin2/py1.cgi/" + filename);
URI test_uri = SAMPLE_HOST;
long offset = 4000L;
BitarchiveRecord arcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
arcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
arcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNull(arcRecord);
}
// Test reading .warc record from invalid offset 5000L
@Test
public void testBuildingBitarchiveRecord9() 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 = 5000L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNull(warcRecord);
}
@Test
public void testBuildingBitarchiveRecord10() 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 = 2L;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by offset errror ");
}
assertNull(warcRecord);
}
// Testing for file that is not in our environment
@Test
public void testBuildingBitarchiveRecord11() 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;
BitarchiveRecord warcRecord = null;
WarcRecordClient warcRecordClient = new WarcRecordClient(test_uri);
try {
warcRecord = warcRecordClient.getWarc(SAMPLE_HOST, offset);
warcRecord.getData(System.out);
} catch (Exception e) {
System.out.println("Nullpointer Exception caused by non-existing file ");
}
assertNull(warcRecord);
}
}