Commits

Ilmari Vacklin committed a075025

Add a Scala version of CommandLineIT.

Let's see if it reads better than the Java version.

Comments (0)

Files changed (5)

             </exclusions>
         </dependency>
 
+        <dependency>
+            <groupId>net.orfjackal.specsy</groupId>
+            <artifactId>specsy</artifactId>
+            <version>1.2.0</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.scala-lang</groupId>
+            <artifactId>scala-library</artifactId>
+            <version>2.9.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
                     </programs>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.scala-tools</groupId>
+                <artifactId>maven-scala-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>testCompile</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
-        
-        
     </build>
 
+    <repositories>
+        <repository>
+            <id>scala-tools.org</id>
+            <name>Scala-tools Maven2 Repository</name>
+            <url>http://scala-tools.org/repo-releases</url>
+        </repository>
+    </repositories>
+
+    <pluginRepositories>
+        <pluginRepository>
+            <id>scala-tools.org</id>
+            <name>Scala-tools Maven2 Repository</name>
+            <url>http://scala-tools.org/repo-releases</url>
+        </pluginRepository>
+    </pluginRepositories>
 </project>

src/test/java/net/wolverian/march/CommandLineIT.java

 package net.wolverian.march;
 
-import com.google.common.io.*;
 import net.wolverian.march.fixtures.*;
 import org.junit.*;
+import org.junit.experimental.runners.*;
+import org.junit.runner.*;
 
 import java.io.*;
 import java.nio.file.*;
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
 
+@RunWith(value = Enclosed.class)
 public class CommandLineIT {
-	@Rule
-	public Projects projects = new Projects();
-
-	private final Path app = Paths.get("target/appassembler/bin/march").toAbsolutePath();
-	private final Path project = projects.createMavenProject();
-
-	@Test
-	public void shouldBuildAMavenProject() throws IOException, InterruptedException {
-        final Process process = runBuild();
-        assertThat(CharStreams.toString(new InputStreamReader(process.getErrorStream())), is(""));
-        assertThat(process.exitValue(), is(0));
-		assertThat(builtFiles(project.resolve("target")), hasItem(endsWith(".jar")));
-	}
-
-    @Test
-    public void shouldPrintBuildStartedNotification() throws IOException, InterruptedException {
-        Process process = runBuild();
-        assertThat(CharStreams.toString(new InputStreamReader(process.getInputStream())), containsString("Build started"));
-    }
-
-    private Process runBuild() throws IOException, InterruptedException {
-        Process process = createBuilder().directory(project.toFile()).start();
-        process.waitFor();
-        return process;
-    }
-
-    private ProcessBuilder createBuilder() {
-        String os = System.getProperty("os.name");
-
-        if (os.startsWith("Windows")) {
-            return new ProcessBuilder(app.toString());
+    public static class SuccessfulRun extends BaseCommandLineIT {
+        @Test
+        public void shouldBuildArtifact() throws IOException {
+            assertThat(builtFiles(project.resolve("target")), hasItem(endsWith(".jar")));
         }
 
-        ProcessBuilder posixStyleBuilder = new ProcessBuilder("/bin/sh", app.toString());
+        @Test
+        public void shouldNotPrintAnythingToErrorStream() {
+            assertThat(textFrom(process.getErrorStream()), is(""));
+        }
 
-        if (os.equals("Mac OS X")) {
-            return withJava7Home(posixStyleBuilder);
+        @Test
+        public void shouldExitWithSuccessExitCode() {
+            assertThat(process.exitValue(), is(0));
         }
+    }
 
-        return posixStyleBuilder;
+    public static class AnyRun extends BaseCommandLineIT {
+        @Test
+        public void shouldPrintBuildStartedNotification() {
+            assertThat(textFrom(process.getInputStream()), containsString("Build started"));
+        }
     }
 
-    private ProcessBuilder withJava7Home(ProcessBuilder builder) {
-        builder.environment().put("JAVA_HOME", "/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home");
-        return builder;
+    @Ignore
+    public static class BaseCommandLineIT {
+        @Rule
+        public Projects projects = new Projects();
+        protected final Path project = projects.createMavenProject();
+        protected final Process process;
+
+        public BaseCommandLineIT() {
+            try {
+                process = runBuild();
+            } catch (InterruptedException | IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        private Process runBuild() throws IOException, InterruptedException {
+            Process process = Builders.createBuilder().directory(project.toFile()).start();
+            process.waitFor();
+            return process;
+        }
     }
 }

src/test/java/net/wolverian/march/CommandLineITSpec.scala

+package net.wolverian.march
+
+import fixtures._
+import Util._
+
+import net.orfjackal.specsy._
+import org.junit.runner.RunWith
+import org.junit._
+import org.junit.Assert._
+import org.hamcrest.Matchers._
+import java.io._
+import com.google.common.io._
+
+@RunWith(classOf[Specsy])
+class CommandLineITSpec extends Spec {
+  "A successful run" >> {
+    "should build the artifact" >> {
+      assertThat(builtFiles(project), hasItem(endsWith(".jar")))
+    }
+
+    "should not print anything to the error stream" >> {
+      assertThat(process.getErrorStream.asText, is(""))
+    }
+
+    "should exit with successful exit code" >> {
+      assertThat(process.exitValue, is(0))
+    }
+  }
+
+  "Any run" >> {
+    "should print a build started notification" >> {
+      assertThat(process.getInputStream.asText, containsString("Build started"))
+    }
+  }
+
+  @Rule val projects = new Projects
+  val project = projects.createMavenProject()
+  val process = {
+    val process = Builders.createBuilder().directory(project.toFile).start()
+    process.waitFor()
+    process
+  }
+
+  implicit def richInputStream(inputStream: InputStream): RichInputStream = new RichInputStream(inputStream)
+
+  class RichInputStream(inputStream: InputStream)  {
+    def asText = CharStreams.toString(new InputStreamReader(inputStream))
+  }
+}

src/test/java/net/wolverian/march/Util.java

 package net.wolverian.march;
 
+import com.google.common.io.*;
+
 import java.io.*;
 import java.nio.file.*;
+import java.nio.file.Files;
 import java.nio.file.attribute.*;
 import java.util.*;
 
 	private Util() {}
 
 	public static List<String> builtFiles(Path target) {
-        return gatherFileNamesRecursivelyIn(target);
+        return gatherFileNamesRecursivelyIn(target.resolve("target"));
 	}
 
     private static List<String> gatherFileNamesRecursivelyIn(Path target) {
 
         return files;
     }
+
+    static String textFrom(InputStream stream) {
+        try {
+            return CharStreams.toString(new InputStreamReader(stream));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }

src/test/java/net/wolverian/march/fixtures/Builders.java

+package net.wolverian.march.fixtures;
+
+import java.nio.file.*;
+
+public final class Builders {
+    private static final Path app = Paths.get("target/appassembler/bin/march").toAbsolutePath();
+
+    private Builders() {}
+
+
+    public static ProcessBuilder createBuilder() {
+        String os = System.getProperty("os.name");
+
+        if (os.startsWith("Windows")) {
+            return new ProcessBuilder(app.toString());
+        }
+
+        ProcessBuilder posixStyleBuilder = new ProcessBuilder("/bin/sh", app.toString());
+
+        if (os.equals("Mac OS X")) {
+            return withJava7Home(posixStyleBuilder);
+        }
+
+        return posixStyleBuilder;
+    }
+
+    private static ProcessBuilder withJava7Home(ProcessBuilder builder) {
+        builder.environment().put("JAVA_HOME", "/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home");
+        return builder;
+    }
+}