]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/unit/test/tree/UnitTestTreeUtil.java
05c56acb69394c8892157da8b094fd5e87561d95
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / unit / test / tree / UnitTestTreeUtil.java
1 package org.argeo.slc.unit.test.tree;
2
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertNotNull;
5 import static junit.framework.Assert.assertNull;
6 import static junit.framework.Assert.fail;
7
8 import org.argeo.slc.core.structure.SimpleSElement;
9 import org.argeo.slc.core.structure.tree.TreeSPath;
10 import org.argeo.slc.core.test.SimpleResultPart;
11 import org.argeo.slc.core.test.TestResultPart;
12 import org.argeo.slc.core.test.tree.PartSubList;
13 import org.argeo.slc.core.test.tree.TreeTestResult;
14
15 /** Utilities for unit tests. */
16 public class UnitTestTreeUtil {
17 public static void assertTreeTestResult(TreeTestResult expected,
18 TreeTestResult reached) {
19 assertEquals(expected.getTestResultId(), reached.getTestResultId());
20 assertEquals(expected.getCloseDate(), reached.getCloseDate());
21
22 assertEquals(expected.getResultParts().size(), reached.getResultParts()
23 .size());
24 for (TreeSPath path : expected.getResultParts().keySet()) {
25 PartSubList lstExpected = expected.getResultParts().get(path);
26 PartSubList lstReached = expected.getResultParts().get(path);
27 if (lstReached == null) {
28 fail("No result for path " + path);
29 return;
30 }
31 assertPartSubList(lstExpected, lstReached);
32 }
33
34 assertEquals(expected.getElements().size(), reached.getElements()
35 .size());
36 for (TreeSPath path : expected.getElements().keySet()) {
37 // String nameExpected = expected.getElements().get(path);
38 // String nameReached = expected.getElements().get(path);
39 SimpleSElement elemExpected = (SimpleSElement) expected
40 .getElements().get(path);
41 SimpleSElement elemReached = (SimpleSElement) expected
42 .getElements().get(path);
43 assertNotNull(elemReached);
44 assertElements(elemExpected, elemReached);
45 }
46
47 }
48
49 public static void assertElements(SimpleSElement expected,
50 SimpleSElement reached) {
51 assertEquals(expected.getLabel(), reached.getLabel());
52 assertEquals(expected.getTags().size(), reached.getTags().size());
53 for (String tagName : expected.getTags().keySet()) {
54 String expectedTagValue = expected.getTags().get(tagName);
55 String reachedTagValue = reached.getTags().get(tagName);
56 assertNotNull(reachedTagValue);
57 assertEquals(expectedTagValue, reachedTagValue);
58 }
59 }
60
61 public static void assertPartSubList(PartSubList lstExpected,
62 PartSubList lstReached) {
63 if (lstExpected.getSlcExecutionUuid() == null) {
64 assertNull(lstReached.getSlcExecutionUuid());
65 } else {
66 assertEquals(lstExpected.getSlcExecutionUuid(), lstReached
67 .getSlcExecutionUuid());
68 }
69
70 if (lstExpected.getSlcExecutionStepUuid() == null) {
71 assertNull(lstReached.getSlcExecutionStepUuid());
72 } else {
73 assertEquals(lstExpected.getSlcExecutionStepUuid(), lstReached
74 .getSlcExecutionStepUuid());
75 }
76
77 assertEquals(lstExpected.getParts().size(), lstReached.getParts()
78 .size());
79 for (int i = 0; i < lstExpected.getParts().size(); i++) {
80 assertPart(lstExpected.getParts().get(i), lstReached.getParts()
81 .get(i));
82 }
83 }
84
85 /**
86 * Assert one part of a tree test result.
87 *
88 * @deprecated use {@link #assertPart(TestResultPart, TestResultPart)}
89 * instead
90 */
91 public static void assertPart(TreeTestResult testResult, String pathStr,
92 int index, Integer status, String message) {
93 TreeSPath path = TreeSPath.parseToCreatePath(pathStr);
94 PartSubList list = testResult.getResultParts().get(path);
95 if (list == null) {
96 fail("No result for path " + path);
97 return;
98 }
99 if (index >= list.getParts().size()) {
100 fail("Not enough parts.");
101 }
102 SimpleResultPart part = (SimpleResultPart) list.getParts().get(index);
103 assertPart(part, status, message, null);
104 }
105
106 public static void assertPart(TestResultPart expected,
107 TestResultPart reached) {
108 assertPart(reached, expected.getStatus(), expected.getMessage(),
109 expected.getException());
110 }
111
112 /** Assert one part of a tree test result. */
113 private static void assertPart(TestResultPart part, Integer status,
114 String message, Exception exception) {
115 assertEquals(status, part.getStatus());
116 assertEquals(message, part.getMessage());
117 if (exception == null) {
118 assertNull(part.getException());
119 } else {
120 assertEquals(exception, part.getException());
121 }
122 }
123
124 /** Makes sure this is a singleton */
125 private UnitTestTreeUtil() {
126
127 }
128 }