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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Steinar Knutsen
*/
public class DefaultErrorHitTestCase {
private static final String SOURCE = "nalle";
DefaultErrorHit de;
@Before
public void setUp() throws Exception {
de = new DefaultErrorHit(SOURCE, ErrorMessage.createUnspecifiedError("DefaultErrorHitTestCase"));
}
@Test
public void testSetSourceTakeTwo() {
assertEquals(SOURCE, de.getSource());
de.setSource(null);
assertNull(de.getSource());
de.setSource("bamse");
assertEquals("bamse", de.getSource());
de.addError(ErrorMessage.createBackendCommunicationError("blblbl"));
final Iterator<ErrorMessage> errorIterator = de.errorIterator();
assertEquals(SOURCE, errorIterator.next().getSource());
assertEquals("bamse", errorIterator.next().getSource());
}
@Test
public void testToString() {
assertEquals("Error: Source 'nalle': 5: Unspecified error: DefaultErrorHitTestCase", de.toString());
}
@Test
public void testSetMainError() {
ErrorMessage e = ErrorMessage.createBackendCommunicationError("abc");
assertNull(e.getSource());
de.addError(e);
assertEquals(SOURCE, e.getSource());
boolean caught = false;
try {
new DefaultErrorHit(SOURCE, (ErrorMessage)null);
} catch (NullPointerException ex) {
caught = true;
}
assertTrue(caught);
caught = false;
try {
de.addError(null);
} catch (NullPointerException ex) {
caught = true;
}
assertTrue(caught);
}
@Test
public void testAddError() {
ErrorMessage e = ErrorMessage.createBackendCommunicationError("ljkhlkjh");
assertNull(e.getSource());
de.addError(e);
assertEquals(SOURCE, e.getSource());
e = ErrorMessage.createBadRequest("kdjfhsdkfhj");
de.addError(e);
int i = 0;
for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
++i;
}
assertEquals(3, i);
}
@Test
public void testAddErrors() {
DefaultErrorHit other = new DefaultErrorHit("abc", ErrorMessage.createBadRequest("sdasd"));
de.addErrors(other);
int i = 0;
for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
++i;
}
assertEquals(2, i);
other = new DefaultErrorHit("abd", ErrorMessage.createEmptyDocsums("uiyoiuy"));
other.addError(ErrorMessage.createNoAnswerWhenPingingNode("xzvczx"));
de.addErrors(other);
i = 0;
for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
++i;
}
assertEquals(4, i);
}
@Test
public void testHasOnlyErrorCode() {
assertTrue(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
assertFalse(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.BACKEND_COMMUNICATION_ERROR.code));
de.addError(ErrorMessage.createUnspecifiedError("dsfsdfs"));
assertTrue(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
assertEquals(com.yahoo.container.protect.Error.UNSPECIFIED.code, de.errors().iterator().next().getCode());
de.addError(ErrorMessage.createBackendCommunicationError("dsfsdfsd"));
assertFalse(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
}
}
|