1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
/*
* ExternalProcessTest.java
*/
package org.biojava.utils.process;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import junit.extensions.RepeatedTest;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test class for {@link org.biojava.utils.process.ExternalProcess}.
* @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
* @version $Revision: 4023 $
*/
public class ExternalProcessTest extends TestCase {
/**
* Input for the more command.
*/
private static final String INPUT_MORE = "test";
/**
* Object to execute external processes.
*/
private static ExternalProcess ep = null;
/**
* OS-specific string for the more command.
*/
private static String commandMore = null;
static {
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
commandMore = "cmd /C more";
} else {
commandMore = "cat";
}
ep = new ExternalProcess();
}
/**
* Main entry point. Runs a repeated test to stress the thread allocation
* and freeing of the <code>ExternalProcess</code> class.
* @param args command line arguments.
*/
public static void main(String[] args) {
TestSuite suite = new TestSuite();
suite.addTest(new TestSuite(ExternalProcessTest.class));
junit.textui.TestRunner.run(new RepeatedTest(suite, 5000));
}
/**
* Initializes the test.
* @param name test name
*/
public ExternalProcessTest(String name) {
super(name);
}
/**
* Test the input/output handling, using input/output streams and not
* readers/writers.
* @throws IOException if execution fails.
* @throws InterruptedException if execution fails.
*/
public void testExecuteStreams() throws IOException,
InterruptedException {
ByteArrayInputStream input = new ByteArrayInputStream(
INPUT_MORE.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
ep.setCommands(commandMore);
ep.setInputHandler(new SimpleInputHandler(input, "in"));
ep.setOutputHandler(new SimpleOutputHandler(output, "out"));
ep.setErrorHandler(new SimpleOutputHandler(error, "err"));
int exitCode = ep.execute();
assertTrue(exitCode == 0);
assertEquals(INPUT_MORE, output.toString().trim());
assertEquals("", error.toString());
}
/**
* Test the input/output handling.
* @throws IOException if execution fails.
* @throws InterruptedException if execution fails.
*/
public void testExecuteMore() throws IOException,
InterruptedException {
StringWriter outputString = new StringWriter();
StringWriter errorString = new StringWriter();
StringReader inputString = new StringReader(INPUT_MORE);
ep.setCommands(commandMore);
ep.setOutputHandler(new WriterOutputHandler(outputString, "out"));
ep.setErrorHandler(new WriterOutputHandler(errorString, "err"));
ep.setInputHandler(new ReaderInputHandler(inputString, "in"));
int exitCode = ep.execute();
assertTrue(exitCode == 0);
assertEquals(INPUT_MORE, outputString.toString().trim());
assertEquals("", errorString.toString());
}
/**
* Tests the static execute method.
* @throws Exception if execution fails.
*/
public void testStaticExecute() throws Exception {
StringWriter outputString = new StringWriter();
StringWriter errorString = new StringWriter();
int exitCode = ExternalProcess.execute(commandMore, INPUT_MORE,
outputString, errorString);
assertTrue(exitCode == 0);
assertEquals(INPUT_MORE, outputString.toString().trim());
assertEquals("", errorString.toString());
}
/**
* Tests the {@link ExternalProcess#resolveCommands(String, Properties)}
* method.
*/
public void testResolveCommands() {
Properties props = new Properties();
props.put("TEST", "test");
String commands = "do %TEST% it";
commands = ExternalProcess.resolveCommands(commands, props);
assertEquals("do test it", commands);
}
/**
* Tests the {@link ExternalProcess#joinCommands(Object[])} method.
*/
public void testJoinCommands() {
String[] commandList = new String[] {"do", "test", "it"};
String commands = ExternalProcess.joinCommands(commandList);
assertEquals("do test it", commands);
}
}
|