You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by qi...@apache.org on 2008/08/22 09:15:27 UTC
svn commit: r687988 [5/11] - in /harmony/enhanced/classlib/branches/java6:
./ depends/build/ depends/build/platform/ depends/jars/
depends/jars/icu4jni_3.4/ depends/manifests/bcel-5.2/
depends/manifests/bcel-5.2/META-INF/ make/ modules/accessibility/ m...
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/BufferedInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/BufferedInputStreamTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/BufferedInputStreamTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/BufferedInputStreamTest.java Fri Aug 22 00:15:00 2008
@@ -1,443 +1,443 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.harmony.luni.tests.java.io;
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import junit.framework.TestCase;
-import tests.support.Support_PlatformFile;
-
-public class BufferedInputStreamTest extends TestCase {
-
- public String fileName;
-
- private BufferedInputStream is;
-
- private FileInputStream isFile;
-
- byte[] ibuf = new byte[4096];
-
- public String fileString = "Test_All_Tests\nTest_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_java_io_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_Cl
assNotFoundException\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_
Object\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_Sock
etException\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.BufferedInputStream(InputStream)
- */
- public void test_ConstructorLjava_io_InputStream() {
- try {
- BufferedInputStream str = new BufferedInputStream(null);
- str.read();
- fail("Expected an IOException");
- } catch (IOException e) {
- // Expected
- }
- }
-
- /*
- * @tests java.io.BufferedInputStream(InputStream)
- */
- public void test_ConstructorLjava_io_InputStreamI() throws IOException {
- try {
- BufferedInputStream str = new BufferedInputStream(null, 1);
- str.read();
- fail("Expected an IOException");
- } catch (IOException e) {
- // Expected
- }
-
- // Test for method java.io.BufferedInputStream(java.io.InputStream, int)
-
- // Create buffer with exact size of file
- is = new BufferedInputStream(isFile, this.fileString.length());
- // Ensure buffer gets filled by evaluating one read
- is.read();
- // Close underlying FileInputStream, all but 1 buffered bytes should
- // still be available.
- isFile.close();
- // Read the remaining buffered characters, no IOException should
- // occur.
- is.skip(this.fileString.length() - 2);
- is.read();
- try {
- // is.read should now throw an exception because it will have to
- // be filled.
- is.read();
- fail("Exception should have been triggered by read()");
- } catch (IOException e) {
- // Expected
- }
-
- // regression test for harmony-2407
- new MockBufferedInputStream(null);
- assertNotNull(MockBufferedInputStream.buf);
- MockBufferedInputStream.buf = null;
- new MockBufferedInputStream(null, 100);
- assertNotNull(MockBufferedInputStream.buf);
- }
-
- static class MockBufferedInputStream extends BufferedInputStream {
- static byte[] buf;
-
- MockBufferedInputStream(InputStream is) throws IOException {
- super(is);
- buf = super.buf;
- }
-
- MockBufferedInputStream(InputStream is, int size) throws IOException {
- super(is, size);
- buf = super.buf;
- }
- }
-
- /**
- * @tests java.io.BufferedInputStream#available()
- */
- public void test_available() throws IOException {
- assertTrue("Returned incorrect number of available bytes", is
- .available() == fileString.length());
-
- // Test that a closed stream throws an IOE for available()
- BufferedInputStream bis = new BufferedInputStream(
- new ByteArrayInputStream(new byte[] { 'h', 'e', 'l', 'l', 'o',
- ' ', 't', 'i', 'm' }));
- int available = bis.available();
- bis.close();
- assertTrue(available != 0);
-
- try {
- bis.available();
- fail("Expected test to throw IOE.");
- } catch (IOException ex) {
- // expected
- }
- }
-
- /**
- * @tests java.io.BufferedInputStream#close()
- */
- public void test_close() throws IOException {
- new BufferedInputStream(isFile).close();
-
- // regression for HARMONY-667
- BufferedInputStream buf = new BufferedInputStream(null, 5);
- buf.close();
- }
-
- /**
- * @tests java.io.BufferedInputStream#mark(int)
- */
- public void test_markI() throws IOException {
- byte[] buf1 = new byte[100];
- byte[] buf2 = new byte[100];
- is.skip(3000);
- is.mark(1000);
- is.read(buf1, 0, buf1.length);
- is.reset();
- is.read(buf2, 0, buf2.length);
- is.reset();
- assertTrue("Failed to mark correct position", new String(buf1, 0,
- buf1.length).equals(new String(buf2, 0, buf2.length)));
-
- byte[] bytes = new byte[256];
- for (int i = 0; i < 256; i++) {
- bytes[i] = (byte) i;
- }
- InputStream in = new BufferedInputStream(
- new ByteArrayInputStream(bytes), 12);
- in.skip(6);
- in.mark(14);
- in.read(new byte[14], 0, 14);
- in.reset();
- assertTrue("Wrong bytes", in.read() == 6 && in.read() == 7);
-
- in = new BufferedInputStream(new ByteArrayInputStream(bytes), 12);
- in.skip(6);
- in.mark(8);
- in.skip(7);
- in.reset();
- assertTrue("Wrong bytes 2", in.read() == 6 && in.read() == 7);
-
- BufferedInputStream buf = new BufferedInputStream(
- new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2);
- buf.mark(3);
- bytes = new byte[3];
- int result = buf.read(bytes);
- assertEquals(3, result);
- assertEquals("Assert 0:", 0, bytes[0]);
- assertEquals("Assert 1:", 1, bytes[1]);
- assertEquals("Assert 2:", 2, bytes[2]);
- assertEquals("Assert 3:", 3, buf.read());
-
- buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0,
- 1, 2, 3, 4 }), 2);
- buf.mark(3);
- bytes = new byte[4];
- result = buf.read(bytes);
- assertEquals(4, result);
- assertEquals("Assert 4:", 0, bytes[0]);
- assertEquals("Assert 5:", 1, bytes[1]);
- assertEquals("Assert 6:", 2, bytes[2]);
- assertEquals("Assert 7:", 3, bytes[3]);
- assertEquals("Assert 8:", 4, buf.read());
- assertEquals("Assert 9:", -1, buf.read());
-
- buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0,
- 1, 2, 3, 4 }), 2);
- buf.mark(Integer.MAX_VALUE);
- buf.read();
- buf.close();
- }
-
- /**
- * @tests java.io.BufferedInputStream#markSupported()
- */
- public void test_markSupported() {
- assertTrue("markSupported returned incorrect value", is.markSupported());
- }
-
- /**
- * @tests java.io.BufferedInputStream#read()
- */
- public void test_read() throws IOException {
- int c = is.read();
- assertTrue("read returned incorrect char", c == fileString.charAt(0));
-
- byte[] bytes = new byte[256];
- for (int i = 0; i < 256; i++) {
- bytes[i] = (byte) i;
- }
- InputStream in = new BufferedInputStream(
- new ByteArrayInputStream(bytes), 12);
- assertEquals("Wrong initial byte", 0, in.read()); // Fill the
- // buffer
- byte[] buf = new byte[14];
- in.read(buf, 0, 14); // Read greater than the buffer
- assertTrue("Wrong block read data", new String(buf, 0, 14)
- .equals(new String(bytes, 1, 14)));
- assertEquals("Wrong bytes", 15, in.read()); // Check next byte
- }
-
- /**
- * @tests java.io.BufferedInputStream#read(byte[], int, int)
- */
- public void test_read$BII_Exception() throws IOException {
- BufferedInputStream bis = new BufferedInputStream(null);
- try {
- bis.read(null, -1, -1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
-
- try {
- bis.read(new byte[0], -1, -1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- bis.read(new byte[0], 1, -1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- bis.read(new byte[0], 1, 1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // expected
- }
-
- bis.close();
-
- try {
- bis.read(null, -1, -1);
- fail("should throw IOException");
- } catch (IOException e) {
- // expected
- }
- }
-
- /**
- * @tests java.io.BufferedInputStream#read(byte[], int, int)
- */
- public void test_read$BII() throws IOException {
- byte[] buf1 = new byte[100];
- is.skip(3000);
- is.mark(1000);
- is.read(buf1, 0, buf1.length);
- assertTrue("Failed to read correct data", new String(buf1, 0,
- buf1.length).equals(fileString.substring(3000, 3100)));
-
- BufferedInputStream bufin = new BufferedInputStream(new InputStream() {
- int size = 2, pos = 0;
-
- byte[] contents = new byte[size];
-
- @Override
- public int read() throws IOException {
- if (pos >= size) {
- throw new IOException("Read past end of data");
- }
- return contents[pos++];
- }
-
- @Override
- public int read(byte[] buf, int off, int len) throws IOException {
- if (pos >= size) {
- throw new IOException("Read past end of data");
- }
- int toRead = len;
- if (toRead > available()) {
- toRead = available();
- }
- System.arraycopy(contents, pos, buf, off, toRead);
- pos += toRead;
- return toRead;
- }
-
- @Override
- public int available() {
- return size - pos;
- }
- });
- bufin.read();
- int result = bufin.read(new byte[2], 0, 2);
- assertTrue("Incorrect result: " + result, result == 1);
- }
-
- /**
- * @tests java.io.BufferedInputStream#reset()
- */
- public void test_reset() throws IOException {
- byte[] buf1 = new byte[10];
- byte[] buf2 = new byte[10];
- is.mark(2000);
- is.read(buf1, 0, 10);
- is.reset();
- is.read(buf2, 0, 10);
- is.reset();
- assertTrue("Reset failed", new String(buf1, 0, buf1.length)
- .equals(new String(buf2, 0, buf2.length)));
-
- BufferedInputStream bIn = new BufferedInputStream(
- new ByteArrayInputStream("1234567890".getBytes()));
- bIn.mark(10);
- for (int i = 0; i < 11; i++) {
- bIn.read();
- }
- bIn.reset();
- }
-
- /**
- * @tests java.io.BufferedInputStream#reset()
- */
- public void test_reset_Exception() throws IOException {
- BufferedInputStream bis = new BufferedInputStream(null);
-
- // throws IOException with message "Mark has been invalidated"
- try {
- bis.reset();
- fail("should throw IOException");
- } catch (IOException e) {
- // expected
- }
-
- // does not throw IOException
- bis.mark(1);
- bis.reset();
-
- bis.close();
-
- // throws IOException with message "stream is closed"
- try {
- bis.reset();
- fail("should throw IOException");
- } catch (IOException e) {
- // expected
- }
- }
-
- /**
- * @tests java.io.BufferedInputStream#skip(long)
- */
- public void test_skipJ() throws IOException {
- byte[] buf1 = new byte[10];
- is.mark(2000);
- is.skip(1000);
- is.read(buf1, 0, buf1.length);
- is.reset();
- assertTrue("Failed to skip to correct position", new String(buf1, 0,
- buf1.length).equals(fileString.substring(1000, 1010)));
-
- // regression for HARMONY-667
- try {
- BufferedInputStream buf = new BufferedInputStream(null, 5);
- buf.skip(10);
- fail("Should throw IOException");
- } catch (IOException e) {
- // Expected
- }
- }
-
- /**
- * Sets up the fixture, for example, open a network connection. This method
- * is called before a test is executed.
- */
- @Override
- protected void setUp() throws IOException {
- fileName = System.getProperty("user.dir");
- String separator = System.getProperty("file.separator");
- if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) {
- fileName = Support_PlatformFile.getNewPlatformFile(fileName,
- "input.tst");
- } else {
- fileName = Support_PlatformFile.getNewPlatformFile(fileName
- + separator, "input.tst");
- }
- OutputStream fos = new FileOutputStream(fileName);
- fos.write(fileString.getBytes());
- fos.close();
- isFile = new FileInputStream(fileName);
- is = new BufferedInputStream(isFile);
- }
-
- /**
- * Tears down the fixture, for example, close a network connection. This
- * method is called after a test is executed.
- */
- @Override
- protected void tearDown() {
- try {
- is.close();
- } catch (Exception e) {
- }
- try {
- File f = new File(fileName);
- f.delete();
- } catch (Exception e) {
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.harmony.luni.tests.java.io;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import junit.framework.TestCase;
+import tests.support.Support_PlatformFile;
+
+public class BufferedInputStreamTest extends TestCase {
+
+ public String fileName;
+
+ private BufferedInputStream is;
+
+ private FileInputStream isFile;
+
+ byte[] ibuf = new byte[4096];
+
+ public String fileString = "Test_All_Tests\nTest_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_java_io_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_Cl
assNotFoundException\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_
Object\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_Sock
etException\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.BufferedInputStream(InputStream)
+ */
+ public void test_ConstructorLjava_io_InputStream() {
+ try {
+ BufferedInputStream str = new BufferedInputStream(null);
+ str.read();
+ fail("Expected an IOException");
+ } catch (IOException e) {
+ // Expected
+ }
+ }
+
+ /*
+ * @tests java.io.BufferedInputStream(InputStream)
+ */
+ public void test_ConstructorLjava_io_InputStreamI() throws IOException {
+ try {
+ BufferedInputStream str = new BufferedInputStream(null, 1);
+ str.read();
+ fail("Expected an IOException");
+ } catch (IOException e) {
+ // Expected
+ }
+
+ // Test for method java.io.BufferedInputStream(java.io.InputStream, int)
+
+ // Create buffer with exact size of file
+ is = new BufferedInputStream(isFile, this.fileString.length());
+ // Ensure buffer gets filled by evaluating one read
+ is.read();
+ // Close underlying FileInputStream, all but 1 buffered bytes should
+ // still be available.
+ isFile.close();
+ // Read the remaining buffered characters, no IOException should
+ // occur.
+ is.skip(this.fileString.length() - 2);
+ is.read();
+ try {
+ // is.read should now throw an exception because it will have to
+ // be filled.
+ is.read();
+ fail("Exception should have been triggered by read()");
+ } catch (IOException e) {
+ // Expected
+ }
+
+ // regression test for harmony-2407
+ new MockBufferedInputStream(null);
+ assertNotNull(MockBufferedInputStream.buf);
+ MockBufferedInputStream.buf = null;
+ new MockBufferedInputStream(null, 100);
+ assertNotNull(MockBufferedInputStream.buf);
+ }
+
+ static class MockBufferedInputStream extends BufferedInputStream {
+ static byte[] buf;
+
+ MockBufferedInputStream(InputStream is) throws IOException {
+ super(is);
+ buf = super.buf;
+ }
+
+ MockBufferedInputStream(InputStream is, int size) throws IOException {
+ super(is, size);
+ buf = super.buf;
+ }
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#available()
+ */
+ public void test_available() throws IOException {
+ assertTrue("Returned incorrect number of available bytes", is
+ .available() == fileString.length());
+
+ // Test that a closed stream throws an IOE for available()
+ BufferedInputStream bis = new BufferedInputStream(
+ new ByteArrayInputStream(new byte[] { 'h', 'e', 'l', 'l', 'o',
+ ' ', 't', 'i', 'm' }));
+ int available = bis.available();
+ bis.close();
+ assertTrue(available != 0);
+
+ try {
+ bis.available();
+ fail("Expected test to throw IOE.");
+ } catch (IOException ex) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#close()
+ */
+ public void test_close() throws IOException {
+ new BufferedInputStream(isFile).close();
+
+ // regression for HARMONY-667
+ BufferedInputStream buf = new BufferedInputStream(null, 5);
+ buf.close();
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#mark(int)
+ */
+ public void test_markI() throws IOException {
+ byte[] buf1 = new byte[100];
+ byte[] buf2 = new byte[100];
+ is.skip(3000);
+ is.mark(1000);
+ is.read(buf1, 0, buf1.length);
+ is.reset();
+ is.read(buf2, 0, buf2.length);
+ is.reset();
+ assertTrue("Failed to mark correct position", new String(buf1, 0,
+ buf1.length).equals(new String(buf2, 0, buf2.length)));
+
+ byte[] bytes = new byte[256];
+ for (int i = 0; i < 256; i++) {
+ bytes[i] = (byte) i;
+ }
+ InputStream in = new BufferedInputStream(
+ new ByteArrayInputStream(bytes), 12);
+ in.skip(6);
+ in.mark(14);
+ in.read(new byte[14], 0, 14);
+ in.reset();
+ assertTrue("Wrong bytes", in.read() == 6 && in.read() == 7);
+
+ in = new BufferedInputStream(new ByteArrayInputStream(bytes), 12);
+ in.skip(6);
+ in.mark(8);
+ in.skip(7);
+ in.reset();
+ assertTrue("Wrong bytes 2", in.read() == 6 && in.read() == 7);
+
+ BufferedInputStream buf = new BufferedInputStream(
+ new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2);
+ buf.mark(3);
+ bytes = new byte[3];
+ int result = buf.read(bytes);
+ assertEquals(3, result);
+ assertEquals("Assert 0:", 0, bytes[0]);
+ assertEquals("Assert 1:", 1, bytes[1]);
+ assertEquals("Assert 2:", 2, bytes[2]);
+ assertEquals("Assert 3:", 3, buf.read());
+
+ buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0,
+ 1, 2, 3, 4 }), 2);
+ buf.mark(3);
+ bytes = new byte[4];
+ result = buf.read(bytes);
+ assertEquals(4, result);
+ assertEquals("Assert 4:", 0, bytes[0]);
+ assertEquals("Assert 5:", 1, bytes[1]);
+ assertEquals("Assert 6:", 2, bytes[2]);
+ assertEquals("Assert 7:", 3, bytes[3]);
+ assertEquals("Assert 8:", 4, buf.read());
+ assertEquals("Assert 9:", -1, buf.read());
+
+ buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0,
+ 1, 2, 3, 4 }), 2);
+ buf.mark(Integer.MAX_VALUE);
+ buf.read();
+ buf.close();
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#markSupported()
+ */
+ public void test_markSupported() {
+ assertTrue("markSupported returned incorrect value", is.markSupported());
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#read()
+ */
+ public void test_read() throws IOException {
+ int c = is.read();
+ assertTrue("read returned incorrect char", c == fileString.charAt(0));
+
+ byte[] bytes = new byte[256];
+ for (int i = 0; i < 256; i++) {
+ bytes[i] = (byte) i;
+ }
+ InputStream in = new BufferedInputStream(
+ new ByteArrayInputStream(bytes), 12);
+ assertEquals("Wrong initial byte", 0, in.read()); // Fill the
+ // buffer
+ byte[] buf = new byte[14];
+ in.read(buf, 0, 14); // Read greater than the buffer
+ assertTrue("Wrong block read data", new String(buf, 0, 14)
+ .equals(new String(bytes, 1, 14)));
+ assertEquals("Wrong bytes", 15, in.read()); // Check next byte
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII_Exception() throws IOException {
+ BufferedInputStream bis = new BufferedInputStream(null);
+ try {
+ bis.read(null, -1, -1);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ bis.read(new byte[0], -1, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ bis.read(new byte[0], 1, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ bis.read(new byte[0], 1, 1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ bis.close();
+
+ try {
+ bis.read(null, -1, -1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() throws IOException {
+ byte[] buf1 = new byte[100];
+ is.skip(3000);
+ is.mark(1000);
+ is.read(buf1, 0, buf1.length);
+ assertTrue("Failed to read correct data", new String(buf1, 0,
+ buf1.length).equals(fileString.substring(3000, 3100)));
+
+ BufferedInputStream bufin = new BufferedInputStream(new InputStream() {
+ int size = 2, pos = 0;
+
+ byte[] contents = new byte[size];
+
+ @Override
+ public int read() throws IOException {
+ if (pos >= size) {
+ throw new IOException("Read past end of data");
+ }
+ return contents[pos++];
+ }
+
+ @Override
+ public int read(byte[] buf, int off, int len) throws IOException {
+ if (pos >= size) {
+ throw new IOException("Read past end of data");
+ }
+ int toRead = len;
+ if (toRead > available()) {
+ toRead = available();
+ }
+ System.arraycopy(contents, pos, buf, off, toRead);
+ pos += toRead;
+ return toRead;
+ }
+
+ @Override
+ public int available() {
+ return size - pos;
+ }
+ });
+ bufin.read();
+ int result = bufin.read(new byte[2], 0, 2);
+ assertTrue("Incorrect result: " + result, result == 1);
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#reset()
+ */
+ public void test_reset() throws IOException {
+ byte[] buf1 = new byte[10];
+ byte[] buf2 = new byte[10];
+ is.mark(2000);
+ is.read(buf1, 0, 10);
+ is.reset();
+ is.read(buf2, 0, 10);
+ is.reset();
+ assertTrue("Reset failed", new String(buf1, 0, buf1.length)
+ .equals(new String(buf2, 0, buf2.length)));
+
+ BufferedInputStream bIn = new BufferedInputStream(
+ new ByteArrayInputStream("1234567890".getBytes()));
+ bIn.mark(10);
+ for (int i = 0; i < 11; i++) {
+ bIn.read();
+ }
+ bIn.reset();
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#reset()
+ */
+ public void test_reset_Exception() throws IOException {
+ BufferedInputStream bis = new BufferedInputStream(null);
+
+ // throws IOException with message "Mark has been invalidated"
+ try {
+ bis.reset();
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+
+ // does not throw IOException
+ bis.mark(1);
+ bis.reset();
+
+ bis.close();
+
+ // throws IOException with message "stream is closed"
+ try {
+ bis.reset();
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.io.BufferedInputStream#skip(long)
+ */
+ public void test_skipJ() throws IOException {
+ byte[] buf1 = new byte[10];
+ is.mark(2000);
+ is.skip(1000);
+ is.read(buf1, 0, buf1.length);
+ is.reset();
+ assertTrue("Failed to skip to correct position", new String(buf1, 0,
+ buf1.length).equals(fileString.substring(1000, 1010)));
+
+ // regression for HARMONY-667
+ try {
+ BufferedInputStream buf = new BufferedInputStream(null, 5);
+ buf.skip(10);
+ fail("Should throw IOException");
+ } catch (IOException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ @Override
+ protected void setUp() throws IOException {
+ fileName = System.getProperty("user.dir");
+ String separator = System.getProperty("file.separator");
+ if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) {
+ fileName = Support_PlatformFile.getNewPlatformFile(fileName,
+ "input.tst");
+ } else {
+ fileName = Support_PlatformFile.getNewPlatformFile(fileName
+ + separator, "input.tst");
+ }
+ OutputStream fos = new FileOutputStream(fileName);
+ fos.write(fileString.getBytes());
+ fos.close();
+ isFile = new FileInputStream(fileName);
+ is = new BufferedInputStream(isFile);
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ @Override
+ protected void tearDown() {
+ try {
+ is.close();
+ } catch (Exception e) {
+ }
+ try {
+ File f = new File(fileName);
+ f.delete();
+ } catch (Exception e) {
+ }
+ }
+}
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/ProcessBuilderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/ProcessBuilderTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/ProcessBuilderTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/ProcessBuilderTest.java Fri Aug 22 00:15:00 2008
@@ -154,6 +154,16 @@
Process process = pb.start();
InputStream in = process.getInputStream();
InputStream err = process.getErrorStream();
+
+ while (true) {
+ try {
+ process.waitFor();
+ break;
+ } catch (InterruptedException e) {
+ // Ignored
+ }
+ }
+
byte[] buf = new byte[1024];
if (in.available() > 0) {
assertTrue(in.read(buf) > 0);
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/MulticastSocketTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/MulticastSocketTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/MulticastSocketTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/MulticastSocketTest.java Fri Aug 22 00:15:00 2008
@@ -142,13 +142,12 @@
/**
* @tests java.net.MulticastSocket#MulticastSocket(int)
*/
- public void test_ConstructorI() {
- // Test for method java.net.MulticastSocket(int)
- // Used in tests
+ public void test_ConstructorI() throws IOException {
+ MulticastSocket orig = new MulticastSocket();
+ int port = orig.getLocalPort();
+ orig.close();
MulticastSocket dup = null;
try {
- mss = new MulticastSocket();
- int port = mss.getLocalPort();
dup = new MulticastSocket(port);
// regression test for Harmony-1162
assertTrue(dup.getReuseAddress());
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/AbstractListTest.java Fri Aug 22 00:15:00 2008
@@ -111,6 +111,13 @@
ListIterator lit2 = list2.listIterator();
lit2.add(new Object());
lit2.next();
+
+ //Regression test for Harmony-5808
+ list = new MockArrayList();
+ ListIterator it = list.listIterator();
+ it.add("one");
+ it.add("two");
+ assertEquals(2,list.size());
}
/**
@@ -222,7 +229,7 @@
}
public void add(int idx, E o) {
- modCount++;
+ modCount += 10;
list.add(idx, o);
}
}
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArrayListTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArrayListTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArrayListTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArrayListTest.java Fri Aug 22 00:15:00 2008
@@ -30,118 +30,118 @@
public class ArrayListTest extends junit.framework.TestCase {
- List alist;
+ List alist;
- static Object[] objArray;
- {
- objArray = new Object[100];
- for (int i = 0; i < objArray.length; i++)
- objArray[i] = new Integer(i);
- }
-
- /**
- * @tests java.util.ArrayList#ArrayList()
- */
- public void test_Constructor() {
- // Test for method java.util.ArrayList()
- new Support_ListTest("", alist).runTest();
-
- ArrayList subList = new ArrayList();
- for (int i = -50; i < 150; i++)
- subList.add(new Integer(i));
- new Support_ListTest("", subList.subList(50, 150)).runTest();
- }
-
- /**
- * @tests java.util.ArrayList#ArrayList(int)
- */
- public void test_ConstructorI() {
- // Test for method java.util.ArrayList(int)
- ArrayList al = new ArrayList(5);
- assertEquals("Incorrect arrayList created", 0, al.size());
- }
-
- /**
- * @tests java.util.ArrayList#ArrayList(java.util.Collection)
- */
- public void test_ConstructorLjava_util_Collection() {
- // Test for method java.util.ArrayList(java.util.Collection)
- ArrayList al = new ArrayList(Arrays.asList(objArray));
- assertTrue("arrayList created from collection has incorrect size", al
- .size() == objArray.length);
- for (int counter = 0; counter < objArray.length; counter++)
- assertTrue(
- "arrayList created from collection has incorrect elements",
- al.get(counter) == objArray[counter]);
-
- }
-
- /**
- * @tests java.util.ArrayList#add(int, java.lang.Object)
- */
- public void test_addILjava_lang_Object() {
- // Test for method void java.util.ArrayList.add(int, java.lang.Object)
- Object o;
- alist.add(50, o = new Object());
- assertTrue("Failed to add Object", alist.get(50) == o);
- assertTrue("Failed to fix up list after insert",
- alist.get(51) == objArray[50]
- && (alist.get(52) == objArray[51]));
- Object oldItem = alist.get(25);
- alist.add(25, null);
- assertNull("Should have returned null", alist.get(25));
- assertTrue("Should have returned the old item from slot 25", alist
- .get(26) == oldItem);
- }
-
- /**
- * @tests java.util.ArrayList#add(java.lang.Object)
- */
- public void test_addLjava_lang_Object() {
- // Test for method boolean java.util.ArrayList.add(java.lang.Object)
- Object o = new Object();
- alist.add(o);
- assertTrue("Failed to add Object", alist.get(alist.size() - 1) == o);
- alist.add(null);
- assertNull("Failed to add null", alist.get(alist.size() - 1));
- }
-
- /**
- * @tests java.util.ArrayList#addAll(int, java.util.Collection)
- */
- public void test_addAllILjava_util_Collection() {
- // Test for method boolean java.util.ArrayList.addAll(int,
- // java.util.Collection)
- alist.addAll(50, alist);
- assertEquals("Returned incorrect size after adding to existing list",
- 200, alist.size());
- for (int i = 0; i < 50; i++)
- assertTrue("Manipulated elements < index",
- alist.get(i) == objArray[i]);
- for (int i = 0; i >= 50 && (i < 150); i++)
- assertTrue("Failed to ad elements properly",
- alist.get(i) == objArray[i - 50]);
- for (int i = 0; i >= 150 && (i < 200); i++)
- assertTrue("Failed to ad elements properly",
- alist.get(i) == objArray[i - 100]);
- ArrayList listWithNulls = new ArrayList();
- listWithNulls.add(null);
- listWithNulls.add(null);
- listWithNulls.add("yoink");
- listWithNulls.add("kazoo");
- listWithNulls.add(null);
- alist.addAll(100, listWithNulls);
- assertTrue("Incorrect size: " + alist.size(), alist.size() == 205);
- assertNull("Item at slot 100 should be null", alist.get(100));
- assertNull("Item at slot 101 should be null", alist.get(101));
- assertEquals("Item at slot 102 should be 'yoink'",
- "yoink", alist.get(102));
- assertEquals("Item at slot 103 should be 'kazoo'",
- "kazoo", alist.get(103));
- assertNull("Item at slot 104 should be null", alist.get(104));
- alist.addAll(205, listWithNulls);
- assertTrue("Incorrect size2: " + alist.size(), alist.size() == 210);
- }
+ static Object[] objArray;
+ {
+ objArray = new Object[100];
+ for (int i = 0; i < objArray.length; i++)
+ objArray[i] = new Integer(i);
+ }
+
+ /**
+ * @tests java.util.ArrayList#ArrayList()
+ */
+ public void test_Constructor() {
+ // Test for method java.util.ArrayList()
+ new Support_ListTest("", alist).runTest();
+
+ ArrayList subList = new ArrayList();
+ for (int i = -50; i < 150; i++)
+ subList.add(new Integer(i));
+ new Support_ListTest("", subList.subList(50, 150)).runTest();
+ }
+
+ /**
+ * @tests java.util.ArrayList#ArrayList(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.util.ArrayList(int)
+ ArrayList al = new ArrayList(5);
+ assertEquals("Incorrect arrayList created", 0, al.size());
+ }
+
+ /**
+ * @tests java.util.ArrayList#ArrayList(java.util.Collection)
+ */
+ public void test_ConstructorLjava_util_Collection() {
+ // Test for method java.util.ArrayList(java.util.Collection)
+ ArrayList al = new ArrayList(Arrays.asList(objArray));
+ assertTrue("arrayList created from collection has incorrect size", al
+ .size() == objArray.length);
+ for (int counter = 0; counter < objArray.length; counter++)
+ assertTrue(
+ "arrayList created from collection has incorrect elements",
+ al.get(counter) == objArray[counter]);
+
+ }
+
+ /**
+ * @tests java.util.ArrayList#add(int, java.lang.Object)
+ */
+ public void test_addILjava_lang_Object() {
+ // Test for method void java.util.ArrayList.add(int, java.lang.Object)
+ Object o;
+ alist.add(50, o = new Object());
+ assertTrue("Failed to add Object", alist.get(50) == o);
+ assertTrue("Failed to fix up list after insert",
+ alist.get(51) == objArray[50]
+ && (alist.get(52) == objArray[51]));
+ Object oldItem = alist.get(25);
+ alist.add(25, null);
+ assertNull("Should have returned null", alist.get(25));
+ assertTrue("Should have returned the old item from slot 25", alist
+ .get(26) == oldItem);
+ }
+
+ /**
+ * @tests java.util.ArrayList#add(java.lang.Object)
+ */
+ public void test_addLjava_lang_Object() {
+ // Test for method boolean java.util.ArrayList.add(java.lang.Object)
+ Object o = new Object();
+ alist.add(o);
+ assertTrue("Failed to add Object", alist.get(alist.size() - 1) == o);
+ alist.add(null);
+ assertNull("Failed to add null", alist.get(alist.size() - 1));
+ }
+
+ /**
+ * @tests java.util.ArrayList#addAll(int, java.util.Collection)
+ */
+ public void test_addAllILjava_util_Collection() {
+ // Test for method boolean java.util.ArrayList.addAll(int,
+ // java.util.Collection)
+ alist.addAll(50, alist);
+ assertEquals("Returned incorrect size after adding to existing list",
+ 200, alist.size());
+ for (int i = 0; i < 50; i++)
+ assertTrue("Manipulated elements < index",
+ alist.get(i) == objArray[i]);
+ for (int i = 0; i >= 50 && (i < 150); i++)
+ assertTrue("Failed to ad elements properly",
+ alist.get(i) == objArray[i - 50]);
+ for (int i = 0; i >= 150 && (i < 200); i++)
+ assertTrue("Failed to ad elements properly",
+ alist.get(i) == objArray[i - 100]);
+ ArrayList listWithNulls = new ArrayList();
+ listWithNulls.add(null);
+ listWithNulls.add(null);
+ listWithNulls.add("yoink");
+ listWithNulls.add("kazoo");
+ listWithNulls.add(null);
+ alist.addAll(100, listWithNulls);
+ assertTrue("Incorrect size: " + alist.size(), alist.size() == 205);
+ assertNull("Item at slot 100 should be null", alist.get(100));
+ assertNull("Item at slot 101 should be null", alist.get(101));
+ assertEquals("Item at slot 102 should be 'yoink'", "yoink", alist
+ .get(102));
+ assertEquals("Item at slot 103 should be 'kazoo'", "kazoo", alist
+ .get(103));
+ assertNull("Item at slot 104 should be null", alist.get(104));
+ alist.addAll(205, listWithNulls);
+ assertTrue("Incorrect size2: " + alist.size(), alist.size() == 210);
+ }
/**
* @tests java.util.ArrayList#addAll(int, java.util.Collection)
@@ -155,7 +155,7 @@
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException e) {
}
-
+
// Regression for HARMONY-5705
String[] data = new String[] { "1", "2", "3", "4", "5", "6", "7", "8" };
ArrayList list1 = new ArrayList();
@@ -169,7 +169,8 @@
list1.remove(0);
list1.addAll(list2);
assertTrue("The object list is not the same as orginal list", list1
- .containsAll(list2) && list2.containsAll(list1));
+ .containsAll(list2)
+ && list2.containsAll(list1));
obj = new ArrayList();
for (int i = 0; i < 100; i++) {
@@ -179,32 +180,33 @@
}
}
assertTrue("The object list is not the same as orginal list", obj
- .containsAll(list1) && list1.containsAll(obj));
-
+ .containsAll(list1)
+ && list1.containsAll(obj));
+
// Regression for Harmony-5799
list1 = new ArrayList();
list2 = new ArrayList();
int location = 2;
-
- String[] strings = {"0","1","2","3","4","5","6"};
- int[] integers = {0,1,2,3,4,5,6,7,8,9};
- for (int i = 0; i < 7; i++){
+
+ String[] strings = { "0", "1", "2", "3", "4", "5", "6" };
+ int[] integers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ for (int i = 0; i < 7; i++) {
list1.add(strings[i]);
}
- for (int i = 0; i < 10; i++){
+ for (int i = 0; i < 10; i++) {
list2.add(integers[i]);
}
list1.remove(location);
- list1.addAll(location,list2);
-
+ list1.addAll(location, list2);
+
// Inserted elements should be equal to integers array
- for(int i = 0; i < integers.length; i ++){
- assertEquals(integers[i],list1.get(location+i));
+ for (int i = 0; i < integers.length; i++) {
+ assertEquals(integers[i], list1.get(location + i));
}
- // Elements after inserted location should
+ // Elements after inserted location should
// be equals to related elements in strings array
- for(int i = location + 1; i < strings.length; i++){
- assertEquals(strings[i],list1.get(i+integers.length-1));
+ for (int i = location + 1; i < strings.length; i++) {
+ assertEquals(strings[i], list1.get(i + integers.length - 1));
}
}
@@ -223,52 +225,51 @@
assertEquals("b", blist.get(1));
assertEquals("d", blist.get(2));
}
-
+
/**
- * @tests java.util.ArrayList#addAll(java.util.Collection)
- */
- public void test_addAllLjava_util_Collection() {
- // Test for method boolean
- // java.util.ArrayList.addAll(java.util.Collection)
- List l = new ArrayList();
- l.addAll(alist);
- for (int i = 0; i < alist.size(); i++)
- assertTrue("Failed to add elements properly", l.get(i).equals(
- alist.get(i)));
- alist.addAll(alist);
- assertEquals("Returned incorrect size after adding to existing list",
- 200, alist.size());
- for (int i = 0; i < 100; i++) {
- assertTrue("Added to list in incorrect order", alist.get(i)
- .equals(l.get(i)));
- assertTrue("Failed to add to existing list", alist.get(i + 100)
- .equals(l.get(i)));
- }
- Set setWithNulls = new HashSet();
- setWithNulls.add(null);
- setWithNulls.add(null);
- setWithNulls.add("yoink");
- setWithNulls.add("kazoo");
- setWithNulls.add(null);
- alist.addAll(100, setWithNulls);
- Iterator i = setWithNulls.iterator();
- assertTrue("Item at slot 100 is wrong: " + alist.get(100), alist
- .get(100) == i.next());
- assertTrue("Item at slot 101 is wrong: " + alist.get(101), alist
- .get(101) == i.next());
- assertTrue("Item at slot 103 is wrong: " + alist.get(102), alist
- .get(102) == i.next());
-
-
+ * @tests java.util.ArrayList#addAll(java.util.Collection)
+ */
+ public void test_addAllLjava_util_Collection() {
+ // Test for method boolean
+ // java.util.ArrayList.addAll(java.util.Collection)
+ List l = new ArrayList();
+ l.addAll(alist);
+ for (int i = 0; i < alist.size(); i++)
+ assertTrue("Failed to add elements properly", l.get(i).equals(
+ alist.get(i)));
+ alist.addAll(alist);
+ assertEquals("Returned incorrect size after adding to existing list",
+ 200, alist.size());
+ for (int i = 0; i < 100; i++) {
+ assertTrue("Added to list in incorrect order", alist.get(i).equals(
+ l.get(i)));
+ assertTrue("Failed to add to existing list", alist.get(i + 100)
+ .equals(l.get(i)));
+ }
+ Set setWithNulls = new HashSet();
+ setWithNulls.add(null);
+ setWithNulls.add(null);
+ setWithNulls.add("yoink");
+ setWithNulls.add("kazoo");
+ setWithNulls.add(null);
+ alist.addAll(100, setWithNulls);
+ Iterator i = setWithNulls.iterator();
+ assertTrue("Item at slot 100 is wrong: " + alist.get(100), alist
+ .get(100) == i.next());
+ assertTrue("Item at slot 101 is wrong: " + alist.get(101), alist
+ .get(101) == i.next());
+ assertTrue("Item at slot 103 is wrong: " + alist.get(102), alist
+ .get(102) == i.next());
+
// Regression test for Harmony-3481
ArrayList<Integer> originalList = new ArrayList<Integer>(12);
for (int j = 0; j < 12; j++) {
originalList.add(j);
}
-
+
originalList.remove(0);
originalList.remove(0);
-
+
ArrayList<Integer> additionalList = new ArrayList<Integer>(11);
for (int j = 0; j < 11; j++) {
additionalList.add(j);
@@ -276,297 +277,317 @@
assertTrue(originalList.addAll(additionalList));
assertEquals(21, originalList.size());
- }
+ }
+
+ // Regression test for HARMONY-5839
+ public void testaddAllHarmony5839() {
+ Collection coll = Arrays.asList(new String[] { "1", "2" });
+ List list = new ArrayList();
+ list.add("a");
+ list.add(0, "b");
+ list.add(0, "c");
+ list.add(0, "d");
+ list.add(0, "e");
+ list.add(0, "f");
+ list.add(0, "g");
+ list.add(0, "h");
+ list.add(0, "i");
+
+ list.addAll(6, coll);
+
+ assertEquals(11, list.size());
+ assertFalse(list.contains(null));
+ }
- /**
+ /**
* @tests java.util.ArrayList#clear()
*/
- public void test_clear() {
- // Test for method void java.util.ArrayList.clear()
- alist.clear();
- assertEquals("List did not clear", 0, alist.size());
- alist.add(null);
- alist.add(null);
- alist.add(null);
- alist.add("bam");
- alist.clear();
- assertEquals("List with nulls did not clear", 0, alist.size());
- /*
- * for (int i = 0; i < alist.size(); i++) assertNull("Failed to clear
- * list", alist.get(i));
- */
-
- }
-
- /**
- * @tests java.util.ArrayList#clone()
- */
- public void test_clone() {
- // Test for method java.lang.Object java.util.ArrayList.clone()
- ArrayList x = (ArrayList) (((ArrayList) (alist)).clone());
- assertTrue("Cloned list was inequal to original", x.equals(alist));
- for (int i = 0; i < alist.size(); i++)
- assertTrue("Cloned list contains incorrect elements",
- alist.get(i) == x.get(i));
-
- alist.add(null);
- alist.add(25, null);
- x = (ArrayList) (((ArrayList) (alist)).clone());
- assertTrue("nulls test - Cloned list was inequal to original", x
- .equals(alist));
- for (int i = 0; i < alist.size(); i++)
- assertTrue("nulls test - Cloned list contains incorrect elements",
- alist.get(i) == x.get(i));
-
- }
-
- /**
- * @tests java.util.ArrayList#contains(java.lang.Object)
- */
- public void test_containsLjava_lang_Object() {
- // Test for method boolean
- // java.util.ArrayList.contains(java.lang.Object)
- assertTrue("Returned false for valid element", alist
- .contains(objArray[99]));
- assertTrue("Returned false for equal element", alist
- .contains(new Integer(8)));
- assertTrue("Returned true for invalid element", !alist
- .contains(new Object()));
- assertTrue("Returned true for null but should have returned false",
- !alist.contains(null));
- alist.add(null);
- assertTrue("Returned false for null but should have returned true",
- alist.contains(null));
- }
-
- /**
- * @tests java.util.ArrayList#ensureCapacity(int)
- */
- public void test_ensureCapacityI() {
- // Test for method void java.util.ArrayList.ensureCapacity(int)
- // TODO : There is no good way to test this as it only really impacts on
- // the private implementation.
-
- Object testObject = new Object();
- int capacity = 20;
- ArrayList al = new ArrayList(capacity);
- int i;
- for (i = 0; i < capacity / 2; i++) {
- al.add(i, new Object());
- }
- al.add(i, testObject);
- int location = al.indexOf(testObject);
- al.ensureCapacity(capacity);
- assertTrue("EnsureCapacity moved objects around in array1.",
- location == al.indexOf(testObject));
- al.remove(0);
- al.ensureCapacity(capacity);
- assertTrue("EnsureCapacity moved objects around in array2.",
- --location == al.indexOf(testObject));
- al.ensureCapacity(capacity + 2);
- assertTrue("EnsureCapacity did not change location.",
- location == al.indexOf(testObject));
- }
-
- /**
- * @tests java.util.ArrayList#get(int)
- */
- public void test_getI() {
- // Test for method java.lang.Object java.util.ArrayList.get(int)
- assertTrue("Returned incorrect element", alist.get(22) == objArray[22]);
- try {
- alist.get(8765);
+ public void test_clear() {
+ // Test for method void java.util.ArrayList.clear()
+ alist.clear();
+ assertEquals("List did not clear", 0, alist.size());
+ alist.add(null);
+ alist.add(null);
+ alist.add(null);
+ alist.add("bam");
+ alist.clear();
+ assertEquals("List with nulls did not clear", 0, alist.size());
+ /*
+ * for (int i = 0; i < alist.size(); i++) assertNull("Failed to clear
+ * list", alist.get(i));
+ */
+
+ }
+
+ /**
+ * @tests java.util.ArrayList#clone()
+ */
+ public void test_clone() {
+ // Test for method java.lang.Object java.util.ArrayList.clone()
+ ArrayList x = (ArrayList) (((ArrayList) (alist)).clone());
+ assertTrue("Cloned list was inequal to original", x.equals(alist));
+ for (int i = 0; i < alist.size(); i++)
+ assertTrue("Cloned list contains incorrect elements",
+ alist.get(i) == x.get(i));
+
+ alist.add(null);
+ alist.add(25, null);
+ x = (ArrayList) (((ArrayList) (alist)).clone());
+ assertTrue("nulls test - Cloned list was inequal to original", x
+ .equals(alist));
+ for (int i = 0; i < alist.size(); i++)
+ assertTrue("nulls test - Cloned list contains incorrect elements",
+ alist.get(i) == x.get(i));
+
+ }
+
+ /**
+ * @tests java.util.ArrayList#contains(java.lang.Object)
+ */
+ public void test_containsLjava_lang_Object() {
+ // Test for method boolean
+ // java.util.ArrayList.contains(java.lang.Object)
+ assertTrue("Returned false for valid element", alist
+ .contains(objArray[99]));
+ assertTrue("Returned false for equal element", alist
+ .contains(new Integer(8)));
+ assertTrue("Returned true for invalid element", !alist
+ .contains(new Object()));
+ assertTrue("Returned true for null but should have returned false",
+ !alist.contains(null));
+ alist.add(null);
+ assertTrue("Returned false for null but should have returned true",
+ alist.contains(null));
+ }
+
+ /**
+ * @tests java.util.ArrayList#ensureCapacity(int)
+ */
+ public void test_ensureCapacityI() {
+ // Test for method void java.util.ArrayList.ensureCapacity(int)
+ // TODO : There is no good way to test this as it only really impacts on
+ // the private implementation.
+
+ Object testObject = new Object();
+ int capacity = 20;
+ ArrayList al = new ArrayList(capacity);
+ int i;
+ for (i = 0; i < capacity / 2; i++) {
+ al.add(i, new Object());
+ }
+ al.add(i, testObject);
+ int location = al.indexOf(testObject);
+ al.ensureCapacity(capacity);
+ assertTrue("EnsureCapacity moved objects around in array1.",
+ location == al.indexOf(testObject));
+ al.remove(0);
+ al.ensureCapacity(capacity);
+ assertTrue("EnsureCapacity moved objects around in array2.",
+ --location == al.indexOf(testObject));
+ al.ensureCapacity(capacity + 2);
+ assertTrue("EnsureCapacity did not change location.", location == al
+ .indexOf(testObject));
+ }
+
+ /**
+ * @tests java.util.ArrayList#get(int)
+ */
+ public void test_getI() {
+ // Test for method java.lang.Object java.util.ArrayList.get(int)
+ assertTrue("Returned incorrect element", alist.get(22) == objArray[22]);
+ try {
+ alist.get(8765);
fail("Failed to throw expected exception for index > size");
- } catch (IndexOutOfBoundsException e) {
- }
- }
-
- /**
- * @tests java.util.ArrayList#indexOf(java.lang.Object)
- */
- public void test_indexOfLjava_lang_Object() {
- // Test for method int java.util.ArrayList.indexOf(java.lang.Object)
- assertEquals("Returned incorrect index",
- 87, alist.indexOf(objArray[87]));
- assertEquals("Returned index for invalid Object", -1, alist
- .indexOf(new Object()));
- alist.add(25, null);
- alist.add(50, null);
- assertTrue("Wrong indexOf for null. Wanted 25 got: "
- + alist.indexOf(null), alist.indexOf(null) == 25);
- }
-
- /**
- * @tests java.util.ArrayList#isEmpty()
- */
- public void test_isEmpty() {
- // Test for method boolean java.util.ArrayList.isEmpty()
- assertTrue("isEmpty returned false for new list", new ArrayList()
- .isEmpty());
- assertTrue("Returned true for existing list with elements", !alist
- .isEmpty());
- }
-
- /**
- * @tests java.util.ArrayList#lastIndexOf(java.lang.Object)
- */
- public void test_lastIndexOfLjava_lang_Object() {
- // Test for method int java.util.ArrayList.lastIndexOf(java.lang.Object)
- alist.add(new Integer(99));
- assertEquals("Returned incorrect index",
- 100, alist.lastIndexOf(objArray[99]));
- assertEquals("Returned index for invalid Object", -1, alist
- .lastIndexOf(new Object()));
- alist.add(25, null);
- alist.add(50, null);
- assertTrue("Wrong lastIndexOf for null. Wanted 50 got: "
- + alist.lastIndexOf(null), alist.lastIndexOf(null) == 50);
- }
-
- /**
- * @tests java.util.ArrayList#remove(int)
- */
- public void test_removeI() {
- // Test for method java.lang.Object java.util.ArrayList.remove(int)
- alist.remove(10);
- assertEquals("Failed to remove element",
- -1, alist.indexOf(objArray[10]));
- try {
- alist.remove(999);
+ } catch (IndexOutOfBoundsException e) {
+ }
+ }
+
+ /**
+ * @tests java.util.ArrayList#indexOf(java.lang.Object)
+ */
+ public void test_indexOfLjava_lang_Object() {
+ // Test for method int java.util.ArrayList.indexOf(java.lang.Object)
+ assertEquals("Returned incorrect index", 87, alist
+ .indexOf(objArray[87]));
+ assertEquals("Returned index for invalid Object", -1, alist
+ .indexOf(new Object()));
+ alist.add(25, null);
+ alist.add(50, null);
+ assertTrue("Wrong indexOf for null. Wanted 25 got: "
+ + alist.indexOf(null), alist.indexOf(null) == 25);
+ }
+
+ /**
+ * @tests java.util.ArrayList#isEmpty()
+ */
+ public void test_isEmpty() {
+ // Test for method boolean java.util.ArrayList.isEmpty()
+ assertTrue("isEmpty returned false for new list", new ArrayList()
+ .isEmpty());
+ assertTrue("Returned true for existing list with elements", !alist
+ .isEmpty());
+ }
+
+ /**
+ * @tests java.util.ArrayList#lastIndexOf(java.lang.Object)
+ */
+ public void test_lastIndexOfLjava_lang_Object() {
+ // Test for method int java.util.ArrayList.lastIndexOf(java.lang.Object)
+ alist.add(new Integer(99));
+ assertEquals("Returned incorrect index", 100, alist
+ .lastIndexOf(objArray[99]));
+ assertEquals("Returned index for invalid Object", -1, alist
+ .lastIndexOf(new Object()));
+ alist.add(25, null);
+ alist.add(50, null);
+ assertTrue("Wrong lastIndexOf for null. Wanted 50 got: "
+ + alist.lastIndexOf(null), alist.lastIndexOf(null) == 50);
+ }
+
+ /**
+ * @tests java.util.ArrayList#remove(int)
+ */
+ public void test_removeI() {
+ // Test for method java.lang.Object java.util.ArrayList.remove(int)
+ alist.remove(10);
+ assertEquals("Failed to remove element", -1, alist
+ .indexOf(objArray[10]));
+ try {
+ alist.remove(999);
fail("Failed to throw exception when index out of range");
- } catch (IndexOutOfBoundsException e) {
- }
+ } catch (IndexOutOfBoundsException e) {
+ }
+
+ ArrayList myList = (ArrayList) (((ArrayList) (alist)).clone());
+ alist.add(25, null);
+ alist.add(50, null);
+ alist.remove(50);
+ alist.remove(25);
+ assertTrue("Removing nulls did not work", alist.equals(myList));
+
+ List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
+ "d", "e", "f", "g" }));
+ assertTrue("Removed wrong element 1", list.remove(0) == "a");
+ assertTrue("Removed wrong element 2", list.remove(4) == "f");
+ String[] result = new String[5];
+ list.toArray(result);
+ assertTrue("Removed wrong element 3", Arrays.equals(result,
+ new String[] { "b", "c", "d", "e", "g" }));
+
+ List l = new ArrayList(0);
+ l.add(new Object());
+ l.add(new Object());
+ l.remove(0);
+ l.remove(0);
+ try {
+ l.remove(-1);
+ fail("-1 should cause exception");
+ } catch (IndexOutOfBoundsException e) {
+ }
+ try {
+ l.remove(0);
+ fail("0 should case exception");
+ } catch (IndexOutOfBoundsException e) {
+ }
+ }
+
+ /**
+ * @tests java.util.ArrayList#set(int, java.lang.Object)
+ */
+ public void test_setILjava_lang_Object() {
+ // Test for method java.lang.Object java.util.ArrayList.set(int,
+ // java.lang.Object)
+ Object obj;
+ alist.set(65, obj = new Object());
+ assertTrue("Failed to set object", alist.get(65) == obj);
+ alist.set(50, null);
+ assertNull("Setting to null did not work", alist.get(50));
+ assertTrue("Setting increased the list's size to: " + alist.size(),
+ alist.size() == 100);
+ }
- ArrayList myList = (ArrayList) (((ArrayList) (alist)).clone());
- alist.add(25, null);
- alist.add(50, null);
- alist.remove(50);
- alist.remove(25);
- assertTrue("Removing nulls did not work", alist.equals(myList));
-
- List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c",
- "d", "e", "f", "g" }));
- assertTrue("Removed wrong element 1", list.remove(0) == "a");
- assertTrue("Removed wrong element 2", list.remove(4) == "f");
- String[] result = new String[5];
- list.toArray(result);
- assertTrue("Removed wrong element 3", Arrays.equals(result,
- new String[] { "b", "c", "d", "e", "g" }));
-
- List l = new ArrayList(0);
- l.add(new Object());
- l.add(new Object());
- l.remove(0);
- l.remove(0);
- try {
- l.remove(-1);
- fail("-1 should cause exception");
- } catch (IndexOutOfBoundsException e) {
- }
- try {
- l.remove(0);
- fail("0 should case exception");
- } catch (IndexOutOfBoundsException e) {
- }
- }
-
- /**
- * @tests java.util.ArrayList#set(int, java.lang.Object)
- */
- public void test_setILjava_lang_Object() {
- // Test for method java.lang.Object java.util.ArrayList.set(int,
- // java.lang.Object)
- Object obj;
- alist.set(65, obj = new Object());
- assertTrue("Failed to set object", alist.get(65) == obj);
- alist.set(50, null);
- assertNull("Setting to null did not work", alist.get(50));
- assertTrue("Setting increased the list's size to: " + alist.size(),
- alist.size() == 100);
- }
-
- /**
- * @tests java.util.ArrayList#size()
- */
- public void test_size() {
- // Test for method int java.util.ArrayList.size()
- assertEquals("Returned incorrect size for exiting list",
- 100, alist.size());
- assertEquals("Returned incorrect size for new list", 0, new ArrayList()
- .size());
- }
-
- /**
- * @tests java.util.ArrayList#toArray()
- */
- public void test_toArray() {
- // Test for method java.lang.Object [] java.util.ArrayList.toArray()
- alist.set(25, null);
- alist.set(75, null);
- Object[] obj = alist.toArray();
- assertEquals("Returned array of incorrect size", objArray.length,
- obj.length);
-
- for (int i = 0; i < obj.length; i++) {
- if ((i == 25) || (i == 75))
- assertNull("Should be null at: " + i + " but instead got: "
- + obj[i], obj[i]);
- else
- assertTrue("Returned incorrect array: " + i,
- obj[i] == objArray[i]);
- }
-
- }
-
- /**
- * @tests java.util.ArrayList#toArray(java.lang.Object[])
- */
- public void test_toArray$Ljava_lang_Object() {
- // Test for method java.lang.Object []
- // java.util.ArrayList.toArray(java.lang.Object [])
- alist.set(25, null);
- alist.set(75, null);
- Integer[] argArray = new Integer[100];
- Object[] retArray;
- retArray = alist.toArray(argArray);
- assertTrue("Returned different array than passed", retArray == argArray);
- argArray = new Integer[1000];
- retArray = alist.toArray(argArray);
- assertNull("Failed to set first extra element to null", argArray[alist
- .size()]);
- for (int i = 0; i < 100; i++) {
- if ((i == 25) || (i == 75))
- assertNull("Should be null: " + i, retArray[i]);
- else
- assertTrue("Returned incorrect array: " + i,
- retArray[i] == objArray[i]);
- }
- }
-
- /**
- * @tests java.util.ArrayList#trimToSize()
- */
- public void test_trimToSize() {
- // Test for method void java.util.ArrayList.trimToSize()
- for (int i = 99; i > 24; i--)
- alist.remove(i);
- ((ArrayList) alist).trimToSize();
- assertEquals("Returned incorrect size after trim", 25, alist.size());
- for (int i = 0; i < alist.size(); i++)
- assertTrue("Trimmed list contained incorrect elements", alist
- .get(i) == objArray[i]);
- Vector v = new Vector();
- v.add("a");
- ArrayList al = new ArrayList(v);
- Iterator it = al.iterator();
- al.trimToSize();
- try {
- it.next();
- fail("should throw a ConcurrentModificationException");
- } catch (ConcurrentModificationException ioobe) {
- // expected
- }
- }
+ /**
+ * @tests java.util.ArrayList#size()
+ */
+ public void test_size() {
+ // Test for method int java.util.ArrayList.size()
+ assertEquals("Returned incorrect size for exiting list", 100, alist
+ .size());
+ assertEquals("Returned incorrect size for new list", 0, new ArrayList()
+ .size());
+ }
+
+ /**
+ * @tests java.util.ArrayList#toArray()
+ */
+ public void test_toArray() {
+ // Test for method java.lang.Object [] java.util.ArrayList.toArray()
+ alist.set(25, null);
+ alist.set(75, null);
+ Object[] obj = alist.toArray();
+ assertEquals("Returned array of incorrect size", objArray.length,
+ obj.length);
+
+ for (int i = 0; i < obj.length; i++) {
+ if ((i == 25) || (i == 75))
+ assertNull("Should be null at: " + i + " but instead got: "
+ + obj[i], obj[i]);
+ else
+ assertTrue("Returned incorrect array: " + i,
+ obj[i] == objArray[i]);
+ }
+
+ }
+
+ /**
+ * @tests java.util.ArrayList#toArray(java.lang.Object[])
+ */
+ public void test_toArray$Ljava_lang_Object() {
+ // Test for method java.lang.Object []
+ // java.util.ArrayList.toArray(java.lang.Object [])
+ alist.set(25, null);
+ alist.set(75, null);
+ Integer[] argArray = new Integer[100];
+ Object[] retArray;
+ retArray = alist.toArray(argArray);
+ assertTrue("Returned different array than passed", retArray == argArray);
+ argArray = new Integer[1000];
+ retArray = alist.toArray(argArray);
+ assertNull("Failed to set first extra element to null", argArray[alist
+ .size()]);
+ for (int i = 0; i < 100; i++) {
+ if ((i == 25) || (i == 75))
+ assertNull("Should be null: " + i, retArray[i]);
+ else
+ assertTrue("Returned incorrect array: " + i,
+ retArray[i] == objArray[i]);
+ }
+ }
+
+ /**
+ * @tests java.util.ArrayList#trimToSize()
+ */
+ public void test_trimToSize() {
+ // Test for method void java.util.ArrayList.trimToSize()
+ for (int i = 99; i > 24; i--)
+ alist.remove(i);
+ ((ArrayList) alist).trimToSize();
+ assertEquals("Returned incorrect size after trim", 25, alist.size());
+ for (int i = 0; i < alist.size(); i++)
+ assertTrue("Trimmed list contained incorrect elements", alist
+ .get(i) == objArray[i]);
+ Vector v = new Vector();
+ v.add("a");
+ ArrayList al = new ArrayList(v);
+ Iterator it = al.iterator();
+ al.trimToSize();
+ try {
+ it.next();
+ fail("should throw a ConcurrentModificationException");
+ } catch (ConcurrentModificationException ioobe) {
+ // expected
+ }
+ }
/**
* @test java.util.ArrayList#addAll(int, Collection)
@@ -579,16 +600,16 @@
list.remove(0);
assertEquals(1, list.size());
-
+
ArrayList collection = new ArrayList();
collection.add("1");
collection.add("2");
collection.add("3");
assertEquals(3, collection.size());
-
+
list.addAll(0, collection);
assertEquals(4, list.size());
-
+
list.remove(0);
list.remove(0);
assertEquals(2, list.size());
@@ -602,20 +623,20 @@
collection.add("10");
collection.add("11");
collection.add("12");
-
+
assertEquals(12, collection.size());
-
+
list.addAll(0, collection);
assertEquals(14, list.size());
}
-
+
public void test_override_size() throws Exception {
ArrayList testlist = new MockArrayList();
// though size is overriden, it should passed without exception
testlist.add("test_0");
testlist.add("test_1");
testlist.add("test_2");
- testlist.add(1,"test_3");
+ testlist.add(1, "test_3");
testlist.get(1);
testlist.remove(2);
testlist.set(1, "test_4");
@@ -638,7 +659,7 @@
return size;
}
}
-
+
public class MockArrayList extends ArrayList {
public int size() {
return 0;
@@ -655,15 +676,15 @@
a.add(new Object());
}
}
-
- /**
- * Sets up the fixture, for example, open a network connection. This method
- * is called before a test is executed.
- */
- protected void setUp() throws Exception {
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() throws Exception {
super.setUp();
- alist = new ArrayList();
- for (int i = 0; i < objArray.length; i++)
- alist.add(objArray[i]);
- }
+ alist = new ArrayList();
+ for (int i = 0; i < objArray.length; i++)
+ alist.add(objArray[i]);
+ }
}
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java Fri Aug 22 00:15:00 2008
@@ -1297,7 +1297,8 @@
/**
* @tests java.util.Arrays#sort(java.lang.Object[], java.util.Comparator)
*/
- public void test_sort$Ljava_lang_ObjectLjava_util_Comparator() {
+ @SuppressWarnings("unchecked")
+ public void test_sort$Ljava_lang_ObjectLjava_util_Comparator() {
// Test for method void java.util.Arrays.sort(java.lang.Object [],
// java.util.Comparator)
ReversedIntegerComparator comp = new ReversedIntegerComparator();
@@ -1307,6 +1308,52 @@
comp
.compare(objectArray[counter],
objectArray[counter + 1]) <= 0);
+
+ // Test the sort functionailty with an Integer array
+ int[] original = { 190, 180, 170, 160, 150, 140, 120, 320, 110, 310,
+ 100, 300, 290, 280, 270, 260, 250, 240, 230, 210, 200 };
+ Integer[] sorted = { 100, 110, 120, 140, 150, 160, 170, 180, 190, 200,
+ 210, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320 };
+
+ Integer[] elements = new Integer[original.length];
+ for (int i=0; i < original.length; i++){
+ elements[i] = new Integer(original[i]);
+ }
+
+ Comparator normalComparator = new Comparator(){
+ public int compare(Object o1, Object o2) {
+ Integer e1 = (Integer)o1;
+ Integer e2 = (Integer)o2;
+ if (e1 > e2){
+ return 1;
+ }else if(e1 < e2){
+ return -1;
+ }else{
+ return 0;
+ }
+ }
+ };
+ Arrays.sort(elements, normalComparator);
+ // After sorting, elements should be the same as sorted array.
+ for(int i = 0; i < original.length; i++){
+ assertEquals(sorted[i],elements[i]);
+ }
+
+ for (int i=0; i < original.length; i++){
+ elements[i] = new Integer(original[i]);
+ }
+ Comparator comparator = new Comparator(){
+ public int compare(Object o1, Object o2) {
+ Integer e1 = (Integer)o1;
+ Integer e2 = (Integer)o2;
+ return e1 > e2 ? 1 : 0;
+ }
+ };
+ Arrays.sort(elements, comparator);
+ // After sorting, elements should be the same as sorted array.
+ for(int i = 0; i < original.length; i++){
+ assertEquals(sorted[i],elements[i]);
+ }
}
/**
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/Collections2Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/Collections2Test.java?rev=687988&r1=687987&r2=687988&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/Collections2Test.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/Collections2Test.java Fri Aug 22 00:15:00 2008
@@ -457,4 +457,11 @@
c = Collections.checkedSortedMap(c, String.class, String.class);
SerializationTester.assertCompabilityEquals(c, "serialization/java/util/Collections_CheckedSortedMap.golden.ser");
}
+
+ public void test_emptyList(){
+ List<Object> emptyList = Collections.emptyList();
+ assertEquals(0, emptyList.size());
+ assertTrue(emptyList instanceof RandomAccess);
+ }
+
}