Example #1
0
  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());
    }
  }
Example #2
0
 /** Deletes all files and dirs in dir, but not dir itself */
 protected static void deleteContents(File dir) {
   File[] contents = dir.listFiles();
   if (contents != null) {
     for (File file : contents) {
       if (file.isDirectory()) {
         deleteContents(file);
         file.delete();
       } else file.delete();
     }
   }
 }
Example #3
0
  /** 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) {
    }
  }
  private void write2ErrorTxtFile(ITestResult iTestResult, File testFolder) {
    if (testFolder == null) {
      LogUtils.log("Can not write error.txt - test folder is null");
      return;
    }
    //noinspection ThrowableResultOfMethodCallIgnored
    if (iTestResult.getThrowable() == null) {
      LogUtils.log("nothing to write to error.txt - throwable is null");
      return;
    }
    // trim if too long
    //noinspection ThrowableResultOfMethodCallIgnored
    String errorMsg = iTestResult.getThrowable().toString();
    if (errorMsg.length() > 120) {
      errorMsg = errorMsg.substring(0, 120 - 3) + "...";
    }

    File errorTxtFile = new File(testFolder.getAbsolutePath(), "error.txt");
    PrintWriter out = null;
    try {
      out = new PrintWriter(new BufferedWriter(new FileWriter(errorTxtFile, true)));
      out.println(errorMsg);
    } catch (IOException ioe) {
      LogUtils.log("Failed to write contents into error.txt file", ioe);
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }
 private void write2LogFile(ITestResult iTestResult, File testFolder) {
   BufferedWriter out = null;
   try {
     if (testFolder == null) {
       LogUtils.log("Can not write to file test folder is null");
       return;
     }
     String output = SGTestNGReporter.getOutput();
     if (StringUtils.isEmpty(output)) {
       LogUtils.log("nothing to write to log file");
       return;
     }
     String parameters = TestNGUtils.extractParameters(iTestResult);
     File testLogFile =
         new File(
             testFolder.getAbsolutePath()
                 + "/"
                 + iTestResult.getName()
                 + "("
                 + parameters
                 + ").log");
     if (!testLogFile.createNewFile()) {
       LogUtils.log(
           "Failed to create log file ["
               + testLogFile
               + "];\n log output: "
               + Reporter.getOutput());
       return;
     }
     FileWriter fstream = new FileWriter(testLogFile);
     out = new BufferedWriter(fstream);
     out.write(output);
   } catch (Exception e) {
     LogUtils.log("Failed to write to log file result - " + iTestResult, e);
   } finally {
     SGTestNGReporter.reset();
     if (out != null) {
       try {
         out.close();
       } catch (IOException e) {
         LogUtils.log("Failed closing stream", e);
         // ignore
       }
     }
   }
 }
Example #6
0
 /** 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 copyAllFilesToLogDir(File node, File parent) throws IOException {
    if (!node.getAbsoluteFile().equals(parent.getAbsoluteFile())
        && node.isFile()
        && !node.getParentFile().equals(parent)) {
      String fileNamePrefix = node.getName().substring(0, node.getName().lastIndexOf('.'));
      String fileNameSuffix = node.getName().substring(node.getName().lastIndexOf('.'));
      String newFilePath =
          node.getParentFile().getAbsolutePath()
              + File.separator
              + fileNamePrefix.replace(".", "_")
              + fileNameSuffix;

      File newNode = new File(newFilePath);
      if (node.renameTo(newNode)) {
        FileUtils.copyFileToDirectory(newNode, parent);
      }
    }
    if (node.isDirectory()) {
      String[] subNote = node.list();
      for (String filename : subNote) {
        copyAllFilesToLogDir(new File(node, filename), parent);
      }
      if (!node.equals(parent)) {
        FileUtils.deleteDirectory(node);
      }
    }
  }
  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();
      }
    }
  }
Example #9
0
  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();
    }
  }