1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package libcore.java.io; 19 20import java.io.ByteArrayOutputStream; 21import java.io.InputStream; 22import java.io.ObjectOutputStream; 23import java.util.Arrays; 24import junit.framework.TestCase; 25import tests.support.Support_GetPutFields; 26import tests.support.Support_GetPutFieldsDeprecated; 27 28/** 29 * Tests the methods of {@code ObjectOutputStream.PutField}. Three things make 30 * this class somewhat difficult to test: 31 * <ol> 32 * <li>It is a completely abstract class; none of the methods is implemented in 33 * {@code ObjectOutputStream.PutField}.</li> 34 * <li>There is no public class that implements 35 * {@code ObjectOutputStream.PutField}. The only way to get an implementation 36 * is by calling {@code ObjectOutputStream.putFields()}.</li> 37 * <li>Invoking the methods of {@code ObjectOutputStream.PutField} only works 38 * from within the private {@code writeObject(ObjectOutputStream)} method of a 39 * class that implements {@code Serializable}; an exception is thrown 40 * otherwise.</li> 41 * </ol> 42 * <p> 43 * Given these restrictions, an indirect approach is used to test 44 * {@code ObjectOutputStream.PutField}: The serializable helper class 45 * {@code tests.support.Support_GetPutFields} implements 46 * {@code writeObject(ObjectOutputStream)} and uses all {@code putX} methods in 47 * {@code PutField} to write data to the output stream. A second helper class, 48 * {@code tests.support.Support_GetPutFieldsDeprecated}, also uses the 49 * deprecated {@code ObjectOutputStream.PutField.write(ObjectOutput)}. 50 * {@code tests.util.FieldTestFileGenerator} can then be used on a reference 51 * platform to write these two classes to the file {@code testFields.ser} and 52 * {@code testFieldsDeprecated.ser} respectively. 53 * </p> 54 * <p> 55 * The test methods in this class expect to find {@code testFields.ser} and 56 * {@code testFieldsDeprecated.ser} as a resource stored at 57 * {@code tests/api/java/io}. The contents of these files is compared to what 58 * is written when {@code Support_GetPutFields.writeObject(ObjectOutputStream)} 59 * and {@code Support_GetPutFieldsDeprecated.writeObject(ObjectOutputStream)} is 60 * called by the test methods. 61 * </p> 62 */ 63public class OldObjectOutputStreamPutFieldTest extends TestCase { 64 65 private final String FILENAME = 66 "/tests/api/java/io/testFields.ser"; 67 private final String DEPRECATED_FILENAME = 68 "/tests/api/java/io/testFieldsDeprecated.ser"; 69 70 public void test_put() throws Exception { 71 Support_GetPutFields toSerialize = new Support_GetPutFields(); 72 byte[] content; 73 byte[] refContent; 74 ObjectOutputStream oos = null; 75 ByteArrayOutputStream baos; 76 77 toSerialize.initTestValues(); 78 79 try { 80 refContent = getRefContent(FILENAME); 81 82 baos = new ByteArrayOutputStream(refContent.length); 83 oos = new ObjectOutputStream(baos); 84 85 oos.writeObject(toSerialize); 86 content = baos.toByteArray(); 87 assertTrue("Serialization is not equal to reference platform.", 88 Arrays.equals(content, refContent)); 89 } 90 finally { 91 if (oos != null) oos.close(); 92 } 93 } 94 95 public void test_writeLjava_io_ObjectOutputStream() throws Exception { 96 Support_GetPutFieldsDeprecated toSerialize = new Support_GetPutFieldsDeprecated(); 97 byte[] content; 98 byte[] refContent; 99 ObjectOutputStream oos = null; 100 ByteArrayOutputStream baos; 101 102 toSerialize.initTestValues(); 103 104 try { 105 refContent = getRefContent(DEPRECATED_FILENAME); 106 107 baos = new ByteArrayOutputStream(refContent.length); 108 oos = new ObjectOutputStream(baos); 109 110 oos.writeObject(toSerialize); 111 content = baos.toByteArray(); 112 assertTrue("Serialization is not equal to reference platform.", 113 Arrays.equals(content, refContent)); 114 } 115 finally { 116 if (oos != null) oos.close(); 117 } 118 } 119 120 private byte[] getRefContent(String path) throws Exception { 121 int bytesRead; 122 byte[] refContent; 123 byte[] streamContent = new byte[2000]; 124 InputStream refStream = null; 125 126 try { 127 refStream = getClass().getResourceAsStream(path); 128 bytesRead = refStream.read(streamContent); 129 assertTrue("Test case implementation error: The byte array to " + 130 "store the reference file is too small.", 131 (refStream.read() == -1)); 132 refContent = new byte[bytesRead]; 133 System.arraycopy(streamContent, 0, refContent, 0, bytesRead); 134 } 135 finally { 136 if (refStream != null) refStream.close(); 137 } 138 return refContent; 139 } 140} 141