Source Code Cross Referenced for ObjectStreamFieldTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » io » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.io;
019:
020:        import java.io.ByteArrayInputStream;
021:        import java.io.ByteArrayOutputStream;
022:        import java.io.IOException;
023:        import java.io.InputStream;
024:        import java.io.NotActiveException;
025:        import java.io.ObjectInputStream;
026:        import java.io.ObjectOutputStream;
027:        import java.io.ObjectStreamClass;
028:        import java.io.ObjectStreamField;
029:        import java.io.Serializable;
030:        import java.io.StreamCorruptedException;
031:        import java.util.Date;
032:
033:        public class ObjectStreamFieldTest extends junit.framework.TestCase {
034:
035:            static class DummyClass implements  Serializable {
036:                private static final long serialVersionUID = 999999999999998L;
037:
038:                long bam = 999L;
039:
040:                int ham = 9999;
041:
042:                int sam = 8888;
043:
044:                Object hola = new Object();
045:
046:                public static long getUID() {
047:                    return serialVersionUID;
048:                }
049:            }
050:
051:            ObjectStreamClass osc;
052:
053:            ObjectStreamField hamField;
054:
055:            ObjectStreamField samField;
056:
057:            ObjectStreamField bamField;
058:
059:            ObjectStreamField holaField;
060:
061:            /**
062:             * @tests java.io.ObjectStreamField#ObjectStreamField(java.lang.String,
063:             *        java.lang.Class)
064:             */
065:            public void test_ConstructorLjava_lang_StringLjava_lang_Class() {
066:                assertTrue("Used to test", true);
067:            }
068:
069:            public void test_equalsLjava_lang_Object() {
070:                // Regression test for HARMONY-4273
071:                assertTrue(samField.equals(samField));
072:                assertFalse(samField.equals(hamField));
073:                assertFalse(samField.equals("fish"));
074:                assertFalse(samField.equals(null));
075:            }
076:
077:            /**
078:             * @tests java.io.ObjectStreamField#compareTo(java.lang.Object)
079:             */
080:            public void test_compareToLjava_lang_Object() {
081:                assertTrue("Object compared to int did not return > 0",
082:                        holaField.compareTo(hamField) > 0);
083:                assertEquals("Int compared to itself did not return 0", 0,
084:                        hamField.compareTo(hamField));
085:                assertTrue("(Int)ham compared to (Int)sam did not return < 0",
086:                        hamField.compareTo(samField) < 0);
087:            }
088:
089:            /**
090:             * @tests java.io.ObjectStreamField#getName()
091:             */
092:            public void test_getName() {
093:                assertEquals("Field did not return correct name", "hola",
094:                        holaField.getName());
095:            }
096:
097:            /**
098:             * @tests java.io.ObjectStreamField#getOffset()
099:             */
100:            public void test_getOffset() {
101:                ObjectStreamField[] osfArray;
102:                osfArray = osc.getFields();
103:                assertTrue("getOffset did not return reasonable values",
104:                        osfArray[0].getOffset() != osfArray[1].getOffset());
105:                assertEquals(
106:                        "getOffset for osfArray[0].getOffset() did not return 0",
107:                        0, osfArray[0].getOffset());
108:                assertEquals("osfArray[1].getOffset() did not return	8", 8,
109:                        osfArray[1].getOffset());
110:                assertEquals("osfArray[2].getOffset() did not return 12", 12,
111:                        osfArray[2].getOffset());
112:            }
113:
114:            /**
115:             * @tests java.io.ObjectStreamField#getType()
116:             */
117:            public void test_getType() {
118:                assertTrue("getType on an Object field did not answer Object",
119:                        holaField.getType().equals(Object.class));
120:            }
121:
122:            /**
123:             * @tests java.io.ObjectStreamField#getTypeCode()
124:             */
125:            public void test_getTypeCode() {
126:                assertEquals(
127:                        "getTypeCode on an Object field did not answer 'L'",
128:                        'L', holaField.getTypeCode());
129:                assertEquals("getTypeCode on a long field did not answer 'J'",
130:                        'J', bamField.getTypeCode());
131:            }
132:
133:            /**
134:             * @tests java.io.ObjectStreamField#getTypeString()
135:             */
136:            public void test_getTypeString() {
137:                assertTrue("getTypeString returned: "
138:                        + holaField.getTypeString(), holaField.getTypeString()
139:                        .indexOf("Object") >= 0);
140:                assertNull("Primitive types' strings should be null", hamField
141:                        .getTypeString());
142:
143:                ObjectStreamField osf = new ObjectStreamField("s",
144:                        String.class, true);
145:                assertTrue(osf.getTypeString() == "Ljava/lang/String;");
146:            }
147:
148:            /**
149:             * @tests java.io.ObjectStreamField#toString()
150:             */
151:            public void test_toString() {
152:                assertTrue("toString on a long returned: "
153:                        + bamField.toString(), bamField.toString().indexOf(
154:                        "bam") >= 0);
155:            }
156:
157:            /**
158:             * @tests java.io.ObjectStreamField#getType()
159:             */
160:            public void test_getType_Deserialized() throws IOException,
161:                    ClassNotFoundException {
162:                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
163:                ObjectOutputStream oos = new ObjectOutputStream(baos);
164:                oos.writeObject(new SerializableObject());
165:                oos.close();
166:                baos.close();
167:
168:                byte[] bytes = baos.toByteArray();
169:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
170:                ObjectInputStream ois = new ObjectInputStream(bais);
171:                SerializableObject obj = (SerializableObject) ois.readObject();
172:
173:                ObjectStreamClass oc = obj.getObjectStreamClass();
174:                ObjectStreamField field = oc.getField("i");
175:                assertEquals(Object.class, field.getType());
176:            }
177:
178:            /**
179:             * @tests java.io.ObjectStreamField#getType()
180:             */
181:            public void test_getType_MockObjectInputStream()
182:                    throws IOException, ClassNotFoundException {
183:                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
184:                ObjectOutputStream oos = new ObjectOutputStream(baos);
185:                oos.writeObject(new SerializableObject());
186:                oos.close();
187:                baos.close();
188:
189:                byte[] bytes = baos.toByteArray();
190:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
191:                MockObjectInputStream ois = new MockObjectInputStream(bais);
192:                ois.readObject();
193:
194:                ObjectStreamClass oc = ois.getObjectStreamClass();
195:                ObjectStreamField field = oc.getField("i");
196:                assertEquals(Object.class, field.getType());
197:            }
198:
199:            public void test_isUnshared() throws Exception {
200:                SerializableObject2 obj = new SerializableObject2();
201:
202:                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
203:                ObjectOutputStream oos = new ObjectOutputStream(baos);
204:                oos.writeObject(obj);
205:                oos.close();
206:                baos.close();
207:                byte[] bytes = baos.toByteArray();
208:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
209:                ObjectInputStream ois = new ObjectInputStream(bais);
210:                SerializableObject2 newObj = (SerializableObject2) ois
211:                        .readObject();
212:
213:                ObjectInputStream.GetField getField = newObj.getGetField();
214:                ObjectStreamClass objectStreamClass = getField
215:                        .getObjectStreamClass();
216:
217:                assertTrue(objectStreamClass.getField("i").isUnshared());
218:                assertFalse(objectStreamClass.getField("d").isUnshared());
219:                assertTrue(objectStreamClass.getField("s").isUnshared());
220:
221:                assertEquals(1000, getField.get("i", null));
222:                assertEquals(SerializableObject2.today, getField.get("d", null));
223:                assertEquals("Richard", getField.get("s", null));
224:
225:                assertTrue(objectStreamClass.getField("s").getTypeString() == "Ljava/lang/String;");
226:
227:                assertEquals(0, objectStreamClass.getField("d").getOffset());
228:                assertEquals(1, objectStreamClass.getField("i").getOffset());
229:                assertEquals(2, objectStreamClass.getField("s").getOffset());
230:            }
231:
232:            /**
233:             * Sets up the fixture, for example, open a network connection. This method
234:             * is called before a test is executed.
235:             */
236:            protected void setUp() {
237:                osc = ObjectStreamClass.lookup(DummyClass.class);
238:                bamField = osc.getField("bam");
239:                samField = osc.getField("sam");
240:                hamField = osc.getField("ham");
241:                holaField = osc.getField("hola");
242:            }
243:        }
244:
245:        class SerializableObject implements  Serializable {
246:            public ObjectInputStream.GetField getField = null;
247:
248:            private static final long serialVersionUID = -2953957835918368056L;
249:
250:            public Date d;
251:
252:            public Integer i;
253:
254:            public Exception e;
255:
256:            public SerializableObject() {
257:                d = new Date();
258:                i = new Integer(1);
259:                e = new Exception("e");
260:            }
261:
262:            private void writeObject(ObjectOutputStream o) throws IOException {
263:                o.putFields().put("d", new Date());
264:                o.putFields().put("i", new Integer(11));
265:                o.writeFields();
266:            }
267:
268:            private void readObject(ObjectInputStream in)
269:                    throws NotActiveException, IOException,
270:                    ClassNotFoundException {
271:                getField = in.readFields();
272:                d = (Date) getField.get("d", null);
273:                i = (Integer) getField.get("i", null);
274:            }
275:
276:            public ObjectStreamClass getObjectStreamClass() {
277:                return getField.getObjectStreamClass();
278:            }
279:        }
280:
281:        class MockObjectInputStream extends ObjectInputStream {
282:            private ObjectStreamClass temp = null;
283:
284:            public MockObjectInputStream() throws SecurityException,
285:                    IOException {
286:                super ();
287:            }
288:
289:            public MockObjectInputStream(InputStream in)
290:                    throws StreamCorruptedException, IOException {
291:                super (in);
292:            }
293:
294:            public ObjectStreamClass readClassDescriptor() throws IOException,
295:                    ClassNotFoundException {
296:                ObjectStreamClass osc = super .readClassDescriptor();
297:                // To get the ObjectStreamClass of SerializableObject
298:                if (osc.getSerialVersionUID() == -2953957835918368056L) {
299:                    temp = osc;
300:                }
301:                return osc;
302:            }
303:
304:            public ObjectStreamClass getObjectStreamClass() {
305:                return temp;
306:            }
307:        }
308:
309:        class SerializableObject2 implements  Serializable {
310:
311:            private static final long serialVersionUID = 1L;
312:
313:            private static final ObjectStreamField[] serialPersistentFields = {
314:                    new ObjectStreamField("i", Integer.class, true),
315:                    new ObjectStreamField("d", Date.class, false),
316:                    new ObjectStreamField("s", String.class, true), };
317:
318:            private ObjectInputStream.GetField getField;
319:
320:            public static Date today = new Date(1172632429156l);
321:
322:            public ObjectInputStream.GetField getGetField() {
323:                return getField;
324:            }
325:
326:            private void writeObject(ObjectOutputStream o) throws IOException {
327:                ObjectOutputStream.PutField putField = o.putFields();
328:                putField.put("i", new Integer(1000));
329:                putField.put("d", today);
330:                putField.put("s", "Richard");
331:                o.writeFields();
332:            }
333:
334:            private void readObject(ObjectInputStream in)
335:                    throws NotActiveException, IOException,
336:                    ClassNotFoundException {
337:                getField = in.readFields();
338:            }
339:        }
w_w__w__.__ja__va__2___s_.__c__o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.