Source Code Cross Referenced for SerializationTest.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 2004 (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: SerializationTest.java,v 1.4 2004/03/22 04:58:13 jackknifebarber Exp $
009:         */
010:
011:        package com.triactive.jdo.test;
012:
013:        import com.triactive.jdo.SCO;
014:        import java.io.BufferedInputStream;
015:        import java.io.BufferedOutputStream;
016:        import java.io.ByteArrayInputStream;
017:        import java.io.ByteArrayOutputStream;
018:        import java.io.IOException;
019:        import java.io.ObjectInputStream;
020:        import java.io.ObjectOutputStream;
021:        import javax.jdo.PersistenceManager;
022:        import javax.jdo.Transaction;
023:        import org.apache.log4j.Category;
024:
025:        /**
026:         * Tests the serializability of persistent objects.
027:         *
028:         * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
029:         * @version $Revision: 1.4 $
030:         */
031:
032:        public class SerializationTest extends PersistenceTestCase {
033:            private static final Category LOG = Category
034:                    .getInstance(SerializationTest.class);
035:            private static final int TEST_OBJECT_COUNT = 10;
036:
037:            private PersistenceManager pm;
038:            private Transaction tx;
039:            private boolean schemaInitialized = false;
040:
041:            public SerializationTest(String name) {
042:                super (name);
043:            }
044:
045:            protected void setUp() throws Exception {
046:                super .setUp();
047:
048:                if (!schemaInitialized) {
049:                    addClassesToSchema(new Class[] { Widget.class,
050:                            DateWidget.class, DecimalWidget.class,
051:                            FloatWidget.class, StringWidget.class,
052:                            BinaryWidget.class, CollectionWidget.class,
053:                            SetWidget.class, ElementWidget.class,
054:                            MapWidget.class, ValueWidget.class,
055:                            HashtableWidget.class });
056:
057:                    schemaInitialized = true;
058:                }
059:
060:                pm = pmf.getPersistenceManager();
061:                tx = pm.currentTransaction();
062:
063:                tx.setRetainValues(true);
064:            }
065:
066:            protected void tearDown() throws Exception {
067:                if (tx.isActive())
068:                    tx.rollback();
069:
070:                pm.close();
071:
072:                super .tearDown();
073:            }
074:
075:            public void testStringWidgets() throws Exception {
076:                runSerializationTestFor(StringWidget.class);
077:            }
078:
079:            public void testBinaryWidgets() throws Exception {
080:                runSerializationTestFor(BinaryWidget.class);
081:            }
082:
083:            public void testDateWidgets() throws Exception {
084:                runSerializationTestFor(DateWidget.class);
085:            }
086:
087:            public void testDecimalWidgets() throws Exception {
088:                runSerializationTestFor(DecimalWidget.class);
089:            }
090:
091:            public void testFloatWidgets() throws Exception {
092:                runSerializationTestFor(FloatWidget.class);
093:            }
094:
095:            public void testCollectionWidgets() throws Exception {
096:                runSerializationTestFor(CollectionWidget.class);
097:            }
098:
099:            public void testSetWidgets() throws Exception {
100:                runSerializationTestFor(SetWidget.class);
101:            }
102:
103:            public void testMapWidgets() throws Exception {
104:                runSerializationTestFor(MapWidget.class);
105:            }
106:
107:            public void testHashtableWidgets() throws Exception {
108:                runSerializationTestFor(HashtableWidget.class);
109:            }
110:
111:            public void runSerializationTestFor(Class c) throws Exception {
112:                TestObject[] persistent = new TestObject[TEST_OBJECT_COUNT];
113:
114:                tx.begin();
115:
116:                for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
117:                    persistent[i] = (TestObject) c.newInstance();
118:                    persistent[i].fillRandom();
119:                }
120:
121:                pm.makePersistentAll(persistent);
122:
123:                tx.commit();
124:
125:                TestObject[] deserialized = new TestObject[TEST_OBJECT_COUNT];
126:                toObjects(toByteArray(persistent), deserialized);
127:
128:                for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
129:                    TestObject p = persistent[i];
130:                    TestObject d = deserialized[i];
131:
132:                    assertTrue(
133:                            "Object did not deserialize into a different object",
134:                            p != d);
135:
136:                    if (d instanceof  HasSCOFields) {
137:                        Object[] vals = ((HasSCOFields) d).getSCOFieldValues();
138:
139:                        for (int j = 0; j < vals.length; ++j)
140:                            assertTrue(
141:                                    "Deserialized object has a wrapper object in an SCO field",
142:                                    !(vals[j] instanceof  SCO));
143:                    }
144:
145:                    assertFieldsEqual(p, d);
146:                }
147:
148:                tx.begin();
149:                pm.deletePersistentAll(persistent);
150:                tx.commit();
151:            }
152:
153:            private void assertFieldsEqual(TestObject expected,
154:                    TestObject actual) {
155:                assertTrue("Incorrect field values in object, was " + actual
156:                        + ", should be " + expected, actual.compareTo(expected));
157:            }
158:
159:            private static byte[] toByteArray(Object[] objs) throws IOException {
160:                ByteArrayOutputStream bout = new ByteArrayOutputStream();
161:                ObjectOutputStream oout = new ObjectOutputStream(
162:                        new BufferedOutputStream(bout));
163:
164:                for (int i = 0; i < objs.length; ++i)
165:                    oout.writeObject(objs[i]);
166:
167:                oout.close();
168:
169:                return bout.toByteArray();
170:            }
171:
172:            private static void toObjects(byte[] ba, Object[] objs)
173:                    throws IOException, ClassNotFoundException {
174:                ByteArrayInputStream bin = new ByteArrayInputStream(ba);
175:                ObjectInputStream oin = new ObjectInputStream(
176:                        new BufferedInputStream(bin));
177:
178:                for (int i = 0; i < objs.length; ++i)
179:                    objs[i] = oin.readObject();
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.