Source Code Cross Referenced for ReflectionComparatorMapTest.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 junit.framework.TestCase;
019:        import org.unitils.reflectionassert.ReflectionComparator.Difference;
020:
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.Map;
024:
025:        /**
026:         * Test class for {@link ReflectionComparator}.
027:         * Contains tests with map types.
028:         *
029:         * @author Tim Ducheyne
030:         * @author Filip Neven
031:         */
032:        public class ReflectionComparatorMapTest extends TestCase {
033:
034:            /* Test map */
035:            private Map<String, Element> mapA;
036:
037:            /* Same as A but different instance */
038:            private Map<String, Element> mapB;
039:
040:            /* Same as A and B but different string value for element 2 */
041:            private Map<String, Element> mapDifferentValue;
042:
043:            /* Same as A and B but different key value for element 2 */
044:            private Map<String, Element> mapDifferentKey;
045:
046:            /* Test map with inner map for element 2 */
047:            private Map<String, Element> mapInnerA;
048:
049:            /* Same as innerA but different instance  */
050:            private Map<String, Element> mapInnerB;
051:
052:            /* Same as innerA and innerB but different string value for inner element 2 */
053:            private Map<String, Element> mapInnerDifferentValue;
054:
055:            /* Test map having a key type that returns false for equals() */
056:            private Map<Element, Element> mapReflectionCompareKeyA;
057:
058:            /* Same as A but different instance */
059:            private Map<Element, Element> mapReflectionCompareKeyB;
060:
061:            /* Same as A and B but with a different key value */
062:            private Map<Element, Element> mapReflectionCompareDifferentKey;
063:
064:            /* Class under test */
065:            private ReflectionComparator reflectionComparator;
066:
067:            /**
068:             * Initializes the test fixture.
069:             */
070:            protected void setUp() throws Exception {
071:                super .setUp();
072:
073:                mapA = createMap("key 2", "test 2", null);
074:                mapB = createMap("key 2", "test 2", null);
075:                mapDifferentValue = createMap("key 2", "XXXXXX", null);
076:                mapDifferentKey = createMap("XXXXX", "test 2", null);
077:
078:                mapInnerA = createMap("key 2", null, mapA);
079:                mapInnerB = createMap("key 2", null, mapB);
080:                mapInnerDifferentValue = createMap("key 2", null,
081:                        mapDifferentValue);
082:
083:                mapReflectionCompareKeyA = createNotEqualsKeyMap("key 2");
084:                mapReflectionCompareKeyB = createNotEqualsKeyMap("key 2");
085:                mapReflectionCompareDifferentKey = createNotEqualsKeyMap("XXXXXX");
086:
087:                reflectionComparator = ReflectionComparatorChainFactory.STRICT_COMPARATOR;
088:            }
089:
090:            /**
091:             * Test for two equal maps.
092:             */
093:            public void testGetDifference_equals() {
094:                Difference result = reflectionComparator.getDifference(mapA,
095:                        mapB);
096:                assertNull(result);
097:            }
098:
099:            /**
100:             * Test for two equal maps as an inner field of an object.
101:             */
102:            public void testGetDifference_equalsInner() {
103:                Difference result = reflectionComparator.getDifference(
104:                        mapInnerA, mapInnerB);
105:                assertNull(result);
106:            }
107:
108:            /**
109:             * Test for two maps that contain different values.
110:             */
111:            public void testGetDifference_notEqualsDifferentValues() {
112:                Difference result = reflectionComparator.getDifference(mapA,
113:                        mapDifferentValue);
114:
115:                assertNotNull(result);
116:                assertEquals("key 2", result.getFieldStack().get(0));
117:                assertEquals("test 2", result.getLeftValue());
118:                assertEquals("XXXXXX", result.getRightValue());
119:            }
120:
121:            /**
122:             * Test for two maps that have a different size.
123:             */
124:            public void testGetDifference_notEqualsDifferentSize() {
125:                Iterator<?> iterator = mapB.entrySet().iterator();
126:                iterator.next();
127:                iterator.remove();
128:
129:                Difference result = reflectionComparator.getDifference(mapA,
130:                        mapB);
131:
132:                assertNotNull(result);
133:                assertTrue(result.getFieldStack().isEmpty());
134:                assertSame(mapA, result.getLeftValue());
135:                assertSame(mapB, result.getRightValue());
136:            }
137:
138:            /**
139:             * Test for objects with inner maps that contain different values.
140:             */
141:            public void testGetDifference_notEqualsInnerDifferentValues() {
142:                Difference result = reflectionComparator.getDifference(
143:                        mapInnerA, mapInnerDifferentValue);
144:
145:                assertNotNull(result);
146:                assertEquals("key 2", result.getFieldStack().get(0));
147:                assertEquals("inner", result.getFieldStack().get(1));
148:                assertEquals("test 2", result.getLeftValue());
149:                assertEquals("XXXXXX", result.getRightValue());
150:            }
151:
152:            /**
153:             * Test for maps that contain different keys.
154:             */
155:            public void testGetDifference_notEqualsDifferentKeys() {
156:                Difference result = reflectionComparator.getDifference(mapA,
157:                        mapDifferentKey);
158:
159:                assertNotNull(result);
160:                assertTrue(result.getFieldStack().isEmpty());
161:                assertSame(mapA, result.getLeftValue());
162:                assertSame(mapDifferentKey, result.getRightValue());
163:            }
164:
165:            /**
166:             * Tests for objects with inner maps that have a different size.
167:             */
168:            public void testGetDifference_notEqualsInnerDifferentSize() {
169:                Iterator<?> iterator = mapB.entrySet().iterator();
170:                iterator.next();
171:                iterator.remove();
172:
173:                Difference result = reflectionComparator.getDifference(
174:                        mapInnerA, mapInnerB);
175:
176:                assertNotNull(result);
177:                assertEquals("key 2", result.getFieldStack().get(0));
178:                assertSame(mapA, result.getLeftValue());
179:                assertSame(mapB, result.getRightValue());
180:            }
181:
182:            /**
183:             * Tests for maps but right value is not a map.
184:             */
185:            public void testGetDifference_notEqualsRightNotMap() {
186:                Difference result = reflectionComparator.getDifference(mapA,
187:                        "Test string");
188:
189:                assertNotNull(result);
190:                assertTrue(result.getFieldStack().empty());
191:                assertSame(mapA, result.getLeftValue());
192:                assertEquals("Test string", result.getRightValue());
193:            }
194:
195:            /**
196:             * Tests for equal maps for which the keys are not equals() but are equal using reflection.
197:             * The reflection comparator uses strict reflection compare on keys.
198:             */
199:            public void testGetDifference_equalsMapComparingKeysUsingReflection() {
200:                Difference result = reflectionComparator.getDifference(
201:                        mapReflectionCompareKeyA, mapReflectionCompareKeyB);
202:                assertNull(result);
203:            }
204:
205:            /**
206:             * Tests for using reflection on key values with a different value for one of the keys.
207:             * The reflection comparator uses strict reflection compare on keys.
208:             */
209:            public void testGetDifference_notEqualsMapComparingKeysUsingReflection() {
210:                Difference result = reflectionComparator.getDifference(
211:                        mapReflectionCompareKeyA,
212:                        mapReflectionCompareDifferentKey);
213:
214:                assertNotNull(result);
215:                assertTrue(result.getFieldStack().empty());
216:                assertSame(mapReflectionCompareKeyA, result.getLeftValue());
217:                assertSame(mapReflectionCompareDifferentKey, result
218:                        .getRightValue());
219:            }
220:
221:            /**
222:             * Creates a map.
223:             *
224:             * @param keyElement2         the key for the 2nd element in the map
225:             * @param stringValueElement2 the value for the 2nd element in the map
226:             * @param innerElement2       the value for the inner array of the 2nd element in the map
227:             * @return the test map
228:             */
229:            private Map<String, Element> createMap(String keyElement2,
230:                    String stringValueElement2, Map<?, ?> innerElement2) {
231:                Map<String, Element> map = new HashMap<String, Element>();
232:                map.put("key 1", new Element("test 1", null));
233:                map.put(keyElement2, new Element(stringValueElement2,
234:                        innerElement2));
235:                map.put("key 3", new Element("test 3", null));
236:                return map;
237:            }
238:
239:            /**
240:             * Creates a map.
241:             *
242:             * @param keyElement2 the key for the 2nd element in the map
243:             * @return the test map
244:             */
245:            private Map<Element, Element> createNotEqualsKeyMap(
246:                    String keyElement2) {
247:                Map<Element, Element> map = new HashMap<Element, Element>();
248:                map
249:                        .put(new Element("key 1", null), new Element("test 1",
250:                                null));
251:                map.put(new Element(keyElement2, null), new Element("test 2",
252:                        null));
253:                return map;
254:            }
255:
256:            /**
257:             * Test class with failing equals.
258:             */
259:            private class Element {
260:
261:                /* A string value */
262:                private String string;
263:
264:                /* An inner map */
265:                private Map<?, ?> inner;
266:
267:                /**
268:                 * Creates and initializes the element.
269:                 *
270:                 * @param string the string value
271:                 * @param inner  the inner map
272:                 */
273:                public Element(String string, Map<?, ?> inner) {
274:                    this .string = string;
275:                    this .inner = inner;
276:                }
277:
278:                /**
279:                 * Gets the string value
280:                 *
281:                 * @return the value
282:                 */
283:                public String getString() {
284:                    return string;
285:                }
286:
287:                /**
288:                 * Gets the inner map
289:                 *
290:                 * @return the map
291:                 */
292:                public Map<?, ?> getInner() {
293:                    return inner;
294:                }
295:
296:                /**
297:                 * Always returns false
298:                 *
299:                 * @param o the object to compare to
300:                 */
301:                @Override
302:                public boolean equals(Object o) {
303:                    return false;
304:                }
305:            }
306:
307:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.