Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 25103 invoked from network); 13 Oct 2007 01:22:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Oct 2007 01:22:37 -0000 Received: (qmail 83844 invoked by uid 500); 13 Oct 2007 01:22:23 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 83786 invoked by uid 500); 13 Oct 2007 01:22:23 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 83777 invoked by uid 99); 13 Oct 2007 01:22:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Oct 2007 18:22:23 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 13 Oct 2007 01:22:34 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 812721A983A; Fri, 12 Oct 2007 18:22:14 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r584357 - in /commons/proper/io/trunk: ./ src/java/org/apache/commons/io/input/ src/java/org/apache/commons/io/output/ src/test/org/apache/commons/io/input/ src/test/org/apache/commons/io/output/ Date: Sat, 13 Oct 2007 01:16:51 -0000 To: commits@commons.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071013012214.812721A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: niallp Date: Fri Oct 12 18:16:14 2007 New Revision: 584357 URL: http://svn.apache.org/viewvc?rev=584357&view=rev Log: IO-122 add Helper classes for controlling closing of streams - thanks to Jukka Zitting Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java (with props) commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java (with props) commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java (with props) commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java (with props) commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java (with props) commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java (with props) commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java (with props) commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java (with props) commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java (with props) commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java (with props) Modified: commons/proper/io/trunk/RELEASE-NOTES.txt commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java Modified: commons/proper/io/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=584357&r1=584356&r2=584357&view=diff ============================================================================== --- commons/proper/io/trunk/RELEASE-NOTES.txt (original) +++ commons/proper/io/trunk/RELEASE-NOTES.txt Fri Oct 12 18:16:14 2007 @@ -41,6 +41,14 @@ - RegexFilter [IO-74] - Add new regular expression file filter implementation +- New Closed Input/Output stream implementations [IO-122] + - AutoCloseInputStream - automatically closes and discards the underlying input stream + - ClosedInputStream - returns -1 for any read attempts + - ClosedOutputStream - throws an IOException for any write attempts + - CloseShieldInputStream - prevents the underlying input stream from being closed. + - CloseShieldOutputStream - prevents the underlying output stream from being closed. + + Feedback -------- Open source works best when you give feedback: Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,117 @@ +/* + * 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.commons.io.input; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Proxy stream that closes and discards the underlying stream as soon as the + * end of input has been reached or when the stream is explicitly closed. + * Not even a reference to the underlying stream is kept after it has been + * closed, so any allocated in-memory buffers can be freed even if the + * client application still keeps a reference to the proxy stream. + *

