/*
 * Copyright (c) 2013 Jean Niklas L'orange. All rights reserved.
 *
 * The use and distribution terms for this software are covered by the
 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 * which can be found in the file LICENSE at the root of this distribution.
 *
 * By using this software in any fashion, you are agreeing to be bound by
 * the terms of this license.
 *
 * You must not remove this notice, or any other, from this software.
 */

package com.hypirion.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.Writer;
import java.io.StringWriter;
import java.io.StringReader;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;

import com.hypirion.io.ClosingPipe;

import org.junit.Test;
import static org.junit.Assert.*;

public class ClosingPipeTest {

    /**
     * Tests that a ClosingPipe closes an OutputStream after the InputStream is
     * properly consumed.
     */
    @Test(timeout=1000)
    public void testBasicStreamClosingCapabilities() throws Exception {
        String input = RandomStringUtils.random(4023);
        InputStream in = IOUtils.toInputStream(input, "UTF-8");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        CloseCheckingOutputStream wrapper = new CloseCheckingOutputStream(out);
        Pipe p = new ClosingPipe(in, wrapper);
        p.start();
        p.join();
        in.close();
        String output = out.toString("UTF-8");
        assertEquals(input, output);
        assertTrue(wrapper.isClosed);
    }

    public static class CloseCheckingOutputStream extends OutputStream {
        volatile boolean isClosed;
        final OutputStream out;

        public CloseCheckingOutputStream(OutputStream out) {
            this.out = out;
        }

        synchronized public void write(int b) throws IOException {
            out.write(b);
        }

        synchronized public void close() throws IOException {
            out.close();
            isClosed = true;
        }
    }
}

@@ -1706,463 +1706,1704 @@
     
-public static class CloseCheckingOutputStream extends OutputStream %7B%0A        volatile boolean isClosed;%0A        final OutputStream out;%0A%0A        public CloseCheckingOutputStream(OutputStream out) %7B%0A            this.out = out;%0A        %7D%0A%0A        synchronized public void write(int b) throws IOException %7B%0A            out.write(b);%0A        %7D%0A%0A        synchronized public void close() throws IOException %7B%0A            out.close();%0A            isClosed = true
+/**%0A     * Tests that a ClosingPipe closes a Writer after the Reader is properly%0A     * consumed.%0A     */%0A    @Test(timeout=1000)%0A    public void testBasicWriteClosingCapabilities() throws Exception %7B%0A        String input = RandomStringUtils.random(4023);%0A        Reader in = new StringReader(input);%0A        StringWriter out = new StringWriter();%0A        CloseCheckingReader wrapper = new CloseCheckingReader(out);%0A        Pipe p = new ClosingPipe(in, wrapper);%0A        p.start();%0A        p.join();%0A        in.close();%0A        String output = out.toString();%0A        assertEquals(input, output);%0A        assertTrue(wrapper.isClosed);%0A    %7D%0A%0A    public static class CloseCheckingOutputStream extends OutputStream %7B%0A        volatile boolean isClosed;%0A        final OutputStream out;%0A%0A        public CloseCheckingOutputStream(OutputStream out) %7B%0A            this.out = out;%0A        %7D%0A%0A        synchronized public void write(int b) throws IOException %7B%0A            out.write(b);%0A        %7D%0A%0A        synchronized public void close() throws IOException %7B%0A            out.close();%0A            isClosed = true;%0A        %7D%0A    %7D%0A%0A    public static class CloseCheckingReader extends Writer %7B%0A        volatile boolean isClosed;%0A        final Writer out;%0A%0A        public CloseCheckingReader(Writer out) %7B%0A            this.out = out;%0A        %7D%0A%0A        synchronized public void close() throws IOException %7B%0A            out.close();%0A            isClosed = true;%0A        %7D%0A%0A        synchronized public void flush() throws IOException %7B%0A            out.flush();%0A        %7D%0A%0A        synchronized public void write(char%5B%5D cbuf, int off, int len)%0A            throws IOException %7B%0A            out.write(cbuf, off, len)
 ;%0A  

Test ClosingPipe with Readers and Writers.