You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by ke...@apache.org on 2010/07/15 00:52:11 UTC

svn commit: r964239 - in /incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io: ./ FixedBufferOutputStreamTest.java NullInputStreamTest.java NullOutputStreamTest.java

Author: kelly
Date: Wed Jul 14 22:52:10 2010
New Revision: 964239

URL: http://svn.apache.org/viewvc?rev=964239&view=rev
Log:
WIP OODT-15
Add test cases for the org.apache.oodt.commons.io classes.

Added:
    incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/
    incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/FixedBufferOutputStreamTest.java
    incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullInputStreamTest.java
    incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullOutputStreamTest.java

Added: incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/FixedBufferOutputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/FixedBufferOutputStreamTest.java?rev=964239&view=auto
==============================================================================
--- incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/FixedBufferOutputStreamTest.java (added)
+++ incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/FixedBufferOutputStreamTest.java Wed Jul 14 22:52:10 2010
@@ -0,0 +1,183 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements.  See the NOTICE.txt 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.oodt.commons.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Arrays;
+import junit.framework.TestCase;
+
+/** Unit test for the {@link FixedBufferOutputStream} class.
+ *
+ * @author Kelly
+ */
+public class FixedBufferOutputStreamTest extends TestCase {
+	/** Construct the test case for the {@link FixedBufferOutputStream} class.
+	 *
+	 * @param name Case name.
+	 */
+	public FixedBufferOutputStreamTest(String name) {
+		super(name);
+	}
+
+	/** Test the fixed buffer output stream's methods with various illegal arguments.
+	 */
+	public void testIllegalArgs() throws IOException {
+		try {
+			new FixedBufferOutputStream(-1);
+			fail("Illegal size failed to throw exception");
+		} catch (IllegalArgumentException good) {}
+		FixedBufferOutputStream out = new FixedBufferOutputStream(100);
+		try {
+			out.write(null);
+			fail("Writing a null byte array failed to throw exception");
+		} catch (NullPointerException good) {}
+		try {
+			out.write(null, 0, 10);
+			fail("Writing a null byte array failed to throw exception");
+		} catch (NullPointerException good) {}
+		try {
+			out.write(TEST_DATA, -1, 10);
+			fail("Writing with negative offset failed to throw exception");
+		} catch (IndexOutOfBoundsException good) {}
+		try {
+			out.write(TEST_DATA, TEST_DATA.length + 1, 10);
+			fail("Writing with offset past end of array failed to throw exception");
+		} catch (IndexOutOfBoundsException good) {}
+		try {
+			out.write(TEST_DATA, 0, -1);
+			fail("Writing with negative length array failed to throw exception");
+		} catch (IndexOutOfBoundsException good) {}
+		try {
+			out.write(TEST_DATA, 2, 5);
+			fail("Writing with offset and length exceeding end of array failed to throw exception");
+		} catch (IndexOutOfBoundsException good) {}
+	}
+
+	/** Test a stream with space for no bytes at all.
+	 */
+	public void testZeroSizeStream() throws IOException {
+		FixedBufferOutputStream out = new FixedBufferOutputStream(0);			    // Space for no byte at all
+		out.write(1);									    // Write a 1 
+		out.write(2);									    // ...and a 2
+		out.write(3);									    // ...and a 3
+		out.write(TEST_DATA);								    // ...and a byte array
+		byte[] buffer = out.getBuffer();						    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(0, buffer.length);							    // But it should be empty
+	}
+
+	/** Test a stream under various conditions.
+	 */
+	public void testStream() throws IOException {
+		FixedBufferOutputStream out = new FixedBufferOutputStream(10);			    // Space for 10 bytes
+		byte[] buffer = out.getBuffer();						    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(0, buffer.length);							    // Nothing written => 0 length
+		out.write(1);									    // [1]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(1, buffer.length);							    // Only 1 byte so far
+		assertEquals(1, buffer[0]);							    // And it should be a 1
+		out.write(2);									    // [1,2]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(2, buffer.length);							    // Two bytes so far
+		assertEquals(1, buffer[0]);							    // And it should be a 1...
+		assertEquals(2, buffer[1]);							    // ...and a 2
+		out.write(3);									    // [1,2,3]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(3, buffer.length);							    // 3 bytes so far
+		assertEquals(1, buffer[0]);							    // They are 1
+		assertEquals(2, buffer[1]);							    // ...and 2
+		assertEquals(3, buffer[2]);							    // ...and 3
+
+		out.write(TEST_DATA);								    // [1,2,3,8,9,10,11]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(7, buffer.length);							    // 7 bytes so far, right?
+		assertEquals(1, buffer[0]);							    // They are 1
+		assertEquals(2, buffer[1]);							    // ...and 2
+		assertEquals(3, buffer[2]);							    // ...and 3
+		assertEquals(8, buffer[3]);							    // ...and 8
+		assertEquals(9, buffer[4]);							    // ...and 9
+		assertEquals(10, buffer[5]);							    // ...and 10
+		assertEquals(11, buffer[6]);							    // ...and 11
+
+		out.write(TEST_DATA);								    // [2,3,8,9,10,11,8,9,10,11]
+		out.write(TEST_DATA, 1, 2);							    // [8,9,10,11,8,9,10,11,9,10]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(10, buffer.length);						    // Full buffer
+		assertEquals(8, buffer[0]);							    // They are 8
+		assertEquals(9, buffer[1]);							    // ...and 9
+		assertEquals(10, buffer[2]);							    // ...and 10
+		assertEquals(11, buffer[3]);							    // ...and 11
+		assertEquals(8, buffer[4]);							    // ...and 8
+		assertEquals(9, buffer[5]);							    // ...and 9
+		assertEquals(10, buffer[6]);							    // ...and 10
+		assertEquals(11, buffer[7]);							    // ...and 11
+		assertEquals(9, buffer[8]);							    // ...and 9
+		assertEquals(10, buffer[9]);							    // ...and 10
+
+		out.write(TEST_DATA);								    // [8,9,10,11,9,10,8,9,10,11]
+		out.write(TEST_DATA, 0, 1);							    // [9,10,11,9,10,8,9,10,11,8]
+		out.write(42);									    // [10,11,9,10,8,9,10,11,8,42]
+		out.write(99);									    // [11,9,10,8,9,10,11,8,42,99]
+		out.write(TEST_DATA, 1, 3);							    // [8,9,10,11,8,42,99,9,10,11]
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertEquals(10, buffer.length);						    // Full buffer
+		assertEquals(8, buffer[0]);							    // They are 8
+		assertEquals(9, buffer[1]);							    // ...and 9
+		assertEquals(10, buffer[2]);							    // ...and 10
+		assertEquals(11, buffer[3]);							    // ...and 11
+		assertEquals(8, buffer[4]);							    // ...and 8
+		assertEquals(42, buffer[5]);							    // ...and 42
+		assertEquals(99, buffer[6]);							    // ...and 99
+		assertEquals(9, buffer[7]);							    // ...and 9
+		assertEquals(10, buffer[8]);							    // ...and 10
+		assertEquals(11, buffer[9]);							    // ...and 11
+
+		byte[] array = new byte[buffer.length];						    // New test data array that's
+		System.arraycopy(buffer, 0, array, 0, buffer.length);				    // the same as the current buf
+		out.write(array);								    // ...and write that
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertTrue(Arrays.equals(array, buffer));					    // Should get same result
+
+		byte[] big = new byte[buffer.length * 3];					    // More new test data array
+		System.arraycopy(buffer, 0, big, 0, buffer.length);				    // ...and add some data
+		System.arraycopy(buffer, 0, big, buffer.length, buffer.length);			    // ...and more data
+		System.arraycopy(buffer, 0, big, buffer.length * 2, buffer.length);		    // ...and LOTS of data
+		out.write(big, buffer.length, buffer.length * 2);				    // Write the last third of it
+		buffer = out.getBuffer();							    // Get the buffer
+		assertNotNull(buffer);								    // It should never be null
+		assertTrue(Arrays.equals(array, buffer));					    // Should get same result!
+
+		out.close();									    // Close it
+		try {										    // ...and try...
+			out.write(1);								    // ...to write to it
+			fail("Writing to a closed stream failed to throw an exception");	    // Shouldn't get here
+		} catch (IOException good) {}							    // Should get here
+	}
+
+	/** Some test data in an array. */
+	private static final byte[] TEST_DATA = new byte[] {
+		(byte) 8, (byte) 9, (byte) 10, (byte) 11
+	};
+}

