FileDocCategorySizeDatePackage
ObjectOutputStreamPutFieldTest.javaAPI DocAndroid 1.5 API8224Wed May 06 22:41:04 BST 2009tests.api.java.io

ObjectOutputStreamPutFieldTest

public class ObjectOutputStreamPutFieldTest extends TestCase
Tests the methods of {@code ObjectOutputStream.PutField}. Three things make this class somewhat difficult to test:
  1. It is a completely abstract class; none of the methods is implemented in {@code ObjectOutputStream.PutField}.
  2. There is no public class that implements {@code ObjectOutputStream.PutField}. The only way to get an implementation is by calling {@code ObjectOutputStream.putFields()}.
  3. Invoking the methods of {@code ObjectOutputStream.PutField} only works from within the private {@code writeObject(ObjectOutputStream)} method of a class that implements {@code Serializable}; an exception is thrown otherwise.

Given these restrictions, an indirect approach is used to test {@code ObjectOutputStream.PutField}: The serializable helper class {@code tests.support.Support_GetPutFields} implements {@code writeObject(ObjectOutputStream)} and uses all {@code putX} methods in {@code PutField} to write data to the output stream. A second helper class, {@code tests.support.Support_GetPutFieldsDeprecated}, also uses the deprecated {@code ObjectOutputStream.PutField.write(ObjectOutput)}. {@code tests.util.FieldTestFileGenerator} can then be used on a reference platform to write these two classes to the file {@code testFields.ser} and {@code testFieldsDeprecated.ser} respectively.

The test methods in this class expect to find {@code testFields.ser} and {@code testFieldsDeprecated.ser} as a resource stored at {@code tests/api/java/io}. The contents of these files is compared to what is written when {@code Support_GetPutFields.writeObject(ObjectOutputStream)} and {@code Support_GetPutFieldsDeprecated.writeObject(ObjectOutputStream)} is called by the test methods.

Fields Summary
private final String
FILENAME
private final String
DEPRECATED_FILENAME
Constructors Summary
Methods Summary
private byte[]getRefContent(java.lang.String path)

        int bytesRead;
        byte[] refContent;
        byte[] streamContent = new byte[2000];
        InputStream refStream = null;
        
        try {
            refStream = getClass().getResourceAsStream(path);
            bytesRead = refStream.read(streamContent);
            assertTrue("Test case implementation error: The byte array to " +
                       "store the reference file is too small.", 
                       (refStream.read() == -1));
            refContent = new byte[bytesRead];
            System.arraycopy(streamContent, 0, refContent, 0, bytesRead);
        }
        finally {
            if (refStream != null) refStream.close();
        }
        return refContent;
    
public voidtest_put()

        Support_GetPutFields toSerialize = new Support_GetPutFields();
        byte[] content;
        byte[] refContent;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos;
 
        toSerialize.initTestValues();

        try {
            refContent = getRefContent(FILENAME);
        
            baos = new ByteArrayOutputStream(refContent.length);
            oos = new ObjectOutputStream(baos);

            oos.writeObject(toSerialize);
            content = baos.toByteArray();
            assertTrue("Serialization is not equal to reference platform.", 
                        Arrays.equals(content, refContent));
        }
        finally {
            if (oos != null) oos.close();
        }
    
public voidtest_writeLjava_io_ObjectOutputStream()

        Support_GetPutFieldsDeprecated toSerialize = new Support_GetPutFieldsDeprecated();
        byte[] content;
        byte[] refContent;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos;
        
        toSerialize.initTestValues();
        
        try {
            refContent = getRefContent(DEPRECATED_FILENAME);
        
            baos = new ByteArrayOutputStream(refContent.length);
            oos = new ObjectOutputStream(baos);

            oos.writeObject(toSerialize);
            content = baos.toByteArray();
            assertTrue("Serialization is not equal to reference platform.", 
                        Arrays.equals(content, refContent));
        }
        finally {
            if (oos != null) oos.close();
        }