0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.io;
0019:
0020: import java.io.ByteArrayInputStream;
0021: import java.io.ByteArrayOutputStream;
0022: import java.io.Externalizable;
0023: import java.io.File;
0024: import java.io.FileInputStream;
0025: import java.io.FileOutputStream;
0026: import java.io.IOException;
0027: import java.io.NotActiveException;
0028: import java.io.NotSerializableException;
0029: import java.io.ObjectInput;
0030: import java.io.ObjectInputStream;
0031: import java.io.ObjectOutput;
0032: import java.io.ObjectOutputStream;
0033: import java.io.ObjectStreamClass;
0034: import java.io.ObjectStreamConstants;
0035: import java.io.ObjectStreamException;
0036: import java.io.ObjectStreamField;
0037: import java.io.OutputStream;
0038: import java.io.Serializable;
0039: import java.io.SerializablePermission;
0040: import java.io.WriteAbortedException;
0041: import java.security.Permission;
0042: import java.util.Arrays;
0043:
0044: import junit.framework.TestCase;
0045:
0046: @SuppressWarnings({"unused","serial"})
0047: public class ObjectOutputStreamTest extends TestCase implements
0048: Serializable {
0049:
0050: File f;
0051:
0052: public class SerializableTestHelper implements Serializable {
0053: public String aField1;
0054:
0055: public String aField2;
0056:
0057: SerializableTestHelper() {
0058: aField1 = null;
0059: aField2 = null;
0060: }
0061:
0062: SerializableTestHelper(String s, String t) {
0063: aField1 = s;
0064: aField2 = t;
0065: }
0066:
0067: private void readObject(ObjectInputStream ois)
0068: throws IOException {
0069: // note aField2 is not read
0070: try {
0071: ObjectInputStream.GetField fields = ois.readFields();
0072: aField1 = (String) fields.get("aField1", "Zap");
0073: } catch (Exception e) {
0074: }
0075: }
0076:
0077: private void writeObject(ObjectOutputStream oos)
0078: throws IOException {
0079: // note aField2 is not written
0080: ObjectOutputStream.PutField fields = oos.putFields();
0081: fields.put("aField1", aField1);
0082: oos.writeFields();
0083: }
0084:
0085: public String getText1() {
0086: return aField1;
0087: }
0088:
0089: public void setText1(String s) {
0090: aField1 = s;
0091: }
0092:
0093: public String getText2() {
0094: return aField2;
0095: }
0096:
0097: public void setText2(String s) {
0098: aField2 = s;
0099: }
0100: }
0101:
0102: private static class SerializationTest implements
0103: java.io.Serializable {
0104: int anInt = INIT_INT_VALUE;
0105:
0106: public SerializationTest() {
0107: super ();
0108: }
0109: }
0110:
0111: private static class SerializationTestSubclass1 extends
0112: SerializationTest implements Serializable {
0113: String aString = INIT_STR_VALUE;
0114:
0115: public SerializationTestSubclass1() {
0116: super ();
0117: // Just to change default superclass init value
0118: anInt = INIT_INT_VALUE / 2;
0119: }
0120: }
0121:
0122: private static class SpecTestSuperClass implements Runnable,
0123: Serializable {
0124: protected java.lang.String instVar;
0125:
0126: public void run() {
0127: }
0128: }
0129:
0130: private static class SpecTest extends SpecTestSuperClass implements
0131: Cloneable, Serializable {
0132: public java.lang.String instVar1;
0133:
0134: public static java.lang.String staticVar1;
0135:
0136: public static java.lang.String staticVar2;
0137: {
0138: instVar1 = "NonStaticInitialValue";
0139: }
0140: static {
0141: staticVar1 = "StaticInitialValue";
0142: staticVar1 = new String(staticVar1);
0143: }
0144:
0145: public Object method(Object objParam, Object objParam2) {
0146: return new Object();
0147: }
0148:
0149: public boolean method(boolean bParam, Object objParam) {
0150: return true;
0151: }
0152:
0153: public boolean method(boolean bParam, Object objParam,
0154: Object objParam2) {
0155: return true;
0156: }
0157:
0158: }
0159:
0160: private static class SpecTestSubclass extends SpecTest implements
0161: Serializable {
0162: public transient java.lang.String transientInstVar = "transientValue";
0163: }
0164:
0165: private static class ReadWriteObject implements
0166: java.io.Serializable {
0167: public boolean calledWriteObject = false;
0168:
0169: public boolean calledReadObject = false;
0170:
0171: public ReadWriteObject() {
0172: super ();
0173: }
0174:
0175: private void readObject(java.io.ObjectInputStream in)
0176: throws java.io.IOException, ClassNotFoundException {
0177: calledReadObject = true;
0178: in.readObject();
0179: }
0180:
0181: private void writeObject(java.io.ObjectOutputStream out)
0182: throws java.io.IOException {
0183: calledWriteObject = true;
0184: out.writeObject(FOO);
0185: }
0186: }
0187:
0188: private static class PublicReadWriteObject implements
0189: java.io.Serializable {
0190: public boolean calledWriteObject = false;
0191:
0192: public boolean calledReadObject = false;
0193:
0194: public PublicReadWriteObject() {
0195: super ();
0196: }
0197:
0198: public void readObject(java.io.ObjectInputStream in)
0199: throws java.io.IOException, ClassNotFoundException {
0200: calledReadObject = true;
0201: in.readObject();
0202: }
0203:
0204: public void writeObject(java.io.ObjectOutputStream out)
0205: throws java.io.IOException {
0206: calledWriteObject = true;
0207: out.writeObject(FOO);
0208: }
0209: }
0210:
0211: private static class FieldOrder implements Serializable {
0212: String aaa1NonPrimitive = "aaa1";
0213:
0214: int bbb1PrimitiveInt = 5;
0215:
0216: boolean aaa2PrimitiveBoolean = true;
0217:
0218: String bbb2NonPrimitive = "bbb2";
0219: }
0220:
0221: private static class JustReadObject implements java.io.Serializable {
0222: public boolean calledReadObject = false;
0223:
0224: public JustReadObject() {
0225: super ();
0226: }
0227:
0228: private void readObject(java.io.ObjectInputStream in)
0229: throws java.io.IOException, ClassNotFoundException {
0230: calledReadObject = true;
0231: in.defaultReadObject();
0232: }
0233: }
0234:
0235: private static class JustWriteObject implements
0236: java.io.Serializable {
0237: public boolean calledWriteObject = false;
0238:
0239: public JustWriteObject() {
0240: super ();
0241: }
0242:
0243: private void writeObject(java.io.ObjectOutputStream out)
0244: throws java.io.IOException, ClassNotFoundException {
0245: calledWriteObject = true;
0246: out.defaultWriteObject();
0247: }
0248: }
0249:
0250: private static class ClassBasedReplacementWhenDumping implements
0251: java.io.Serializable {
0252: public boolean calledReplacement = false;
0253:
0254: public ClassBasedReplacementWhenDumping() {
0255: super ();
0256: }
0257:
0258: private Object writeReplace() {
0259: calledReplacement = true;
0260: return FOO; // Replacement is a String
0261: }
0262: }
0263:
0264: private static class MultipleClassBasedReplacementWhenDumping
0265: implements java.io.Serializable {
0266: private static class C1 implements java.io.Serializable {
0267: private Object writeReplace() {
0268: return new C2();
0269: }
0270: }
0271:
0272: private static class C2 implements java.io.Serializable {
0273: private Object writeReplace() {
0274: return new C3();
0275: }
0276: }
0277:
0278: private static class C3 implements java.io.Serializable {
0279: private Object writeReplace() {
0280: return FOO;
0281: }
0282: }
0283:
0284: public MultipleClassBasedReplacementWhenDumping() {
0285: super ();
0286: }
0287:
0288: private Object writeReplace() {
0289: return new C1();
0290: }
0291: }
0292:
0293: private static class ClassBasedReplacementWhenLoading implements
0294: java.io.Serializable {
0295: public ClassBasedReplacementWhenLoading() {
0296: super ();
0297: }
0298:
0299: private Object readResolve() {
0300: return FOO; // Replacement is a String
0301: }
0302: }
0303:
0304: private static class ClassBasedReplacementWhenLoadingViolatesFieldType
0305: implements java.io.Serializable {
0306: public ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading();
0307:
0308: public ClassBasedReplacementWhenLoadingViolatesFieldType() {
0309: super ();
0310: }
0311: }
0312:
0313: private static class MyExceptionWhenDumping implements
0314: java.io.Serializable {
0315: private static class MyException extends java.io.IOException {
0316: };
0317:
0318: public boolean anInstanceVar = false;
0319:
0320: public MyExceptionWhenDumping() {
0321: super ();
0322: }
0323:
0324: private void readObject(java.io.ObjectInputStream in)
0325: throws java.io.IOException, ClassNotFoundException {
0326: in.defaultReadObject();
0327: }
0328:
0329: private void writeObject(java.io.ObjectOutputStream out)
0330: throws java.io.IOException, ClassNotFoundException {
0331: throw new MyException();
0332: }
0333: }
0334:
0335: private static class NonSerializableExceptionWhenDumping implements
0336: java.io.Serializable {
0337: public Object anInstanceVar = new Object();
0338:
0339: public NonSerializableExceptionWhenDumping() {
0340: super ();
0341: }
0342: }
0343:
0344: private static class MyUnserializableExceptionWhenDumping implements
0345: java.io.Serializable {
0346: private static class MyException extends java.io.IOException {
0347: private Object notSerializable = new Object();
0348: };
0349:
0350: public boolean anInstanceVar = false;
0351:
0352: public MyUnserializableExceptionWhenDumping() {
0353: super ();
0354: }
0355:
0356: private void readObject(java.io.ObjectInputStream in)
0357: throws java.io.IOException, ClassNotFoundException {
0358: in.defaultReadObject();
0359: }
0360:
0361: private void writeObject(java.io.ObjectOutputStream out)
0362: throws java.io.IOException, ClassNotFoundException {
0363: throw new MyException();
0364: }
0365: }
0366:
0367: private static class WithUnmatchingSerialPersistentFields implements
0368: java.io.Serializable {
0369: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
0370: "value", String.class) };
0371:
0372: public int anInstanceVar = 5;
0373:
0374: public WithUnmatchingSerialPersistentFields() {
0375: super ();
0376: }
0377: }
0378:
0379: private static class WithMatchingSerialPersistentFields implements
0380: java.io.Serializable {
0381: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
0382: "anInstanceVar", String.class) };
0383:
0384: public String anInstanceVar = FOO + FOO;
0385:
0386: public WithMatchingSerialPersistentFields() {
0387: super ();
0388: }
0389: }
0390:
0391: private static class SerialPersistentFields implements
0392: java.io.Serializable {
0393: private static final String SIMULATED_FIELD_NAME = "text";
0394:
0395: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
0396: SIMULATED_FIELD_NAME, String.class) };
0397:
0398: public int anInstanceVar = 5;
0399:
0400: public SerialPersistentFields() {
0401: super ();
0402: }
0403:
0404: private void readObject(java.io.ObjectInputStream in)
0405: throws java.io.IOException, ClassNotFoundException {
0406: ObjectInputStream.GetField fields = in.readFields();
0407: anInstanceVar = Integer.parseInt((String) fields.get(
0408: SIMULATED_FIELD_NAME, "-5"));
0409: }
0410:
0411: private void writeObject(java.io.ObjectOutputStream out)
0412: throws java.io.IOException, ClassNotFoundException {
0413: ObjectOutputStream.PutField fields = out.putFields();
0414: fields.put(SIMULATED_FIELD_NAME, Integer
0415: .toString(anInstanceVar));
0416: out.writeFields();
0417: }
0418: }
0419:
0420: private static class WriteFieldsWithoutFetchingPutFields implements
0421: java.io.Serializable {
0422: private static final String SIMULATED_FIELD_NAME = "text";
0423:
0424: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
0425: SIMULATED_FIELD_NAME, String.class) };
0426:
0427: public int anInstanceVar = 5;
0428:
0429: public WriteFieldsWithoutFetchingPutFields() {
0430: super ();
0431: }
0432:
0433: private void readObject(java.io.ObjectInputStream in)
0434: throws java.io.IOException, ClassNotFoundException {
0435: in.readFields();
0436: }
0437:
0438: private void writeObject(java.io.ObjectOutputStream out)
0439: throws java.io.IOException, ClassNotFoundException {
0440: out.writeFields();
0441: }
0442: }
0443:
0444: private static class SerialPersistentFieldsWithoutField implements
0445: java.io.Serializable {
0446: public int anInstanceVar = 5;
0447:
0448: public SerialPersistentFieldsWithoutField() {
0449: super ();
0450: }
0451:
0452: private void readObject(java.io.ObjectInputStream in)
0453: throws java.io.IOException, ClassNotFoundException {
0454: in.readFields();
0455: }
0456:
0457: private void writeObject(java.io.ObjectOutputStream out)
0458: throws java.io.IOException, ClassNotFoundException {
0459: out.putFields();
0460: out.writeFields();
0461: }
0462: }
0463:
0464: private static class NotSerializable {
0465: private int foo;
0466:
0467: public NotSerializable() {
0468: }
0469:
0470: protected Object writeReplace() throws ObjectStreamException {
0471: return new Integer(42);
0472: }
0473: }
0474:
0475: private static class WriteReplaceObject implements Serializable {
0476: private Object replaceObject;
0477:
0478: private static enum Color {
0479: red, blue, green
0480: };
0481:
0482: public WriteReplaceObject(Object o) {
0483: replaceObject = o;
0484: }
0485:
0486: protected Object writeReplace() throws ObjectStreamException {
0487: return replaceObject;
0488: }
0489: }
0490:
0491: private static class ExternalizableWithReplace implements
0492: Externalizable {
0493: private int foo;
0494:
0495: public ExternalizableWithReplace() {
0496: }
0497:
0498: protected Object writeReplace() throws ObjectStreamException {
0499: return new Integer(42);
0500: }
0501:
0502: public void writeExternal(ObjectOutput out) {
0503: }
0504:
0505: public void readExternal(ObjectInput in) {
0506: }
0507: }
0508:
0509: private static class ObjectOutputStreamWithReplace extends
0510: ObjectOutputStream {
0511: public ObjectOutputStreamWithReplace(OutputStream out)
0512: throws IOException {
0513: super (out);
0514: enableReplaceObject(true);
0515: }
0516:
0517: protected Object replaceObject(Object obj) throws IOException {
0518: if (obj instanceof NotSerializable) {
0519: return new Long(10);
0520: }
0521: if (obj instanceof Integer) {
0522: return new Long(((Integer) obj).longValue());
0523: }
0524: return super .replaceObject(obj);
0525: }
0526: }
0527:
0528: private static class ObjectOutputStreamWithReplace2 extends
0529: ObjectOutputStream {
0530: public ObjectOutputStreamWithReplace2(OutputStream out)
0531: throws IOException {
0532: super (out);
0533: enableReplaceObject(true);
0534: }
0535:
0536: protected Object replaceObject(Object obj) throws IOException {
0537: return new Long(10);
0538: }
0539: }
0540:
0541: private static class ObjectOutputStreamWriteOverride extends
0542: ObjectOutputStream {
0543: String test = "test";
0544:
0545: protected ObjectOutputStreamWriteOverride() throws IOException,
0546: SecurityException {
0547: super ();
0548: }
0549:
0550: @Override
0551: protected void writeObjectOverride(Object object)
0552: throws IOException {
0553: test = null;
0554: super .writeObjectOverride(object);
0555: }
0556: }
0557:
0558: protected static final String MODE_XLOAD = "xload";
0559:
0560: protected static final String MODE_XDUMP = "xdump";
0561:
0562: static final String FOO = "foo";
0563:
0564: static final String MSG_WITE_FAILED = "Failed to write: ";
0565:
0566: private static final boolean DEBUG = false;
0567:
0568: protected static boolean xload = false;
0569:
0570: protected static boolean xdump = false;
0571:
0572: protected static String xFileName = null;
0573:
0574: protected ObjectInputStream ois;
0575:
0576: protected ObjectOutputStream oos;
0577:
0578: protected ByteArrayOutputStream bao;
0579:
0580: static final int INIT_INT_VALUE = 7;
0581:
0582: static final String INIT_STR_VALUE = "a string that is blortz";
0583:
0584: /**
0585: * @tests java.io.ObjectOutputStream#ObjectOutputStream(java.io.OutputStream)
0586: */
0587: public void test_ConstructorLjava_io_OutputStream()
0588: throws IOException {
0589: // Test for method java.io.ObjectOutputStream(java.io.OutputStream)
0590: oos.close();
0591: oos = new ObjectOutputStream(new ByteArrayOutputStream());
0592: oos.close();
0593: }
0594:
0595: /**
0596: * @tests java.io.ObjectOutputStream#ObjectOutputStream(java.io.OutputStream)
0597: */
0598: public void test_ConstructorLjava_io_OutputStream_subtest0()
0599: throws IOException {
0600:
0601: // custom security manager
0602: SecurityManager sm = new SecurityManager() {
0603:
0604: final SerializablePermission forbidenPermission = new SerializablePermission(
0605: "enableSubclassImplementation");
0606:
0607: public void checkPermission(Permission perm) {
0608: if (forbidenPermission.equals(perm)) {
0609: throw new SecurityException();
0610: }
0611: }
0612: };
0613:
0614: SecurityManager oldSm = System.getSecurityManager();
0615: System.setSecurityManager(sm);
0616: try {
0617: ByteArrayOutputStream out = new ByteArrayOutputStream();
0618: // should not cause SecurityException
0619: new ObjectOutputStream(out);
0620: // should not cause SecurityException
0621: class SubTest1 extends ObjectOutputStream {
0622: SubTest1(OutputStream out) throws IOException {
0623: super (out);
0624: }
0625: }
0626:
0627: // should not cause SecurityException
0628: new SubTest1(out);
0629: class SubTest2 extends ObjectOutputStream {
0630: SubTest2(OutputStream out) throws IOException {
0631: super (out);
0632: }
0633:
0634: public void writeUnshared(Object obj)
0635: throws IOException {
0636: }
0637: }
0638:
0639: try {
0640: new SubTest2(out);
0641: fail("should throw SecurityException 1");
0642: } catch (SecurityException e) {
0643: }
0644: class SubTest3 extends ObjectOutputStream {
0645: SubTest3(OutputStream out) throws IOException {
0646: super (out);
0647: }
0648:
0649: public PutField putFields() throws IOException {
0650: return null;
0651: }
0652: }
0653:
0654: try {
0655: new SubTest3(out);
0656: fail("should throw SecurityException 2");
0657: } catch (SecurityException e) {
0658: }
0659: } finally {
0660: System.setSecurityManager(oldSm);
0661: }
0662: }
0663:
0664: /**
0665: * @tests java.io.ObjectOutputStream#close()
0666: */
0667: public void test_close() {
0668: // Test for method void java.io.ObjectOutputStream.close()
0669: }
0670:
0671: /**
0672: * @tests java.io.ObjectOutputStream#defaultWriteObject()
0673: */
0674: public void test_defaultWriteObject() throws IOException {
0675: // Test for method void java.io.ObjectOutputStream.defaultWriteObject()
0676: try {
0677: oos.defaultWriteObject();
0678: fail("Failed to throw NotActiveException");
0679: } catch (NotActiveException e) {
0680: // Correct
0681: }
0682: }
0683:
0684: /**
0685: * @tests java.io.ObjectOutputStream#flush()
0686: */
0687: public void test_flush() throws Exception {
0688: // Test for method void java.io.ObjectOutputStream.flush()
0689: int size = bao.size();
0690: oos.writeByte(127);
0691: assertTrue("Data flushed already", bao.size() == size);
0692: oos.flush();
0693: assertTrue("Failed to flush data", bao.size() > size);
0694: // we don't know how many bytes are actually written for 1
0695: // byte, so we test > <before>
0696: oos.close();
0697: oos = null;
0698: }
0699:
0700: /**
0701: * @tests java.io.ObjectOutputStream#putFields()
0702: */
0703: public void test_putFields() throws Exception {
0704: // Test for method java.io.ObjectOutputStream$PutField
0705: // java.io.ObjectOutputStream.putFields()
0706:
0707: SerializableTestHelper sth;
0708:
0709: /*
0710: * "SerializableTestHelper" is an object created for these tests with
0711: * two fields (Strings) and simple implementations of readObject and
0712: * writeObject which simply read and write the first field but not the
0713: * second
0714: */
0715:
0716: oos.writeObject(new SerializableTestHelper("Gabba", "Jabba"));
0717: oos.flush();
0718: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0719: .toByteArray()));
0720: sth = (SerializableTestHelper) (ois.readObject());
0721: assertEquals(
0722: "readFields / writeFields failed--first field not set",
0723: "Gabba", sth.getText1());
0724: assertNull(
0725: "readFields / writeFields failed--second field should not have been set",
0726: sth.getText2());
0727: }
0728:
0729: /**
0730: * @tests java.io.ObjectOutputStream#reset()
0731: */
0732: public void test_reset() throws Exception {
0733: // Test for method void java.io.ObjectOutputStream.reset()
0734: String o = "HelloWorld";
0735: oos.writeObject(o);
0736: oos.writeObject(o);
0737: oos.reset();
0738: oos.writeObject(o);
0739: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0740: .toByteArray()));
0741: ois.close();
0742: }
0743:
0744: private static class ExternalTest implements Externalizable {
0745: public String value;
0746:
0747: public ExternalTest() {
0748: }
0749:
0750: public void setValue(String val) {
0751: value = val;
0752: }
0753:
0754: public String getValue() {
0755: return value;
0756: }
0757:
0758: public void writeExternal(ObjectOutput output) {
0759: try {
0760: output.writeUTF(value);
0761: } catch (IOException e) {
0762: e.printStackTrace();
0763: }
0764: }
0765:
0766: public void readExternal(ObjectInput input) {
0767: try {
0768: value = input.readUTF();
0769: } catch (IOException e) {
0770: e.printStackTrace();
0771: }
0772: }
0773: }
0774:
0775: /**
0776: * @tests java.io.ObjectOutputStream#useProtocolVersion(int)
0777: */
0778: public void test_useProtocolVersionI() throws Exception {
0779: // Test for method void
0780: // java.io.ObjectOutputStream.useProtocolVersion(int)
0781: oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_1);
0782: ExternalTest t1 = new ExternalTest();
0783: t1.setValue("hello1");
0784: oos.writeObject(t1);
0785: oos.close();
0786: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0787: .toByteArray()));
0788: ExternalTest t2 = (ExternalTest) ois.readObject();
0789: ois.close();
0790: assertTrue(
0791: "Cannot read/write PROTOCAL_VERSION_1 Externalizable objects: "
0792: + t2.getValue(), t1.getValue().equals(
0793: t2.getValue()));
0794:
0795: // Cannot set protocol version when stream in-flight
0796: ObjectOutputStream out = new ObjectOutputStream(
0797: new ByteArrayOutputStream());
0798: out.writeObject("hello world");
0799: try {
0800: out
0801: .useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);
0802: fail("Expected IllegalStateException");
0803: } catch (IllegalStateException e) {
0804: // Expected
0805: }
0806: }
0807:
0808: /**
0809: * @tests java.io.ObjectOutputStream#write(byte[])
0810: */
0811: public void test_write$B() throws Exception {
0812: // Test for method void java.io.ObjectOutputStream.write(byte [])
0813: byte[] buf = new byte[10];
0814: oos.write("HelloWorld".getBytes());
0815: oos.close();
0816: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0817: .toByteArray()));
0818: ois.read(buf, 0, 10);
0819: ois.close();
0820: assertEquals("Read incorrect bytes", "HelloWorld", new String(
0821: buf, 0, 10));
0822: }
0823:
0824: /**
0825: * @tests java.io.ObjectOutputStream#write(byte[], int, int)
0826: */
0827: public void test_write$BII() throws Exception {
0828: // Test for method void java.io.ObjectOutputStream.write(byte [], int,
0829: // int)
0830: byte[] buf = new byte[10];
0831: oos.write("HelloWorld".getBytes(), 0, 10);
0832: oos.close();
0833: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0834: .toByteArray()));
0835: ois.read(buf, 0, 10);
0836: ois.close();
0837: assertEquals("Read incorrect bytes", "HelloWorld", new String(
0838: buf, 0, 10));
0839: }
0840:
0841: /**
0842: * @tests java.io.ObjectOutputStream#write(int)
0843: */
0844: public void test_writeI() throws Exception {
0845: // Test for method void java.io.ObjectOutputStream.write(int)
0846: oos.write('T');
0847: oos.close();
0848: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0849: .toByteArray()));
0850: assertEquals("Read incorrect byte", 'T', ois.read());
0851: ois.close();
0852: }
0853:
0854: /**
0855: * @tests java.io.ObjectOutputStream#writeBoolean(boolean)
0856: */
0857: public void test_writeBooleanZ() throws Exception {
0858: // Test for method void java.io.ObjectOutputStream.writeBoolean(boolean)
0859: oos.writeBoolean(true);
0860: oos.close();
0861: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0862: .toByteArray()));
0863: assertTrue("Wrote incorrect byte value", ois.readBoolean());
0864: }
0865:
0866: /**
0867: * @tests java.io.ObjectOutputStream#writeByte(int)
0868: */
0869: public void test_writeByteI() throws Exception {
0870: // Test for method void java.io.ObjectOutputStream.writeByte(int)
0871: oos.writeByte(127);
0872: oos.close();
0873: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0874: .toByteArray()));
0875: assertEquals("Wrote incorrect byte value", 127, ois.readByte());
0876: }
0877:
0878: /**
0879: * @tests java.io.ObjectOutputStream#writeBytes(java.lang.String)
0880: */
0881: public void test_writeBytesLjava_lang_String() throws Exception {
0882: // Test for method void
0883: // java.io.ObjectOutputStream.writeBytes(java.lang.String)
0884: byte[] buf = new byte[10];
0885: oos.writeBytes("HelloWorld");
0886: oos.close();
0887: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0888: .toByteArray()));
0889: ois.readFully(buf);
0890: ois.close();
0891: assertEquals("Wrote incorrect bytes value", "HelloWorld",
0892: new String(buf, 0, 10));
0893: }
0894:
0895: /**
0896: * @tests java.io.ObjectOutputStream#writeChar(int)
0897: */
0898: public void test_writeCharI() throws Exception {
0899: // Test for method void java.io.ObjectOutputStream.writeChar(int)
0900: oos.writeChar('T');
0901: oos.close();
0902: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0903: .toByteArray()));
0904: assertEquals("Wrote incorrect char value", 'T', ois.readChar());
0905: }
0906:
0907: /**
0908: * @tests java.io.ObjectOutputStream#writeChars(java.lang.String)
0909: */
0910: public void test_writeCharsLjava_lang_String() throws Exception {
0911: // Test for method void
0912: // java.io.ObjectOutputStream.writeChars(java.lang.String)
0913: int avail = 0;
0914: char[] buf = new char[10];
0915: oos.writeChars("HelloWorld");
0916: oos.close();
0917: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0918: .toByteArray()));
0919: // Number of prim data bytes in stream / 2 to give char index
0920: avail = ois.available() / 2;
0921: for (int i = 0; i < avail; ++i)
0922: buf[i] = ois.readChar();
0923: ois.close();
0924: assertEquals("Wrote incorrect chars", "HelloWorld", new String(
0925: buf, 0, 10));
0926: }
0927:
0928: /**
0929: * @tests java.io.ObjectOutputStream#writeDouble(double)
0930: */
0931: public void test_writeDoubleD() throws Exception {
0932: // Test for method void java.io.ObjectOutputStream.writeDouble(double)
0933: oos.writeDouble(Double.MAX_VALUE);
0934: oos.close();
0935: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0936: .toByteArray()));
0937: assertTrue("Wrote incorrect double value",
0938: ois.readDouble() == Double.MAX_VALUE);
0939: }
0940:
0941: /**
0942: * @tests java.io.ObjectOutputStream#writeFields()
0943: */
0944: public void test_writeFields() {
0945: // Test for method void java.io.ObjectOutputStream.writeFields()
0946: assertTrue("Used to test", true);
0947: }
0948:
0949: /**
0950: * @tests java.io.ObjectOutputStream#writeFloat(float)
0951: */
0952: public void test_writeFloatF() throws Exception {
0953: // Test for method void java.io.ObjectOutputStream.writeFloat(float)
0954: oos.writeFloat(Float.MAX_VALUE);
0955: oos.close();
0956: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0957: .toByteArray()));
0958: assertTrue("Wrote incorrect double value",
0959: ois.readFloat() == Float.MAX_VALUE);
0960: ois.close();
0961: ois = null;
0962: }
0963:
0964: /**
0965: * @tests java.io.ObjectOutputStream#writeInt(int)
0966: */
0967: public void test_writeIntI() throws Exception {
0968: // Test for method void java.io.ObjectOutputStream.writeInt(int)
0969: oos.writeInt(Integer.MAX_VALUE);
0970: oos.close();
0971: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0972: .toByteArray()));
0973: assertTrue("Wrote incorrect double value",
0974: ois.readInt() == Integer.MAX_VALUE);
0975: ois.close();
0976: }
0977:
0978: /**
0979: * @tests java.io.ObjectOutputStream#writeLong(long)
0980: */
0981: public void test_writeLongJ() throws Exception {
0982: // Test for method void java.io.ObjectOutputStream.writeLong(long)
0983: oos.writeLong(Long.MAX_VALUE);
0984: oos.close();
0985: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0986: .toByteArray()));
0987: assertTrue("Wrote incorrect double value",
0988: ois.readLong() == Long.MAX_VALUE);
0989: }
0990:
0991: /**
0992: * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object)
0993: */
0994: public void test_writeObjectLjava_lang_Object() throws Exception {
0995: // Test for method void
0996: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0997:
0998: Object objToSave = null;
0999: Object objLoaded;
1000:
1001: SerialPersistentFieldsWithoutField spf = new SerialPersistentFieldsWithoutField();
1002: final int CONST = -500;
1003: spf.anInstanceVar = CONST;
1004: objToSave = spf;
1005: if (DEBUG)
1006: System.out.println("Obj = " + objToSave);
1007: objLoaded = dumpAndReload(objToSave);
1008: assertTrue(
1009: "serialPersistentFields do not work properly in this implementation",
1010: ((SerialPersistentFieldsWithoutField) objLoaded).anInstanceVar != CONST);
1011:
1012: }
1013:
1014: /**
1015: * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object)
1016: */
1017: public void test_writeObject_NotSerializable() throws Exception {
1018: ObjectOutput out = null;
1019: try {
1020: out = new ObjectOutputStream(new ByteArrayOutputStream());
1021: out.writeObject(new NotSerializable());
1022: fail("Expected NotSerializableException");
1023: } catch (NotSerializableException e) {
1024: }
1025: out.writeObject(new ExternalizableWithReplace());
1026: }
1027:
1028: /**
1029: * @tests {@link java.io.ObjectOutputStream#writeObjectOverride(Object)}
1030: */
1031: public void test_writeObject_WriteOverride() throws Exception {
1032: ObjectOutputStreamWriteOverride mockOut = new ObjectOutputStreamWriteOverride();
1033: mockOut.writeObject(new Object());
1034: assertNull(mockOut.test);
1035: }
1036:
1037: /**
1038: * @tests java.io.ObjectOutputStream#writeShort(int)
1039: */
1040: public void test_writeShortI() throws Exception {
1041: // Test for method void java.io.ObjectOutputStream.writeShort(int)
1042: oos.writeShort(127);
1043: oos.close();
1044: ois = new ObjectInputStream(new ByteArrayInputStream(bao
1045: .toByteArray()));
1046: assertEquals("Wrote incorrect short value", 127, ois
1047: .readShort());
1048: }
1049:
1050: /**
1051: * @tests java.io.ObjectOutputStream#writeUTF(java.lang.String)
1052: */
1053: public void test_writeUTFLjava_lang_String() throws Exception {
1054: // Test for method void
1055: // java.io.ObjectOutputStream.writeUTF(java.lang.String)
1056: oos.writeUTF("HelloWorld");
1057: oos.close();
1058: ois = new ObjectInputStream(new ByteArrayInputStream(bao
1059: .toByteArray()));
1060: assertEquals("Wrote incorrect UTF value", "HelloWorld", ois
1061: .readUTF());
1062: }
1063:
1064: /**
1065: * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object)
1066: */
1067: public void test_writeObject_Exception()
1068: throws ClassNotFoundException, IOException {
1069: ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
1070: ObjectOutputStream oos = new ObjectOutputStream(baos);
1071:
1072: try {
1073: oos.writeObject(new Object());
1074: fail("should throw ObjectStreamException");
1075: } catch (ObjectStreamException e) {
1076: // expected
1077: } finally {
1078: oos.close();
1079: baos.close();
1080: }
1081:
1082: byte[] bytes = baos.toByteArray();
1083: ObjectInputStream ois = new ObjectInputStream(
1084: new ByteArrayInputStream(bytes));
1085: try {
1086: ois.readObject();
1087: fail("should throw WriteAbortedException");
1088: } catch (WriteAbortedException e) {
1089: // expected
1090: } finally {
1091: ois.close();
1092: }
1093: }
1094:
1095: /**
1096: * Sets up the fixture, for example, open a network connection. This method
1097: * is called before a test is executed.
1098: */
1099: protected void setUp() throws Exception {
1100: super .setUp();
1101: oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
1102: }
1103:
1104: /**
1105: * Tears down the fixture, for example, close a network connection. This
1106: * method is called after a test is executed.
1107: */
1108: protected void tearDown() throws Exception {
1109: super .tearDown();
1110: if (oos != null) {
1111: try {
1112: oos.close();
1113: } catch (Exception e) {
1114: }
1115: }
1116: if (f != null && f.exists()) {
1117: if (!f.delete()) {
1118: fail("Error cleaning up files during teardown");
1119: }
1120: }
1121: }
1122:
1123: protected Object reload() throws IOException,
1124: ClassNotFoundException {
1125:
1126: // Choose the load stream
1127: if (xload || xdump) {
1128: // Load from pre-existing file
1129: ois = new ObjectInputStream(new FileInputStream(xFileName
1130: + "-" + getName() + ".ser"));
1131: } else {
1132: // Just load from memory, we dumped to memory
1133: ois = new ObjectInputStream(new ByteArrayInputStream(bao
1134: .toByteArray()));
1135: }
1136:
1137: try {
1138: return ois.readObject();
1139: } finally {
1140: ois.close();
1141: }
1142: }
1143:
1144: protected void dump(Object o) throws IOException,
1145: ClassNotFoundException {
1146:
1147: // Choose the dump stream
1148: if (xdump) {
1149: oos = new ObjectOutputStream(new FileOutputStream(
1150: f = new java.io.File(xFileName + "-" + getName()
1151: + ".ser")));
1152: } else {
1153: oos = new ObjectOutputStream(
1154: bao = new ByteArrayOutputStream());
1155: }
1156:
1157: // Dump the object
1158: try {
1159: oos.writeObject(o);
1160: } finally {
1161: oos.close();
1162: }
1163: }
1164:
1165: /**
1166: * @tests java.io.ObjectOutputStream#writeInt(int)
1167: * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object)
1168: * @tests java.io.ObjectOutputStream#writeUTF(java.lang.String)
1169: */
1170:
1171: public void testMixPrimitivesAndObjects() throws Exception {
1172: int i = 7;
1173: String s1 = "string 1";
1174: String s2 = "string 2";
1175: byte[] bytes = { 1, 2, 3 };
1176: try {
1177: oos = new ObjectOutputStream(
1178: bao = new ByteArrayOutputStream());
1179: oos.writeInt(i);
1180: oos.writeObject(s1);
1181: oos.writeUTF(s2);
1182: oos.writeObject(bytes);
1183: oos.close();
1184:
1185: ois = new ObjectInputStream(new ByteArrayInputStream(bao
1186: .toByteArray()));
1187:
1188: int j = ois.readInt();
1189: assertTrue("Wrong int :" + j, i == j);
1190:
1191: String l1 = (String) ois.readObject();
1192: assertTrue("Wrong obj String :" + l1, s1.equals(l1));
1193:
1194: String l2 = ois.readUTF();
1195: assertTrue("Wrong UTF String :" + l2, s2.equals(l2));
1196:
1197: byte[] bytes2 = (byte[]) ois.readObject();
1198: assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2));
1199: } finally {
1200: try {
1201: if (oos != null)
1202: oos.close();
1203: if (ois != null)
1204: ois.close();
1205: } catch (IOException e) {
1206: }
1207: }
1208: }
1209:
1210: /**
1211: * @tests java.io.ObjectOutputStream#writeUnshared(java.lang.Object)
1212: */
1213: public void test_writeUnshared() throws Exception {
1214: // Regression for HARMONY-187
1215: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1216: ObjectOutputStream oos = new ObjectOutputStream(baos);
1217:
1218: Object o = "foobar";
1219: oos.writeObject(o);
1220: oos.writeUnshared(o);
1221: oos.writeObject(o);
1222: oos.flush();
1223:
1224: ObjectInputStream ois = new ObjectInputStream(
1225: new ByteArrayInputStream(baos.toByteArray()));
1226:
1227: Object[] oa = new Object[3];
1228: for (int i = 0; i < oa.length; i++) {
1229: oa[i] = ois.readObject();
1230: }
1231:
1232: oos.close();
1233: ois.close();
1234:
1235: // All three conditions must be met
1236: assertNotSame("oa[0] != oa[1]", oa[0], oa[1]);
1237: assertNotSame("oa[1] != oa[2]", oa[1], oa[2]);
1238: assertSame("oa[0] == oa[2]", oa[0], oa[2]);
1239: }
1240:
1241: /**
1242: * @tests java.io.ObjectOutputStream#writeUnshared(java.lang.Object)
1243: */
1244: public void test_writeUnshared2() throws Exception {
1245: // Regression for HARMONY-187
1246: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1247: ObjectOutputStream oos = new ObjectOutputStream(baos);
1248:
1249: Object o = new Object[1];
1250: oos.writeObject(o);
1251: oos.writeUnshared(o);
1252: oos.writeObject(o);
1253: oos.flush();
1254:
1255: ObjectInputStream ois = new ObjectInputStream(
1256: new ByteArrayInputStream(baos.toByteArray()));
1257:
1258: Object[] oa = new Object[3];
1259: for (int i = 0; i < oa.length; i++) {
1260: oa[i] = ois.readObject();
1261: }
1262:
1263: oos.close();
1264: ois.close();
1265:
1266: // All three conditions must be met
1267: assertNotSame("oa[0] != oa[1]", oa[0], oa[1]);
1268: assertNotSame("oa[1] != oa[2]", oa[1], oa[2]);
1269: assertSame("oa[0] == oa[2]", oa[0], oa[2]);
1270: }
1271:
1272: protected Object dumpAndReload(Object o) throws IOException,
1273: ClassNotFoundException {
1274: dump(o);
1275: return reload();
1276: }
1277:
1278: /**
1279: * @tests java.io.ObjectOutputStream#useProtocolVersion(int)
1280: */
1281: public void test_useProtocolVersionI_2() throws Exception {
1282: ObjectOutputStream oos = new ObjectOutputStream(
1283: new ByteArrayOutputStream());
1284:
1285: oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_1);
1286: oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_2);
1287: try {
1288: oos.useProtocolVersion(3);
1289: fail("Protocol 3 should not be accepted");
1290: } catch (IllegalArgumentException e) {
1291: // expected
1292: } finally {
1293: oos.close();
1294: }
1295: }
1296:
1297: /**
1298: * @tests java.io.ObjectOutputStream#replaceObject(java.lang.Object)
1299: */
1300: public void test_replaceObject() throws Exception {
1301: // Regression for HARMONY-1429
1302: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1303: ObjectOutputStreamWithReplace oos = new ObjectOutputStreamWithReplace(
1304: baos);
1305:
1306: oos.writeObject(new NotSerializable());
1307: oos.flush();
1308: ObjectInputStream ois = new ObjectInputStream(
1309: new ByteArrayInputStream(baos.toByteArray()));
1310: Object obj = ois.readObject();
1311: oos.close();
1312: ois.close();
1313: assertTrue("replaceObject has not been called",
1314: (obj instanceof Long));
1315:
1316: // Regression for HARMONY-2239
1317: Object replaceObject = int.class;
1318: baos = new ByteArrayOutputStream();
1319: ObjectOutputStreamWithReplace2 oos2 = new ObjectOutputStreamWithReplace2(
1320: baos);
1321: oos2.writeObject(new WriteReplaceObject(replaceObject));
1322: oos2.flush();
1323: ois = new ObjectInputStream(new ByteArrayInputStream(baos
1324: .toByteArray()));
1325: obj = ois.readObject();
1326: oos.close();
1327: ois.close();
1328: assertTrue("replaceObject has not been called",
1329: (obj instanceof Long));
1330:
1331: replaceObject = ObjectStreamClass.lookup(Integer.class);
1332: baos = new ByteArrayOutputStream();
1333: oos2 = new ObjectOutputStreamWithReplace2(baos);
1334: oos2.writeObject(new WriteReplaceObject(replaceObject));
1335: oos2.flush();
1336: ois = new ObjectInputStream(new ByteArrayInputStream(baos
1337: .toByteArray()));
1338: obj = ois.readObject();
1339: oos.close();
1340: ois.close();
1341: assertTrue("replaceObject has not been called",
1342: (obj instanceof Long));
1343:
1344: replaceObject = WriteReplaceObject.Color.red;
1345: baos = new ByteArrayOutputStream();
1346: oos2 = new ObjectOutputStreamWithReplace2(baos);
1347: oos2.writeObject(new WriteReplaceObject(replaceObject));
1348: oos2.flush();
1349: ois = new ObjectInputStream(new ByteArrayInputStream(baos
1350: .toByteArray()));
1351: obj = ois.readObject();
1352: oos.close();
1353: ois.close();
1354: assertTrue("replaceObject has not been called",
1355: (obj instanceof Long));
1356:
1357: // Regression for HARMONY-3158
1358: Object obj1;
1359: Object obj2;
1360: Object obj3;
1361:
1362: baos = new ByteArrayOutputStream();
1363: oos = new ObjectOutputStreamWithReplace(baos);
1364:
1365: oos.writeObject(new Integer(99));
1366: oos.writeObject(Integer.class);
1367: oos.writeObject(ObjectStreamClass.lookup(Integer.class));
1368: oos.flush();
1369:
1370: ois = new ObjectInputStream(new ByteArrayInputStream(baos
1371: .toByteArray()));
1372: obj1 = ois.readObject();
1373: obj2 = ois.readObject();
1374: obj3 = ois.readObject();
1375: oos.close();
1376: ois.close();
1377:
1378: assertTrue("1st replaceObject worked incorrectly",
1379: obj1 instanceof Long);
1380: assertEquals("1st replaceObject worked incorrectly", 99,
1381: ((Long) obj1).longValue());
1382: assertEquals("2nd replaceObject worked incorrectly",
1383: Integer.class, obj2);
1384: assertEquals("3rd replaceObject worked incorrectly",
1385: ObjectStreamClass.class, obj3.getClass());
1386: }
1387: }
|