Package org.apache.hadoop.mrunit.internal.util

Examples of org.apache.hadoop.mrunit.internal.util.Errors


   *          result
   */
  protected void validate(final List<Pair<K2, V2>> outputs,
      final boolean orderMatters) {

    final Errors errors = new Errors(LOG);

    if (!outputs.isEmpty()) {
      // were we supposed to get output in the first place?
      if (expectedOutputs.isEmpty()) {
        errors.record("Expected no outputs; got %d outputs.", outputs.size());
      }
      // check that user's key and value writables implement equals, hashCode, toString
      checkOverrides(outputs.get(0));
    }

    final Map<Pair<K2, V2>, List<Integer>> expectedPositions = buildPositionMap(expectedOutputs);
    final Map<Pair<K2, V2>, List<Integer>> actualPositions = buildPositionMap(outputs);

    for (final Pair<K2, V2> output : expectedPositions.keySet()) {
      final List<Integer> expectedPositionList = expectedPositions.get(output);
      final List<Integer> actualPositionList = actualPositions.get(output);
      if (actualPositionList != null) {
        // the expected value has been seen - check positions
        final int expectedPositionsCount = expectedPositionList.size();
        final int actualPositionsCount = actualPositionList.size();
        if (orderMatters) {
          // order is important, so the positions must match exactly
          if (expectedPositionList.equals(actualPositionList)) {
            LOG.debug(String.format("Matched expected output %s at "
                + "positions %s", output, expectedPositionList.toString()));
          } else {
            int i = 0;
            while (expectedPositionsCount > i || actualPositionsCount > i) {
              if (expectedPositionsCount > i && actualPositionsCount > i) {
                final int expectedPosition = expectedPositionList.get(i);
                final int actualPosition = actualPositionList.get(i);
                if (expectedPosition == actualPosition) {
                  LOG.debug(String.format("Matched expected output %s at "
                      + "position %d", output, expectedPosition));
                } else {
                  errors.record("Matched expected output %s but at "
                      + "incorrect position %d (expected position %d)", output,
                      actualPosition, expectedPosition);
                }
              } else if (expectedPositionsCount > i) {
                // not ok, value wasn't seen enough times
                errors.record("Missing expected output %s at position %d.",
                    output, expectedPositionList.get(i));
              } else {
                // not ok, value seen too many times
                errors.record("Received unexpected output %s at position %d.",
                    output, actualPositionList.get(i));
              }
              i++;
            }
          }
        } else {
          // order is unimportant, just check that the count of times seen match
          if (expectedPositionsCount == actualPositionsCount) {
            // ok, counts match
            LOG.debug(String.format("Matched expected output %s in "
                + "%d positions", output, expectedPositionsCount));
          } else if (expectedPositionsCount > actualPositionsCount) {
            // not ok, value wasn't seen enough times
            for (int i = 0; i < expectedPositionsCount - actualPositionsCount; i++) {
              errors.record("Missing expected output %s", output);
            }
          } else {
            // not ok, value seen too many times
            for (int i = 0; i < actualPositionsCount - expectedPositionsCount; i++) {
              errors.record("Received unexpected output %s", output);
            }
          }
        }
        actualPositions.remove(output);
      } else {
        // the expected value was not found anywhere - output errors
        checkTypesAndLogError(outputs, output, expectedPositionList,
            orderMatters, errors, "Missing expected output");
      }
    }

    for (final Pair<K2, V2> output : actualPositions.keySet()) {
      // anything left in actual set is unexpected
      checkTypesAndLogError(outputs, output, actualPositions.get(output),
          orderMatters, errors, "Received unexpected output");
    }
   
    errors.assertNone();
  }
