Source Code Cross Referenced for ReflectionComparatorLenientTest.java in  » Testing » unitils » org » unitils » reflectionassert » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » unitils » org.unitils.reflectionassert 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006-2007,  Unitils.org
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.unitils.reflectionassert;
017:
018:        import org.unitils.reflectionassert.ReflectionComparator.Difference;
019:
020:        import java.util.Date;
021:
022:        import junit.framework.TestCase;
023:
024:        /**
025:         * Test class for {@link ReflectionComparator}.
026:         * Contains tests for ignore defaults and lenient dates.
027:         *
028:         * @author Tim Ducheyne
029:         * @author Filip Neven
030:         */
031:        public class ReflectionComparatorLenientTest extends TestCase {
032:
033:            /* Test object with no java defaults */
034:            private Element elementNoDefaultsA;
035:
036:            /* Same as A but different instance */
037:            private Element elementNoDefaultsB;
038:
039:            /* Test object with only defaults */
040:            private Element elementAllDefaults;
041:
042:            /* Same as A but with null date */
043:            private Element elementNoDefaultsNullDateA;
044:
045:            /* Same as null date  A but different instance */
046:            private Element elementNoDefaultsNullDateB;
047:
048:            /* Same as A but different date */
049:            private Element elementNoDefaultsDifferentDate;
050:
051:            /* Class under test */
052:            private ReflectionComparator reflectionComparator,
053:                    ignoreDefaultsReflectionComparator,
054:                    lenientDatesReflectionComparator,
055:                    ignoreDefaultsLenientDatesComparator;
056:
057:            /**
058:             * Initializes the test fixture.
059:             */
060:            protected void setUp() throws Exception {
061:                super .setUp();
062:
063:                Date date = new Date();
064:                elementNoDefaultsA = new Element(true, 'c', (byte) 1,
065:                        (short) 2, 3, 4l, 5.0f, 6.0, date, "object");
066:                elementNoDefaultsB = new Element(true, 'c', (byte) 1,
067:                        (short) 2, 3, 4l, 5.0f, 6.0, date, "object");
068:                elementNoDefaultsNullDateA = new Element(true, 'c', (byte) 1,
069:                        (short) 2, 3, 4l, 5.0f, 6.0, null, "object");
070:                elementNoDefaultsNullDateB = new Element(true, 'c', (byte) 1,
071:                        (short) 2, 3, 4l, 5.0f, 6.0, null, "object");
072:                elementNoDefaultsDifferentDate = new Element(true, 'c',
073:                        (byte) 1, (short) 2, 3, 4l, 5.0f, 6.0, new Date(),
074:                        "object");
075:                elementAllDefaults = new Element(false, (char) 0, (byte) 0,
076:                        (short) 0, 0, 0l, 0.0f, 0.0, null, null);
077:
078:                reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
079:                ignoreDefaultsReflectionComparator = ReflectionComparatorChainFactory.IGNOREDEFAULTS_COMPARATOR;
080:                lenientDatesReflectionComparator = ReflectionComparatorChainFactory.LENIENTDATES_COMPARATOR;
081:                ignoreDefaultsLenientDatesComparator = ReflectionComparatorChainFactory.IGNOREDEFAULTS_LENIENTDATES_COMPARATOR;
082:            }
083:
084:            /**
085:             * Test for two equal objects without java defaults.
086:             */
087:            public void testGetDifference_equals() {
088:                Difference result = reflectionComparator.getDifference(
089:                        elementNoDefaultsA, elementNoDefaultsB);
090:                assertNull(result);
091:            }
092:
093:            /**
094:             * Test with left object containing only java defaults.
095:             */
096:            public void testGetDifference_equalsIgnoreDefaults() {
097:                Difference result = ignoreDefaultsReflectionComparator
098:                        .getDifference(elementAllDefaults, elementNoDefaultsA);
099:                assertNull(result);
100:            }
101:
102:            /**
103:             * Test with ignore defaults and left object null.
104:             */
105:            public void testGetDifference_equalsIgnoreDefaultsLeftNull() {
106:                Difference result = ignoreDefaultsReflectionComparator
107:                        .getDifference(null, elementNoDefaultsA);
108:                assertNull(result);
109:            }
110:
111:            /**
112:             * Test with ignore defaults and right object null
113:             */
114:            public void testGetDifference_notEqualsIgnoreDefaultsRightNull() {
115:                Difference result = ignoreDefaultsReflectionComparator
116:                        .getDifference(elementNoDefaultsA, null);
117:
118:                assertNotNull(result);
119:                assertTrue(result.getFieldStack().isEmpty());
120:                assertSame(elementNoDefaultsA, result.getLeftValue());
121:                assertNull(result.getRightValue());
122:            }
123:
124:            /**
125:             * Test with ignore defaults and left value 0.
126:             */
127:            public void testGetDifference_equalsIgnoreDefaultsLeft0() {
128:                Difference result = ignoreDefaultsReflectionComparator
129:                        .getDifference(0, 999);
130:                assertNull(result);
131:            }
132:
133:            /**
134:             * Test with ignore defaults and right value 0.
135:             */
136:            public void testGetDifference_equalsIgnoreDefaultsRight0() {
137:                Difference result = ignoreDefaultsReflectionComparator
138:                        .getDifference(999, 0);
139:
140:                assertNotNull(result);
141:                assertTrue(result.getFieldStack().isEmpty());
142:                assertEquals(999, result.getLeftValue());
143:                assertEquals(0, result.getRightValue());
144:            }
145:
146:            /**
147:             * Test for lenient dates with 2 null dates.
148:             */
149:            public void testGetDifference_equalsLenientDatesBothNull() {
150:                Difference result = lenientDatesReflectionComparator
151:                        .getDifference(elementNoDefaultsNullDateA,
152:                                elementNoDefaultsNullDateB);
153:                assertNull(result);
154:            }
155:
156:            /**
157:             * Test for lenient dates with 2 not null dates.
158:             */
159:            public void testGetDifference_equalsLenientDatesBothNotNull() {
160:                Difference result = lenientDatesReflectionComparator
161:                        .getDifference(elementNoDefaultsA,
162:                                elementNoDefaultsDifferentDate);
163:                assertNull(result);
164:            }
165:
166:            /**
167:             * Test with left object containing only java defaults but no ignore defaults.
168:             */
169:            public void testGetDifference_notEqualsNoIgnoreDefaults() {
170:                Difference result = reflectionComparator.getDifference(
171:                        elementAllDefaults, elementNoDefaultsB);
172:
173:                assertNotNull(result);
174:                assertEquals("booleanValue", result.getFieldStack().get(0));
175:                assertEquals(Boolean.FALSE, result.getLeftValue());
176:                assertEquals(Boolean.TRUE, result.getRightValue());
177:            }
178:
179:            /**
180:             * Test with right instead of left object containing only java defaults.
181:             */
182:            public void testGetDifference_notEqualsIgnoreDefaultsButDefaultsRight() {
183:                Difference result = ignoreDefaultsReflectionComparator
184:                        .getDifference(elementNoDefaultsB, elementAllDefaults);
185:
186:                assertNotNull(result);
187:                assertEquals("booleanValue", result.getFieldStack().get(0));
188:                assertEquals(Boolean.TRUE, result.getLeftValue());
189:                assertEquals(Boolean.FALSE, result.getRightValue());
190:            }
191:
192:            /**
193:             * Test for lenient dates but with only right date null.
194:             */
195:            public void testGetDifference_notEqualsLenientDatesRightDateNull() {
196:                Difference result = lenientDatesReflectionComparator
197:                        .getDifference(elementNoDefaultsDifferentDate,
198:                                elementNoDefaultsNullDateA);
199:
200:                assertNotNull(result);
201:                assertEquals("dateValue", result.getFieldStack().get(0));
202:                assertEquals(elementNoDefaultsDifferentDate.getDateValue(),
203:                        result.getLeftValue());
204:                assertNull(result.getRightValue());
205:            }
206:
207:            /**
208:             * Test for lenient dates but with only left date null.
209:             */
210:            public void testGetDifference_notEqualsLenientDatesLeftDateNull() {
211:                Difference result = lenientDatesReflectionComparator
212:                        .getDifference(elementNoDefaultsNullDateA,
213:                                elementNoDefaultsDifferentDate);
214:
215:                assertNotNull(result);
216:                assertEquals("dateValue", result.getFieldStack().get(0));
217:                assertNull(result.getLeftValue());
218:                assertEquals(elementNoDefaultsDifferentDate.getDateValue(),
219:                        result.getRightValue());
220:            }
221:
222:            /**
223:             * Test for lenient dates while ignore defaults but with only left date null (= not treated as default).
224:             */
225:            public void testGetDifference_notEqualsLenientDatesAndIgnoreDefaultsWithLeftDateNull() {
226:                Difference result = ignoreDefaultsLenientDatesComparator
227:                        .getDifference(elementNoDefaultsNullDateA,
228:                                elementNoDefaultsDifferentDate);
229:
230:                assertNotNull(result);
231:                assertEquals("dateValue", result.getFieldStack().get(0));
232:                assertNull(result.getLeftValue());
233:                assertEquals(elementNoDefaultsDifferentDate.getDateValue(),
234:                        result.getRightValue());
235:            }
236:
237:            /**
238:             * Test class with failing equals.
239:             */
240:            private class Element {
241:
242:                /* A boolean value */
243:                private boolean booleanValue;
244:
245:                /* A char value */
246:                private char charValue;
247:
248:                /* A byte value */
249:                private byte byteValue;
250:
251:                /* A short value */
252:                private short shortValue;
253:
254:                /* An int value */
255:                private int intValue;
256:
257:                /* A long value */
258:                private long longValue;
259:
260:                /* A float value */
261:                private float floatValue;
262:
263:                /* A double value */
264:                private double doubleValue;
265:
266:                /* A date value */
267:                private Date dateValue;
268:
269:                /* An object value */
270:                private Object objectValue;
271:
272:                /**
273:                 * Creates and initializes the element.
274:                 *
275:                 * @param booleanValue a boolean value
276:                 * @param charValue    a char value
277:                 * @param byteValue    a byte value
278:                 * @param shortValue   a short value
279:                 * @param intValue     an int value
280:                 * @param longValue    a long value
281:                 * @param floatValue   a float value
282:                 * @param doubleValue  a double value
283:                 * @param dateValue    a date value
284:                 * @param objectValue  an object value
285:                 */
286:                public Element(boolean booleanValue, char charValue,
287:                        byte byteValue, short shortValue, int intValue,
288:                        long longValue, float floatValue, double doubleValue,
289:                        Date dateValue, Object objectValue) {
290:                    this .booleanValue = booleanValue;
291:                    this .charValue = charValue;
292:                    this .byteValue = byteValue;
293:                    this .shortValue = shortValue;
294:                    this .intValue = intValue;
295:                    this .longValue = longValue;
296:                    this .floatValue = floatValue;
297:                    this .doubleValue = doubleValue;
298:                    this .dateValue = dateValue;
299:                    this .objectValue = objectValue;
300:                }
301:
302:                /**
303:                 * Gets the boolean value.
304:                 *
305:                 * @return the boolean value
306:                 */
307:                public boolean isBooleanValue() {
308:                    return booleanValue;
309:                }
310:
311:                /**
312:                 * Gets the char value.
313:                 *
314:                 * @return the char value
315:                 */
316:                public char getCharValue() {
317:                    return charValue;
318:                }
319:
320:                /**
321:                 * Gets the byte value.
322:                 *
323:                 * @return the byte value
324:                 */
325:                public byte getByteValue() {
326:                    return byteValue;
327:                }
328:
329:                /**
330:                 * Gets the short value.
331:                 *
332:                 * @return the short value
333:                 */
334:                public short getShortValue() {
335:                    return shortValue;
336:                }
337:
338:                /**
339:                 * Gets the int value.
340:                 *
341:                 * @return the int value
342:                 */
343:                public int getIntValue() {
344:                    return intValue;
345:                }
346:
347:                /**
348:                 * Gets the long value.
349:                 *
350:                 * @return the long value
351:                 */
352:                public long getLongValue() {
353:                    return longValue;
354:                }
355:
356:                /**
357:                 * Gets the float value.
358:                 *
359:                 * @return the float value
360:                 */
361:                public float getFloatValue() {
362:                    return floatValue;
363:                }
364:
365:                /**
366:                 * Gets the double value.
367:                 *
368:                 * @return the double value
369:                 */
370:                public double getDoubleValue() {
371:                    return doubleValue;
372:                }
373:
374:                /**
375:                 * Gets the date value.
376:                 *
377:                 * @return the date value
378:                 */
379:                public Date getDateValue() {
380:                    return dateValue;
381:                }
382:
383:                /**
384:                 * Gets the object value.
385:                 *
386:                 * @return the object value
387:                 */
388:                public Object getObjectValue() {
389:                    return objectValue;
390:                }
391:
392:                /**
393:                 * Always returns false
394:                 *
395:                 * @param o the object to compare to
396:                 */
397:                @Override
398:                public boolean equals(Object o) {
399:                    return false;
400:                }
401:            }
402:
403:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.