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.InvalidClassException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ObjectStreamClass;
028: import java.io.ObjectStreamException;
029: import java.io.Serializable;
030: import java.util.ArrayList;
031:
032: import junit.framework.TestCase;
033:
034: import org.apache.harmony.testframework.serialization.SerializationTest;
035:
036: public class ObjectInputStream2Test extends TestCase {
037:
038: public void test_readUnshared() throws IOException,
039: ClassNotFoundException {
040: // Regression test for HARMONY-819
041: ByteArrayOutputStream baos = new ByteArrayOutputStream();
042: try {
043: ObjectOutputStream oos = new ObjectOutputStream(baos);
044: oos.writeObject("abc");
045: oos.writeObject("abc");
046: oos.close();
047:
048: ObjectInputStream ois = new ObjectInputStream(
049: new ByteArrayInputStream(baos.toByteArray()));
050: ois.readUnshared();
051: ois.readObject();
052: ois.close();
053: fail("Expected ObjectStreamException");
054: } catch (ObjectStreamException e) {
055: // expected
056: }
057: }
058:
059: /**
060: * Micro-scenario of de/serialization of an object with non-serializable
061: * superclass. The super-constructor only should be invoked on the
062: * deserialized instance.
063: */
064: public void test_readObject_Hierarchy() throws IOException,
065: ClassNotFoundException {
066: ByteArrayOutputStream baos = new ByteArrayOutputStream();
067:
068: ObjectOutputStream oos = new ObjectOutputStream(baos);
069: oos.writeObject(new B());
070: oos.close();
071:
072: ObjectInputStream ois = new ObjectInputStream(
073: new ByteArrayInputStream(baos.toByteArray()));
074: B b = (B) ois.readObject();
075: ois.close();
076:
077: assertTrue("should construct super", A.list.contains(b));
078: assertFalse("should not construct self", B.list.contains(b));
079: assertEquals("super field A.s", A.DEFAULT, ((A) b).s);
080: assertNull("transient field B.s", b.s);
081: }
082:
083: /**
084: * @tests {@link java.io.ObjectInputStream#readNewLongString()}
085: */
086: public void test_readNewLongString() throws Exception {
087: LongString longString = new LongString();
088: SerializationTest.verifySelf(longString);
089: }
090:
091: @SuppressWarnings("serial")
092: private static class LongString implements Serializable {
093: String lString;
094:
095: public LongString() {
096: StringBuilder builder = new StringBuilder();
097: // construct a string whose length > 64K
098: for (int i = 0; i < 65636; i++) {
099: builder.append('1');
100: }
101: lString = builder.toString();
102: }
103:
104: @Override
105: public boolean equals(Object o) {
106: if (o == this ) {
107: return true;
108: }
109: if (o instanceof LongString) {
110: LongString l = (LongString) o;
111: return l.lString.equals(l.lString);
112: }
113: return true;
114: }
115:
116: @Override
117: public int hashCode() {
118: return lString.hashCode();
119: }
120: }
121:
122: static class A {
123: static final ArrayList<A> list = new ArrayList<A>();
124: String s;
125: public static final String DEFAULT = "aaa";
126:
127: public A() {
128: s = DEFAULT;
129: list.add(this );
130: }
131: }
132:
133: static class B extends A implements Serializable {
134: private static final long serialVersionUID = 1L;
135: static final ArrayList<A> list = new ArrayList<A>();
136: transient String s;
137:
138: public B() {
139: s = "bbb";
140: list.add(this );
141: }
142: }
143:
144: class OIS extends ObjectInputStream {
145:
146: OIS() throws IOException {
147: super ();
148: }
149:
150: void test() throws ClassNotFoundException, IOException {
151: readClassDescriptor();
152: }
153:
154: }
155:
156: public void test_readClassDescriptor()
157: throws ClassNotFoundException, IOException {
158: try {
159: new OIS().test();
160: fail("Should throw NullPointerException");
161: } catch (NullPointerException e) {
162: // expected
163: }
164: }
165:
166: static class TestObjectInputStream extends ObjectInputStream {
167: public TestObjectInputStream(InputStream in) throws IOException {
168: super (in);
169: }
170:
171: @Override
172: @SuppressWarnings("unchecked")
173: protected Class resolveClass(ObjectStreamClass desc)
174: throws IOException, ClassNotFoundException {
175: if (desc.getName().endsWith(
176: "ObjectInputStream2Test$TestClass1")) {
177: return TestClass2.class;
178: }
179: return super .resolveClass(desc);
180: }
181: }
182:
183: static class TestClass1 implements Serializable {
184: private static final long serialVersionUID = 11111L;
185: int i = 0;
186: }
187:
188: static class TestClass2 implements Serializable {
189: private static final long serialVersionUID = 11111L;
190: int i = 0;
191: }
192:
193: public void test_resolveClass_invalidClassName() throws Exception {
194: // Regression test for HARMONY-1920
195: TestClass1 to1 = new TestClass1();
196: ByteArrayOutputStream baos = new ByteArrayOutputStream();
197: ObjectOutputStream oos = new ObjectOutputStream(baos);
198: ByteArrayInputStream bais;
199: ObjectInputStream ois;
200:
201: to1.i = 555;
202: oos.writeObject(to1);
203: oos.flush();
204: byte[] bytes = baos.toByteArray();
205: bais = new ByteArrayInputStream(bytes);
206: ois = new TestObjectInputStream(bais);
207:
208: try {
209: ois.readObject();
210: fail("Should throw InvalidClassException");
211: } catch (InvalidClassException ice) {
212: // Excpected
213: }
214: }
215: }
|