/*
 * The MIT License
 *
 * Copyright 2012 Jesse Glick.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package hudson.cli;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import hudson.model.FreeStyleProject;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Locale;
import org.apache.commons.io.input.NullInputStream;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockFolder;

import static org.hamcrest.MatcherAssert.assertThat;

import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;

@SuppressWarnings("DM_DEFAULT_ENCODING")
public class CopyJobCommandTest {

    @Rule public JenkinsRule j = new JenkinsRule();

    @Test public void copyBetweenFolders() throws Exception {
        MockFolder dir1 = j.createFolder("dir1");
        MockFolder dir2 = j.createFolder("dir2");
        FreeStyleProject p = dir1.createProject(FreeStyleProject.class, "p1");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream outS = new PrintStream(out);
        int result = new CopyJobCommand().main(Arrays.asList("dir1/p1", "dir2/p2"), Locale.ENGLISH, new NullInputStream(0), outS, outS);
        outS.flush();
        assertEquals(out.toString(), 0, result);
        assertEquals("", out.toString());
        assertNotNull(j.jenkins.getItemByFullName("dir2/p2"));
        // TODO test copying from/to root, or into nonexistent folder
    }

    // hold off build until saved only makes sense on the UI with config screen shown after copying;
    // expect the CLI copy command to leave the job buildable
    @Test public void copiedJobIsBuildable() throws Exception {
        FreeStyleProject p1 = j.createFreeStyleProject();
        String copiedProjectName = "p2";

        CLICommandInvoker.Result result = new CLICommandInvoker(j, new CopyJobCommand())
                .invokeWithArgs(p1.getName(), copiedProjectName);

        assertThat("Command expected to succeed; " + result.stderr() + ' ' + result.stdout(), result, succeeded());
        assertThat("stdout empty", result, hasNoStandardOutput());
        assertThat("stderr empty", result, hasNoErrorOutput());

        FreeStyleProject p2 = (FreeStyleProject)j.jenkins.getItem(copiedProjectName);

        assertNotNull(p2);
        assertTrue(p2.isBuildable());
    }

}

@@ -1256,176 +1256,8 @@
 ort 
-java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.Arrays;%0Aimport java.util.Locale;%0Aimport org.apache.commons.io.input.NullInputStream;%0Aimport 
 stat
@@ -1277,16 +1277,41 @@
 Assert.*
+;%0Aimport org.junit.Before
 ;%0Aimport
@@ -1819,16 +1819,162 @@
 sRule();
+%0A    private CLICommandInvoker command;%0A%0A    @Before public void setUp() %7B%0A        command = new CLICommandInvoker(j, new CopyJobCommand());%0A    %7D
 %0A%0A    @T
@@ -2208,24 +2208,25 @@
  %22p1%22);%0A
+%0A
         
 ByteArra
@@ -2221,271 +2221,128 @@
     
-ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        PrintStream outS = new PrintStream(out);%0A        int result = new CopyJobCommand().main(Arrays.asList(%22dir1/p1%22, %22dir2/p2%22), Locale.ENGLISH, new NullInputStream(0), outS, outS);%0A        outS.flush
+CLICommandInvoker.Result result = command.invokeWithArgs(%22dir1/p1%22, %22dir2/p2%22);%0A%0A        assertThat(result, succeeded
 ()
+)
 ;%0A  
@@ -2357,85 +2357,94 @@
 sert
-Equals(out.toString(), 0, result);%0A        assertEquals(%22%22, out.toString
+That(result, hasNoStandardOutput());%0A        assertThat(result, hasNoErrorOutput
 ());%0A
+%0A
     
@@ -2952,71 +2952,15 @@
 t = 
-new CLICommandInvoker(j, new CopyJobCommand())%0A                
+command
 .inv
@@ -3029,83 +3029,8 @@
 hat(
-%22Command expected to succeed; %22 + result.stderr() + ' ' + result.stdout(), 
 resu
@@ -3070,24 +3070,8 @@
 hat(
-%22stdout empty%22, 
 resu
@@ -3121,24 +3121,8 @@
 hat(
-%22stderr empty%22, 
 resu

Refactor CopyJobCommandTest