2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.unit
.test
.tree
;
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
;
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
;
34 /** Utilities for unit tests. */
35 public class UnitTestTreeUtil
{
36 private final static Log log
= LogFactory
.getLog(UnitTestTreeUtil
.class);
38 public static void assertTreeTestResult(TreeTestResult expected
,
39 TreeTestResult reached
) {
40 assertEquals(expected
.getUuid(), reached
.getUuid());
41 assertDateSec(expected
.getCloseDate(), reached
.getCloseDate());
44 //assertEquals(expected.getAttributes().size(), reached.getAttributes()
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
);
54 assertEquals(expected
.getResultParts().size(), reached
.getResultParts()
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
);
63 assertPartSubList(lstExpected
, lstReached
);
67 assertEquals(expected
.getElements().size(), reached
.getElements()
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
);
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
);
94 public static void assertPartSubList(PartSubList lstExpected
,
95 PartSubList lstReached
) {
96 assertEquals(lstExpected
.getParts().size(), lstReached
.getParts()
98 for (int i
= 0; i
< lstExpected
.getParts().size(); i
++) {
99 assertPart(lstExpected
.getParts().get(i
), lstReached
.getParts()
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
);
110 fail("No result for path " + path
);
113 if (index
>= list
.getParts().size()) {
114 fail("Not enough parts.");
116 SimpleResultPart part
= (SimpleResultPart
) list
.getParts().get(index
);
117 assertPart(part
, status
, message
, null, part
.getTestRunUuid(), true);
120 public static void assertPart(TestResultPart expected
,
121 TestResultPart reached
) {
122 String expectedTestRunUuid
= null;
123 if (expected
instanceof SimpleResultPart
) {
124 expectedTestRunUuid
= ((SimpleResultPart
) expected
)
128 assertPart(reached
, expected
.getStatus(), expected
.getMessage(),
129 expected
.getExceptionMessage(), expectedTestRunUuid
, false);
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());
138 if (message
!= null) {
139 if (log
.isTraceEnabled()) {
140 log
.trace("Expected message:" + message
);
141 log
.trace("Reached message:" + part
.getMessage());
143 assertEquals(message
, part
.getMessage());
146 if (!skipExceptionMessage
) {
147 if (exceptionDescription
== null) {
148 assertNull(part
.getExceptionMessage());
150 if (log
.isTraceEnabled()) {
151 log
.trace("Expected exception message:"
152 + exceptionDescription
);
153 log
.trace("Reached exception message:"
154 + part
.getExceptionMessage());
157 assertEquals(exceptionDescription
, part
.getExceptionMessage());
161 if (expectedTestRunUuid
!= null) {
162 SimpleResultPart reachedPart
= (SimpleResultPart
) part
;
163 assertNotNull(reachedPart
.getTestRunUuid());
164 assertEquals(expectedTestRunUuid
, reachedPart
.getTestRunUuid());
166 if (part
instanceof SimpleResultPart
) {
167 assertNull(((SimpleResultPart
) part
).getTestRunUuid());
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
));
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
);
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
));
199 /** Makes sure this is a singleton */
200 private UnitTestTreeUtil() {