devinsy-strings/test/CmdExecSandbox.java

92 lines
2 KiB
Java
Raw Permalink Normal View History

2013-06-26 16:40:18 +02:00
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2013-06-25 02:31:19 +02:00
import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.cmdexec.StreamGobbler;
/**
*
*/
class CmdExecSandbox
2013-06-25 02:31:19 +02:00
{
2013-06-26 16:40:18 +02:00
static private final Logger logger;
2013-06-25 02:31:19 +02:00
static
{
// Initialize logger.
org.apache.log4j.BasicConfigurator.configure();
2013-06-26 16:40:18 +02:00
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
2013-06-25 02:31:19 +02:00
logger = LoggerFactory.getLogger(CmdExecSandbox.class);
2013-06-25 02:31:19 +02:00
logger.info("Enter");
//
logger.info("Set the log file format...");
2013-06-26 16:40:18 +02:00
org.apache.log4j.Logger defaultLogger = org.apache.log4j.Logger.getRootLogger();
defaultLogger.removeAllAppenders();
defaultLogger.addAppender(new ConsoleAppender(new PatternLayout("%d{ISO8601} - dutils [%-5p] %34.34c.%-25M - %m%n")));
2013-06-25 02:31:19 +02:00
logger.info("... done.");
logger.debug("Exit");
}
/**
*
*/
public static String check(final String title, final StringBuffer source, final String model)
{
String result;
if (source.indexOf(model) == -1)
{
result = String.format("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model;
}
else
{
result = String.format("%-40s [ OK ] ", title);
}
//
return (result);
}
/**
*
*/
public static void main(final String[] args)
{
System.out.println("Automatic test action for CmdExec!");
test1();
}
/**
*
*/
public static void test1()
{
try
{
System.out.println("Launch ...");
// String command = "/bin/sort -r /etc/passwd";
String[] command = { "/bin/sort", "-r", "/etc/passwd" };
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
System.out.println("exitVal=[" + cmd.getExitValue() + "]");
System.out.println("out=[" + cmd.getOutStream() + "]");
System.out.println("err=[" + cmd.getErrStream() + "]");
}
catch (Exception exception)
{
exception.printStackTrace();
logger.info("ERRRO=" + exception);
}
}
}