Answer 11: Testing I/O

    public void testSerialization() throws IOException, ClassNotFoundException {
        Fraction source = new Fraction(1, 2);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(out);
        oout.writeObject(source);
        oout.close();
        
        byte[] data = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream oin = new ObjectInputStream(in);
        Fraction result = (Fraction) oin.readObject();
        
        assertEquals(source, result);
        assertNotSame(source, result);
    }

Previous | Next | Top | Cafe con Leche

Copyright 2005-2007 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 28, 2007