Package edu.ncsu.muclipse.exec

Source Code of edu.ncsu.muclipse.exec.RunTests

package edu.ncsu.muclipse.exec;

import java.io.*;

import mujava.MutationSystem;
//import mujava.TestExecuter;
import mujava.TestExecuter_new;
import mujava.test.TestResult;
import mujava.util.Debug;
import java.util.Vector;

/**
* A Java Program which is called by MuClipse to perform
* unit testing on originals and compare its results with the
* results of unit testing on created mutants.
* @author Ben Smith
*/
public class RunTests {

  /**
   * Determines how much console output there will be.
   */
  public static final boolean debug = true;
 
  /**
   * The method which actually performs the unit testing
   * on the originals and results. It has the following
   * parameters as arguments:<p />
   *
   * 0: Project root<br />
   * 1: Location of source<br />
   * 2: Location of binaries<br />
   * 3: Location of the results of mutation<br />
   * 4: Location of unit test sourcecode<br />
   * 5: Class name of the test to be run<br />
   * 6: Class names of the target classes<br />
   * 7: The timeout value for each unit test<br />
   * 8: Test level: (1 = traditional 2 = classlevel 3 = both)<br />
   * @param args The arguments passed (see above)
   */
  public static void main(String[] args) {
    String syshome = args[0];
    syshome = syshome.substring(0, syshome.length() - 1);
    String source = "/" + args[1];
    String classes = "/" + args[2];
    String result = "/" + args[3];
    String tests = "/" + args[4];
    String whichTest = args[5];
   
    String classlist = args[6];
    String timeout = args[7];
    int testlevel = Integer.parseInt(args[8]);
    boolean runTrad = (testlevel > 1);
    boolean runClas = (testlevel == 1 || testlevel == 3);
     
    if (debug)
    {
      System.out.println("MuClipse v1.3R2");
      System.out.println("MuClipse is running tests...");
     
      System.out.println("For project located in:\n" + syshome);
      System.out.println("Source located in: " + source);
      System.out.println("Classes located in: " + classes);
      System.out.println("Results located in: " + result);
      System.out.println("Tests located in: " + tests);
      System.out.println("Test to run: " + whichTest);
      System.out.println("With timeout: " + timeout);
      if (runTrad)
        System.out.println("Will run traditional");
      if (runClas)
        System.out.println("Will run class");
    }

    MutationSystem.setJMutationStructure(
        syshome, source, classes, result, tests);
   
      System.out.println("------------------------------- \n Tests complete! Results:");
      String targetClassName = classlist;
      System.out.println("Starting with class: " + targetClassName);
     
      /*
      if (whichTest.endsWith(".class"))
        whichTest = whichTest.substring(0, whichTest.length() - 6);
      if (whichTest.endsWith(".java"))
        whichTest = whichTest.substring(0, whichTest.length() - 5);
      whichTest = whichTest.substring(whichTest.indexOf("/", 1) + 1);
      whichTest = whichTest.replace('/', '.');
      */

      System.out.println("Final testclass name: " + whichTest);
      Debug.setDebugLevel(3);
      TestResult tr = new TestResult();
      TestResult tr2 = new TestResult();
     
      if (runTrad)
      {
        System.out.println("Running against traditional mutants...");
        tr = runTest(targetClassName, whichTest, Integer.parseInt(timeout), false);
      }
     
      if (runClas)
      {
        System.out.println("Running against classlevel mutants...");
        tr2 = runTest(targetClassName, whichTest, Integer.parseInt(timeout), true);
      }
      int live_num2 = tr2.live_mutants.size();
      int killed_num2 = tr2.killed_mutants.size();
      int live_num1 = tr.live_mutants.size();
      int killed_num1 = tr.killed_mutants.size();
     
      int live_num = live_num1 + live_num2;
      int killed_num = killed_num1 + killed_num2;
     
      Float mutant_score = new Float((killed_num * 100)
          / (killed_num + live_num));
     
      System.out.println("-------------------------------");
      System.out.println("Results for class " + targetClassName);
      System.out.println("-------------------------------");
      System.out.println("Live mutants: " + live_num);
      System.out.println("Killed mutants: " + killed_num);
      System.out.println("Mutation Score: " + mutant_score);
      System.out.println("Writing to file...");
      try
      {
        FileWriter data = new FileWriter(syshome + result + "/" + targetClassName + "/testresults.mjv", false);
       
        data.write("MuJava\n");
        for (int j=0; j<tr.live_mutants.size(); j++)
        {
          data.write(tr.live_mutants.get(j) + ",");
        }
        for (int j=0; j<tr2.live_mutants.size(); j++)
        {
          data.write(tr2.live_mutants.get(j) + ",");
        }
       
        data.write("\n");
        for (int j=0; j<tr.killed_mutants.size(); j++)
        {
          data.write(tr.killed_mutants.get(j) + ",");
        }
        for (int j=0; j<tr2.killed_mutants.size(); j++)
        {
          data.write(tr2.killed_mutants.get(j) + ",");
        }
       
        data.close();
      }
      catch (Exception e) {
        System.err.println("Error writing to file.");
      }
  }

  static TestResult runTest(String targetClassName, String testSetName, int timeout_secs, boolean mode){
   
      if((targetClassName!=null)&&(testSetName!=null)){
        try{
            TestExecuter_new test_engine = new TestExecuter_new(targetClassName);
            test_engine.setTimeOut(timeout_secs);

            // First, read (load) test suite class.
            test_engine.readTestSet(testSetName);

            TestResult test_result = new TestResult();
            if(mode==true){
              test_result = test_engine.runClassMutants();
            }else{
              test_result = test_engine.runTraditionalMutants("All method");
            }
            return test_result;
        }
        catch(Exception e){
            System.err.println(e);
            return null;
        }
      }else{
          System.out.println(" [Error] Please check test target or test suite ");
          return null;
      }

    }

  private static void setMutationSystemPathFor(String file_name){
      try{
        String temp;
        temp = file_name.substring(0,file_name.length()-".java".length());
        temp = temp.replace('/','.');
        temp = temp.replace('\\','.');
        int separator_index = temp.lastIndexOf(".");
        if(separator_index>=0){
          MutationSystem.CLASS_NAME=temp.substring(separator_index+1,temp.length());
        }else{
          MutationSystem.CLASS_NAME = temp;
        }

        String mutant_dir_path = MutationSystem.MUTANT_HOME+"/"+temp;
        File mutant_path = new File(mutant_dir_path);
        mutant_path.mkdir();

        String class_mutant_dir_path = mutant_dir_path + "/" + MutationSystem.CM_DIR_NAME;
        File class_mutant_path = new File(class_mutant_dir_path);
        class_mutant_path.mkdir();

        String traditional_mutant_dir_path = mutant_dir_path + "/" + MutationSystem.TM_DIR_NAME;
        File traditional_mutant_path = new File(traditional_mutant_dir_path);
        traditional_mutant_path.mkdir();

        String original_dir_path = mutant_dir_path + "/" + MutationSystem.ORIGINAL_DIR_NAME;
        File original_path = new File(original_dir_path);
        original_path.mkdir();

        MutationSystem.CLASS_MUTANT_PATH = class_mutant_dir_path;
        MutationSystem.TRADITIONAL_MUTANT_PATH = traditional_mutant_dir_path;
        MutationSystem.ORIGINAL_PATH = original_dir_path;
        MutationSystem.DIR_NAME = temp;

      }catch(Exception e){
          System.err.println(e);
      }
    }

}
TOP

Related Classes of edu.ncsu.muclipse.exec.RunTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.