Adds StreamGobblerTest
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/d89062b8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/d89062b8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/d89062b8 Branch: refs/heads/master Commit: d89062b86f99d2cfae0d1558c4697b23ab7d3c7c Parents: ce16828 Author: Aled Sage <[email protected]> Authored: Thu Jan 8 22:19:47 2015 +0000 Committer: Aled Sage <[email protected]> Committed: Wed Jan 14 22:38:08 2015 +0000 ---------------------------------------------------------------------- .../brooklyn/util/stream/StreamGobblerTest.java | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d89062b8/utils/common/src/test/java/brooklyn/util/stream/StreamGobblerTest.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/brooklyn/util/stream/StreamGobblerTest.java b/utils/common/src/test/java/brooklyn/util/stream/StreamGobblerTest.java new file mode 100644 index 0000000..4722829 --- /dev/null +++ b/utils/common/src/test/java/brooklyn/util/stream/StreamGobblerTest.java @@ -0,0 +1,88 @@ +/* + * 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 brooklyn.util.stream; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import org.testng.annotations.Test; + +import brooklyn.test.Asserts; + +public class StreamGobblerTest { + + @Test + public void testGobbleStream() throws Exception { + byte[] bytes = new byte[] {'a','b','c'}; + InputStream stream = new ByteArrayInputStream(bytes); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + StreamGobbler gobbler = new StreamGobbler(stream, out, null); + gobbler.start(); + try { + gobbler.join(10*1000); + assertFalse(gobbler.isAlive()); + assertEquals(new String(out.toByteArray()), "abc\n"); + } finally { + gobbler.close(); + gobbler.interrupt(); + } + } + + @Test + public void testGobbleMultiLineBlockingStream() throws Exception { + PipedOutputStream pipedOutputStream = new PipedOutputStream(); + PipedInputStream stream = new PipedInputStream(pipedOutputStream); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + StreamGobbler gobbler = new StreamGobbler(stream, out, null); + gobbler.start(); + try { + pipedOutputStream.write("line1\n".getBytes()); + assertEqualsEventually(out, "line1\n"); + + pipedOutputStream.write("line2\n".getBytes()); + assertEqualsEventually(out, "line1\nline2\n"); + + pipedOutputStream.write("line".getBytes()); + pipedOutputStream.write("3\n".getBytes()); + assertEqualsEventually(out, "line1\nline2\nline3\n"); + + pipedOutputStream.close(); + + gobbler.join(10*1000); + assertFalse(gobbler.isAlive()); + assertEquals(new String(out.toByteArray()), "line1\nline2\nline3\n"); + } finally { + gobbler.close(); + gobbler.interrupt(); + } + } + + private void assertEqualsEventually(final ByteArrayOutputStream out, final String expected) { + Asserts.succeedsEventually(new Runnable() { + public void run() { + assertEquals(new String(out.toByteArray()), expected); + }}); + } +}
