Esempio n. 1
0
  /**
   * Create the content of the condition
   *
   * @param ctInvocation
   * @return
   */
  private CtBlock<Object> createThen(CtInvocation<?> ctInvocation) {
    CtBlock<Object> thenStatement = ctInvocation.getFactory().Core().createBlock();
    thenStatement.addStatement(
        (getFactory().Code().createCodeSnippetStatement("System.out.println(\"Else...\")")));
    thenStatement.addStatement(
        (getFactory()
            .Code()
            .createCodeSnippetStatement(
                "System.out.println(" + Debug.class.getCanonicalName() + ".getSolvedPC())")));

    CtAssert<Object> ctAssert = ctInvocation.getFactory().Core().createAssert();

    CtCodeSnippetExpression<Boolean> assertExpression =
        getFactory().Core().createCodeSnippetExpression();
    assertExpression.setValue("false");

    ctAssert.setAssertExpression(assertExpression);
    ctAssert.setExpression(
        getFactory()
            .Code()
            .createCodeSnippetExpression(
                String.format("\"%s\"", ctInvocation.toString().replaceAll("\"", "'"))));

    thenStatement.addStatement(ctAssert);

    return thenStatement;
  }
Esempio n. 2
0
  /**
   * replace assert Equals
   *
   * @param ctInvocation
   * @return
   */
  private CtStatement replaceAssertEquals(CtInvocation<?> ctInvocation) {
    List<CtExpression<?>> arguments = ctInvocation.getArguments();
    CtIf newIf = ctInvocation.getFactory().Core().createIf();
    CtCodeSnippetExpression<Boolean> condition =
        ctInvocation.getFactory().Core().createCodeSnippetExpression();
    CtExpression<?> elem1 = arguments.get(0);
    CtExpression<?> elem2 = arguments.get(1);
    if (arguments.size() > 2
        && (elem1.getType().equals(ctInvocation.getFactory().Class().STRING)
            || elem1.getType().equals(getFactory().Class().nullType()))) {
      elem1 = arguments.get(1);
      elem2 = arguments.get(2);
    }

    boolean isNull = false;
    Class<?> classArg1 = null;
    if (elem1.toString().equals("null")) {
      isNull = true;
    } else {
      try {
        classArg1 = ((CtTypedElement<?>) elem1).getType().getActualClass();
      } catch (SpoonException e) {
        e.printStackTrace();
      }
    }
    if (isNull
        || (classArg1 != null
            && (classArg1.equals(int.class)
                || classArg1.equals(Integer.class)
                || classArg1.equals(boolean.class)
                || classArg1.equals(Boolean.class)
                || classArg1.equals(byte.class)
                || classArg1.equals(Byte.class)
                || classArg1.equals(long.class)
                || classArg1.equals(Long.class)
                || classArg1.equals(double.class)
                || classArg1.equals(Double.class)
                || classArg1.equals(float.class)
                || classArg1.equals(Float.class)
                || classArg1.equals(short.class)
                || classArg1.equals(Short.class)
                || classArg1.equals(char.class)
                || classArg1.equals(Character.class)))) {
      condition.setValue("" + elem1 + " == " + elem2 + "");
    } else {
      condition.setValue("(" + elem1 + ".equals(" + elem2 + "))");
    }
    newIf.setCondition(condition);
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement(
    // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")"));
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement("System.out.println("+Debug.class.getCanonicalName()+".getSolvedPC())"));
    /*
     * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core()
     * .createBlock(); thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(\"Then...\")")));
     * thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(" +
     * Debug.class.getCanonicalName() + ".getSolvedPC())")));
     * newIf.setThenStatement(thenStatement);
     */
    newIf.setThenStatement(createThen(ctInvocation));

    return newIf;
  }