+ * This class is typically used to release any resources related to an open + * stream as soon as possible even if the client application (by not explicitly + * closing the stream when no longer needed) or the underlying stream (by not + * releasing resources once the last byte has been read) do not do that. + * + * @since Commons IO 1.4 + */ +public class AutoCloseInputStream extends ProxyInputStream { + + /** + * Creates an automatically closing proxy for the given input stream. + * + * @param in underlying input stream + */ + public AutoCloseInputStream(InputStream in) { + super(in); + } + + /** + * Closes the underlying input stream and replaces the reference to it + * with a {@link ClosedInputStream} instance. + *

+ * This method is automatically called by the read methods when the end + * of input has been reached. + *

+ * Note that it is safe to call this method any number of times. The original + * underlying input stream is closed and discarded only once when this + * method is first called. + * + * @throws IOException if the underlying input stream can not be closed + */ + public void close() throws IOException { + in.close(); + in = new ClosedInputStream(); + } + + /** + * Reads and returns a single byte from the underlying input stream. + * If the underlying stream returns -1, the {@link #close()} method is + * called to automatically close and discard the stream. + * + * @return next byte in the stream, or -1 if no more bytes are available + * @throws IOException if the stream could not be read or closed + */ + public int read() throws IOException { + int n = in.read(); + if (n == -1) { + close(); + } + return n; + } + + /** + * Reads and returns bytes from the underlying input stream to the given + * buffer. If the underlying stream returns -1, the {@link #close()} method + * i called to automatically close and discard the stream. + * + * @param b buffer to which bytes from the stream are written + * @return number of bytes read, or -1 if no more bytes are available + * @throws IOException if the stream could not be read or closed + */ + public int read(byte[] b) throws IOException { + int n = in.read(b); + if (n == -1) { + close(); + } + return n; + } + + /** + * Reads and returns bytes from the underlying input stream to the given + * buffer. If the underlying stream returns -1, the {@link #close()} method + * i called to automatically close and discard the stream. + * + * @param b buffer to which bytes from the stream are written + * @param off start offset within the buffer + * @param len maximum number of bytes to read + * @return number of bytes read, or -1 if no more bytes are available + * @throws IOException if the stream could not be read or closed + */ + public int read(byte[] b, int off, int len) throws IOException { + int n = in.read(b, off, len); + if (n == -1) { + close(); + } + return n; + } + +} Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/AutoCloseInputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,51 @@ +/* + * 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.commons.io.input; + +import java.io.InputStream; + +/** + * Proxy stream that prevents the underlying input stream from being closed. + *

+ * This class is typically used in cases where an input stream needs to be + * passed to a component that wants to explicitly close the stream even if + * more input would still be available to other components. + * + * @since Commons IO 1.4 + */ +public class CloseShieldInputStream extends ProxyInputStream { + + /** + * Creates a proxy that shields the given input stream from being + * closed. + * + * @param in underlying input stream + */ + public CloseShieldInputStream(InputStream in) { + super(in); + } + + /** + * Replaces the underlying input stream with a {@link ClosedInputStream} + * sentinel. The original input stream will remain open, but this proxy + * will appear closed. + */ + public void close() { + in = new ClosedInputStream(); + } + +} Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/CloseShieldInputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,42 @@ +/* + * 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.commons.io.input; + +import java.io.InputStream; + +/** + * Closed input stream. This stream returns -1 to all attempts to read + * something from the stream. + *

+ * Typically uses of this class include testing for corner cases in methods + * that accept input streams and acting as a sentinel value instead of a + * null input stream. + * + * @since Commons IO 1.4 + */ +public class ClosedInputStream extends InputStream { + + /** + * Returns -1 to indicate that the stream is closed. + * + * @return always -1 + */ + public int read() { + return -1; + } + +} Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/ClosedInputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,51 @@ +/* + * 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.commons.io.output; + +import java.io.OutputStream; + +/** + * Proxy stream that prevents the underlying output stream from being closed. + *

+ * This class is typically used in cases where an output stream needs to be + * passed to a component that wants to explicitly close the stream even if + * other components would still use the stream for output. + * + * @since Commons IO 1.4 + */ +public class CloseShieldOutputStream extends ProxyOutputStream { + + /** + * Creates a proxy that shields the given output stream from being + * closed. + * + * @param out underlying output stream + */ + public CloseShieldOutputStream(OutputStream out) { + super(out); + } + + /** + * Replaces the underlying output stream with a {@link ClosedOutputStream} + * sentinel. The original output stream will remain open, but this proxy + * will appear closed. + */ + public void close() { + out = new ClosedOutputStream(); + } + +} Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/output/CloseShieldOutputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,44 @@ +/* + * 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.commons.io.output; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Closed output stream. This stream throws an exception on all attempts to + * write something to the stream. + *

+ * Typically uses of this class include testing for corner cases in methods + * that accept an output stream and acting as a sentinel value instead of + * a null output stream. + * + * @since Commons IO 1.4 + */ +public class ClosedOutputStream extends OutputStream { + + /** + * Throws an {@link IOException} to indicate that the stream is closed. + * + * @param b ignored + * @throws IOException always thrown + */ + public void write(int b) throws IOException { + throw new IOException("write(" + b + ") failed: stream is closed"); + } + +} Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/output/ClosedOutputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,104 @@ +/* + * 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.commons.io.input; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.TestCase; + +/** + * JUnit Test Case for {@link AutoCloseInputStream}. + */ +public class AutoCloseInputStreamTest extends TestCase { + + private byte[] data; + + private InputStream stream; + + private boolean closed; + + protected void setUp() { + data = new byte[] { 'x', 'y', 'z' }; + stream = new AutoCloseInputStream(new ByteArrayInputStream(data) { + public void close() { + closed = true; + } + }); + closed = false; + } + + /** + * Test the close() method. + */ + public void testClose() throws IOException { + stream.close(); + assertTrue("closed", closed); + assertEquals("read()", -1, stream.read()); + } + + + /** + * Test the read() method. + */ + public void testRead() throws IOException { + for (int i = 0; i < data.length; i++) { + assertEquals("read()", data[i], stream.read()); + assertFalse("closed", closed); + } + assertEquals("read()", -1, stream.read()); + assertTrue("closed", closed); + } + + /** + * Test the read(b) method. + */ + public void testReadBuffer() throws IOException { + byte[] b = new byte[data.length * 2]; + int total = 0; + for (int n = 0; n != -1; n = stream.read(b)) { + assertFalse("closed", closed); + for (int i = 0; i < n; i++) { + assertEquals("read(b)", data[total + i], b[i]); + } + total += n; + } + assertEquals("read(b)", data.length, total); + assertTrue("closed", closed); + assertEquals("read(b)", -1, stream.read(b)); + } + + /** + * Test the read(b, off, len) method. + */ + public void testReadBufferOffsetLength() throws IOException { + byte[] b = new byte[data.length * 2]; + int total = 0; + for (int n = 0; n != -1; n = stream.read(b, total, b.length - total)) { + assertFalse("closed", closed); + total += n; + } + assertEquals("read(b, off, len)", data.length, total); + for (int i = 0; i < data.length; i++) { + assertEquals("read(b, off, len)", data[i], b[i]); + } + assertTrue("closed", closed); + assertEquals("read(b, off, len)", -1, stream.read(b, 0, b.length)); + } + +} Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/AutoCloseInputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,59 @@ +/* + * 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.commons.io.input; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.TestCase; + +/** + * JUnit Test Case for {@link CloseShieldInputStream}. + */ +public class CloseShieldInputStreamTest extends TestCase { + + private byte[] data; + + private InputStream original; + + private InputStream shielded; + + private boolean closed; + + protected void setUp() { + data = new byte[] { 'x', 'y', 'z' }; + original = new ByteArrayInputStream(data) { + public void close() { + closed = true; + } + }; + shielded = new CloseShieldInputStream(original); + closed = false; + } + + /** + * Test the close() method. + */ + public void testClose() throws IOException { + shielded.close(); + assertFalse("closed", closed); + assertEquals("read()", -1, shielded.read()); + assertEquals("read()", data[0], original.read()); + } + +} Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/CloseShieldInputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,33 @@ +/* + * 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.commons.io.input; + +import junit.framework.TestCase; + +/** + * JUnit Test Case for {@link ClosedInputStream}. + */ +public class ClosedInputStreamTest extends TestCase { + + /** + * Test the read() method. + */ + public void testRead() { + assertEquals("read()", -1, new ClosedInputStream().read()); + } + +} Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/ClosedInputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java?rev=584357&r1=584356&r2=584357&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java (original) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java Fri Oct 12 18:16:14 2007 @@ -33,7 +33,10 @@ public static Test suite() { TestSuite suite = new TestSuite("IO Utilities - input"); + suite.addTest(new TestSuite(AutoCloseInputStreamTest.class)); suite.addTest(new TestSuite(ClassLoaderObjectInputStreamTest.class)); + suite.addTest(new TestSuite(ClosedInputStreamTest.class)); + suite.addTest(new TestSuite(CloseShieldInputStreamTest.class)); suite.addTest(new TestSuite(CountingInputStreamTest.class)); suite.addTest(new TestSuite(NullInputStreamTest.class)); suite.addTest(new TestSuite(NullReaderTest.class)); Added: commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,62 @@ +/* + * 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.commons.io.output; + +import java.io.IOException; +import java.io.OutputStream; + +import junit.framework.TestCase; + +/** + * JUnit Test Case for {@link CloseShieldOutputStream}. + */ +public class CloseShieldOutputStreamTest extends TestCase { + + private ByteArrayOutputStream original; + + private OutputStream shielded; + + private boolean closed; + + protected void setUp() { + original = new ByteArrayOutputStream() { + public void close() { + closed = true; + } + }; + shielded = new CloseShieldOutputStream(original); + closed = false; + } + + /** + * Test the close() method. + */ + public void testClose() throws IOException { + shielded.close(); + assertFalse("closed", closed); + try { + shielded.write('x'); + fail("write(b)"); + } catch (IOException e) { + // expected + } + original.write('y'); + assertEquals(1, original.size()); + assertEquals('y', original.toByteArray()[0]); + } + +} Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/output/CloseShieldOutputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java?rev=584357&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java Fri Oct 12 18:16:14 2007 @@ -0,0 +1,40 @@ +/* + * 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.commons.io.output; + +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * JUnit Test Case for {@link ClosedOutputStream}. + */ +public class ClosedOutputStreamTest extends TestCase { + + /** + * Test the write(b) method. + */ + public void testRead() { + try { + new ClosedOutputStream().write('x'); + fail("write(b)"); + } catch (IOException e) { + // expected + } + } + +} Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/output/ClosedOutputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java?rev=584357&r1=584356&r2=584357&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java (original) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java Fri Oct 12 18:16:14 2007 @@ -34,6 +34,8 @@ public static Test suite() { TestSuite suite = new TestSuite("IO Utilities - output"); suite.addTest(new TestSuite(ByteArrayOutputStreamTestCase.class)); + suite.addTest(new TestSuite(ClosedOutputStreamTest.class)); + suite.addTest(new TestSuite(CloseShieldOutputStreamTest.class)); suite.addTest(new TestSuite(CountingOutputStreamTest.class)); suite.addTest(new TestSuite(DeferredFileOutputStreamTest.class)); suite.addTest(new TestSuite(LockableFileWriterTest.class));