Ejemplo n.º 1
0
  /**
   * Get the current entry at the given position
   *
   * @param position a int.
   * @param var a {@link org.evosuite.testcase.variable.VariableReference} object.
   * @return a T object.
   */
  public synchronized T getEntry(int position, VariableReference var) {
    if (!trace.containsKey(position)) {
      trace.put(position, new HashMap<Integer, T>());
      return null;
    }

    if (!trace.get(position).containsKey(var.getStPosition())) return null;

    return trace.get(position).get(var.getStPosition());
  }
Ejemplo n.º 2
0
  /**
   * Get the current entry at the given position
   *
   * @param position a int.
   * @param var a {@link org.evosuite.testcase.variable.VariableReference} object.
   * @return a boolean.
   */
  public boolean containsEntry(int position, VariableReference var) {
    if (!trace.containsKey(position)) {
      trace.put(position, new HashMap<Integer, T>());
      return false;
    }

    if (!trace.get(position).containsKey(var.getStPosition())) return false;

    return true;
  }
Ejemplo n.º 3
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()));
    }
  }
Ejemplo n.º 4
0
  /**
   * Insert a new entry into the trace
   *
   * @param position a int.
   * @param entry a T object.
   * @param var a {@link org.evosuite.testcase.variable.VariableReference} object.
   * @param <T> a T object.
   */
  public synchronized void addEntry(int position, VariableReference var, T entry) {
    if (!trace.containsKey(position)) trace.put(position, new HashMap<Integer, T>());

    trace.get(position).put(var.getStPosition(), entry);
  }