Example #1
0
  /** {@inheritDoc} */
  @Override
  public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    try {
      for (VariableReference var : statement.getVariableReferences()) {
        if (var.equals(statement.getReturnValue())
            || var.equals(statement.getReturnValue().getAdditionalVariableReference())) continue;
        Object object = var.getObject(scope);

        if (var.isPrimitive()) {
          ConstantValue value = new ConstantValue(test, var.getGenericClass());
          value.setValue(object);
          // logger.info("Statement before inlining: " + statement.getCode());
          statement.replace(var, value);
          // logger.info("Statement after inlining: " + statement.getCode());
        } else if (var.isString() && object != null) {
          ConstantValue value = new ConstantValue(test, var.getGenericClass());
          try {
            String val = StringEscapeUtils.unescapeJava(object.toString());
            value.setValue(val);
            statement.replace(var, value);
          } catch (IllegalArgumentException e) {
            // Exceptions may happen if strings are not valid unicode
            logger.info("Cannot escape invalid string: " + object);
          }
          // logger.info("Statement after inlining: " + statement.getCode());
        } else if (var.isArrayIndex()) {
          // If this is an array index and there is an object outside the array
          // then replace the array index with that object
          for (VariableReference otherVar : scope.getElements(var.getType())) {
            Object otherObject = otherVar.getObject(scope);
            if (otherObject == object
                && !otherVar.isArrayIndex()
                && otherVar.getStPosition() < statement.getPosition()) {
              statement.replace(var, otherVar);
              break;
            }
          }
        } else {
          // TODO: Ignoring exceptions during getObject, but keeping the assertion for now
          if (object == null) {
            ConstantValue value = new ConstantValue(test, var.getGenericClass());
            value.setValue(null);
            // logger.info("Statement before inlining: " + statement.getCode());
            statement.replace(var, value);
            // logger.info("Statement after inlining: " + statement.getCode());
          }
        }
      }
    } catch (CodeUnderTestException e) {
      logger.warn("Not inlining test: " + e.getCause());
      // throw new AssertionError("This case isn't handled yet: " + e.getCause()
      //        + ", " + Arrays.asList(e.getStackTrace()));
    }
  }
  @Override
  public String getTestCode(String varName) {
    String testCode = "";
    VariableReference retval = getReturnValue();
    Object value = getValue();

    if (value != null) {
      String escapedURL = StringUtil.getEscapedString(((EvoSuiteURL) value).getUrl());
      testCode +=
          ((Class<?>) retval.getType()).getSimpleName()
              + " "
              + varName
              + " = new "
              + ((Class<?>) retval.getType()).getSimpleName()
              + "(\""
              + escapedURL
              + "\");\n";
    } else {
      testCode += ((Class<?>) retval.getType()).getSimpleName() + " " + varName + " = null;\n";
    }
    return testCode;
  }