📄 xmlobjectstreamunit.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util.testing;import java.io.*;import java.lang.reflect.Array;import java.util.*;import java.util.zip.*;import rice.p2p.util.*;/** * DESCRIBE THE CLASS * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author jeffh */public class XMLObjectStreamUnit { /** * DESCRIBE THE FIELD */ protected XMLObjectOutputStream xoos; /** * DESCRIBE THE FIELD */ protected XMLObjectInputStream xois; /** * DESCRIBE THE FIELD */ protected ByteArrayOutputStream baos; /** * DESCRIBE THE FIELD */ protected ByteArrayInputStream bais; /** * Constructor for XMLObjectStreamUnit. * * @exception IOException DESCRIBE THE EXCEPTION */ public XMLObjectStreamUnit() throws IOException { reset(); } /** * DESCRIBE THE METHOD * * @exception IOException DESCRIBE THE EXCEPTION */ protected void reset() throws IOException { baos = new ByteArrayOutputStream(); // xoos = new XMLObjectOutputStream(new GZIPOutputStream(new BufferedOutputStream(baos))); xoos = new XMLObjectOutputStream(new BufferedOutputStream(baos)); bais = null; xois = null; } /** * DESCRIBE THE METHOD * * @exception IOException DESCRIBE THE EXCEPTION */ protected void flip() throws IOException { xoos.close(); bais = new ByteArrayInputStream(baos.toByteArray()); // xois = new XMLObjectInputStreamm(new GZIPInputStream(bais)); xois = new XMLObjectInputStream(bais); } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testInt(int i) { try { xoos.writeInt(i); flip(); int j = xois.readInt(); reset(); if (i != j) { throw new IOException("int " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param b DESCRIBE THE PARAMETER */ protected void testBoolean(boolean b) { try { xoos.writeBoolean(b); flip(); boolean c = xois.readBoolean(); String xml = new String(baos.toByteArray()); reset(); if (b != c) { throw new IOException("boolean " + c + " was not equal to " + b + ". XML: " + xml); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testByte(byte i) { try { xoos.writeByte(i); flip(); byte j = xois.readByte(); reset(); if (i != j) { throw new IOException("byte " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testChar(char i) { try { xoos.writeChar(i); flip(); char j = xois.readChar(); reset(); if (i != j) { throw new IOException("char " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testDouble(double i) { try { xoos.writeDouble(i); flip(); double j = xois.readDouble(); reset(); if (i != j) { throw new IOException("double " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testFloat(float i) { try { xoos.writeFloat(i); flip(); float j = xois.readFloat(); reset(); if (i != j) { throw new IOException("float " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testLong(long i) { try { xoos.writeLong(i); flip(); long j = xois.readLong(); reset(); if (i != j) { throw new IOException("long " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit * * @param i DESCRIBE THE PARAMETER */ protected void testShort(short i) { try { xoos.writeShort(i); flip(); short j = xois.readShort(); reset(); if (i != j) { throw new IOException("short " + j + " was not equal to " + i); } } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit */ protected void testMultiplePrimitives() { try { int i = -1029; double d = 2939.22; long l = 1929029389303L; xoos.writeInt(i); xoos.writeDouble(d); xoos.writeLong(l); flip(); if ((xois.readInt() != i) || (xois.readDouble() != d) || (xois.readLong() != l)) { throw new IOException("Multiple primitives were not read correctly!"); } reset(); } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * DESCRIBE THE METHOD * * @param o1 DESCRIBE THE PARAMETER * @param o2 DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ protected boolean compare(Object o1, Object o2) { if (o1.equals(o2)) { return true; }// System.out.println("comparing: "+o1.getClass()+": "+o1+" "+o2.getClass()+" "+o2); if (o1.getClass().isArray() && o2.getClass().isArray() && o1.getClass().getComponentType().equals(o2.getClass().getComponentType())) { if (Array.getLength(o1) != Array.getLength(o2)) { return false; } if (o2.getClass().getComponentType().isPrimitive()) { Class c = o2.getClass().getComponentType(); if (c.equals(Integer.TYPE)) { return Arrays.equals((int[]) o1, (int[]) o2); } else if (c.equals(Boolean.TYPE)) { return Arrays.equals((boolean[]) o1, (boolean[]) o2); } else if (c.equals(Byte.TYPE)) { return Arrays.equals((byte[]) o1, (byte[]) o2); } else if (c.equals(Character.TYPE)) { return Arrays.equals((char[]) o1, (char[]) o2); } else if (c.equals(Double.TYPE)) { return Arrays.equals((double[]) o1, (double[]) o2); } else if (c.equals(Float.TYPE)) { return Arrays.equals((float[]) o1, (float[]) o2); } else if (c.equals(Long.TYPE)) { return Arrays.equals((long[]) o1, (long[]) o2); } else if (c == Short.TYPE) { return Arrays.equals((short[]) o1, (short[]) o2); } else { throw new IllegalArgumentException("Class " + c + " is not primitive!"); } } else { for (int i = 0; i < Array.getLength(o1); i++) { if (!compare(Array.get(o1, i), Array.get(o2, i))) { return false; } } return true; } } return false; } /** * A unit test for JUnit * * @param o DESCRIBE THE PARAMETER */ protected void test(Object o) { try { long start = System.currentTimeMillis(); xoos.writeObject(o); flip(); long mid = System.currentTimeMillis(); Object o2 = xois.readObject(); long end = System.currentTimeMillis(); System.out.println("WRITE: " + (mid - start) + " READ: " + (end - mid)); if (o == o2) { throw new IOException("Returned object is identicial to first!"); } if (!compare(o, o2)) { System.out.println("XML IS: " + (new String(baos.toByteArray()))); throw new IOException("Object " + o2 + " was not equal to " + o); } reset(); } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } catch (ClassNotFoundException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit */ protected void testHashtable() { try { Hashtable table = new Hashtable(); Integer i = new Integer(1020); table.put("monkey", i); table.put("cat", i); table.put(i, i); xoos.writeObject(table); flip(); Hashtable table2 = (Hashtable) xois.readObject(); if (table == table2) { throw new IOException("Returned table is identicial to first!"); } if (!(table.equals(table2))) { throw new IOException("Object " + table2 + " was not equal to " + table); } if (!(table2.get("monkey") == table2.get("cat"))) { throw new IOException("Object " + table2.get("monkey") + " was not equal to " + table2.get("cat")); } reset(); } catch (IOException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } catch (ClassNotFoundException e) { System.out.println("test produced exception " + e); e.printStackTrace(); } } /** * A unit test for JUnit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -