/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed 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.gradle.util;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.io.InputStream;

/**
 * A Junit rule which restores System.in at the end of the test.
 */
public class RedirectStdIn implements TestRule {

    @Override
    public Statement apply(final Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                final InputStream originalStdIn = System.in;
                try {
                    base.evaluate();
                } finally {
                    System.setIn(originalStdIn);
                }
            }
        };
    }
}

@@ -636,16 +636,121 @@
 .util;%0A%0A
+import org.gradle.internal.UncheckedException;%0Aimport org.gradle.internal.concurrent.CompositeStoppable;%0A
 import o
@@ -874,142 +874,424 @@
 io.I
-nputStream;%0A%0A/**%0A * A Junit rule which restores System.in at the end of the test.%0A */%0Apublic class RedirectStdIn implements TestRule %7B
+OException;%0Aimport java.io.InputStream;%0Aimport java.io.PipedInputStream;%0Aimport java.io.PipedOutputStream;%0A%0A/**%0A * A Junit rule which restores System.in at the end of the test.%0A *%0A * Provides a pipe for providing input to System.in in the tests%0A */%0Apublic class RedirectStdIn implements TestRule %7B%0A    private final PipedInputStream emulatedSystemIn = new PipedInputStream();%0A    private PipedOutputStream stdinPipe;
 %0A%0A  
@@ -1548,16 +1548,92 @@
 tem.in;%0A
+                initPipe();%0A                System.setIn(emulatedSystemIn);%0A
         
@@ -1780,34 +1780,509 @@
     
-%7D%0A            %7D%0A        %7D;
+    CompositeStoppable.stoppable(stdinPipe, emulatedSystemIn).stop();%0A                %7D%0A            %7D%0A        %7D;%0A    %7D%0A%0A    public PipedOutputStream getStdinPipe() %7B%0A        initPipe();%0A        return stdinPipe;%0A    %7D%0A%0A    private void initPipe() %7B%0A        if (stdinPipe == null) %7B%0A            try %7B%0A                stdinPipe = new PipedOutputStream(emulatedSystemIn);%0A            %7D catch (IOException e) %7B%0A                throw UncheckedException.throwAsUncheckedException(e);%0A            %7D%0A        %7D
 %0A   

replace System.in in tests using RedirectStdIn - pipe for feeding input to System.in