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

SerializationStressTest

public class SerializationStressTest extends TestCase implements Serializable
Automated Test Suite for class java.io.ObjectOutputStream

Fields Summary
static final String
FOO
static final String
MSG_TEST_FAILED
protected static final boolean
DEBUG
protected static boolean
xload
protected static boolean
xdump
protected static String
xFileName
protected transient int
dumpCount
protected transient ObjectInputStream
ois
protected transient ObjectOutputStream
oos
protected transient ByteArrayOutputStream
bao
static final Map
TABLE
static final Map
MAP
static final SortedMap
TREE
static final LinkedHashMap
LINKEDMAP
static final LinkedHashSet
LINKEDSET
static final IdentityHashMap
IDENTITYMAP
static final List
ALIST
static final List
LIST
static final Set
SET
static final Permission
PERM
static final PermissionCollection
PERMCOL
static final SortedSet
SORTSET
static final DateFormat
DATEFORM
static final ChoiceFormat
CHOICE
static final NumberFormat
NUMBERFORM
static final MessageFormat
MESSAGE
static final LinkedList
LINKEDLIST
static final SimpleTimeZone
TIME_ZONE
static final Calendar
CALENDAR
static Exception
INITIALIZE_EXCEPTION
Constructors Summary
public SerializationStressTest()


     
        try {
            TABLE.put("one", "1");
            TABLE.put("two", "2");
            TABLE.put("three", "3");
            MAP.put("one", "1");
            MAP.put("two", "2");
            MAP.put("three", "3");
            LINKEDMAP.put("one", "1");
            LINKEDMAP.put("two", "2");
            LINKEDMAP.put("three", "3");
            IDENTITYMAP.put("one", "1");
            IDENTITYMAP.put("two", "2");
            IDENTITYMAP.put("three", "3");
            LINKEDSET.add("one");
            LINKEDSET.add("two");
            LINKEDSET.add("three");
            TREE.put("one", "1");
            TREE.put("two", "2");
            TREE.put("three", "3");
            PERMCOL.add(PERM);
            // To make sure they all use the same Calendar
            CALENDAR.setTimeZone(new SimpleTimeZone(0, "GMT"));
            CALENDAR.set(1999, Calendar.JUNE, 23, 15, 47, 13);
            CALENDAR.set(Calendar.MILLISECOND, 553);
            DATEFORM.setCalendar(CALENDAR);
            java.text.DateFormatSymbols symbols = new java.text.DateFormatSymbols();
            symbols.setZoneStrings(new String[][] { { "a", "b", "c", "d" },
                    { "e", "f", "g", "h" } });
            ((java.text.SimpleDateFormat) DATEFORM).setDateFormatSymbols(symbols);
            DATEFORM.setNumberFormat(new java.text.DecimalFormat("#.#;'-'#.#"));
            DATEFORM.setTimeZone(TimeZone.getTimeZone("EST"));
            ((java.text.DecimalFormat) NUMBERFORM).applyPattern("#.#;'-'#.#");
            MESSAGE.setFormat(0, DATEFORM);
            MESSAGE.setFormat(1, DATEFORM);
        } catch (Exception e) {
            INITIALIZE_EXCEPTION = e;
        }
    
    
public SerializationStressTest(String name)

        super(name);
    
Methods Summary
protected voiddump(java.lang.Object o)

        if (dumpCount > 0)
            setUp();
        // Dump the object
        try {
            oos.writeObject(o);
        } finally {
            oos.close();
        }
    
protected java.lang.ObjectdumpAndReload(java.lang.Object o)

        dump(o);
        return reload();
    
public java.lang.StringgetDumpName()

        return getName() + dumpCount;
    
protected java.io.InputStreamloadStream()

        // Choose the load stream
        if (xload || xdump) {
            // Load from pre-existing file
            return new FileInputStream(xFileName + "-" + getDumpName() + ".ser");
        } else {
            // Just load from memory, we dumped to memory
            return new ByteArrayInputStream(bao.toByteArray());
        }
    
protected java.lang.Objectreload()

        ois = new ObjectInputStream(loadStream());
        dumpCount++;
        try {
            return ois.readObject();
        } finally {
            ois.close();
        }
    
protected voidsetUp()
Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

        if (INITIALIZE_EXCEPTION != null) {
            throw new ExceptionInInitializerError(INITIALIZE_EXCEPTION);
        }
        try {
            if (xdump) {
                oos = new ObjectOutputStream(new FileOutputStream(xFileName
                        + "-" + getDumpName() + ".ser"));
            } else {
                oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
            }
        } catch (Exception e) {
            fail("Exception thrown during setup : " + e.getMessage());
        }
    
protected voidt_MixPrimitivesAndObjects()


        
             
        int i = 7;
        String s1 = "string 1";
        String s2 = "string 2";
        byte[] bytes = { 1, 2, 3 };

        oos.writeInt(i);
        oos.writeObject(s1);
        oos.writeUTF(s2);
        oos.writeObject(bytes);
        oos.close();
        try {
            ois = new ObjectInputStream(loadStream());

            int j = ois.readInt();
            assertTrue("Wrong int :" + j, i == j);

            String l1 = (String) ois.readObject();
            assertTrue("Wrong obj String :" + l1, s1.equals(l1));

            String l2 = (String) ois.readUTF();
            assertTrue("Wrong UTF String :" + l2, s2.equals(l2));

            byte[] bytes2 = (byte[]) ois.readObject();
            assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2));

        } finally {
            ois.close();
        }
    
protected voidtearDown()
Tears down the fixture, for example, close a network connection. This method is called after a test is executed.

        if (oos != null) {
            try {
                oos.close();
            } catch (Exception e) {
            }
        }