protected void setupStreams(ITestResult result, boolean printMethodName) { String test_name = result.getTestClass().getName(); File dir = new File(output_dir + File.separator + test_name); if (!dir.exists()) dir.mkdirs(); File _tests = new File(dir, TESTS), _stdout = new File(dir, STDOUT), _stderr = new File(dir, STDERR); try { Class<?> clazz = result.getTestClass().getRealClass(); if (!tests.containsKey(clazz)) { DataOutputStream output = new DataOutputStream(new FileOutputStream(_tests, true)); DataOutputStream tmp = tests.putIfAbsent(clazz, output); if (tmp != null) { Util.close(output); output = tmp; } } if (stdout.get() == null) stdout.set(new PrintStream(new FileOutputStream(_stdout, true))); if (stderr.get() == null) stderr.set(new PrintStream(new FileOutputStream(_stderr, true))); if (printMethodName) stdout.get().println("\n\n------------- " + getMethodName(result) + " -----------"); } catch (IOException e) { error(e.toString()); } }
protected static void process(File dir) throws IOException { File file = new File(dir, TESTS); if (!file.exists()) throw new IOException(file + " not found"); List<TestCase> test_cases = new ArrayList<>(); DataInputStream input = new DataInputStream(new FileInputStream(file)); try { for (; ; ) { TestCase test_case = new TestCase(); try { test_case.readFrom(input); test_cases.add(test_case); } catch (Exception e) { break; } } } finally { Util.close(input); } if (test_cases.isEmpty()) return; Reader stdout_reader = null, stderr_reader = null; File tmp = new File(dir, STDOUT); if (tmp.exists() && tmp.length() > 0) stdout_reader = new FileReader(tmp); tmp = new File(dir, STDERR); if (tmp.exists() && tmp.length() > 0) stderr_reader = new FileReader(tmp); File parent = dir.getParentFile(); File xml_file = new File(parent, "TESTS-" + dir.getName() + "-" + parent.getName() + ".xml"); Writer out = new FileWriter(xml_file); String classname = dir.getName(); String suffix = parent.getName(); if (suffix != null && !suffix.isEmpty()) classname = classname + "-" + suffix; try { generateReport(out, classname, test_cases, stdout_reader, stderr_reader); } finally { out.close(); if (stdout_reader != null) stdout_reader.close(); if (stderr_reader != null) stderr_reader.close(); } }
/** Invoked at the start of the test, before any of the classes in the test are run */ public void onStart(ITestContext context) { output_dir = context.getOutputDirectory(); // Uncomment to delete dir created by previous run of this testsuite File dir = new File(output_dir); if (dir.exists()) deleteContents(dir); try { System.setOut(new MyOutput(1)); System.setErr(new MyOutput(2)); } catch (FileNotFoundException e) { } }
/** Generate the XML report from all the test results */ protected void generateReports() throws IOException { File root_dir = new File(output_dir); if (!root_dir.exists()) throw new IOException(root_dir + " not found"); File[] subdirs = root_dir.listFiles( new FileFilter() { public boolean accept(File f) { return f.isDirectory(); } }); if (subdirs != null) { for (File dir : subdirs) { try { process(dir); } catch (Throwable e) { error(e.toString()); } } } }
private void initLogstash2(ITestResult tr) { initLogstashHost(); String simpleClassName = tr.getTestClass().getRealClass().getSimpleName(); String pathToLogstash = SGTestHelper.getSGTestRootDir().replace("\\", "/") + "/src/main/resources/logstash"; String confFilePath2 = pathToLogstash + "/logstash-shipper-client-2.conf"; String backupFilePath2 = pathToLogstash + "/logstash-shipper-client-2-" + simpleClassName + ".conf"; File backupFile2 = new File(backupFilePath2); LogUtils.log( "trying to start logstash agent number 2. simple class name is " + simpleClassName); if (backupFile2.exists()) { LogUtils.log("the file " + backupFilePath2 + " already exists. not starting logstash"); } if (!isAfter(tr) && !backupFile2.exists()) { try { // backupFilePath2 = IOUtils.backupFile(confFilePath2); LogUtils.log("copying file " + confFilePath2 + " to " + backupFilePath2); IOUtils.copyFile(confFilePath2, backupFilePath2); IOUtils.replaceTextInFile(backupFilePath2, "<path_to_build>", SGTestHelper.getBuildDir()); IOUtils.replaceTextInFile( backupFilePath2, "<suite_number>", "suite_" + System.getProperty("iTests.suiteId", "0")); IOUtils.replaceTextInFile( backupFilePath2, "<path_to_test_class_folder>", SGTestHelper.getSGTestRootDir().replace("\\", "/") + "/../" + suiteName + "/" + tr.getTestClass().getName()); IOUtils.replaceTextInFile(backupFilePath2, "<suite_name>", suiteName); IOUtils.replaceTextInFile(backupFilePath2, "<test_name>", simpleClassName); IOUtils.replaceTextInFile(backupFilePath2, "<build_number>", buildNumber); IOUtils.replaceTextInFile(backupFilePath2, "<version>", version); IOUtils.replaceTextInFile(backupFilePath2, "<host>", logstashHost); String logstashJarPath = DeploymentUtils.getLocalRepository() + "net/logstash/1.2.2/logstash-1.2.2.jar"; logstashLogPath2 = pathToLogstash + "/logstash-" + simpleClassName + "-2.txt"; String cmdLine = "java -jar " + logstashJarPath + " agent -f " + backupFilePath2 + " -l " + logstashLogPath2; final String[] parts = cmdLine.split(" "); final ProcessBuilder pb = new ProcessBuilder(parts); LogUtils.log("Executing Command line: " + cmdLine); process2 = pb.start(); } catch (IOException e) { e.printStackTrace(); } } }