blob: b399b1197dadaa1a276c2a0b90e66d04b763eb3e (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespaxmlparser;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.vespa.http.server.MetaStream;
import com.yahoo.vespa.http.server.util.ByteLimitedInputStream;
import com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation;
import java.io.InputStream;
import java.lang.reflect.Field;
/**
* Mock for ExternalFeedTestCase which had to override package private methods.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class MockReader implements FeedReader {
MetaStream stream;
boolean finished = false;
public MockReader(InputStream stream) throws Exception {
this.stream = getMetaStream(stream);
}
private static MetaStream getMetaStream(InputStream stream) {
if (stream instanceof MetaStream) {
return (MetaStream) stream;
}
if (!(stream instanceof ByteLimitedInputStream)) {
throw new IllegalStateException("Given unknown stream type.");
}
//Ooooooo this is so ugly
try {
ByteLimitedInputStream byteLimitedInputStream = (ByteLimitedInputStream) stream;
Field f = byteLimitedInputStream.getClass().getDeclaredField("wrappedStream"); //NoSuchFieldException
f.setAccessible(true);
return (MetaStream) f.get(byteLimitedInputStream);
} catch (Exception e) {
throw new IllegalStateException("Implementation of ByteLimitedInputStream has changed.", e);
}
}
@Override
public void read(Operation operation) throws Exception {
if (finished) {
return;
}
byte whatToDo = stream.getNextOperation();
DocumentId id = new DocumentId("id:banana:banana::doc1");
DocumentType docType = new DocumentType("banana");
switch (whatToDo) {
case 0:
finished = true;
break;
case 1:
Document doc = new Document(docType, id);
operation.setDocument(doc);
break;
case 2:
operation.setRemove(id);
break;
case 3:
operation.setDocumentUpdate(new DocumentUpdate(docType, id));
break;
case 4:
throw new RuntimeException("boom");
}
}
}
|