package SBST2015; import java.nio.file.*; import java.util.*; import java.util.regex.*; import java.io.* ; /** * For generating Junit classes that will replay generated test suites. */ public class JUnitGeneratorCmd { // check how many trace files in the direcoty match the given regex public static List match(String regexpr, String dir) throws Exception { Pattern pattern = Pattern.compile(regexpr); Path dir_ = FileSystems.getDefault().getPath(dir) ; List matches = new LinkedList() ; if (!Files.isDirectory(dir_)) return matches ; DirectoryStream stream = Files.newDirectoryStream(dir_) ; for (Path entry: stream) { String entry_ = entry.getFileName().toString() ; if (Files.isDirectory(entry)) continue ; if (entry_.startsWith(".")) continue ; Matcher m = pattern.matcher(entry_) ; if (! m.matches()) continue ; if (entry_.endsWith(".tr")) matches.add(entry_) ; } return matches ; } static private String getSimpleName(String CUT) { int k = CUT.lastIndexOf('.') ; if (k<0) return CUT ; return CUT.substring(k+1) ; } static private void save(String fname, String content) throws Exception { File file = new File(fname); FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); } static private String mkClassName(String CUTfullname, String groupName, int sequenceNr) { String s = "T3Test_" ; s += getSimpleName(CUTfullname) + "_" + groupName + "_" + sequenceNr ; return s ; } static private String mkTestClassWorker(String tracedir, String CUTfullname, String groupName, int sequenceNr, String suiteFileName ) { String s = "" ; s += "package " + getSimpleName(CUTfullname) + " ;\n\n" ; s += "import org.junit.* ;\n" ; s += "import static org.junit.Assert.* ;\n" ; s += "import Sequenic.T3.Sequence.Datatype.SUITE;\n" ; s += "import SBST2015.* ;\n" ; s += "public class " + mkClassName(CUTfullname,groupName,sequenceNr) + " {\n\n" ; s += " @Test\n" ; s += " public void test() throws Exception {\n" ; s += " SUITE S = SUITE.load(\"" + tracedir + "/" + suiteFileName + "\") ;\n" ; s += " assertTrue(MiniReplayer.test(S)) ; \n" ; s += " }\n" ; s += "}" ; return s ; } static private void mkTestClass(String whereToPutTheGeneratedFile, String tracedir, String CUTfullname, String groupName, int sequenceNr, String suiteFileName ) throws Exception { String path = whereToPutTheGeneratedFile + "/" + getSimpleName(CUTfullname) + "/" + mkClassName(CUTfullname,groupName,sequenceNr) + ".java" ; save(path,mkTestClassWorker(tracedir,CUTfullname,groupName,sequenceNr,suiteFileName)) ; System.err.println("** " + path + " is generated.") ; } static private String mkSuperTestClassWorker( String CUTfullname, List testclasses) { String s = "" ; s += "import org.junit.runner.RunWith ;\n" ; s += "import org.junit.runners.Suite ;\n" ; s += "import org.junit.runners.Suite.SuiteClasses;\n" ; s += "import " + getSimpleName(CUTfullname) + ".* ; \n\n" ; s += "@RunWith(Suite.class)\n" ; s += "@Suite.SuiteClasses({" ; int k = 0 ; for (String f : testclasses) { if (k>0) s += ",\n " ; s += "" + f + ".class" ; k++ ; } s += " })\n\n" ; s += "public class " + getSimpleName(CUTfullname) + "_AllTests { }" ; return s ; } static private void mkSuperTest ( String whereToPutTheGeneratedFile, String CUTfullname, List testclasses) throws Exception { String path = whereToPutTheGeneratedFile + "/" + getSimpleName(CUTfullname) + "_AllTests.java" ; save(path,mkSuperTestClassWorker(CUTfullname,testclasses)) ; System.err.println("** " + path + " is generated.") ; } static public void main(String[] args) throws Exception { String CUT = args[0] ; //String CUT = "SBST2015.TestMe" ; String traceDir = args[1] ; //String traceDir = "d:/workshop/t2framework/repos/benchmark/SBST2015competition/data/traces" ; String whereToPutTheGeneratedFile = args[2] ; //String whereToPutTheGeneratedFile = "d:/workshop/t2framework/repos/benchmark/SBST2015competition/T3testSrc" ; String CUT_ = Common.mkSaveFileName(CUT) ; String pattern1 = "suite_" + CUT_ + ".*" ; String pattern2 = "minOrcSuite_" + CUT_ + ".*" ; String pattern3 = "inner_suite_" + CUT_ + ".*" ; String pattern4 = "inner_minOrcSuite_" + CUT_ + ".*" ; // scan the names of the test suites: if (!traceDir.equals("null")) Common.setTraceDir(traceDir) ; List suites = match(pattern1, Common.dirToSaveTraces) ; List minOrcs = match(pattern2, Common.dirToSaveTraces) ; List inners = match(pattern3, Common.dirToSaveTraces) ; List innerMinOrcs = match(pattern4, Common.dirToSaveTraces) ; // prepare a package directory to put the test classes: String pckgName = getSimpleName(CUT) ; File theDir = new File(whereToPutTheGeneratedFile + "/" + pckgName); if (!theDir.exists()) theDir.mkdir() ; // now generate the Junit classes: int seqNr = 0 ; List namesOfGeneratedClasses = new LinkedList() ; for (String sname : suites) { mkTestClass(whereToPutTheGeneratedFile,traceDir,CUT,"suite",seqNr,sname) ; namesOfGeneratedClasses.add(mkClassName(CUT,"suite",seqNr)) ; seqNr++ ; } for (String sname : minOrcs) { mkTestClass(whereToPutTheGeneratedFile,traceDir,CUT,"minOrc",seqNr,sname) ; namesOfGeneratedClasses.add(mkClassName(CUT,"minOrc",seqNr)) ; seqNr++ ; } for (String sname : inners) { mkTestClass(whereToPutTheGeneratedFile,traceDir,CUT,"inner",seqNr,sname) ; namesOfGeneratedClasses.add(mkClassName(CUT,"inner",seqNr)) ; seqNr++ ; } for (String sname : innerMinOrcs) { mkTestClass(whereToPutTheGeneratedFile,traceDir,CUT,"inner_minOrc",seqNr,sname) ; namesOfGeneratedClasses.add(mkClassName(CUT,"inner_minOrc",seqNr)) ; seqNr++ ; } String path = "" + whereToPutTheGeneratedFile + "/../allInOneT3tests" ; theDir = new File(path); if (!theDir.exists()) theDir.mkdir() ; mkSuperTest(path,CUT,namesOfGeneratedClasses) ; System.err.println("** standard suites: " + suites.size()) ; System.err.println("** minOrc suites: " + minOrcs.size()) ; System.err.println("** inner-classes standard suites: " + inners.size()) ; System.err.println("** inner-classes minOrc suites: " + innerMinOrcs.size()) ; } }