View Full Code Here


   *
   * @param counterWrapper
   */
  private void validateExpectedAgainstActual(
      final CounterWrapper counterWrapper) {
    final Errors errors = new Errors(LOG);
 
    // Firstly check enumeration based counters
    for (final Pair<Enum<?>, Long> expected : expectedEnumCounters) {
      final long actualValue = counterWrapper.findCounterValue(expected
          .getFirst());
 
      if (actualValue != expected.getSecond()) {
        errors.record("Counter %s.%s has value %d instead of expected %d",
            expected.getFirst().getDeclaringClass().getCanonicalName(),
            expected.getFirst().toString(), actualValue, expected.getSecond());
      }
    }
 
    // Second string based counters
    for (final Pair<Pair<String, String>, Long> expected : expectedStringCounters) {
      final Pair<String, String> counter = expected.getFirst();
 
      final long actualValue = counterWrapper.findCounterValue(
          counter.getFirst(), counter.getSecond());
 
      if (actualValue != expected.getSecond()) {
        errors
            .record(
                "Counter with category %s and name %s has value %d instead of expected %d",
                counter.getFirst(), counter.getSecond(), actualValue,
                expected.getSecond());
      }
    }
   
    errors.assertNone();
  }
View Full Code Here

   *
   * @param counterWrapper
   */
  private void validateActualAgainstExpected(final CounterWrapper counterWrapper) {
    if (strictCountersChecking) {
      final Errors errors = new Errors(LOG);
      Collection<Pair<String, String>> unmatchedCounters = counterWrapper.findCounterValues();
      Collection<Pair<String, String>> findExpectedCounterValues = findExpectedCounterValues();
      unmatchedCounters.removeAll(findExpectedCounterValues);
      if(!unmatchedCounters.isEmpty()) {
        for (Pair<String, String> unmatcherCounter : unmatchedCounters) {
          errors
              .record(
                  "Actual counter (\"%s\",\"%s\") was not found in expected counters",
                  unmatcherCounter.getFirst(), unmatcherCounter.getSecond());
        }
      }
      errors.assertNone();
    }
  }
View Full Code Here

    // expected nothing and got nothing, everything is fine
    if (outputs.isEmpty() && expectedOutputs.isEmpty()) {
      return;
    }

    final Errors errors = new Errors(LOG);
    // expected nothing but got something
    if (!outputs.isEmpty() && expectedOutputs.isEmpty()) {
      errors.record("Expected no output; got %d output(s).", outputs.size());
      errors.assertNone();
    }
    // expected something but got nothing
    if (outputs.isEmpty() && !expectedOutputs.isEmpty()) {
      errors.record("Expected %d output(s); got no output.",
          expectedOutputs.size());
      errors.assertNone();
    }

    // now, the smart test needs to be done
    // check that user's key and value writables implement equals, hashCode,
    // toString
    checkOverrides(outputs, expectedOutputs);

    final PairEquality<K, V> equality = new PairEquality<K, V>(keyComparator,
        valueComparator);
    if (orderMatters) {
      validateWithOrder(outputs, errors, equality);
    } else {
      validateWithoutOrder(outputs, errors, equality);
    }

    // if there are errors, it might be due to types and not clear from the
    // message
    if (!errors.isEmpty()) {
      Class<?> outputKeyClass = null;
      Class<?> outputValueClass = null;
      Class<?> expectedKeyClass = null;
      Class<?> expectedValueClass = null;

      for (Pair<K, V> output : outputs) {
        if (output.getFirst() != null) {
          outputKeyClass = output.getFirst().getClass();
        }
        if (output.getSecond() != null) {
          outputValueClass = output.getSecond().getClass();
        }
        if (outputKeyClass != null && outputValueClass != null) {
          break;
        }
      }

      for (Pair<K, V> expected : expectedOutputs) {
        if (expected.getFirst() != null) {
          expectedKeyClass = expected.getFirst().getClass();
        }
        if (expected.getSecond() != null) {
          expectedValueClass = expected.getSecond().getClass();
        }
        if (expectedKeyClass != null && expectedValueClass != null) {
          break;
        }
      }

      if (outputKeyClass != null && expectedKeyClass != null
          && !outputKeyClass.equals(expectedKeyClass)) {
        errors.record("Mismatch in key class: expected: %s actual: %s",
            expectedKeyClass, outputKeyClass);
      }

      if (outputValueClass != null && expectedValueClass != null
          && !outputValueClass.equals(expectedValueClass)) {
        errors.record("Mismatch in value class: expected: %s actual: %s",
            expectedValueClass, outputValueClass);
      }
    }
    errors.assertNone();
  }