Added: incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullInputStreamTest.java?rev=964239&view=auto
==============================================================================
--- incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullInputStreamTest.java (added)
+++ incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullInputStreamTest.java Wed Jul 14 22:52:10 2010
@@ -0,0 +1,67 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements.  See the NOTICE.txt 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.oodt.commons.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import junit.framework.TestCase;
+
+/**
+ * Unit test the {@link NullInputStream} class.
+ *
+ * @author Kelly
+ */ 
+public class NullInputStreamTest extends TestCase {
+        /**
+	 * Creates a new <code>NullInputStreamTest</code> instance.
+	 *
+	 * @param name Test case name.
+	 */
+	public NullInputStreamTest(String name) {
+                super(name);
+        }
+
+        protected void setUp() throws Exception {
+                in = new NullInputStream();
+        }
+
+        public void testClosing() {
+                try {
+                        in.close();
+                } catch (IOException ex) {
+                        fail("Should not throw an exception on close: " + ex.getMessage());
+                }
+                try {
+                        in.read();
+                        fail("Should not be able to read a closed stream");
+                } catch (IOException ignore) {}
+                try {
+                        in.close();
+                        fail("Should not be able to close a closed stream");
+                } catch (IOException ignore) {}
+        }
+
+        public void testReading() {
+                try {
+                        assertEquals(-1, in.read());
+                } catch (IOException ex) {
+                        fail("Should not throw an exception on reading: " + ex.getMessage());
+                }
+        }
+
+        /** The {@link NullInputStream} we're testing. */
+        private NullInputStream in;
+}

