You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [11/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/PushbackReaderTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,288 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.io;
+
+import java.io.CharArrayReader;
+import java.io.FilterReader;
+import java.io.IOException;
+import java.io.PushbackReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+public class PushbackReaderTest extends junit.framework.TestCase {
+
+ PushbackReader pbr;
+
+ String pbString = "Hello World";
+
+ /**
+ * @tests java.io.PushbackReader#PushbackReader(java.io.Reader)
+ */
+ public void test_ConstructorLjava_io_Reader() {
+ // Test for method java.io.PushbackReader(java.io.Reader)
+ try {
+ pbr.close();
+ pbr = new PushbackReader(new StringReader(pbString));
+ char buf[] = new char[5];
+ pbr.read(buf, 0, 5);
+ pbr.unread(buf);
+ } catch (IOException e) {
+ // Correct
+ return;
+ }
+ fail("Created reader with buffer larger than 1");
+ }
+
+ /**
+ * @tests java.io.PushbackReader#PushbackReader(java.io.Reader, int)
+ */
+ public void test_ConstructorLjava_io_ReaderI() {
+ // Test for method java.io.PushbackReader(java.io.Reader, int)
+ assertTrue("Used to test", true);
+ }
+
+ /**
+ * @tests java.io.PushbackReader#close()
+ */
+ public void test_close() {
+ // Test for method void java.io.PushbackReader.close()
+ try {
+ pbr.close();
+ pbr.read();
+ } catch (Exception e) {
+ return;
+ }
+ fail("Failed to throw exception reading from closed reader");
+ }
+
+ /**
+ * @tests java.io.PushbackReader#mark(int)
+ */
+ public void test_markI() {
+ try {
+ pbr.mark(3);
+ } catch (IOException e) {
+ // correct
+ return;
+ }
+ fail("mark failed to throw expected IOException");
+ }
+
+ /**
+ * @tests java.io.PushbackReader#markSupported()
+ */
+ public void test_markSupported() {
+ // Test for method boolean java.io.PushbackReader.markSupported()
+ assertTrue("markSupported returned true", !pbr.markSupported());
+ }
+
+ /**
+ * @tests java.io.PushbackReader#read()
+ */
+ public void test_read() {
+ // Test for method int java.io.PushbackReader.read()
+ try {
+ char c;
+ pbr.read();
+ c = (char) pbr.read();
+ assertTrue("Failed to read char: " + c, c == pbString.charAt(1));
+ Reader reader = new PushbackReader(new CharArrayReader(
+ new char[] { '\u8765' }));
+ assertTrue("Wrong double byte character", reader.read() == '\u8765');
+ } catch (IOException e) {
+ fail("IOException during read test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#read(char[], int, int)
+ */
+ public void test_read$CII() {
+ // Test for method int java.io.PushbackReader.read(char [], int, int)
+ try {
+ char[] c = new char[5];
+ pbr.read(c, 0, 5);
+ assertTrue("Failed to read chars", new String(c).equals(pbString
+ .substring(0, 5)));
+ } catch (IOException e) {
+ fail("IOException during read test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#ready()
+ */
+ public void test_ready() {
+ // Test for method boolean java.io.PushbackReader.ready()
+ try {
+ char[] c = new char[11];
+ if (c.length > 0)
+ ;// use c to avoid warning msg
+ assertTrue("Ready stream returned false to ready()", pbr.ready());
+ } catch (IOException e) {
+ fail("IOException during ready() test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#reset()
+ */
+ public void test_reset() {
+ try {
+ pbr.reset();
+ } catch (IOException e) {
+ // correct
+ return;
+ }
+ fail("mark failed to throw expected IOException");
+ }
+
+ /**
+ * @tests java.io.PushbackReader#unread(char[])
+ */
+ public void test_unread$C() {
+ // Test for method void java.io.PushbackReader.unread(char [])
+ try {
+ char[] c = new char[5];
+ pbr.read(c, 0, 5);
+ pbr.unread(c);
+ pbr.read(c, 0, 5);
+ assertTrue("Failed to unread chars", new String(c).equals(pbString
+ .substring(0, 5)));
+ } catch (IOException e) {
+ fail("IOException during read test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#skip(long)
+ */
+ public void test_skip$J() {
+ char chars[] = new char[] { 'h', 'e', 'l', 'l', 'o' };
+ for (int i = 0; i < 3; i++) {
+ Reader reader, reader2;
+ switch (i) {
+ case 0:
+ reader = new StringReader(new String(chars));
+ reader2 = new StringReader(new String(chars));
+ break;
+ case 1:
+ reader = new FilterReader(new StringReader(new String(chars))) {
+ };
+ reader2 = new FilterReader(new StringReader(new String(chars))) {
+ };
+ break;
+ default:
+ reader = new CharArrayReader(chars);
+ reader2 = new CharArrayReader(chars);
+ }
+ PushbackReader pReader = new PushbackReader(reader, 2);
+ PushbackReader pReader2 = new PushbackReader(reader2, 2);
+ boolean skipped = false;
+ long numSkipped = 0;
+ try {
+ numSkipped = pReader2.skip(3);
+ pReader2.unread('a');
+ pReader2.unread('b');
+ numSkipped += pReader2.skip(10);
+ numSkipped += pReader2.skip(10);
+ numSkipped += pReader2.skip(10);
+ numSkipped += pReader2.skip(10);
+ numSkipped += pReader2.skip(10);
+ numSkipped += pReader2.skip(10);
+ assertTrue("Did not skip correct number of characters",
+ numSkipped == 7);
+ numSkipped = 0;
+ numSkipped += pReader.skip(2);
+ pReader.unread('i');
+ numSkipped += pReader.skip(2);
+ numSkipped += pReader.skip(0);
+ skipped = true;
+ numSkipped += pReader.skip(-1);
+ fail("Failed to throw "
+ + new IllegalArgumentException().getClass().getName());
+ } catch (IllegalArgumentException e) {
+ assertTrue("Failed to skip characters" + e, skipped);
+ } catch (IOException e) {
+ fail("Failed to skip characters" + e);
+ }
+ try {
+ numSkipped += pReader.skip(1);
+ numSkipped += pReader.skip(1);
+ numSkipped += pReader.skip(1);
+ assertTrue("Failed to skip all characters", numSkipped == 6);
+ long nextSkipped = pReader.skip(1);
+ assertTrue("skipped empty reader", nextSkipped == 0);
+ } catch (IOException e) {
+ fail("Failed to skip more characters" + e);
+ }
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#unread(char[], int, int)
+ */
+ public void test_unread$CII() {
+ // Test for method void java.io.PushbackReader.unread(char [], int, int)
+ try {
+ char[] c = new char[5];
+ pbr.read(c, 0, 5);
+ pbr.unread(c, 0, 2);
+ pbr.read(c, 0, 5);
+ assertTrue("Failed to unread chars", new String(c).equals(pbString
+ .substring(0, 2)
+ + pbString.substring(5, 8)));
+ } catch (IOException e) {
+ fail("IOException during unread test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.PushbackReader#unread(int)
+ */
+ public void test_unreadI() {
+ // Test for method void java.io.PushbackReader.unread(int)
+
+ try {
+ int c;
+ pbr.read();
+ c = pbr.read();
+ pbr.unread(c);
+ assertTrue("Failed to unread char", pbr.read() == c);
+ } catch (IOException e) {
+ fail("IOException during unread test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ pbr = new PushbackReader(new StringReader(pbString), 10);
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ try {
+ pbr.close();
+ } catch (IOException e) {
+ }
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,765 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.io;
+
+import tests.support.Support_PlatformFile;
+
+public class RandomAccessFileTest extends junit.framework.TestCase {
+
+ public String fileName;
+
+ public boolean ufile = true;
+
+ java.io.RandomAccessFile raf;
+
+ java.io.FileInputStream fis;
+
+ java.io.File f;
+
+ String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064";
+
+ java.io.FileOutputStream fos;
+
+ public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_Class
NotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Obj
ect\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketE
xception\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
+
+ /**
+ * @tests java.io.RandomAccessFile#RandomAccessFile(java.io.File,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_io_FileLjava_lang_String() {
+ // Test for method java.io.RandomAccessFile(java.io.File,
+ // java.lang.String)
+ try {
+ raf = new java.io.RandomAccessFile(f, "rw");
+ raf.write(20);
+ raf.seek(0);
+ assertTrue("Incorrect int read/written", raf.read() == 20);
+ raf.close();
+ } catch (Exception e) {
+ fail("Exception during constructor test: " + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_lang_String() {
+ // Test for method java.io.RandomAccessFile(java.lang.String,
+ // java.lang.String)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.write("Test".getBytes(), 0, 4);
+ } catch (java.io.IOException e) {
+ fail("Constructor test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#close()
+ */
+ public void test_close() {
+ // Test for method void java.io.RandomAccessFile.close()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.close();
+ raf.write("Test".getBytes(), 0, 4);
+ fail("Failed to close file properly");
+ } catch (java.io.IOException e) {
+ }
+
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#getFD()
+ */
+ public void test_getFD() {
+ // Test for method java.io.FileDescriptor
+ // java.io.RandomAccessFile.getFD()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ assertTrue("Returned invalid fd", raf.getFD().valid());
+ raf.close();
+ assertTrue("Returned valid fd after close", !raf.getFD().valid());
+ } catch (java.io.IOException e) {
+ fail("getFD test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#getFilePointer()
+ */
+ public void test_getFilePointer() {
+ // Test for method long java.io.RandomAccessFile.getFilePointer()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.write(fileString.getBytes(), 0, 1000);
+ assertTrue("Incorrect filePointer returned",
+ raf.getFilePointer() == 1000);
+ } catch (java.io.IOException e) {
+ fail("getFilePointer test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#length()
+ */
+ public void test_length() {
+ // Test for method long java.io.RandomAccessFile.length()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.write(fileString.getBytes());
+ assertTrue("Incorrect length returned", raf.length() == fileString
+ .length());
+ } catch (java.io.IOException e) {
+ fail("length test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#read()
+ */
+ public void test_read() {
+ // Test for method int java.io.RandomAccessFile.read()
+ try {
+ java.io.FileOutputStream fos = new java.io.FileOutputStream(
+ fileName);
+ fos.write(fileString.getBytes(), 0, fileString.length());
+ fos.close();
+ int c;
+ raf = new java.io.RandomAccessFile(fileName, "r");
+ c = raf.read();
+ assertTrue("Incorrect bytes returned from read", c == fileString
+ .charAt(0));
+ } catch (java.io.IOException e) {
+ fail("Read test threw an IOException : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#read(byte[])
+ */
+ public void test_read$B() {
+ // Test for method int java.io.RandomAccessFile.read(byte [])
+ try {
+ java.io.FileOutputStream fos = new java.io.FileOutputStream(
+ fileName);
+ fos.write(fileString.getBytes(), 0, fileString.length());
+ fos.close();
+ raf = new java.io.RandomAccessFile(fileName, "r");
+ byte[] rbuf = new byte[4000];
+ raf.read(rbuf);
+ assertTrue("Incorrect bytes returned from read", fileString
+ .equals(new String(rbuf, 0, fileString.length())));
+ } catch (java.io.IOException e) {
+ fail("Read test threw an IOException : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#read(byte[], int, int)
+ */
+ public void test_read$BII() {
+ // Test for method int java.io.RandomAccessFile.read(byte [], int, int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ byte[] rbuf = new byte[4000];
+ java.io.FileOutputStream fos = new java.io.FileOutputStream(
+ fileName);
+ fos.write(fileString.getBytes(), 0, fileString.length());
+ fos.close();
+ raf.read(rbuf, 0, fileString.length());
+ assertTrue("Incorrect bytes returned from read", fileString
+ .equals(new String(rbuf, 0, fileString.length())));
+ } catch (java.io.IOException e) {
+ fail("Read test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readBoolean()
+ */
+ public void test_readBoolean() {
+ // Test for method boolean java.io.RandomAccessFile.readBoolean()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBoolean(true);
+ raf.seek(0);
+ assertTrue("Incorrect boolean read/written", raf.readBoolean());
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readBoolean test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readByte()
+ */
+ public void test_readByte() {
+ // Test for method byte java.io.RandomAccessFile.readByte()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeByte(127);
+ raf.seek(0);
+ assertTrue("Incorrect bytes read/written", raf.readByte() == 127);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readByte test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readChar()
+ */
+ public void test_readChar() {
+ // Test for method char java.io.RandomAccessFile.readChar()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeChar('T');
+ raf.seek(0);
+ assertTrue("Incorrect char read/written", raf.readChar() == 'T');
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readChar test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readDouble()
+ */
+ public void test_readDouble() {
+ // Test for method double java.io.RandomAccessFile.readDouble()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeDouble(Double.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect double read/written",
+ raf.readDouble() == Double.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readDouble test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readFloat()
+ */
+ public void test_readFloat() {
+ // Test for method float java.io.RandomAccessFile.readFloat()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeFloat(Float.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect float read/written",
+ raf.readFloat() == Float.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readFloat test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readFully(byte[])
+ */
+ public void test_readFully$B() {
+ // Test for method void java.io.RandomAccessFile.readFully(byte [])
+ try {
+ byte[] buf = new byte[10];
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBytes("HelloWorld");
+ raf.seek(0);
+ raf.readFully(buf);
+ assertTrue("Incorrect bytes read/written", "HelloWorld"
+ .equals(new String(buf, 0, 10)));
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readFully threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readFully(byte[], int, int)
+ */
+ public void test_readFully$BII() {
+ // Test for method void java.io.RandomAccessFile.readFully(byte [], int,
+ // int)
+ try {
+ byte[] buf = new byte[10];
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBytes("HelloWorld");
+ raf.seek(0);
+ raf.readFully(buf, 0, buf.length);
+ assertTrue("Incorrect bytes read/written", "HelloWorld"
+ .equals(new String(buf, 0, 10)));
+ try {
+ raf.readFully(buf, 0, buf.length);
+ } catch (java.io.EOFException e) {
+ // correct
+ return;
+ }
+ fail("Reading past end of buffer did not throw EOFException");
+ } catch (java.io.IOException e) {
+ fail("readFully test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readInt()
+ */
+ public void test_readInt() {
+ // Test for method int java.io.RandomAccessFile.readInt()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeInt(Integer.MIN_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect int read/written",
+ raf.readInt() == Integer.MIN_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readInt test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readLine()
+ */
+ public void test_readLine() {
+ // Test for method java.lang.String java.io.RandomAccessFile.readLine()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ String s = "Goodbye\nCruel\nWorld\n";
+ raf.write(s.getBytes(), 0, s.length());
+ raf.seek(0);
+ assertTrue("1st readLine returned incorrect string", "Goodbye"
+ .equals(raf.readLine()));
+ assertTrue("2nd readLine returned incorrect string", "Cruel"
+ .equals(raf.readLine()));
+ assertTrue("3rd readLine returned incorrect string", "World"
+ .equals(raf.readLine()));
+ } catch (java.io.IOException e) {
+ fail("readLine test threw an IOException : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readLong()
+ */
+ public void test_readLong() {
+ // Test for method long java.io.RandomAccessFile.readLong()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeLong(Long.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect long read/written",
+ raf.readLong() == Long.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readLongtest threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readShort()
+ */
+ public void test_readShort() {
+ // Test for method short java.io.RandomAccessFile.readShort()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeShort(Short.MIN_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect long read/written",
+ raf.readShort() == Short.MIN_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readShort test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readUnsignedByte()
+ */
+ public void test_readUnsignedByte() {
+ // Test for method int java.io.RandomAccessFile.readUnsignedByte()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeByte(-1);
+ raf.seek(0);
+ assertTrue("Incorrect byte read/written",
+ raf.readUnsignedByte() == 255);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readUnsignedByte test threw an IOException : "
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readUnsignedShort()
+ */
+ public void test_readUnsignedShort() {
+ // Test for method int java.io.RandomAccessFile.readUnsignedShort()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeShort(-1);
+ raf.seek(0);
+ assertTrue("Incorrect byte read/written",
+ raf.readUnsignedShort() == 65535);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readUnsignedShort test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#readUTF()
+ */
+ public void test_readUTF() {
+ // Test for method java.lang.String java.io.RandomAccessFile.readUTF()
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeUTF(unihw);
+ raf.seek(0);
+ assertTrue("Incorrect utf string read", raf.readUTF().equals(unihw));
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("readUTF test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#seek(long)
+ */
+ public void test_seekJ() {
+ // Test for method void java.io.RandomAccessFile.seek(long)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.write(fileString.getBytes(), 0, fileString.length());
+ raf.seek(12);
+ assertTrue("Seek failed to set filePointer",
+ raf.getFilePointer() == 12);
+ } catch (java.io.IOException e) {
+ fail("seek test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#skipBytes(int)
+ */
+ public void test_skipBytesI() {
+ // Test for method int java.io.RandomAccessFile.skipBytes(int)
+ try {
+ byte[] buf = new byte[5];
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBytes("HelloWorld");
+ raf.seek(0);
+ raf.skipBytes(5);
+ raf.readFully(buf);
+ assertTrue("Failed to skip bytes", "World".equals(new String(buf,
+ 0, 5)));
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("skipBytes threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#write(byte[])
+ */
+ public void test_write$B() {
+ // Test for method void java.io.RandomAccessFile.write(byte [])
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ byte[] rbuf = new byte[4000];
+ raf.write(fileString.getBytes());
+ raf.close();
+ fis = new java.io.FileInputStream(fileName);
+ fis.read(rbuf, 0, fileString.length());
+ assertTrue("Incorrect bytes written", fileString.equals(new String(
+ rbuf, 0, fileString.length())));
+ } catch (java.io.IOException e) {
+ fail("Write test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#write(byte[], int, int)
+ */
+ public void test_write$BII() {
+ // Test for method void java.io.RandomAccessFile.write(byte [], int,
+ // int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ byte[] rbuf = new byte[4000];
+ raf.write(fileString.getBytes(), 0, fileString.length());
+ raf.close();
+ fis = new java.io.FileInputStream(fileName);
+ fis.read(rbuf, 0, fileString.length());
+ assertTrue("Incorrect bytes written", fileString.equals(new String(
+ rbuf, 0, fileString.length())));
+ } catch (java.io.IOException e) {
+ fail("Write test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#write(int)
+ */
+ public void test_writeI() {
+ // Test for method void java.io.RandomAccessFile.write(int)
+ try {
+ byte[] rbuf = new byte[4000];
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.write('t');
+ raf.close();
+ fis = new java.io.FileInputStream(fileName);
+ fis.read(rbuf, 0, 1);
+ assertTrue("Incorrect byte written", 't' == rbuf[0]);
+ } catch (java.io.IOException e) {
+ fail("Write test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeBoolean(boolean)
+ */
+ public void test_writeBooleanZ() {
+ // Test for method void java.io.RandomAccessFile.writeBoolean(boolean)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBoolean(true);
+ raf.seek(0);
+ assertTrue("Incorrect boolean read/written", raf.readBoolean());
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeBoolean test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeByte(int)
+ */
+ public void test_writeByteI() {
+ // Test for method void java.io.RandomAccessFile.writeByte(int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeByte(127);
+ raf.seek(0);
+ assertTrue("Incorrect byte read/written", raf.readByte() == 127);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("Write test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeBytes(java.lang.String)
+ */
+ public void test_writeBytesLjava_lang_String() {
+ // Test for method void
+ // java.io.RandomAccessFile.writeBytes(java.lang.String)
+ try {
+ byte[] buf = new byte[10];
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeBytes("HelloWorld");
+ raf.seek(0);
+ raf.readFully(buf);
+ assertTrue("Incorrect bytes read/written", "HelloWorld"
+ .equals(new String(buf, 0, 10)));
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeBytes threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeChar(int)
+ */
+ public void test_writeCharI() {
+ // Test for method void java.io.RandomAccessFile.writeChar(int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeChar('T');
+ raf.seek(0);
+ assertTrue("Incorrect char read/written", raf.readChar() == 'T');
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeChar test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeChars(java.lang.String)
+ */
+ public void test_writeCharsLjava_lang_String() {
+ // Test for method void
+ // java.io.RandomAccessFile.writeChars(java.lang.String)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeChars("HelloWorld");
+ char[] hchars = new char[10];
+ "HelloWorld".getChars(0, 10, hchars, 0);
+ raf.seek(0);
+ for (int i = 0; i < hchars.length; i++)
+ assertTrue("Incorrect string written",
+ raf.readChar() == hchars[i]);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeChars test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeDouble(double)
+ */
+ public void test_writeDoubleD() {
+ // Test for method void java.io.RandomAccessFile.writeDouble(double)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeDouble(Double.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect double read/written",
+ raf.readDouble() == Double.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeDouble test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeFloat(float)
+ */
+ public void test_writeFloatF() {
+ // Test for method void java.io.RandomAccessFile.writeFloat(float)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeFloat(Float.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect float read/written",
+ raf.readFloat() == Float.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeFloat test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeInt(int)
+ */
+ public void test_writeIntI() {
+ // Test for method void java.io.RandomAccessFile.writeInt(int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeInt(Integer.MIN_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect int read/written",
+ raf.readInt() == Integer.MIN_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeLong test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeLong(long)
+ */
+ public void test_writeLongJ() {
+ // Test for method void java.io.RandomAccessFile.writeLong(long)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeLong(Long.MAX_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect long read/written",
+ raf.readLong() == Long.MAX_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeLong test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeShort(int)
+ */
+ public void test_writeShortI() {
+ // Test for method void java.io.RandomAccessFile.writeShort(int)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeShort(Short.MIN_VALUE);
+ raf.seek(0);
+ assertTrue("Incorrect long read/written",
+ raf.readShort() == Short.MIN_VALUE);
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeShort test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.RandomAccessFile#writeUTF(java.lang.String)
+ */
+ public void test_writeUTFLjava_lang_String() {
+ // Test for method void
+ // java.io.RandomAccessFile.writeUTF(java.lang.String)
+ try {
+ raf = new java.io.RandomAccessFile(fileName, "rw");
+ raf.writeUTF(unihw);
+ raf.seek(0);
+ assertTrue("Incorrect utf string", raf.readUTF().equals(unihw));
+ raf.close();
+ } catch (java.io.IOException e) {
+ fail("writeUTF test threw an IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ try {
+ String fname = Support_PlatformFile.getNewPlatformFile("",
+ "raf.tst");
+ f = new java.io.File(System.getProperty("user.dir"), fname);
+ fileName = f.getAbsolutePath();
+ if (f.exists())
+ if (!f.delete()) {
+ fail("Unable to delete test file : " + f);
+ }
+ } catch (Exception e) {
+ fail("Exception during setUp : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ try {
+ if (fis != null)
+ fis.close();
+ if (fos != null)
+ fos.close();
+ if (raf != null)
+ raf.close();
+ } catch (Throwable e) {
+ }
+ if (f.exists())
+ f.delete();
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SequenceInputStreamTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,151 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.SequenceInputStream;
+import java.util.Enumeration;
+
+public class SequenceInputStreamTest extends junit.framework.TestCase {
+
+ SequenceInputStream si;
+
+ String s1 = "Hello";
+
+ String s2 = "World";
+
+ /**
+ * @tests java.io.SequenceInputStream#SequenceInputStream(java.io.InputStream,
+ * java.io.InputStream)
+ */
+ public void test_ConstructorLjava_io_InputStreamLjava_io_InputStream() {
+ // Test for method java.io.SequenceInputStream(java.io.InputStream,
+ // java.io.InputStream)
+ // Used in tests
+ }
+
+ /**
+ * @tests java.io.SequenceInputStream#SequenceInputStream(java.util.Enumeration)
+ */
+ public void test_ConstructorLjava_util_Enumeration() {
+ // Test for method java.io.SequenceInputStream(java.util.Enumeration)
+ class StreamEnumerator implements Enumeration {
+ InputStream streams[] = new InputStream[2];
+
+ int count = 0;
+
+ public StreamEnumerator() {
+ streams[0] = new ByteArrayInputStream(s1.getBytes());
+ streams[1] = new ByteArrayInputStream(s2.getBytes());
+ }
+
+ public boolean hasMoreElements() {
+ return count < streams.length;
+ }
+
+ public Object nextElement() {
+ return streams[count++];
+ }
+ }
+
+ try {
+ si = new SequenceInputStream(new StreamEnumerator());
+ byte buf[] = new byte[s1.length() + s2.length()];
+ si.read(buf, 0, s1.length());
+ si.read(buf, s1.length(), s2.length());
+ assertTrue("Read incorrect bytes: " + new String(buf), new String(
+ buf).equals(s1 + s2));
+ } catch (IOException e) {
+ fail("IOException during read test : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.io.SequenceInputStream#available()
+ */
+ public void test_available() {
+ // Test for method int java.io.SequenceInputStream.available()
+ try {
+
+ assertTrue("Returned incorrect number of bytes: " + si.available(),
+ si.available() == s1.length());
+ } catch (IOException e) {
+ fail("IOException during available test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.SequenceInputStream#close()
+ */
+ public void test_close() {
+ // Test for method void java.io.SequenceInputStream.close()
+ try {
+ si.close();
+ } catch (IOException e) {
+ fail("IOException during close test : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.io.SequenceInputStream#read()
+ */
+ public void test_read() {
+ // Test for method int java.io.SequenceInputStream.read()
+ try {
+ si.read();
+ assertTrue("Read incorrect char", (char) si.read() == s1.charAt(1));
+ } catch (IOException e) {
+ fail("IOException during read test: " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.io.SequenceInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() {
+ // Test for method int java.io.SequenceInputStream.read(byte [], int,
+ // int)
+ try {
+ byte buf[] = new byte[s1.length() + s2.length()];
+ si.read(buf, 0, s1.length());
+ si.read(buf, s1.length(), s2.length());
+ assertTrue("Read incorrect bytes: " + new String(buf), new String(
+ buf).equals(s1 + s2));
+ } catch (IOException e) {
+ fail("IOException during read test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ si = new SequenceInputStream(new ByteArrayInputStream(s1.getBytes()),
+ new ByteArrayInputStream(s2.getBytes()));
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializablePermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializablePermissionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializablePermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializablePermissionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,57 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.io;
+
+import java.io.SerializablePermission;
+
+public class SerializablePermissionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.io.SerializablePermission#SerializablePermission(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.io.SerializablePermission(java.lang.String)
+ assertTrue("permission ill-formed", new SerializablePermission(
+ "enableSubclassImplementation").getName().equals(
+ "enableSubclassImplementation"));
+ }
+
+ /**
+ * @tests java.io.SerializablePermission#SerializablePermission(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_lang_String() {
+ // Test for method java.io.SerializablePermission(java.lang.String,
+ // java.lang.String)
+ assertTrue("permission ill-formed", new SerializablePermission(
+ "enableSubclassImplementation", "").getName().equals(
+ "enableSubclassImplementation"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializationStressTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializationStressTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializationStressTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/SerializationStressTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1051 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InvalidObjectException;
+import java.io.NotActiveException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.io.StreamCorruptedException;
+import java.io.WriteAbortedException;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.IdentityHashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.PropertyPermission;
+import java.util.Set;
+import java.util.SimpleTimeZone;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TimeZone;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.Vector;
+
+/**
+ * Automated Test Suite for class java.io.ObjectOutputStream
+ *
+ */
+public class SerializationStressTest extends junit.framework.TestCase implements
+ Serializable {
+
+ // protected static final String MODE_XLOAD = "xload";
+
+ // protected static final String MODE_XDUMP = "xdump";
+
+ static final String FOO = "foo";
+
+ static final String MSG_TEST_FAILED = "Failed to write/read/assertion checking: ";
+
+ protected static final boolean DEBUG = false;
+
+ protected static boolean xload = false;
+
+ protected static boolean xdump = false;
+
+ protected static String xFileName = null;
+
+ protected transient int dumpCount = 0;
+
+ protected transient ObjectInputStream ois;
+
+ protected transient ObjectOutputStream oos;
+
+ protected transient ByteArrayOutputStream bao;
+
+ // -----------------------------------------------------------------------------------
+
+ private static class ObjectInputStreamSubclass extends ObjectInputStream {
+ private Vector resolvedClasses = new Vector();
+
+ public ObjectInputStreamSubclass(InputStream in) throws IOException,
+ StreamCorruptedException {
+ super(in);
+ }
+
+ public Class resolveClass(ObjectStreamClass osClass)
+ throws IOException, ClassNotFoundException {
+ Class result = super.resolveClass(osClass);
+ resolvedClasses.addElement(result);
+ return result;
+ }
+
+ public Class[] resolvedClasses() {
+ return (Class[]) resolvedClasses.toArray(new Class[resolvedClasses
+ .size()]);
+ }
+ }
+
+ static final Map TABLE = new Hashtable();
+
+ static final Map MAP = new HashMap();
+
+ static final SortedMap TREE = new TreeMap();
+
+ static final LinkedHashMap LINKEDMAP = new LinkedHashMap();
+
+ static final LinkedHashSet LINKEDSET = new LinkedHashSet();
+
+ static final IdentityHashMap IDENTITYMAP = new IdentityHashMap();
+
+ static final List ALIST = Arrays.asList(new String[] { "a", "list", "of",
+ "strings" });
+
+ static final List LIST = new ArrayList(ALIST);
+
+ static final Set SET = new HashSet(Arrays.asList(new String[] { "one",
+ "two", "three" }));
+
+ static final Permission PERM = new PropertyPermission("file.encoding",
+ "write");
+
+ static final PermissionCollection PERMCOL = PERM.newPermissionCollection();
+
+ static final SortedSet SORTSET = new TreeSet(Arrays.asList(new String[] {
+ "one", "two", "three" }));
+
+ static final java.text.DateFormat DATEFORM = java.text.DateFormat
+ .getInstance();
+
+ static final java.text.ChoiceFormat CHOICE = new java.text.ChoiceFormat(
+ "1#one|2#two|3#three");
+
+ static final java.text.NumberFormat NUMBERFORM = java.text.NumberFormat
+ .getInstance();
+
+ static final java.text.MessageFormat MESSAGE = new java.text.MessageFormat(
+ "the time: {0,time} and date {0,date}");
+
+ static final LinkedList LINKEDLIST = new LinkedList(Arrays
+ .asList(new String[] { "a", "linked", "list", "of", "strings" }));
+
+ static final SimpleTimeZone TIME_ZONE = new SimpleTimeZone(3600000,
+ "S-TEST");
+
+ static final Calendar CALENDAR = new GregorianCalendar(TIME_ZONE);
+
+ static {
+ 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);
+ }
+
+ public SerializationStressTest() {
+ }
+
+ public SerializationStressTest(String name) {
+ super(name);
+ }
+
+ public String getDumpName() {
+ return getName() + dumpCount;
+ }
+
+ protected void dump(Object o) throws IOException, ClassNotFoundException {
+ if (dumpCount > 0)
+ setUp();
+ // Dump the object
+ try {
+ oos.writeObject(o);
+ } finally {
+ oos.close();
+ }
+ }
+
+ protected Object dumpAndReload(Object o) throws IOException,
+ ClassNotFoundException {
+ dump(o);
+ return reload();
+ }
+
+ protected InputStream loadStream() throws IOException {
+ // 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 Object reload() throws IOException, ClassNotFoundException {
+ ois = new ObjectInputStream(loadStream());
+ dumpCount++;
+ try {
+ return ois.readObject();
+ } finally {
+ ois.close();
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ 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());
+ }
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ if (oos != null) {
+ try {
+ oos.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ public void test_1_Constructor() {
+ // Test for method java.io.ObjectOutputStream(java.io.OutputStream)
+
+ try {
+ oos.close();
+ oos = new ObjectOutputStream(new ByteArrayOutputStream());
+ oos.close();
+ } catch (Exception e) {
+ fail("Failed to create ObjectOutputStream : " + e.getMessage());
+ }
+ }
+
+ public void test_2_close() {
+ // Test for method void java.io.ObjectOutputStream.close()
+ try {
+ oos.close();
+ oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
+ oos.close();
+ oos.writeChar('T');
+ oos.writeObject(FOO);
+ // Writing to a closed stream does not cause problems. This is
+ // the expected behavior
+ } catch (IOException e) {
+ fail("Operation on closed stream threw IOException : "
+ + e.getMessage());
+ }
+ }
+
+ public void test_3_defaultWriteObject() {
+ // Test for method void java.io.ObjectOutputStream.defaultWriteObject()
+
+ try {
+ oos.defaultWriteObject();
+ } catch (NotActiveException e) {
+ // Correct
+ return;
+ } catch (IOException e) {
+ }
+ fail(
+ "Failed to throw NotActiveException when invoked outside readObject");
+ }
+
+ public void test_4_flush() {
+ // Test for method void java.io.ObjectOutputStream.flush()
+ try {
+ oos.close();
+ oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
+ int size = bao.size();
+ oos.writeByte(127);
+ assertTrue("Data flushed already", bao.size() == size);
+ oos.flush();
+ assertTrue("Failed to flush data", bao.size() > size);
+ // we don't know how many bytes are actually written for 1 byte,
+ // so we test > <before>
+ oos.close();
+ oos = null;
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_5_reset() {
+ // Test for method void java.io.ObjectOutputStream.reset()
+ try {
+ String o = "HelloWorld";
+ oos.writeObject(o);
+ oos.writeObject(o);
+ oos.reset();
+ oos.writeObject(o);
+ ois = new ObjectInputStream(loadStream());
+ ois.close();
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_6_write() {
+ // Test for method void java.io.ObjectOutputStream.write(byte [], int,
+ // int)
+ try {
+ byte[] buf = new byte[255];
+ byte[] output = new byte[255];
+ for (int i = 0; i < output.length; i++)
+ output[i] = (byte) i;
+ oos.write(output, 0, output.length);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ ois.readFully(buf);
+ ois.close();
+ for (int i = 0; i < output.length; i++)
+ if (buf[i] != output[i])
+ fail("Read incorrect byte: " + i);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_6a_write() {
+ // Test for method void java.io.ObjectOutputStream.write(byte [], int,
+ // int)
+ try {
+ byte[] buf = new byte[256];
+ byte[] output = new byte[256];
+ for (int i = 0; i < output.length; i++)
+ output[i] = (byte) (i & 0xff);
+ oos.write(output, 0, output.length);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ ois.readFully(buf);
+ ois.close();
+ for (int i = 0; i < output.length; i++)
+ if (buf[i] != output[i])
+ fail("Read incorrect byte: " + i);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_7_write() {
+ // Test for method void java.io.ObjectOutputStream.write(int)
+ try {
+ byte[] buf = new byte[10];
+ oos.write('T');
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Read incorrect byte", ois.read() == 'T');
+ ois.close();
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_8_write() {
+ // Test for method void java.io.ObjectOutputStream.write(byte [])
+ try {
+ byte[] buf = new byte[10];
+ oos.write("HelloWorld".getBytes());
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ ois.read(buf, 0, 10);
+ ois.close();
+ assertTrue("Read incorrect bytes", new String(buf, 0, 10)
+ .equals("HelloWorld"));
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_9_writeBoolean() {
+ // Test for method void java.io.ObjectOutputStream.writeBoolean(boolean)
+ try {
+ oos.writeBoolean(true);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect byte value", ois.readBoolean());
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_10_writeByte() {
+ // Test for method void java.io.ObjectOutputStream.writeByte(int)
+ try {
+ oos.writeByte(127);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect byte value", ois.readByte() == 127);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_11_writeBytes() {
+ // Test for method void
+ // java.io.ObjectOutputStream.writeBytes(java.lang.String)
+ try {
+ byte[] buf = new byte[10];
+ oos.writeBytes("HelloWorld");
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ ois.readFully(buf);
+ ois.close();
+ assertTrue("Wrote incorrect bytes value", new String(buf, 0, 10)
+ .equals("HelloWorld"));
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_12_writeChar() {
+ // Test for method void java.io.ObjectOutputStream.writeChar(int)
+ try {
+ oos.writeChar('T');
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect char value", ois.readChar() == 'T');
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_13_writeChars() {
+ // Test for method void
+ // java.io.ObjectOutputStream.writeChars(java.lang.String)
+ try {
+ int avail = 0;
+ char[] buf = new char[10];
+ oos.writeChars("HelloWorld");
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ // Number of prim data bytes in stream / 2 to give char index
+ avail = ois.available() / 2;
+ for (int i = 0; i < avail; ++i)
+ buf[i] = ois.readChar();
+ ois.close();
+ assertTrue("Wrote incorrect chars", new String(buf, 0, 10)
+ .equals("HelloWorld"));
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_14_writeDouble() {
+ // Test for method void java.io.ObjectOutputStream.writeDouble(double)
+ try {
+ oos.writeDouble(Double.MAX_VALUE);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect double value",
+ ois.readDouble() == Double.MAX_VALUE);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_15_writeFloat() {
+ // Test for method void java.io.ObjectOutputStream.writeFloat(float)
+ try {
+ oos.writeFloat(Float.MAX_VALUE);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect double value",
+ ois.readFloat() == Float.MAX_VALUE);
+ ois.close();
+ ois = null;
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_16_writeInt() {
+ // Test for method void java.io.ObjectOutputStream.writeInt(int)
+ try {
+ oos.writeInt(Integer.MAX_VALUE);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect double value",
+ ois.readInt() == Integer.MAX_VALUE);
+ ois.close();
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_17_writeLong() {
+ // Test for method void java.io.ObjectOutputStream.writeLong(long)
+ try {
+ oos.writeLong(Long.MAX_VALUE);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect double value",
+ ois.readLong() == Long.MAX_VALUE);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_19_writeShort() {
+ // Test for method void java.io.ObjectOutputStream.writeShort(int)
+ try {
+ oos.writeShort(127);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect short value", ois.readShort() == 127);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_20_writeUTF() {
+ // Test for method void
+ // java.io.ObjectOutputStream.writeUTF(java.lang.String)
+ try {
+ oos.writeUTF("HelloWorld");
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ assertTrue("Wrote incorrect UTF value", ois.readUTF().equals(
+ "HelloWorld"));
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_25_available() {
+ try {
+ oos.writeObject(FOO);
+ oos.writeObject(FOO);
+ oos.flush();
+ int available1 = 0;
+ int available2 = 0;
+ Object obj1 = null;
+ Object obj2 = null;
+ ObjectInputStream ois = new ObjectInputStream(loadStream());
+ available1 = ois.available();
+ obj1 = ois.readObject();
+ available2 = ois.available();
+ obj2 = ois.readObject();
+
+ assertTrue("available returned incorrect value", available1 == 0);
+ assertTrue("available returned incorrect value", available2 == 0);
+
+ assertTrue("available caused incorrect reading", FOO.equals(obj1));
+ assertTrue("available returned incorrect value", FOO.equals(obj2));
+
+ } catch (IOException e) {
+ fail("IOException serializing object : " + e.getMessage());
+ } catch (ClassNotFoundException e) {
+ fail("Unable to read Object type : " + e.toString());
+ } catch (Error err) {
+ System.out.println("Error " + err);
+ throw err;
+ }
+
+ }
+
+ protected void t_MixPrimitivesAndObjects() throws IOException,
+ ClassNotFoundException {
+ 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();
+ }
+ }
+
+ public void test_resolveClass() {
+ try {
+ oos.writeObject(new Object[] { Integer.class, new Integer(1) });
+ oos.close();
+
+ ois = new ObjectInputStreamSubclass(loadStream());
+ ois.readObject();
+ ois.close();
+ } catch (IOException e1) {
+ fail("IOException : " + e1.getMessage());
+ } catch (ClassNotFoundException e2) {
+ fail("ClassNotFoundException : " + e2.getMessage());
+ }
+
+ Class[] resolvedClasses = ((ObjectInputStreamSubclass) ois)
+ .resolvedClasses();
+ assertTrue("missing resolved", resolvedClasses.length == 3);
+ assertTrue("resolved class 1", resolvedClasses[0] == Object[].class);
+ assertTrue("resolved class 2", resolvedClasses[1] == Integer.class);
+ assertTrue("resolved class 3", resolvedClasses[2] == Number.class);
+ }
+
+ public void test_reset() {
+ try {
+ oos.reset();
+ oos.writeObject("R");
+ oos.reset();
+ oos.writeByte(24);
+ oos.close();
+
+ DataInputStream dis = new DataInputStream(loadStream());
+ byte[] input = new byte[dis.available()];
+ dis.readFully(input);
+ byte[] result = new byte[] { (byte) 0xac, (byte) 0xed, (byte) 0,
+ (byte) 5, (byte) 0x79, (byte) 0x74, (byte) 0, (byte) 1,
+ (byte) 'R', (byte) 0x79, (byte) 0x77, (byte) 1, (byte) 24 };
+ assertTrue("incorrect output", Arrays.equals(input, result));
+
+ ois = new ObjectInputStreamSubclass(loadStream());
+ assertTrue("Wrong result from readObject()", ois.readObject()
+ .equals("R"));
+ assertTrue("Wrong result from readByte()", ois.readByte() == 24);
+ ois.close();
+ } catch (IOException e1) {
+ fail("IOException : " + e1.getMessage());
+ } catch (ClassNotFoundException e2) {
+ fail("ClassNotFoundException : " + e2.getMessage());
+ }
+ }
+
+ public void test_serialVersionUID(Class clazz, long svUID) {
+ final String idWrong = "serialVersionUID is wrong for: ";
+ long reflectedSvUID = 0L;
+ try {
+ reflectedSvUID = clazz.getField("serialVersionUID").getLong(null);
+ } catch (Exception e) {
+ fail("Unable to determine serialVersionUID of " + clazz);
+ }
+ assertTrue(idWrong + clazz + ": " + reflectedSvUID + " does not equal "
+ + svUID, reflectedSvUID == svUID);
+ }
+
+ private static class ResolveObjectTest implements Serializable {
+ Object field1, field2;
+ }
+
+ private static class ResolveObjectInputStream extends ObjectInputStream {
+ ResolveObjectInputStream(InputStream in)
+ throws StreamCorruptedException, IOException {
+ super(in);
+ }
+
+ public void enableResolve() {
+ enableResolveObject(true);
+ }
+
+ public Object resolveObject(Object obj) {
+ if (obj instanceof Vector) // test_1_resolveObject()
+ return new Hashtable();
+ else if ("abc".equals(obj)) // test_2_resolveObject()
+ return "ABC";
+ else if (obj instanceof String) // test_3_resolveObject()
+ return String.valueOf(((String) obj).length());
+ else if (obj instanceof int[]) // test_4_resolveObject()
+ return new Object[1];
+ else if (obj instanceof Object[] && ((Object[]) obj).length == 2) // test_5_resolveObject()
+ return new char[1];
+ return obj;
+ }
+ }
+
+ public void test_1_resolveObject() {
+ try {
+ ResolveObjectTest obj = new ResolveObjectTest();
+ obj.field1 = new Vector();
+ obj.field2 = obj.field1;
+ oos.writeObject(obj);
+ oos.close();
+ ois = new ResolveObjectInputStream(loadStream());
+ ((ResolveObjectInputStream) ois).enableResolve();
+ ResolveObjectTest result = null;
+ try {
+ result = (ResolveObjectTest) ois.readObject();
+ } catch (ClassNotFoundException e) {
+ fail(e.toString());
+ }
+ assertTrue("Object not resolved",
+ result.field1 instanceof Hashtable);
+ assertTrue("Second reference not resolved",
+ result.field1 == result.field2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_2_resolveObject() {
+ try {
+ ResolveObjectTest obj = new ResolveObjectTest();
+ obj.field1 = "abc";
+ obj.field2 = obj.field1;
+ oos.writeObject(obj);
+ oos.close();
+ ois = new ResolveObjectInputStream(loadStream());
+ ((ResolveObjectInputStream) ois).enableResolve();
+ ResolveObjectTest result = null;
+ try {
+ result = (ResolveObjectTest) ois.readObject();
+ } catch (ClassNotFoundException e) {
+ fail(e.toString());
+ }
+ assertTrue("String not resolved", "ABC".equals(result.field1));
+ assertTrue("Second reference not resolved",
+ result.field1 == result.field2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_3_resolveObject() {
+ try {
+ ResolveObjectTest obj = new ResolveObjectTest();
+ char[] lchars = new char[70000];
+ obj.field1 = new String(lchars);
+ obj.field2 = obj.field1;
+ oos.writeObject(obj);
+ oos.close();
+ ois = new ResolveObjectInputStream(loadStream());
+ ((ResolveObjectInputStream) ois).enableResolve();
+ ResolveObjectTest result = null;
+ try {
+ result = (ResolveObjectTest) ois.readObject();
+ } catch (ClassNotFoundException e) {
+ fail(e.toString());
+ }
+ assertTrue("Long String not resolved", "70000"
+ .equals(result.field1));
+ assertTrue("Second reference not resolved",
+ result.field1 == result.field2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_4_resolveObject() {
+ try {
+ ResolveObjectTest obj = new ResolveObjectTest();
+ obj.field1 = new int[5];
+ obj.field2 = obj.field1;
+ oos.writeObject(obj);
+ oos.close();
+ ois = new ResolveObjectInputStream(loadStream());
+ ((ResolveObjectInputStream) ois).enableResolve();
+ ResolveObjectTest result = null;
+ try {
+ result = (ResolveObjectTest) ois.readObject();
+ } catch (ClassNotFoundException e) {
+ fail(e.toString());
+ }
+ Class cl = new Object[0].getClass();
+ assertTrue("int[] not resolved", result.field1.getClass() == cl);
+ assertTrue("Second reference not resolved",
+ result.field1 == result.field2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_5_resolveObject() {
+ try {
+ ResolveObjectTest obj = new ResolveObjectTest();
+ obj.field1 = new Object[2];
+ obj.field2 = obj.field1;
+ oos.writeObject(obj);
+ oos.close();
+ ois = new ResolveObjectInputStream(loadStream());
+ ((ResolveObjectInputStream) ois).enableResolve();
+ ResolveObjectTest result = null;
+ try {
+ result = (ResolveObjectTest) ois.readObject();
+ } catch (ClassNotFoundException e) {
+ fail(e.toString());
+ }
+ Class cl = new char[0].getClass();
+ assertTrue("int[] not resolved", result.field1.getClass() == cl);
+ assertTrue("Second reference not resolved",
+ result.field1 == result.field2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ }
+ }
+
+ static class WriteReplaceTestA implements Serializable {
+ public Object writeReplace() throws ObjectStreamException {
+ return new ReadResolveTestB();
+ }
+ }
+
+ static class WriteReplaceTestB extends WriteReplaceTestA {
+ }
+
+ static class WriteReplaceTestC extends WriteReplaceTestA {
+ public Object writeReplace() throws ObjectStreamException {
+ return new ReadResolveTestC();
+ }
+ }
+
+ static class WriteReplaceTestD implements Serializable {
+ private Object writeReplace() throws ObjectStreamException {
+ return new ReadResolveTestD();
+ }
+ }
+
+ static class WriteReplaceTestE extends WriteReplaceTestD {
+ }
+
+ static class WriteReplaceTestF implements Serializable {
+ int type, readType;
+
+ public WriteReplaceTestF(int type, int readType) {
+ this.type = type;
+ this.readType = readType;
+ }
+
+ public Object writeReplace() throws ObjectStreamException {
+ switch (type) {
+ case 0:
+ throw new InvalidObjectException("invalid");
+ case 1:
+ throw new RuntimeException("runtime");
+ case 2:
+ throw new Error("error");
+ default:
+ return new ReadResolveTestE(readType);
+ }
+ }
+ }
+
+ static class ReadResolveTestA implements Serializable {
+ public Object readResolve() throws ObjectStreamException {
+ return new ReadResolveTestA();
+ }
+ }
+
+ static class ReadResolveTestB extends ReadResolveTestA {
+ }
+
+ static class ReadResolveTestC implements Serializable {
+ private Object readResolve() throws ObjectStreamException {
+ return new ReadResolveTestB();
+ }
+ }
+
+ static class ReadResolveTestD extends ReadResolveTestC {
+ }
+
+ static class ReadResolveTestE implements Serializable {
+ int type;
+
+ public ReadResolveTestE(int type) {
+ this.type = type;
+ }
+
+ public Object readResolve() throws ObjectStreamException {
+ switch (type) {
+ case 0:
+ throw new InvalidObjectException("invalid");
+ case 1:
+ throw new RuntimeException("runtime");
+ case 2:
+ throw new Error("error");
+ case 3:
+ return this;
+ default:
+ return new ReadResolveTestF();
+ }
+ }
+ }
+
+ static class ReadResolveTestF implements Serializable {
+ }
+
+ public void test_1_writeReplace() {
+ try {
+ Vector v = new Vector();
+ v.addElement(new WriteReplaceTestA());
+ v.addElement(new WriteReplaceTestB());
+ v.addElement(new WriteReplaceTestB());
+ v.addElement(new WriteReplaceTestC());
+ v.addElement(new WriteReplaceTestD());
+ v.addElement(new WriteReplaceTestE());
+ oos.writeObject(v);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ Vector result = (Vector) ois.readObject();
+ assertTrue("invalid 0 : " + result.elementAt(0), result
+ .elementAt(0).getClass() == ReadResolveTestA.class);
+ assertTrue("invalid 1 : " + result.elementAt(1), result
+ .elementAt(1).getClass() == ReadResolveTestA.class);
+ assertTrue("invalid 2 : " + result.elementAt(2), result
+ .elementAt(2).getClass() == ReadResolveTestA.class);
+ assertTrue("invalid 3 : " + result.elementAt(3), result
+ .elementAt(3).getClass() == ReadResolveTestB.class);
+ assertTrue("invalid 4 : " + result.elementAt(4), result
+ .elementAt(4).getClass() == ReadResolveTestD.class);
+ assertTrue("invalid 5 : " + result.elementAt(5), result
+ .elementAt(5).getClass() == WriteReplaceTestE.class);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ } catch (ClassNotFoundException e) {
+ fail("ClassNotFoundException serializing data : " + e.getMessage());
+ }
+ }
+
+ public void test_2_writeReplace() {
+ try {
+ boolean exception = false;
+ try {
+ oos.writeObject(new WriteReplaceTestF(0, -1));
+ } catch (ObjectStreamException e) {
+ exception = true;
+ }
+ assertTrue("Should throw ObjectStreamException", exception);
+ exception = false;
+ try {
+ oos.writeObject(new WriteReplaceTestF(1, -1));
+ } catch (RuntimeException e) {
+ exception = true;
+ }
+ assertTrue("Should throw RuntimeException", exception);
+ exception = false;
+ try {
+ oos.writeObject(new WriteReplaceTestF(2, -1));
+ } catch (Error e) {
+ exception = true;
+ }
+ assertTrue("Should throw Error", exception);
+
+ oos.writeObject(new WriteReplaceTestF(3, 0));
+ oos.writeObject(new WriteReplaceTestF(3, 1));
+ oos.writeObject(new WriteReplaceTestF(3, 2));
+ WriteReplaceTestF test = new WriteReplaceTestF(3, 3);
+ oos.writeObject(test);
+ oos.writeObject(test);
+ WriteReplaceTestF test2 = new WriteReplaceTestF(3, 4);
+ oos.writeObject(test2);
+ oos.writeObject(test2);
+ oos.close();
+ ois = new ObjectInputStream(loadStream());
+ try {
+ ois.readObject();
+ } catch (WriteAbortedException e) {
+ }
+
+ exception = false;
+ try {
+ ois.readObject();
+ } catch (ObjectStreamException e) {
+ exception = true;
+ }
+ assertTrue("Expected ObjectStreamException", exception);
+ exception = false;
+ try {
+ ois.readObject();
+ } catch (RuntimeException e) {
+ exception = true;
+ }
+ assertTrue("Expected RuntimeException", exception);
+ exception = false;
+ try {
+ ois.readObject();
+ } catch (Error e) {
+ exception = true;
+ }
+ assertTrue("Expected Error", exception);
+
+ Object readE1 = ois.readObject();
+ Object readE2 = ois.readObject();
+ assertTrue("Replaced objects should be identical", readE1 == readE2);
+ Object readF1 = ois.readObject();
+ Object readF2 = ois.readObject();
+ assertTrue("Replaced resolved objects should be identical: "
+ + readF1 + " " + readF2, readF1 == readF2);
+ } catch (IOException e) {
+ fail("IOException serializing data : " + e.getMessage());
+ } catch (ClassNotFoundException e) {
+ fail("ClassNotFoundException serializing data : " + e.getMessage());
+ }
+ }
+}