Source Code Cross Referenced for MapFieldTester.java in  » Database-ORM » TJDO » com » triactive » jdo » test » 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 » Database ORM » TJDO » com.triactive.jdo.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002 (C) TJDO.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the TJDO License version 1.0.
006:         * See the terms of the TJDO License in the documentation provided with this software.
007:         *
008:         * $Id: MapFieldTester.java,v 1.3 2002/11/08 05:06:27 jackknifebarber Exp $
009:         */
010:
011:        package com.triactive.jdo.test;
012:
013:        import java.util.Arrays;
014:        import java.util.HashMap;
015:        import java.util.HashSet;
016:        import java.util.Iterator;
017:        import java.util.List;
018:        import java.util.Map;
019:        import javax.jdo.InstanceCallbacks;
020:        import javax.jdo.JDOHelper;
021:        import javax.jdo.PersistenceManager;
022:        import junit.framework.Assert;
023:
024:        /**
025:         * A test object for testing <code>java.util.Map</code> fields.
026:         *
027:         * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
028:         * @version $Revision: 1.3 $
029:         */
030:
031:        public class MapFieldTester implements  InstanceCallbacks {
032:            private Map personsByNumber = new HashMap();
033:            private Map husbandsByWife = new HashMap();
034:            private Map wivesByHusband = new HashMap();
035:
036:            /**
037:             * Constructs an empty map field tester object.
038:             */
039:
040:            public MapFieldTester() {
041:            }
042:
043:            /**
044:             * Adds the two <code>Person</code> objects to the test <code>Map</code>
045:             * fields.  A test failure is reported if either of the objects are already
046:             * present in the maps.
047:             *
048:             * @param husband   A test <code>Person</code> object.  Can contain any
049:             *                  arbitrary field values.
050:             * @param wife      A test <code>Person</code> object.  Can contain any
051:             *                  arbitrary field values.
052:             * @param a         Used to report test assertion failures.
053:             */
054:
055:            public void addPair(Person husband, Person wife) {
056:                Long hNum = new Long(husband.getPersonNum());
057:                Long wNum = new Long(wife.getPersonNum());
058:
059:                Assert.assertEquals(null, personsByNumber.put(hNum, husband));
060:                Assert.assertEquals(null, personsByNumber.put(wNum, wife));
061:                Assert.assertTrue(personsByNumber.containsKey(hNum));
062:                Assert.assertTrue(personsByNumber.containsKey(wNum));
063:                Assert.assertTrue(personsByNumber.containsValue(husband));
064:                Assert.assertTrue(personsByNumber.containsValue(wife));
065:
066:                Assert.assertEquals(null, husbandsByWife.put(wife, husband));
067:                Assert.assertEquals(null, wivesByHusband.put(husband, wife));
068:                Assert.assertTrue(husbandsByWife.containsKey(wife));
069:                Assert.assertTrue(husbandsByWife.containsValue(husband));
070:                Assert.assertTrue(wivesByHusband.containsKey(husband));
071:                Assert.assertTrue(wivesByHusband.containsValue(wife));
072:            }
073:
074:            /**
075:             * Adds the two <code>Person</code> objects to the test <code>Map</code>
076:             * fields.  A test failure is reported if either of the objects are already
077:             * present in the maps.
078:             *
079:             * @param husband   A test <code>Person</code> object.  Can contain any
080:             *                  arbitrary field values.
081:             * @param wife      A test <code>Person</code> object.  Can contain any
082:             *                  arbitrary field values.
083:             * @param a         Used to report test assertion failures.
084:             */
085:
086:            public void removePair(Person husband, Person wife) {
087:                Long hNum = new Long(husband.getPersonNum());
088:                Long wNum = new Long(wife.getPersonNum());
089:
090:                Assert.assertEquals(husband, personsByNumber.remove(hNum));
091:                Assert.assertEquals(wife, personsByNumber.remove(wNum));
092:                Assert.assertTrue(!personsByNumber.containsKey(hNum));
093:                Assert.assertTrue(!personsByNumber.containsKey(wNum));
094:                Assert.assertTrue(!personsByNumber.containsValue(husband));
095:                Assert.assertTrue(!personsByNumber.containsValue(wife));
096:
097:                Assert.assertEquals(husband, husbandsByWife.remove(wife));
098:                Assert.assertEquals(wife, wivesByHusband.remove(husband));
099:                Assert.assertTrue(!husbandsByWife.containsKey(wife));
100:                Assert.assertTrue(!husbandsByWife.containsValue(husband));
101:                Assert.assertTrue(!wivesByHusband.containsKey(husband));
102:                Assert.assertTrue(!wivesByHusband.containsValue(wife));
103:
104:                if (JDOHelper.isPersistent(husband))
105:                    JDOHelper.getPersistenceManager(husband).deletePersistent(
106:                            husband);
107:
108:                if (JDOHelper.isPersistent(wife))
109:                    JDOHelper.getPersistenceManager(wife)
110:                            .deletePersistent(wife);
111:            }
112:
113:            /**
114:             * Compares the values in the given arrays of test <code>Person</code>
115:             * objects to the objects in the map fields.  The map fields are validated
116:             * using a variety of techniques that exercise most of the <code>Map</code>
117:             * interface to assert that the map contents are correct.
118:             *
119:             * @param husbands  A set of husband objects that are expected to equal the
120:             *                  husband objects in the map fields.
121:             * @param wives     A set of wife objects that are expected to equal the
122:             *                  wife objects in the map fields.  Must be the same length
123:             *                  as the husband array.
124:             * @param a         Used to report test assertion failures.
125:             */
126:
127:            public void assertMapsEqual(List husbands, List wives) {
128:                Assert.assertEquals(husbands.size(), wives.size());
129:                Assert.assertEquals(husbands.size() + wives.size(),
130:                        personsByNumber.size());
131:                Assert.assertEquals(husbands.size(), husbandsByWife.size());
132:                Assert.assertEquals(wives.size(), wivesByHusband.size());
133:
134:                Person[] pHusbands = new Person[husbands.size()];
135:                Person[] pWives = new Person[wives.size()];
136:                HashMap personsByNumber1 = new HashMap();
137:
138:                for (int i = 0; i < husbands.size(); ++i) {
139:                    Person expected;
140:
141:                    expected = (Person) husbands.get(i);
142:                    pHusbands[i] = (Person) personsByNumber.get(new Long(
143:                            expected.getPersonNum()));
144:                    expected.assertEquals(pHusbands[i]);
145:                    Assert.assertTrue(husbandsByWife
146:                            .containsValue(pHusbands[i]));
147:                    Assert.assertTrue(wivesByHusband.containsKey(pHusbands[i]));
148:
149:                    expected = (Person) wives.get(i);
150:                    pWives[i] = (Person) personsByNumber.get(new Long(expected
151:                            .getPersonNum()));
152:                    expected.assertEquals(pWives[i]);
153:                    Assert.assertTrue(husbandsByWife.containsKey(pWives[i]));
154:                    Assert.assertTrue(wivesByHusband.containsValue(pWives[i]));
155:
156:                    Assert
157:                            .assertEquals(JDOHelper.getObjectId(pHusbands[i]),
158:                                    JDOHelper.getObjectId(husbandsByWife
159:                                            .get(pWives[i])));
160:
161:                    Assert.assertEquals(JDOHelper.getObjectId(pWives[i]),
162:                            JDOHelper.getObjectId(wivesByHusband
163:                                    .get(pHusbands[i])));
164:
165:                    personsByNumber1.put(new Long(pHusbands[i].getPersonNum()),
166:                            pHusbands[i]);
167:                    personsByNumber1.put(new Long(pWives[i].getPersonNum()),
168:                            pWives[i]);
169:                }
170:
171:                /*
172:                 * Use containsAll() to assert that the collections returned by keySet()
173:                 * and values() contain the expected elements.
174:                 */
175:                Assert.assertTrue(husbandsByWife.keySet().containsAll(
176:                        Arrays.asList(pWives)));
177:                Assert.assertTrue(husbandsByWife.values().containsAll(
178:                        Arrays.asList(pHusbands)));
179:                Assert.assertTrue(wivesByHusband.keySet().containsAll(
180:                        Arrays.asList(pHusbands)));
181:                Assert.assertTrue(wivesByHusband.values().containsAll(
182:                        Arrays.asList(pWives)));
183:
184:                Assert.assertEquals(personsByNumber, personsByNumber1);
185:
186:                /*
187:                 * Iterate over the entire entry set of personsByNumber and assert that
188:                 * every expected entry is returned.
189:                 */
190:                Iterator i = personsByNumber.entrySet().iterator();
191:
192:                while (i.hasNext()) {
193:                    Map.Entry entry = (Map.Entry) i.next();
194:
195:                    Person p = (Person) personsByNumber1.get(entry.getKey());
196:
197:                    Assert.assertEquals(entry.getKey(), new Long(p
198:                            .getPersonNum()));
199:                    Assert.assertEquals(entry.getValue(), p);
200:
201:                    personsByNumber1.remove(entry.getKey());
202:                }
203:
204:                Assert.assertEquals(0, personsByNumber1.size());
205:            }
206:
207:            public int hashCode() {
208:                Object id = JDOHelper.getObjectId(this );
209:
210:                return id == null ? super .hashCode() : id.hashCode();
211:            }
212:
213:            public boolean equals(Object o) {
214:                if (o == this )
215:                    return true;
216:
217:                Object id = JDOHelper.getObjectId(this );
218:
219:                return id == null ? super .equals(o) : id.equals(JDOHelper
220:                        .getObjectId(o));
221:            }
222:
223:            public void jdoPreStore() {
224:            }
225:
226:            public void jdoPostLoad() {
227:            }
228:
229:            public void jdoPreClear() {
230:            }
231:
232:            public void jdoPreDelete() {
233:                PersistenceManager pm = JDOHelper.getPersistenceManager(this );
234:
235:                /* Delete all dependent Person objects. */
236:                HashSet persons = new HashSet(personsByNumber.values());
237:
238:                personsByNumber.clear();
239:                husbandsByWife.clear();
240:                wivesByHusband.clear();
241:
242:                pm.deletePersistentAll(persons);
243:            }
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.