View Full Code Here

      }
    }
  }

  protected void validate(final MockMultipleOutputs mos) {
    final Errors errors = new Errors(LOG);

    if (mos != null && !mos.isNamedOutputsEmpty()
        && expectedMultipleOutputs.isEmpty()) {
      errors.record(
          "Expected no multiple outputs; got %d named MultipleOutputs.",
          mos.getMultipleOutputsCount());
    }

    Map<String, List<Pair<?, ?>>> actuals = buildActualMultipleOutputs(mos);
    Map<String, List<Pair<?, ?>>> expects = buildExpectedMultipleOutputs();

    validateOutputList("namedOutput", errors, actuals, expects);

    actuals.clear();
    expects.clear();

    if (mos != null && !mos.isPathOutputsEmpty()
        && expectedPathOutputs.isEmpty()) {
      errors.record("Expected no pathOutputs; got %d pathOutputs.",
          mos.getPathOutputsCount());
    }

    actuals = buildActualPathOutputs(mos);
    expects = buildExpectedPathOutputs();

    validateOutputList("PathOutput", errors, actuals, expects);

    errors.assertNone();
  }
View Full Code Here

   * proper values.
   *
   * @param counterWrapper
   */
  private void validateExpectedAgainstActual(final CounterWrapper counterWrapper) {
    final Errors errors = new Errors(LOG);

    // Firstly check enumeration based counters
    for (final Pair<Enum<?>, Long> expected : expectedEnumCounters) {
      final long actualValue = counterWrapper.findCounterValue(expected
          .getFirst());

      if (actualValue != expected.getSecond()) {
        errors.record("Counter %s.%s has value %d instead of expected %d",
            expected.getFirst().getDeclaringClass().getCanonicalName(),
            expected.getFirst().toString(), actualValue, expected.getSecond());
      }
    }

    // Second string based counters
    for (final Pair<Pair<String, String>, Long> expected : expectedStringCounters) {
      final Pair<String, String> counter = expected.getFirst();

      final long actualValue = counterWrapper.findCounterValue(
          counter.getFirst(), counter.getSecond());

      if (actualValue != expected.getSecond()) {
        errors
            .record(
                "Counter with category %s and name %s has value %d instead of expected %d",
                counter.getFirst(), counter.getSecond(), actualValue,
                expected.getSecond());
      }
    }

    errors.assertNone();
  }
View Full Code Here

   *
   * @param counterWrapper
   */
  private void validateActualAgainstExpected(final CounterWrapper counterWrapper) {
    if (strictCountersChecking) {
      final Errors errors = new Errors(LOG);
      Collection<Pair<String, String>> unmatchedCounters = counterWrapper
          .findCounterValues();
      Collection<Pair<String, String>> findExpectedCounterValues = findExpectedCounterValues();
      unmatchedCounters.removeAll(findExpectedCounterValues);
      if (!unmatchedCounters.isEmpty()) {
        for (Pair<String, String> unmatcherCounter : unmatchedCounters) {
          errors
              .record(
                  "Actual counter (\"%s\",\"%s\") was not found in expected counters",
                  unmatcherCounter.getFirst(), unmatcherCounter.getSecond());
        }
      }
      errors.assertNone();
    }
  }
