Added unit tests for NoCloseInput/OutputStream and NoCloseReader/Writer

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/b222a67b
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/b222a67b
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/b222a67b

Branch: refs/heads/master
Commit: b222a67b38fd54afe72b2b575e70d439fb4c569a
Parents: a02a9bf
Author: Goldstein Lyor <[email protected]>
Authored: Mon Aug 13 09:55:40 2018 +0300
Committer: Goldstein Lyor <[email protected]>
Committed: Mon Aug 13 14:15:46 2018 +0300

----------------------------------------------------------------------
 .../common/util/io/NoCloseInputStreamTest.java  | 89 ++++++++++++++++++
 .../common/util/io/NoCloseOutputStreamTest.java | 69 ++++++++++++++
 .../sshd/common/util/io/NoCloseReaderTest.java  | 95 ++++++++++++++++++++
 .../sshd/common/util/io/NoCloseWriterTest.java  | 73 +++++++++++++++
 .../apache/sshd/util/test/BaseTestSupport.java  | 13 +++
 5 files changed, 339 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b222a67b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseInputStreamTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseInputStreamTest.java
 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseInputStreamTest.java
new file mode 100644
index 0000000..2990fa6
--- /dev/null
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseInputStreamTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.sshd.common.util.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Date;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NoCloseInputStreamTest extends BaseTestSupport {
+    public NoCloseInputStreamTest() {
+        super();
+    }
+
+    @Test
+    public void testCanKeepReadingAfterClose() throws IOException {
+        byte[] expected = (getClass().getName() + "#" + getCurrentTestName() + 
"@" + new Date()).getBytes(StandardCharsets.UTF_8);
+        Path dir = createTempClassFolder();
+        Path file = Files.write(dir.resolve(getCurrentTestName() + ".txt"), 
expected);
+        try (InputStream fileStream = Files.newInputStream(file);
+             InputStream shielded = new NoCloseInputStream(fileStream)) {
+            int index = 0;
+
+            for (; index < (expected.length / 2); index++) {
+                shielded.close();
+
+                int readValue = shielded.read();
+                if (readValue == -1) {
+                    fail("Premature EOF after shield read of " + index + " 
bytes");
+                }
+
+                byte expValue = expected[index];
+                byte actValue = (byte) (readValue & 0xFF);
+                if (expValue != actValue) {
+                    fail("Mismatched shielded read value after " + index + " 
bytes");
+                }
+            }
+
+            for (; index < expected.length; index++) {
+                int readValue = fileStream.read();
+                if (readValue == -1) {
+                    fail("Premature EOF after original read of " + index + " 
bytes");
+                }
+                byte expValue = expected[index];
+                byte actValue = (byte) (readValue & 0xFF);
+                if (expValue != actValue) {
+                    fail("Mismatched original read value after " + index + " 
bytes");
+                }
+            }
+
+            int readValue = shielded.read();
+            assertEquals("Shielded EOF not signalled", -1, readValue);
+
+            readValue = fileStream.read();
+            assertEquals("Original EOF not signalled", -1, readValue);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b222a67b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseOutputStreamTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseOutputStreamTest.java
 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseOutputStreamTest.java
new file mode 100644
index 0000000..fc211c3
--- /dev/null
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseOutputStreamTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.sshd.common.util.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Date;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NoCloseOutputStreamTest extends BaseTestSupport {
+    public NoCloseOutputStreamTest() {
+        super();
+    }
+
+    @Test
+    public void testCanKeepWritingAfterClose() throws IOException {
+        Path dir = createTempClassFolder();
+        Path file = dir.resolve(getCurrentTestName() + ".txt");
+        Files.deleteIfExists(file);
+
+        String expectedOutput = getClass().getName() + "#" + 
getCurrentTestName() + "@" + new Date();
+        byte[] expected = expectedOutput.getBytes(StandardCharsets.UTF_8);
+        try (OutputStream fileStream = Files.newOutputStream(file);
+             OutputStream shielded = new NoCloseOutputStream(fileStream)) {
+            int index = 0;
+            for (; index < (expected.length / 2); index++) {
+                shielded.close();
+                shielded.write(expected[index] & 0xFF);
+            }
+
+            fileStream.write(expected, index, expected.length - index);
+        }
+
+        byte[] actual = Files.readAllBytes(file);
+        String actualOutput = new String(actual, StandardCharsets.UTF_8);
+        assertEquals(expectedOutput, actualOutput);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b222a67b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseReaderTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseReaderTest.java 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseReaderTest.java
new file mode 100644
index 0000000..193aabd
--- /dev/null
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseReaderTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.sshd.common.util.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Date;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NoCloseReaderTest extends BaseTestSupport {
+    public NoCloseReaderTest() {
+        super();
+    }
+
+    @Test
+    public void testCanKeepReadingAfterClose() throws IOException {
+        String expected = getClass().getName() + "#" + getCurrentTestName() + 
"@" + new Date();
+        Path dir = createTempClassFolder();
+        Path file = Files.write(dir.resolve(getCurrentTestName() + ".txt"), 
expected.getBytes(StandardCharsets.UTF_8));
+        try (InputStream fileStream = Files.newInputStream(file);
+             Reader rdr = new InputStreamReader(fileStream, 
StandardCharsets.UTF_8);
+             Reader shielded = new NoCloseReader(rdr)) {
+            int index = 0;
+
+            int availLen = expected.length();
+            for (; index < (availLen / 2); index++) {
+                shielded.close();
+
+                int readValue = shielded.read();
+                if (readValue == -1) {
+                    fail("Premature EOF after shield read of " + index + " 
bytes");
+                }
+
+                char expValue = expected.charAt(index);
+                char actValue = (char) (readValue & 0xFFFF);
+                if (expValue != actValue) {
+                    fail("Mismatched shielded read value after " + index + " 
bytes");
+                }
+            }
+
+            for (; index < availLen; index++) {
+                int readValue = rdr.read();
+                if (readValue == -1) {
+                    fail("Premature EOF after original read of " + index + " 
bytes");
+                }
+
+                char expValue = expected.charAt(index);
+                char actValue = (char) (readValue & 0xFFFF);
+                if (expValue != actValue) {
+                    fail("Mismatched original read value after " + index + " 
bytes");
+                }
+            }
+
+            int readValue = shielded.read();
+            assertEquals("Shielded EOF not signalled", -1, readValue);
+
+            readValue = rdr.read();
+            assertEquals("Original EOF not signalled", -1, readValue);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b222a67b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseWriterTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseWriterTest.java 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseWriterTest.java
new file mode 100644
index 0000000..efc6a91
--- /dev/null
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NoCloseWriterTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.sshd.common.util.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Date;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:[email protected]";>Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NoCloseWriterTest extends BaseTestSupport {
+    public NoCloseWriterTest() {
+        super();
+    }
+
+    @Test
+    public void testCanKeepWritingAfterClose() throws IOException {
+        Path dir = createTempClassFolder();
+        Path file = dir.resolve(getCurrentTestName() + ".txt");
+        Files.deleteIfExists(file);
+
+        String expected = getClass().getName() + "#" + getCurrentTestName() + 
"@" + new Date();
+        try (OutputStream fileStream = Files.newOutputStream(file);
+             Writer w = new OutputStreamWriter(fileStream, 
StandardCharsets.UTF_8);
+                Writer shielded = new NoCloseWriter(w)) {
+            int index = 0;
+            int availLen = expected.length();
+            for (; index < (availLen / 2); index++) {
+                shielded.close();
+                shielded.write(expected.charAt(index));
+            }
+
+            w.write(expected, index, availLen - index);
+        }
+
+        byte[] actualBytes = Files.readAllBytes(file);
+        String actual = new String(actualBytes, StandardCharsets.UTF_8);
+        assertEquals(expected, actual);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/b222a67b/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java 
b/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java
index 911328a..ba58c5e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java
+++ b/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java
@@ -223,6 +223,19 @@ public abstract class BaseTestSupport extends Assert {
         return targetFolder;
     }
 
+    /**
+     * Creates a folder bearing the class's simple name under the project's 
target temporary folder
+     *
+     * @return The created folder {@link Path}
+     * @throws IOException If failed to detect or create the folder's location
+     * @see #detectTargetFolder() detectTargetFolder
+     * @see #assertHierarchyTargetFolderExists(Path, LinkOption...) 
assertHierarchyTargetFolderExists
+     */
+    protected Path createTempClassFolder() throws IOException {
+        Path tmpDir = detectTargetFolder();
+        return 
assertHierarchyTargetFolderExists(tmpDir.resolve(getClass().getSimpleName()));
+    }
+
     protected Path detectSourcesFolder() throws IllegalStateException {
         Path target = detectTargetFolder();
         Path parent = target.getParent();

Reply via email to