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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.context.test;
import com.yahoo.search.Query;
import com.yahoo.search.query.context.QueryContext;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Steinar Knutsen
*/
public class TraceTestCase {
@Test
void testBasicTracing() {
Query query = new Query();
QueryContext h = query.getContext(true);
h.trace("first message", 0);
h.trace("second message", 0);
assertEquals("trace: [ [ first message second message ] ]", h.toString());
}
@Test
void testCloning() throws IOException {
Query query = new Query();
QueryContext h = query.getContext(true);
h.trace("first message", 0);
QueryContext h2 = query.clone().getContext(true);
h2.trace("second message", 0);
QueryContext h3 = query.clone().getContext(true);
h3.trace("third message", 0);
h.trace("fourth message", 0);
h2.trace("fifth message", 0);
Writer w = new StringWriter();
Writer w2 = new StringWriter();
h2.render(w2);
String finishedBelow = w2.toString();
h.render(w);
String toplevel = w.toString();
// check no matter which QueryContext ends up in the final Result,
// all context info is rendered
assertEquals(toplevel, finishedBelow);
// basic sanity test
assertEquals("trace: [ [ " +
"first message second message third message " +
"fourth message fifth message ] ]", h.toString());
Iterator<String> i = h.getTrace().traceNode().root().descendants(String.class).iterator();
assertEquals("first message", i.next());
assertEquals("second message", i.next());
assertEquals("third message", i.next());
assertEquals("fourth message", i.next());
assertEquals("fifth message", i.next());
}
@Test
void testEmpty() throws IOException {
Query query = new Query();
QueryContext h = query.getContext(true);
Writer w = new StringWriter();
h.render(w);
assertEquals("", w.toString());
}
@Test
void testEmptySubSequence() {
Query query = new Query();
QueryContext h = query.getContext(true);
query.clone().getContext(true);
Writer w = new StringWriter();
try {
h.render(w);
} catch (IOException e) {
assertTrue(false, "rendering empty subsequence crashed");
}
}
@Test
void testAttachedTraces() throws IOException {
String needle0 = "nalle";
String needle1 = "tralle";
String needle2 = "trolle";
String needle3 = "bamse";
Query q = new Query("/?tracelevel=1");
q.trace(needle0, false, 1);
Query q2 = new Query();
q.attachContext(q2);
q2.trace(needle1, false, 1);
q2.trace(needle2, false, 1);
q.trace(needle3, false, 1);
Writer w = new StringWriter();
q.getContext(false).render(w);
String trace = w.toString();
int nalle = trace.indexOf(needle0);
int tralle = trace.indexOf(needle1);
int trolle = trace.indexOf(needle2);
int bamse = trace.indexOf(needle3);
assertTrue(nalle > 0, "Could not find first manual context to main query.");
assertTrue(bamse > 0, "Could not find second manual context to main query.");
assertTrue(tralle > 0, "Could not find first manual context to attached query.");
assertTrue(trolle > 0, "Could not find second manual context to attached query.");
}
}
|