Added: incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullOutputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullOutputStreamTest.java?rev=964239&view=auto
==============================================================================
--- incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullOutputStreamTest.java (added)
+++ incubator/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/NullOutputStreamTest.java Wed Jul 14 22:52:10 2010
@@ -0,0 +1,79 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements.  See the NOTICE.txt 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.oodt.commons.io;
+
+import java.io.*;
+import java.util.*;
+import junit.framework.*;
+
+/** Unit test the {@link NullOutputStream} class.
+ *
+ * @author Kelly
+ */ 
+public class NullOutputStreamTest extends TestCase {
+	/** Construct the test case for the {@link NullOutputStream} class. */
+	public NullOutputStreamTest(String name) {
+		super(name);
+	}
+
+	protected void setUp() throws Exception {
+		out = new NullOutputStream();
+	}
+
+	public void testClosing() {
+		try {
+			out.close();
+		} catch (IOException ex) {
+			fail("Should not throw an exception on close: " + ex.getMessage());
+		}
+		try {
+			out.write(1);
+			fail("Should not be able to write to a closed stream");
+		} catch (IOException ignore) {}
+		try {
+			out.flush();
+			fail("Should not be able to flush a closed stream");
+		} catch (IOException ignore) {}
+		try {
+			out.close();
+			fail("Should not be able to close a closed stream");
+		} catch (IOException ignore) {}
+	}
+
+	public void testFlush() {
+		try {
+			out.flush();
+		} catch (IOException ex) {
+			fail("Should not throw an exception on flush: " + ex.getMessage());
+		}
+	}
+
+	public void testWriting() {
+		try {
+			byte[] array = new byte[]{(byte)0, (byte)1, (byte)2};
+			out.write(1);
+			out.write(array);
+			out.write(array, 1, 1);
+		} catch (IOException ex) {
+			fail("Should not throw an exception on writing: " + ex.getMessage());
+		}
+	}
+
+	/** The {@link NullOutputStream} we're testing. */
+	private NullOutputStream out;
+
+}
+