]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/test/tree/UnitTestTreeUtil.java
98db1cdd8e04592b60a6e3b1af63699c149f68c7
[gpl/argeo-slc.git] / runtime / org.argeo.slc.unit / src / main / java / org / argeo / slc / unit / test / tree / UnitTestTreeUtil.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.unit.test.tree;
18
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertNotNull;
21 import static junit.framework.Assert.assertNull;
22 import static junit.framework.Assert.fail;
23 import static org.argeo.slc.unit.UnitUtils.assertDateSec;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.core.structure.SimpleSElement;
28 import org.argeo.slc.core.structure.tree.TreeSPath;
29 import org.argeo.slc.core.test.SimpleResultPart;
30 import org.argeo.slc.core.test.tree.PartSubList;
31 import org.argeo.slc.core.test.tree.TreeTestResult;
32 import org.argeo.slc.test.TestResultPart;
33
34 /** Utilities for unit tests. */
35 public class UnitTestTreeUtil {
36 private final static Log log = LogFactory.getLog(UnitTestTreeUtil.class);
37
38 public static void assertTreeTestResult(TreeTestResult expected,
39 TreeTestResult reached) {
40 assertEquals(expected.getUuid(), reached.getUuid());
41 assertDateSec(expected.getCloseDate(), reached.getCloseDate());
42
43 // Attributes
44 //assertEquals(expected.getAttributes().size(), reached.getAttributes()
45 // .size());
46 for (String key : expected.getAttributes().keySet()) {
47 String expectedValue = expected.getAttributes().get(key);
48 String reachedValue = reached.getAttributes().get(key);
49 assertNotNull(reachedValue);
50 assertEquals(expectedValue, reachedValue);
51 }
52
53 // Result parts
54 assertEquals(expected.getResultParts().size(), reached.getResultParts()
55 .size());
56 for (TreeSPath path : expected.getResultParts().keySet()) {
57 PartSubList lstExpected = expected.getResultParts().get(path);
58 PartSubList lstReached = expected.getResultParts().get(path);
59 if (lstReached == null) {
60 fail("No result for path " + path);
61 return;
62 }
63 assertPartSubList(lstExpected, lstReached);
64 }
65
66 // Elements
67 assertEquals(expected.getElements().size(), reached.getElements()
68 .size());
69 for (TreeSPath path : expected.getElements().keySet()) {
70 // String nameExpected = expected.getElements().get(path);
71 // String nameReached = expected.getElements().get(path);
72 SimpleSElement elemExpected = (SimpleSElement) expected
73 .getElements().get(path);
74 SimpleSElement elemReached = (SimpleSElement) expected
75 .getElements().get(path);
76 assertNotNull(elemReached);
77 assertElements(elemExpected, elemReached);
78 }
79
80 }
81
82 public static void assertElements(SimpleSElement expected,
83 SimpleSElement reached) {
84 assertEquals(expected.getLabel(), reached.getLabel());
85 assertEquals(expected.getTags().size(), reached.getTags().size());
86 for (String tagName : expected.getTags().keySet()) {
87 String expectedTagValue = expected.getTags().get(tagName);
88 String reachedTagValue = reached.getTags().get(tagName);
89 assertNotNull(reachedTagValue);
90 assertEquals(expectedTagValue, reachedTagValue);
91 }
92 }
93
94 public static void assertPartSubList(PartSubList lstExpected,
95 PartSubList lstReached) {
96 assertEquals(lstExpected.getParts().size(), lstReached.getParts()
97 .size());
98 for (int i = 0; i < lstExpected.getParts().size(); i++) {
99 assertPart(lstExpected.getParts().get(i), lstReached.getParts()
100 .get(i));
101 }
102 }
103
104 /** Asserts one part of a tree test result */
105 public static void assertPart(TreeTestResult testResult, String pathStr,
106 int index, Integer status, String message) {
107 TreeSPath path = new TreeSPath(pathStr);
108 PartSubList list = testResult.getResultParts().get(path);
109 if (list == null) {
110 fail("No result for path " + path);
111 return;
112 }
113 if (index >= list.getParts().size()) {
114 fail("Not enough parts.");
115 }
116 SimpleResultPart part = (SimpleResultPart) list.getParts().get(index);
117 assertPart(part, status, message, null, part.getTestRunUuid(), true);
118 }
119
120 public static void assertPart(TestResultPart expected,
121 TestResultPart reached) {
122 String expectedTestRunUuid = null;
123 if (expected instanceof SimpleResultPart) {
124 expectedTestRunUuid = ((SimpleResultPart) expected)
125 .getTestRunUuid();
126 }
127
128 assertPart(reached, expected.getStatus(), expected.getMessage(),
129 expected.getExceptionMessage(), expectedTestRunUuid, false);
130 }
131
132 /** Assert one part of a tree test result. */
133 private static void assertPart(TestResultPart part, Integer status,
134 String message, String exceptionDescription,
135 String expectedTestRunUuid, boolean skipExceptionMessage) {
136 assertEquals(status, part.getStatus());
137
138 if (message != null) {
139 if (log.isTraceEnabled()) {
140 log.trace("Expected message:" + message);
141 log.trace("Reached message:" + part.getMessage());
142 }
143 assertEquals(message, part.getMessage());
144 }
145
146 if (!skipExceptionMessage) {
147 if (exceptionDescription == null) {
148 assertNull(part.getExceptionMessage());
149 } else {
150 if (log.isTraceEnabled()) {
151 log.trace("Expected exception message:"
152 + exceptionDescription);
153 log.trace("Reached exception message:"
154 + part.getExceptionMessage());
155 }
156
157 assertEquals(exceptionDescription, part.getExceptionMessage());
158 }
159 }
160
161 if (expectedTestRunUuid != null) {
162 SimpleResultPart reachedPart = (SimpleResultPart) part;
163 assertNotNull(reachedPart.getTestRunUuid());
164 assertEquals(expectedTestRunUuid, reachedPart.getTestRunUuid());
165 } else {
166 if (part instanceof SimpleResultPart) {
167 assertNull(((SimpleResultPart) part).getTestRunUuid());
168 }
169
170 }
171
172 }
173
174 public static void describeTreeTestResult(TreeTestResult ttr) {
175 log.info("TreeTestResult #" + ttr.getUuid());
176 log.info(" Close date: " + ttr.getCloseDate());
177 log.info(" Attributes:");
178 for (String key : ttr.getAttributes().keySet())
179 log.info(" " + key + "=" + ttr.getAttributes().get(key));
180
181 log.info(" Result parts: (size=" + ttr.getResultParts().size() + ")");
182 for (TreeSPath path : ttr.getResultParts().keySet()) {
183 log.info(" Path: " + path);
184 PartSubList lst = ttr.getResultParts().get(path);
185 for (TestResultPart part : lst.getParts())
186 log.info(" " + part);
187 }
188
189 log.info(" Elements: (size=" + ttr.getElements().size() + ")");
190 for (TreeSPath path : ttr.getElements().keySet()) {
191 SimpleSElement elem = (SimpleSElement) ttr.getElements().get(path);
192 log.info(" Path: " + path + ", Element: " + elem.getLabel());
193 for (String tag : elem.getTags().keySet())
194 log.info(" " + tag + "=" + elem.getTags().get(tag));
195 }
196
197 }
198
199 /** Makes sure this is a singleton */
200 private UnitTestTreeUtil() {
201
202 }
203 }