devinsy-utils/tests/CmdExecTester.java

99 lines
2.1 KiB
Java
Raw Normal View History

2010-06-17 21:34:38 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.cmdexec.StreamGobbler;
/**
*
*/
2013-06-20 01:08:32 +02:00
class CmdExecTester
{
2010-06-17 21:34:38 +02:00
static private org.apache.log4j.Logger logger;
2013-06-20 01:08:32 +02:00
static
{
2010-06-17 21:34:38 +02:00
// Initialize logger.
org.apache.log4j.Logger logger = null;
org.apache.log4j.BasicConfigurator.configure();
2010-06-17 21:34:38 +02:00
logger = org.apache.log4j.Logger.getRootLogger();
// logger.setLevel (org.apache.log4j.Level.INFO);
logger.setLevel(org.apache.log4j.Level.INFO);
2010-06-17 21:34:38 +02:00
logger.info("Enter");
2010-06-17 21:34:38 +02:00
//
logger.info("Set the log file format...");
2010-06-17 21:34:38 +02:00
// log =
// org.apache.log4j.Category.getInstance(Application.class.getName());
logger.info("... done.");
2010-06-17 21:34:38 +02:00
logger.debug("Exit");
logger = org.apache.log4j.Logger.getLogger(CmdExecTester.class);
2010-06-17 21:34:38 +02:00
}
/**
*
*/
2013-06-20 01:08:32 +02:00
public static String check(final String title, final StringBuffer source, final String model)
{
2010-06-17 21:34:38 +02:00
String result;
2013-06-20 01:08:32 +02:00
if (source.indexOf(model) == -1)
{
result = String.format("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model;
2010-06-17 21:34:38 +02:00
2013-06-20 01:08:32 +02:00
}
else
{
result = String.format("%-40s [ OK ] ", title);
2010-06-17 21:34:38 +02:00
}
//
return (result);
}
/**
*
2010-06-17 21:34:38 +02:00
*/
2013-06-20 01:08:32 +02:00
public static void main(final String[] args)
{
System.out.println("Automatic test action for CmdExec!");
2013-06-20 01:08:32 +02:00
Matcher m = Pattern.compile("^#\\sowner:\\s([a-z_][a-z0-9_-]*)$").matcher("# owner: cpm");
m.matches();
// System.out.println("owner=[" + m.matches() + "]");
System.out.println("owner=[" + m.groupCount() + "]");
System.out.println("owner=[" + m.group(1) + "]");
// test1();
2010-06-17 21:34:38 +02:00
}
2010-06-17 21:34:38 +02:00
/**
*
*/
2013-06-20 01:08:32 +02:00
public static void test1()
{
try
{
2010-06-17 21:34:38 +02:00
System.out.println("Launch ...");
// String command = "/bin/sort -r /etc/passwd";
2013-06-20 01:08:32 +02:00
String[] command = { "/bin/sort", "-r", "/etc/passwd" };
2010-06-17 21:34:38 +02:00
2013-06-20 01:08:32 +02:00
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
2010-06-17 21:34:38 +02:00
System.out.println("exitVal=[" + cmd.getExitValue() + "]");
System.out.println("out=[" + cmd.getOutStream() + "]");
System.out.println("err=[" + cmd.getErrStream() + "]");
2013-06-20 01:08:32 +02:00
}
catch (Exception exception)
{
2010-06-17 21:34:38 +02:00
exception.printStackTrace();
logger.info("ERRRO=" + exception);
}
}
}