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: }
|