View Full Code Here

    // expected nothing and got nothing, everything is fine
    if (outputs.isEmpty() && expectedOutputs.isEmpty()) {
      return;
    }

    final Errors errors = new Errors(LOG);
    // expected nothing but got something
    if (!outputs.isEmpty() && expectedOutputs.isEmpty()) {
      errors.record("Expected no output; got %d output(s).", outputs.size());
      errors.assertNone();
    }
    // expected something but got nothing
    if (outputs.isEmpty() && !expectedOutputs.isEmpty()) {
      errors.record("Expected %d output(s); got no output.",
          expectedOutputs.size());
      errors.assertNone();
    }

    // now, the smart test needs to be done
    // check that user's key and value writables implement equals, hashCode,
    // toString
    checkOverrides(outputs, expectedOutputs);

    final PairEquality<K, V> equality = new PairEquality<K, V>(keyComparator,
        valueComparator);
    if (orderMatters) {
      validateWithOrder(outputs, errors, equality);
    } else {
      validateWithoutOrder(outputs, errors, equality);
    }

    // if there are errors, it might be due to types and not clear from the
    // message
    if (!errors.isEmpty()) {
      Class<?> outputKeyClass = null;
      Class<?> outputValueClass = null;
      Class<?> expectedKeyClass = null;
      Class<?> expectedValueClass = null;

      for (Pair<K, V> output : outputs) {
        if (output.getFirst() != null) {
          outputKeyClass = output.getFirst().getClass();
        }
        if (output.getSecond() != null) {
          outputValueClass = output.getSecond().getClass();
        }
        if (outputKeyClass != null && outputValueClass != null) {
          break;
        }
      }

      for (Pair<K, V> expected : expectedOutputs) {
        if (expected.getFirst() != null) {
          expectedKeyClass = expected.getFirst().getClass();
        }
        if (expected.getSecond() != null) {
          expectedValueClass = expected.getSecond().getClass();
        }
        if (expectedKeyClass != null && expectedValueClass != null) {
          break;
        }
      }

      if (outputKeyClass != null && expectedKeyClass != null
          && !outputKeyClass.equals(expectedKeyClass)) {
        errors.record("Mismatch in key class: expected: %s actual: %s",
            expectedKeyClass, outputKeyClass);
      }

      if (outputValueClass != null && expectedValueClass != null
          && !outputValueClass.equals(expectedValueClass)) {
        errors.record("Mismatch in value class: expected: %s actual: %s",
            expectedValueClass, outputValueClass);
      }
    }
    errors.assertNone();
  }
View Full Code Here

      }
    }
  }

  protected void validate(final MockMultipleOutputs mos) {
    final Errors errors = new Errors(LOG);

    if (mos != null && !mos.isNamedOutputsEmpty()
        && expectedMultipleOutputs.isEmpty()) {
      errors.record(
          "Expected no multiple outputs; got %d named MultipleOutputs.",
          mos.getMultipleOutputsCount());
    }

    Map<String, List<Pair<?, ?>>> actuals = buildActualMultipleOutputs(mos);
    Map<String, List<Pair<?, ?>>> expects = buildExpectedMultipleOutputs();

    validateOutputList("namedOutput", errors, actuals, expects);

    actuals.clear();
    expects.clear();

    if (mos != null && !mos.isPathOutputsEmpty()
        && expectedPathOutputs.isEmpty()) {
      errors.record("Expected no pathOutputs; got %d pathOutputs.",
          mos.getPathOutputsCount());
    }

    actuals = buildActualPathOutputs(mos);
    expects = buildExpectedPathOutputs();

    validateOutputList("PathOutput", errors, actuals, expects);

    errors.assertNone();
  }
View Full Code Here

   * proper values.
   *
   * @param counterWrapper
   */
  private void validateExpectedAgainstActual(final CounterWrapper counterWrapper) {
    final Errors errors = new Errors(LOG);

    // Firstly check enumeration based counters
    for (final Pair<Enum<?>, Long> expected : expectedEnumCounters) {
      final long actualValue = counterWrapper.findCounterValue(expected
          .getFirst());

      if (actualValue != expected.getSecond()) {
        errors.record("Counter %s.%s has value %d instead of expected %d",
            expected.getFirst().getDeclaringClass().getCanonicalName(),
            expected.getFirst().toString(), actualValue, expected.getSecond());
      }
    }

    // Second string based counters
    for (final Pair<Pair<String, String>, Long> expected : expectedStringCounters) {
      final Pair<String, String> counter = expected.getFirst();

      final long actualValue = counterWrapper.findCounterValue(
          counter.getFirst(), counter.getSecond());

      if (actualValue != expected.getSecond()) {
        errors
            .record(
                "Counter with category %s and name %s has value %d instead of expected %d",
                counter.getFirst(), counter.getSecond(), actualValue,
                expected.getSecond());
      }
    }

    errors.assertNone();
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.mrunit.internal.util.Errors

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.