Exemple #1
0
  private void createExecutionMethod(CtInvocation<?> ctInvocation) {
    CtClass<?> parent = ctInvocation.getParent(CtClass.class);
    if (!parent.isTopLevel()) {
      return;
    }
    if (parent.getModifiers().contains(ModifierKind.ABSTRACT)) {
      return;
    }
    CtTypeReference<String[]> typedReference = getFactory().Class().createReference(String[].class);
    if (parent.getMethod("runJPFTest", typedReference) != null) {
      return;
    }
    CtTypeReference<Object> returntypedReference = getFactory().Class().createReference("void");

    Set<ModifierKind> modifiers = new LinkedHashSet<ModifierKind>(2);
    modifiers.add(ModifierKind.PUBLIC);
    modifiers.add(ModifierKind.STATIC);

    String invocation = parent.getQualifiedName() + "(";
    if (parent.getConstructor() == null) {
      // CtTypeReference<?> superClass = parent.getSuperclass();
      if (parent.getConstructor(ctInvocation.getFactory().Class().STRING) != null) {
        invocation += "\"" + parent.getSimpleName() + "\"";
      } else {
        return;
      }
    }
    invocation += ")";

    CtBlock<?> body = getFactory().Core().createBlock();
    String bodyString = "for (String method : methods) {\n";
    bodyString += "\t\tSystem.out.println(method);\n\t\t";
    bodyString += parent.getQualifiedName() + " instance = new " + invocation + ";\n\t\t";
    if (parent.getMethod("setUp") != null) {
      bodyString += "instance.setUp();";
    }
    bodyString +=
        parent.getQualifiedName() + ".class.getMethod(method, null).invoke(instance);\n\t\t";
    bodyString += "}\n";
    body.addStatement(getFactory().Code().createCodeSnippetStatement(bodyString));
    HashSet<CtTypeReference<? extends Throwable>> exceptions =
        new HashSet<CtTypeReference<? extends Throwable>>();
    exceptions.add(getFactory().Class().createReference(Exception.class));
    CtMethod<?> method =
        getFactory()
            .Method()
            .create(
                parent,
                modifiers,
                returntypedReference,
                "runJPFTest",
                new ArrayList<CtParameter<?>>(),
                exceptions,
                body);
    getFactory().Method().createParameter(method, typedReference, "methods");
  }
  protected List<InputContext> getInputContexts(CtMethod method) {
    List<InputContext> inputContexts = new ArrayList<>();

    List<CtStatement> statements = getAssertStatement(method);
    for (CtStatement stmt : statements) {
      Set<CtVariableReference> varRefs = new HashSet<>();
      for (CtLocalVariable var : getLocalVarInScope(stmt)) {
        varRefs.add(method.getFactory().Code().createLocalVariableReference(var));
      }

      inputContexts.add(new InputContext(varRefs));
    }

    return inputContexts;
  }
  public void reset(Coverage coverage, CtType testClass) {
    AmplificationHelper.reset();
    literalsByMethod = new HashMap<>();

    Set<CtType> codeFragmentsProvide = AmplificationHelper.computeClassProvider(testClass);

    List<Statement> codeFragmentsByClass =
        codeFragmentsProvide
            .stream()
            .flatMap(
                cl -> {
                  List<CtStatement> list = Query.getElements(cl, new TypeFilter(CtStatement.class));
                  return list.stream();
                })
            .filter(
                stmt -> {
                  try {
                    return stmt.getParent() != null;
                  } catch (Exception e) {
                    return false;
                  }
                })
            .filter(stmt -> stmt.getParent() instanceof CtBlock)
            .filter(stmt -> !stmt.toString().startsWith("super"))
            .filter(stmt -> !stmt.toString().startsWith("this("))
            .map(stmt -> new Statement(stmt))
            .collect(Collectors.toList());

    if (findClassUnderTest(testClass) != null) {
      coverageBycodeFragments = buildCodeFragmentFor(findClassUnderTest(testClass), coverage);
    } else {
      coverageBycodeFragments = new HashMap<>();
    }

    Set<Integer> ids = new HashSet<>();
    localVars =
        codeFragmentsByClass
            .stream()
            .filter(cf -> isValidCodeFragment(cf))
            .filter(cf -> ids.add(cf.id()))
            .collect(Collectors.toList());
  }