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 package junitx.ddtunit.data;
38
39 import java.util.Collection;
40 import java.util.Iterator;
41
42 import junit.framework.AssertionFailedError;
43 import junitx.ddtunit.DDTException;
44 import junitx.framework.ArrayAssert;
45 import junitx.framework.Assert;
46
47
48
49
50
51
52
53 public class ObjectAsserter extends AssertObject {
54
55
56
57
58
59
60 public ObjectAsserter(String id, String type, String action) {
61 super(id, type, action);
62 }
63
64
65
66
67 public static final String ASSERT_ACTION_ISEQUAL = "ISEQUAL";
68
69
70
71
72 public static final String ASSERT_ACTION_ISNOTEQUAL = "ISNOTEQUAL";
73
74
75
76
77 public static final String ASSERT_ACTION_ISSAME = "ISSAME";
78
79
80
81
82 public static final String ASSERT_ACTION_ISNOTSAME = "ISNOTSAME";
83
84
85
86
87 public static final String ASSERT_ACTION_ISNULL = "ISNULL";
88
89
90
91
92 public static final String ASSERT_ACTION_ISNOTNULL = "ISNOTNULL";
93
94 private boolean validated = false;
95
96 public static final String ASSERT_ACTION_ISTRUE = "ISTRUE";
97
98 public static final String ASSERT_ACTION_ISFALSE = "ISFALSE";
99
100 public static final String ASSERT_ACTION_ISGT = "ISGT";
101
102 public static final String ASSERT_ACTION_ISNOTGT = "ISNOTGT";
103
104 public static final String ASSERT_ACTION_ISLT = "ISLT";
105
106 public static final String ASSERT_ACTION_ISNOTLT = "ISNOTLT";
107
108 public static final String ASSERT_ACTION_ISCONTAINEDIN = "ISCONTAINEDIN";
109
110 public static final String ASSERT_ACTION_ISNOTCONTAINEDIN = "ISNOTCONTAINEDIN";
111
112 public static final String ASSERT_ACTION_ISINRANGE = "ISINRANGE";
113
114 public static final String ASSERT_ACTION_ISNOTINRANGE = "ISNOTINRANGE";
115
116
117
118
119
120
121 public void validate(boolean mark) {
122 if (!this.actualObjectSet) {
123 throw new DDTException("Actual object for assertion not provided");
124 } else if (ASSERT_ACTION_ISEQUAL.equals(this.action.toUpperCase())) {
125 this.markAsProcessed = mark;
126 isSameType();
127 if (this.getActualType() != null
128 && this.getActualType().startsWith("[L")
129 && this.getType().startsWith("[L")) {
130 ArrayAssert.assertEquals("Wrong isEqual assert ("
131 + this.getId() + ") on arrays", (Object[]) this
132 .getValue(), (Object[]) this.getActualObject());
133 } else {
134 if ("java.lang.String".equals(this.getActualType())
135 && "java.lang.String".equals(this.getType())) {
136 String actualText = (String) getActualObject();
137 String expectedText = (String) getValue();
138 String[] actual = null;
139 String[] expected = null;
140 if (actualText != null && actualText.indexOf("\r\n") > -1) {
141 actual = actualText.split("\r\n");
142 } else {
143 actual = actualText.split("\n");
144 }
145 if (expectedText != null
146 && expectedText.indexOf("\r\n") > -1) {
147 expected = expectedText.split("\r\n");
148 } else {
149 expected = expectedText.split("\n");
150 }
151 ArrayAssert.assertEquals(
152 "Wrong isEqual assert on (multiline) string", expected,
153 actual);
154 } else {
155 Assert.assertEquals("Wrong isEqual assert (" + this.getId()
156 + ")", this.getValue(), getActualObject());
157 }
158 }
159 } else if (ASSERT_ACTION_ISNOTEQUAL.equals(this.action.toUpperCase())) {
160 this.markAsProcessed = mark;
161 Assert.assertNotEquals("Wrong isNotEqual assert (" + this.getId()
162 + ")", this.getValue(), getActualObject());
163 } else if (ASSERT_ACTION_ISSAME.equals(this.action.toUpperCase())) {
164 this.markAsProcessed = mark;
165 isSameType();
166 Assert.assertSame("Wrong isSame assert (" + this.getId() + ")",
167 this.getValue(), getActualObject());
168 } else if (ASSERT_ACTION_ISNOTSAME.equals(this.action.toUpperCase())) {
169 this.markAsProcessed = mark;
170 Assert.assertNotSame("Wrong isNotSame assert (" + this.getId()
171 + ")", this.getValue(), getActualObject());
172 } else if (ASSERT_ACTION_ISNULL.equals(this.action.toUpperCase())) {
173 this.markAsProcessed = mark;
174 isSameType();
175 Assert.assertNull("Object should be null on assert ("
176 + this.getId() + ")", getActualObject());
177 } else if (ASSERT_ACTION_ISNOTNULL.equals(this.action.toUpperCase())) {
178 this.markAsProcessed = mark;
179 isSameType();
180 Assert.assertNotNull("Object should not be null on assert ("
181 + this.getId() + ")", getActualObject());
182 } else if (ASSERT_ACTION_ISTRUE.equals(this.action.toUpperCase())) {
183 this.markAsProcessed = mark;
184 if (Boolean.class.isInstance(getActualObject())) {
185 isSameType();
186 Assert.assertTrue("Object should be true on assert ("
187 + this.getId() + ")", ((Boolean) getActualObject())
188 .booleanValue());
189 } else {
190 throw new UnsupportedOperationException(
191 "Wrong type used under assert action 'ISTRUE':"
192 + getActualObject());
193 }
194 } else if (ASSERT_ACTION_ISFALSE.equals(this.action.toUpperCase())) {
195 this.markAsProcessed = mark;
196 if (Boolean.class.isInstance(getActualObject())) {
197 isSameType();
198 Assert.assertFalse("Object should be false on assert ("
199 + this.getId() + ")", ((Boolean) getActualObject())
200 .booleanValue());
201 } else {
202 throw new UnsupportedOperationException(
203 "Wrong type used under assert action 'ISFALSE':"
204 + getActualObject());
205 }
206 } else if (ASSERT_ACTION_ISGT.equals(this.action.toUpperCase())
207 || ASSERT_ACTION_ISNOTGT.equals(this.action.toUpperCase())
208 || ASSERT_ACTION_ISLT.equals(this.action.toUpperCase())
209 || ASSERT_ACTION_ISNOTLT.equals(this.action.toUpperCase())
210 || ASSERT_ACTION_ISINRANGE.equals(this.action.toUpperCase())
211 || ASSERT_ACTION_ISNOTINRANGE.equals(this.action.toUpperCase())) {
212 this.markAsProcessed = mark;
213
214 if (!(this.getValue() instanceof IRange)) {
215 isSameType();
216 }
217 if (Comparable.class.isInstance(this.actualObject)) {
218 if (ASSERT_ACTION_ISGT.equals(this.action.toUpperCase())) {
219 if (((Comparable) this.getActualObject()).compareTo(this
220 .getValue()) <= 0) {
221 Assert.fail("Expected action: " + this.getAction()
222 + ". Got: " + this.getActualObject() + " <= "
223 + this.getValue() + " on assert ("
224 + this.getId() + ")");
225 }
226 } else if (ASSERT_ACTION_ISNOTGT.equals(this.action
227 .toUpperCase())) {
228 if (((Comparable) this.getActualObject()).compareTo(this
229 .getValue()) > 0) {
230 Assert.fail("Expected action: " + this.getAction()
231 + ". Got: " + this.getActualObject() + " > "
232 + this.getValue() + "on assert ("
233 + this.getId() + ")");
234 }
235 } else if (ASSERT_ACTION_ISLT.equals(this.action.toUpperCase())) {
236 if (((Comparable) this.getActualObject()).compareTo(this
237 .getValue()) >= 0) {
238 Assert.fail("Expected action: " + this.getAction()
239 + ". Got: " + this.getActualObject() + " >= "
240 + this.getValue() + " on assert ("
241 + this.getId() + ")");
242 }
243 } else if (ASSERT_ACTION_ISNOTLT.equals(this.action
244 .toUpperCase())) {
245 if (((Comparable) this.getActualObject()).compareTo(this
246 .getValue()) >= 0) {
247 Assert.fail("Expected action: " + this.getAction()
248 + ". Got: " + this.getActualObject() + " > "
249 + this.getValue() + " on assert ("
250 + this.getId() + ")");
251 }
252 } else if (ASSERT_ACTION_ISINRANGE.equals(this.action
253 .toUpperCase())) {
254 if (!((IRange) this.getValue()).isInRange((Comparable) this
255 .getActualObject())) {
256 Assert.fail("Expected action: " + this.getAction()
257 + ". Got: " + this.getActualObject()
258 + " not in " + this.getValue() + " on assert ("
259 + this.getId() + ")");
260 }
261 } else if (ASSERT_ACTION_ISNOTINRANGE.equals(this.action
262 .toUpperCase())) {
263 if (((IRange) this.getValue()).isInRange((Comparable) this
264 .getActualObject())) {
265 Assert.fail("Expected action: " + this.getAction()
266 + ". Got: " + this.getActualObject() + " in "
267 + this.getValue() + " on assert ("
268 + this.getId() + ")");
269 }
270 }
271 } else {
272 throw new DDTException(
273 "Asserted type does not implement Comparable interface: "
274 + this.getType());
275 }
276 } else if (ASSERT_ACTION_ISCONTAINEDIN
277 .equals(this.action.toUpperCase())
278 || ASSERT_ACTION_ISNOTCONTAINEDIN.equals(this.action
279 .toUpperCase())) {
280 this.markAsProcessed = mark;
281 boolean match = false;
282 RuntimeException assertEx = null;
283 if (ASSERT_ACTION_ISCONTAINEDIN.equals(this.action.toUpperCase())) {
284
285 if (this.getActualType().startsWith("[L")) {
286 match = isArrayContainedinList(assertEx);
287 if (!match) {
288 throw assertEx;
289 }
290 } else {
291 Assert.assertTrue(
292 "Object should be member in List on assert ("
293 + this.getId() + ")", ((Collection) this
294 .getValue()).contains(getActualObject()));
295 }
296 } else {
297
298 if (this.getActualType().startsWith("[L")) {
299 match = isArrayContainedinList(assertEx);
300 if (match) {
301 throw new AssertionFailedError(
302 "Array not found in expected list on assert ("
303 + this.getId() + ")");
304 }
305 } else {
306 Assert.assertFalse(
307 "Object should not be contained in List on assert ("
308 + this.getId() + ")", ((Collection) this
309 .getValue()).contains(getActualObject()));
310 }
311 }
312 } else {
313 throw new DDTException("Unsupported assert action \"" + this.action
314 + "\"");
315 }
316 }
317
318
319
320
321
322
323
324 private boolean isArrayContainedinList(RuntimeException assertEx) {
325 boolean match = false;
326 for (Iterator iter = ((Collection) this.getValue()).iterator(); iter
327 .hasNext();) {
328 Object array = iter.next();
329 try {
330 ArrayAssert.assertEquals((Object[]) array, (Object[]) this
331 .getActualObject());
332 match = true;
333 break;
334 } catch (RuntimeException ex) {
335
336 assertEx = ex;
337 }
338 }
339 return match;
340 }
341
342
343
344
345 private void isSameType() {
346 Assert.assertEquals("Class type differs between assert objects, ",
347 getType(),
348 (this.actualObject != null ? getActualType() : getType()));
349 }
350
351
352
353
354 public boolean isValidated() {
355 return validated;
356 }
357
358 public Object clone() {
359 ObjectAsserter newObj = new ObjectAsserter(this.getId(),
360 this.getType(), this.getAction());
361 newObj.setValue(this.getValue());
362 return newObj;
363 }
364
365 }