Skip to content

Commit

Permalink
Allow per-version input and output.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Sep 13, 2011
1 parent bdb0302 commit c7c1d67
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
54 changes: 40 additions & 14 deletions snippet/src/test/java/ExamplesIT.java
Expand Up @@ -18,12 +18,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import junitx.framework.FileAssert;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.environment.EnvironmentUtils;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -81,6 +84,7 @@ public static Collection<Object[]> data() {
private File example; // parameter
private File actualOutputDir = new File(PROJECT_BASE_DIR, "output");
private Map<String, String> env;
private String version;

public ExamplesIT(File example) {
this.example = example;
Expand All @@ -100,29 +104,40 @@ public void setUp() throws IOException {
env.put("HADOOP_CONF_DIR", "snippet/bin/local");
env.put("HADOOP_CLASSPATH", "hadoop-examples.jar");

String versionOut = execute("hadoop version");
for (String line : Splitter.on("\n").split(versionOut)) {
Matcher matcher = Pattern.compile("^Hadoop (.+)+$").matcher(line);
if (matcher.matches()) {
version = matcher.group(1);
}
}
assertNotNull("Version not found", version);

if (actualOutputDir.exists()) {
Files.deleteRecursively(actualOutputDir);
}
}

@Test
public void test() throws Exception {
File inputFile = new File(example, "input.txt");
File expectedOutputDir = new File(example, "output");

ByteArrayOutputStream stdout = new ByteArrayOutputStream();
try {
PumpStreamHandler psh = new PumpStreamHandler(stdout);
CommandLine cl = CommandLine.parse("/bin/bash " +
inputFile.getAbsolutePath());
DefaultExecutor exec = new DefaultExecutor();
exec.setWorkingDirectory(PROJECT_BASE_DIR);
exec.setStreamHandler(psh);
exec.execute(cl, env);
} finally {
System.out.println(stdout.toString());
File inputFile;
File expectedOutputDir;

// First look for data for the particular version
File versionedExample = new File(example, version);
if (versionedExample.exists()) {
inputFile = new File(versionedExample, "input.txt");
expectedOutputDir = new File(versionedExample, "output");
// if empty then skip
assumeTrue(inputFile.exists());
} else { // Otherwise use the standard fallback
inputFile = new File(example, "input.txt");
expectedOutputDir = new File(example, "output");
}

String systemOut = execute(inputFile.getAbsolutePath());
System.out.println(systemOut);

if (!expectedOutputDir.exists()) {
FileUtils.copyDirectory(actualOutputDir, expectedOutputDir);
fail(expectedOutputDir + " does not exist - creating.");
Expand All @@ -149,6 +164,17 @@ public void test() throws Exception {
}
}

private String execute(String commandLine) throws ExecuteException, IOException {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
CommandLine cl = CommandLine.parse("/bin/bash " + commandLine);
DefaultExecutor exec = new DefaultExecutor();
exec.setWorkingDirectory(PROJECT_BASE_DIR);
exec.setStreamHandler(psh);
exec.execute(cl, env);
return stdout.toString();
}

private File decompress(File file) throws IOException {
File decompressed = File.createTempFile(getClass().getSimpleName(), ".txt");
decompressed.deleteOnExit();
Expand Down

0 comments on commit c7c1d67

Please sign in to comment.