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.ObjectStreamClass;
021: import java.io.ObjectStreamField;
022: import java.io.Serializable;
023: import java.lang.reflect.Proxy;
024:
025: import junit.framework.TestCase;
026:
027: public class ObjectStreamClassTest extends TestCase {
028:
029: static class DummyClass implements Serializable {
030: private static final long serialVersionUID = 999999999999999L;
031:
032: long bam = 999L;
033:
034: int ham = 9999;
035:
036: public static long getUID() {
037: return serialVersionUID;
038: }
039: }
040:
041: /**
042: * @tests java.io.ObjectStreamClass#forClass()
043: */
044: public void test_forClass() {
045: // Need to test during serialization to be sure an instance is
046: // returned
047: ObjectStreamClass osc = ObjectStreamClass
048: .lookup(DummyClass.class);
049: assertTrue("forClass returned an object: " + osc.forClass(),
050: osc.forClass().equals(DummyClass.class));
051: }
052:
053: /**
054: * @tests java.io.ObjectStreamClass#getField(java.lang.String)
055: */
056: public void test_getFieldLjava_lang_String() {
057: ObjectStreamClass osc = ObjectStreamClass
058: .lookup(DummyClass.class);
059: assertEquals("getField did not return correct field", 'J', osc
060: .getField("bam").getTypeCode());
061: assertNull("getField did not null for non-existent field", osc
062: .getField("wham"));
063: }
064:
065: /**
066: * @tests java.io.ObjectStreamClass#getFields()
067: */
068: public void test_getFields() {
069: ObjectStreamClass osc = ObjectStreamClass
070: .lookup(DummyClass.class);
071: ObjectStreamField[] osfArray = osc.getFields();
072: assertTrue(
073: "Array of fields should be of length 2 but is instead of length: "
074: + osfArray.length, osfArray.length == 2);
075: }
076:
077: /**
078: * @tests java.io.ObjectStreamClass#getName()
079: */
080: public void test_getName() {
081: ObjectStreamClass osc = ObjectStreamClass
082: .lookup(DummyClass.class);
083: assertTrue(
084: "getName returned incorrect name: " + osc.getName(),
085: osc
086: .getName()
087: .equals(
088: "org.apache.harmony.luni.tests.java.io.ObjectStreamClassTest$DummyClass"));
089: }
090:
091: /**
092: * @tests java.io.ObjectStreamClass#getSerialVersionUID()
093: */
094: public void test_getSerialVersionUID() {
095: ObjectStreamClass osc = ObjectStreamClass
096: .lookup(DummyClass.class);
097: assertTrue("getSerialversionUID returned incorrect uid: "
098: + osc.getSerialVersionUID() + " instead of "
099: + DummyClass.getUID(),
100: osc.getSerialVersionUID() == DummyClass.getUID());
101: }
102:
103: /**
104: * @tests java.io.ObjectStreamClass#lookup(java.lang.Class)
105: */
106: public void test_lookupLjava_lang_Class() {
107: ObjectStreamClass osc = ObjectStreamClass
108: .lookup(DummyClass.class);
109: assertTrue(
110: "lookup returned wrong class: " + osc.getName(),
111: osc
112: .getName()
113: .equals(
114: "org.apache.harmony.luni.tests.java.io.ObjectStreamClassTest$DummyClass"));
115: }
116:
117: /**
118: * @tests java.io.ObjectStreamClass#toString()
119: */
120: public void test_toString() {
121: ObjectStreamClass osc = ObjectStreamClass
122: .lookup(DummyClass.class);
123: String oscString = osc.toString();
124:
125: // The previous test was more specific than the spec so it was replaced
126: // with the test below
127: assertTrue("toString returned incorrect string: "
128: + osc.toString(),
129: oscString.indexOf("serialVersionUID") >= 0
130: && oscString.indexOf("999999999999999L") >= 0);
131: ;
132: }
133:
134: public void testSerialization() {
135: ObjectStreamClass osc = ObjectStreamClass
136: .lookup(ObjectStreamClass.class);
137: assertEquals(0, osc.getFields().length);
138: }
139:
140: public void test_specialTypes() {
141: Class<?> proxyClass = Proxy.getProxyClass(this .getClass()
142: .getClassLoader(), new Class[] { Runnable.class });
143:
144: ObjectStreamClass proxyStreamClass = ObjectStreamClass
145: .lookup(proxyClass);
146:
147: assertEquals("Proxy classes should have zero serialVersionUID",
148: 0, proxyStreamClass.getSerialVersionUID());
149: ObjectStreamField[] proxyFields = proxyStreamClass.getFields();
150: assertEquals("Proxy classes should have no serialized fields",
151: 0, proxyFields.length);
152:
153: ObjectStreamClass enumStreamClass = ObjectStreamClass
154: .lookup(Thread.State.class);
155:
156: assertEquals("Enum classes should have zero serialVersionUID",
157: 0, enumStreamClass.getSerialVersionUID());
158: ObjectStreamField[] enumFields = enumStreamClass.getFields();
159: assertEquals("Enum classes should have no serialized fields",
160: 0, enumFields.length);
161: }
162: }
|