Source Code Cross Referenced for CollectionComparerTestBase.java in  » Net » Terracotta » com » tc » test » collections » 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 » Net » Terracotta » com.tc.test.collections 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice.  All rights reserved.
003:         */package com.tc.test.collections;
004:
005:        import org.apache.commons.lang.builder.HashCodeBuilder;
006:
007:        import com.tc.test.TCTestCase;
008:        import com.tc.util.EqualityComparator;
009:        import com.tc.util.Stringifier;
010:
011:        import java.util.ArrayList;
012:        import java.util.Arrays;
013:        import java.util.HashSet;
014:        import java.util.Iterator;
015:        import java.util.List;
016:        import java.util.Set;
017:
018:        /**
019:         * Base for all tests that test {@link CollectionComparer}s.
020:         */
021:        public class CollectionComparerTestBase extends TCTestCase {
022:
023:            protected static boolean CASE_INSENSITIVE = false;
024:
025:            protected static class MyObj {
026:                public final String value;
027:
028:                public MyObj(String value) {
029:                    this .value = value;
030:                }
031:
032:                public String toString() {
033:                    return "X" + this .value + "Y";
034:                }
035:
036:                public int hashCode() {
037:                    return new HashCodeBuilder().append(this .value)
038:                            .toHashCode();
039:                }
040:            }
041:
042:            private static class MyStringifier implements  Stringifier {
043:                private static String PREFIX = "A";
044:                private static String SUFFIX = "B";
045:
046:                public String toString(Object o) {
047:                    if (o == null)
048:                        return "__NULL__";
049:                    else
050:                        return PREFIX + o.toString() + SUFFIX;
051:                }
052:            }
053:
054:            private static class MyComparator implements  EqualityComparator {
055:                public boolean isEquals(Object one, Object two) {
056:                    if (!(one instanceof  MyObj))
057:                        return false;
058:                    if (!(two instanceof  MyObj))
059:                        return false;
060:
061:                    MyObj myOne = (MyObj) one;
062:                    MyObj myTwo = (MyObj) two;
063:
064:                    if ((one == null) != (two == null))
065:                        return false;
066:                    if (one == null)
067:                        return true;
068:
069:                    if (CASE_INSENSITIVE)
070:                        return myOne.value.equalsIgnoreCase(myTwo.value);
071:                    else
072:                        return myOne.value.equals(myTwo.value);
073:                }
074:            }
075:
076:            protected static final CollectionMismatch[] NO_MISMATCHES = new CollectionMismatch[0];
077:
078:            protected Stringifier describer;
079:            protected EqualityComparator equalityComparator;
080:            protected CollectionComparer comparer;
081:
082:            public void setUp() throws Exception {
083:                this .describer = new MyStringifier();
084:                this .equalityComparator = new MyComparator();
085:            }
086:
087:            public void testEmpty() throws Exception {
088:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
089:                        new Object[0], new Object[0]));
090:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
091:                        new Object[0], iterator(new Object[0])));
092:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
093:                        new Object[0], list(new Object[0])));
094:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
095:                        new Object[0], set(new Object[0])));
096:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
097:                        iterator(new Object[0]), new Object[0]));
098:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
099:                        list(new Object[0]), new Object[0]));
100:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
101:                        set(new Object[0]), new Object[0]));
102:            }
103:
104:            public void testSingleEquals() throws Exception {
105:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
106:                        new Object[] { new MyObj("foo") },
107:                        new Object[] { new MyObj("foo") }));
108:            }
109:
110:            public void testDifferentCollectionTypes() throws Exception {
111:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
112:                        new Object[] { new MyObj("foo") },
113:                        new Object[] { new MyObj("foo") }));
114:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
115:                        new Object[] { new MyObj("foo") },
116:                        iterator(new Object[] { new MyObj("foo") })));
117:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
118:                        new Object[] { new MyObj("foo") },
119:                        list(new Object[] { new MyObj("foo") })));
120:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
121:                        new Object[] { new MyObj("foo") },
122:                        set(new Object[] { new MyObj("foo") })));
123:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
124:                        iterator(new Object[] { new MyObj("foo") }),
125:                        new Object[] { new MyObj("foo") }));
126:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
127:                        list(new Object[] { new MyObj("foo") }),
128:                        new Object[] { new MyObj("foo") }));
129:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
130:                        set(new Object[] { new MyObj("foo") }),
131:                        new Object[] { new MyObj("foo") }));
132:            }
133:
134:            public void testSingleDoesNotEqualNothing() throws Exception {
135:                MyObj missingObj = new MyObj("foo");
136:
137:                checkMismatches(
138:                        new CollectionMismatch[] { new MissingObjectCollectionMismatch(
139:                                missingObj, true, 0, this .describer) },
140:                        this .comparer.getMismatches(
141:                                new Object[] { missingObj }, new Object[0]));
142:            }
143:
144:            public void testNothingDoesNotEqualSingle() throws Exception {
145:                MyObj missingObj = new MyObj("foo");
146:
147:                checkMismatches(
148:                        new CollectionMismatch[] { new MissingObjectCollectionMismatch(
149:                                missingObj, false, 0, this .describer) },
150:                        this .comparer.getMismatches(new Object[0],
151:                                new Object[] { missingObj }));
152:            }
153:
154:            public void testSameCollections() throws Exception {
155:                Object[] collectionOne = new Object[] { new MyObj("a"),
156:                        new MyObj("q"), new MyObj("c"), new MyObj("BBBBBBB") };
157:                Object[] collectionTwo = new Object[] { new MyObj("a"),
158:                        new MyObj("q"), new MyObj("c"), new MyObj("BBBBBBB") };
159:
160:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
161:                        collectionOne, collectionTwo));
162:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
163:                        collectionTwo, collectionOne));
164:
165:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
166:                        iterator(collectionOne), collectionTwo));
167:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
168:                        iterator(collectionTwo), collectionOne));
169:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
170:                        collectionOne, iterator(collectionTwo)));
171:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
172:                        collectionTwo, iterator(collectionOne)));
173:
174:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
175:                        list(collectionOne), collectionTwo));
176:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
177:                        list(collectionTwo), collectionOne));
178:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
179:                        collectionOne, list(collectionTwo)));
180:                checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
181:                        collectionTwo, list(collectionOne)));
182:            }
183:
184:            public void testUsesStringifier() throws Exception {
185:                String oldPrefix = MyStringifier.PREFIX;
186:                String oldSuffix = MyStringifier.SUFFIX;
187:
188:                try {
189:                    MyStringifier.PREFIX = "FOO";
190:                    MyStringifier.SUFFIX = "BAR";
191:
192:                    CollectionMismatch[] actual = this .comparer.getMismatches(
193:                            new Object[] { new MyObj("P") },
194:                            new Object[] { new MyObj("Q") });
195:
196:                    StringBuffer buf = new StringBuffer();
197:                    for (int i = 0; i < actual.length; ++i) {
198:                        buf.append(actual[i].toString());
199:                    }
200:
201:                    String theString = buf.toString();
202:
203:                    assertTrue(theString.indexOf("FOOXPYBAR") >= 0);
204:                    assertTrue(theString.indexOf("FOOXQYBAR") >= 0);
205:                } finally {
206:                    MyStringifier.PREFIX = oldPrefix;
207:                    MyStringifier.SUFFIX = oldSuffix;
208:                }
209:            }
210:
211:            public void testExtraElements() throws Exception {
212:                MyObj extra = new MyObj("qz");
213:
214:                checkMismatches(
215:                        new CollectionMismatch[] { new MissingObjectCollectionMismatch(
216:                                extra, true, 1, this .describer) },
217:                        this .comparer.getMismatches(new Object[] {
218:                                new MyObj("a"), extra },
219:                                new Object[] { new MyObj("a") }));
220:
221:                checkMismatches(
222:                        new CollectionMismatch[] { new MissingObjectCollectionMismatch(
223:                                extra, false, 1, this .describer) },
224:                        this .comparer.getMismatches(new Object[] { new MyObj(
225:                                "a") }, new Object[] { new MyObj("a"), extra }));
226:            }
227:
228:            public void testChecksArguments() throws Exception {
229:                try {
230:                    this .comparer.getMismatches(new Object[0], null);
231:                    fail("Didn't get NPE on no second collection");
232:                } catch (NullPointerException npe) {
233:                    // ok
234:                }
235:
236:                try {
237:                    this .comparer.getMismatches(null, new Object[0]);
238:                    fail("Didn't get NPE on no first collection");
239:                } catch (NullPointerException npe) {
240:                    // ok
241:                }
242:
243:                try {
244:                    this .comparer.getMismatches("foo", new Object[0]);
245:                    fail("Didn't get IAE on bogus first argument");
246:                } catch (IllegalArgumentException iae) {
247:                    // ok
248:                }
249:
250:                try {
251:                    this .comparer.getMismatches(new Object[0], "foo");
252:                    fail("Didn't get IAE on bogus second argument");
253:                } catch (IllegalArgumentException iae) {
254:                    // ok
255:                }
256:            }
257:
258:            private Iterator iterator(Object[] data) {
259:                return Arrays.asList(data).iterator();
260:            }
261:
262:            private List list(Object[] data) {
263:                ArrayList out = new ArrayList();
264:                out.addAll(Arrays.asList(data));
265:                return out;
266:            }
267:
268:            private Set set(Object[] data) {
269:                HashSet out = new HashSet();
270:                out.addAll(Arrays.asList(data));
271:                return out;
272:            }
273:
274:            // NOTE 2004-12-27 andrew -- This compares in an ordered fashion. This could cause problems if the implementation is
275:            // changed to return CollectionMismatches in a different order; if so, we'll need to enhance this method to not care
276:            // about order. However, we should NOT use a CollectionComparer to do that, for the obvious reasons -- you don't want
277:            // to use the code you're testing as part of the test's infrastructure itself.
278:            protected final void checkMismatches(CollectionMismatch[] expected,
279:                    CollectionMismatch[] actual) {
280:                StringBuffer out = new StringBuffer();
281:
282:                for (int i = 0; i < expected.length; ++i) {
283:                    if (i >= actual.length) {
284:                        out.append("Missing an expected mismatch at index " + i
285:                                + ": " + expected[i] + "\n");
286:                    } else if (!expected[i].equals(actual[i])) {
287:                        out.append("Wrong mismatch at index " + i
288:                                + ": expected " + expected[i] + ", but got "
289:                                + actual[i] + "\n");
290:                    }
291:                }
292:
293:                if (actual.length > expected.length) {
294:                    for (int i = expected.length; i < actual.length; ++i) {
295:                        out.append("Got an unexpected mismatch at index " + i
296:                                + ": " + actual[i]);
297:                    }
298:                }
299:
300:                if (out.toString().length() > 0) {
301:                    fail(out.toString());
302:                }
303:            }
304:
305:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.