Esempio n. 1
0
  @Override
  public boolean compile(TestData data) {
    System.out.println("...enter JavaCompiler comple(data)...");

    if (data == null) return false;
    // 读取源码
    String source = data.getStr(JobConst.TARGET_SOURCE);

    //		System.out.println("++++++++++++++++++++++");
    //		System.out.println("souce code : " + source);
    //		if(source.indexOf("();")!=-1){
    //			String s = new String(source);
    //			Pattern pattern = Pattern.compile("=\\s*\\w*\\(\\)\\;");
    //			Matcher matcher = pattern.matcher(s);
    ////			System.out.println(matcher.replaceAll("= 5"));
    //			source = matcher.replaceAll(" = 5;\\/\\/need mocked");
    //		}
    String s = new String(source);
    Pattern pattern = Pattern.compile("valueReturned\\s*=\\s*\\w");
    Matcher matcher = pattern.matcher(s);
    //		System.out.println(matcher.replaceAll(s +"\\/\\/need mocked"));
    source = matcher.replaceAll("valueReturned = 8;\\/\\/need mocked");
    //		System.out.println("++++++++++++++++++++++");
    // 输出源码

    data.copyList(getSrc());
    // 组合输入文件名, 写入临时输出目录
    IOUtil.write(getSrc() + data.getSourceFile().getName(), source);
    setStaticSourceFileName(data.getSourceFile().getName());
    //		System.out.println("========++++++" + data.getSourceFile().getName());
    // 输出信息
    warnFile = getSrc() + WARN;

    String classPath = data.getStr(JobConst.CLASS_PATH);
    if (classPath.isEmpty()) {
      classPath = getClassPath();
    }
    // Debug.println(classPath);

    // 输出make文件
    StringBuilder sb = new StringBuilder();
    ArrayList<String> arr = data.getCompiledList();
    for (int i = 0; i < arr.size(); i++) {
      sb.append(getSrc() + arr.get(i) + JobConst.ENTER);
    }
    Log.println("1111" + getSrc());
    // Debug.println(classPath);
    IOUtil.write(getSrc() + MAKE, sb.toString());

    if (!compile(classPath, getBin(), "@" + getSrc() + MAKE, warnFile)) {
      data.getResult().addReport(IOUtil.read(warnFile));
      // data.put(JobConst.COMPILED_REPORT, IOUtil.read(warnFile));
      return false;
    } else return true;
  }
  /**
   * BP测试,以下三个参数是必的 <br>
   * FileName=[TestClass.java]<br>
   * TargetClass=[TestClass]<br>
   * TargetMethod=[testMethod]<br>
   */
  @Override
  public String testing(String code, String options) {

    // Add line number```
    // begin
    TestData data = new TestData();
    Properties ipro = PropertiesUtil.parse(options);

    String ifileName = ipro.getProperty(JobConst.FILENAME);
    data.setSourceCode(code, ifileName);
    data.put(JobConst.TARGET_CLASS, ipro.getProperty(JobConst.TARGET_CLASS));
    data.put(JobConst.TARGET_METHOD, ipro.getProperty(JobConst.TARGET_METHOD));

    JavaParser p = new JavaParser();
    BPVistor2 v = new BPVistor2();

    p.run(data);
    v.run(data);

    FlowGraph ifg = (FlowGraph) data.get(JobConst.CONTROL_FLOW_GRAPH);
    Iterator<GraphNode> iitGn = ifg.iterator();
    // end

    BPStumpWorker w = new BPStumpWorker();
    // TestData d = new TestData();
    Properties pro = PropertiesUtil.parse(options);

    String fileName = pro.getProperty(JobConst.FILENAME);
    // d.setSourceCode(code, fileName);
    // d.put(JobConst.TARGET_CLASS, pro.getProperty(JobConst.TARGET_CLASS));
    // d.put(JobConst.TARGET_METHOD,
    // pro.getProperty(JobConst.TARGET_METHOD));

    // boolean b = w.run(d);

    if (true) {

      // To generate the Flow Graph
      String rs = "<FlowGraph>\r\n";

      int preNodeLineNum = 1;
      boolean Flag = true;

      // test
      ArrayList<Integer> A = new ArrayList<Integer>();
      ArrayList<Integer> B = new ArrayList<Integer>();
      A.add(-1);
      B.add(-1);
      boolean isDual = false;
      Iterator<GraphNode> iitGn2 = ifg.iterator();

      while (iitGn2.hasNext()) {
        GraphNode inode = iitGn2.next(); // changed by me```
        if (inode.getFirstId() != -1) {
          A.add(inode.getId());
          B.add(inode.getFirstId());
        }
        if (inode.getSecondId() != -1) {

          A.add(inode.getId());
          B.add(inode.getFirstId());
        }
      }

      while (iitGn.hasNext()) {
        GraphNode inode = iitGn.next(); // changed by me```

        Flag = false;
        rs +=
            (("  <Node id=\""
                + inode.getId()
                + "\" name=\""
                + inode.getBeginLine()
                + " - "
                + inode.getEndline()
                + "\" nodeSize=\"30\" nodeColor=\"0x4682b4\"/>\r\n"));
        if (inode.getFirstId() != -1) {
          for (int i = 0; i < A.size(); i++) {
            if (A.get(i) == inode.getFirstId() && B.get(i) == inode.getId()) {
              isDual = true;
            }
          }
          if (isDual) {
            rs +=
                (("    <Edge fromID=\""
                    + inode.getId()
                    + "\" toID=\""
                    + inode.getFirstId()
                    + "\" color=\"0x4682b4\" edgeLabel=\"DUAL\"/>\r\n"));
          } else {
            rs +=
                (("    <Edge fromID=\""
                    + inode.getId()
                    + "\" toID=\""
                    + inode.getFirstId()
                    + "\" color=\"0x4682b4\"/>\r\n"));
          }
          isDual = false;
        }
        if (inode.getSecondId() != -1) {
          for (int i = 0; i < A.size(); i++) {
            if (A.get(i) == inode.getSecondId() && B.get(i) == inode.getId()) {
              isDual = true;
            }
          }
          if (isDual) {
            rs +=
                (("    <Edge fromID=\""
                    + inode.getId()
                    + "\" toID=\""
                    + inode.getSecondId()
                    + "\" color=\"0x4682b4\" edgeLabel=\"DUAL\"/>\r\n"));
          } else {
            rs +=
                (("    <Edge fromID=\""
                    + inode.getId()
                    + "\" toID=\""
                    + inode.getSecondId()
                    + "\" color=\"0x4682b4\"/>\r\n"));
          }
          isDual = false;
        }
      }

      // while (iitGn.hasNext())
      // {
      // GraphNode inode = iitGn.next();// changed by me```
      //
      // if (Flag)
      // {
      // Flag = false;
      // rs += (("  <Node id=\"" + inode.getId() + "\" name=\"" +
      // inode.getBeginLine() + " - "
      // + inode.getEndline() +
      // "\" nodeSize=\"10\" nodeColor=\"0x4682b4\"/>\r\n"));
      // if (inode.getFirstId() != -1)
      // {
      // rs += (("    <Edge fromID=\"" + inode.getId() + "\" toID=\"" +
      // inode.getFirstId() +
      // "\" color=\"0x4682b4\" edgeClass=\"rain\" flow=\"400\" />\r\n"));
      // }
      // if (inode.getSecondId() != -1)
      // {
      // rs += (("    <Edge fromID=\"" + inode.getId() + "\" toID=\"" +
      // inode.getSecondId() +
      // "\"  color=\"0xb682b4\" edgeClass=\"rain\" flow=\"1\" />\r\n"));
      // }
      // }
      // // preNodeLineNum = inode.getLineNum();
      // //
      // rs+=(("  <Node id=\""+node.getId()+"\" name=\""+preNodeLineNum+"-"+inode.getLineNum()+"\"
      // nodeSize=\"30\" nodeColor=\"0x4682b4\"/>\r\n"));
      // else
      // {
      // rs += (("  <Node id=\"" + inode.getId() + "\" name=\"" +
      // inode.getBeginLine() + " - "
      // + inode.getEndline() +
      // "\" nodeSize=\"10\" nodeColor=\"0x4682b4\"/>\r\n"));
      // // preNodeLineNum = inode.getLineNum();
      // if (inode.getFirstId() != -1)
      // {
      // rs += (("    <Edge fromID=\"" + inode.getId() + "\" toID=\"" +
      // inode.getFirstId() +
      // "\"   color=\"0x4682b4\" edgeClass=\"rain\" flow=\"400\" />\r\n"));
      // }
      // if (inode.getSecondId() != -1)
      // {
      // rs += (("    <Edge fromID=\"" + inode.getId() + "\" toID=\"" +
      // inode.getSecondId() +
      // "\"  color=\"0xb682b4\" edgeClass=\"rain\" flow=\"1\"  />\r\n"));
      // }
      // }
      // }

      rs += "</FlowGraph>\r\n";

      rs += "$$$$$\n";

      // To generate the Basic Path Set
      rs += "<basicPathSet>\r\n";
      String[] temp = null;
      ifg = (FlowGraph) data.get(JobConst.CONTROL_FLOW_GRAPH);
      GraphPathArray targetPaths = ifg.generatPaths();
      Iterator<GraphPath> itGp = targetPaths.iterator();
      while (itGp.hasNext()) {
        GraphPath path = itGp.next();
        rs += (("  <basicPath>\r\n"));
        temp = path.toString().substring(0, path.toString().indexOf(" {")).split(" ");
        for (int i = 0; i < temp.length; ++i) rs += (("    <node id = \"" + temp[i] + "\"/>\r\n"));
        rs += (("  </basicPath>\r\n"));
      }
      rs += "</basicPathSet>\r\n";
      return rs;

    } else {
      return data.getResult().getReport();
    }
  }