Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.hadoop.mrunit;
 
 
 import java.util.List;
 
 
 public abstract class TestDriver<K1, V1, K2, V2> {
 
   public static final Log LOG = LogFactory.getLog(TestDriver.class);
 
   protected List<Pair<K2, V2>> expectedOutputs;
   
   protected Configuration configuration;
 
   public TestDriver() {
      = new ArrayList<Pair<K2, V2>>();
      = new Configuration();
   }

  

Returns:
the list of (k, v) pairs expected as output from this driver
 
   public List<Pair<K2, V2>> getExpectedOutputs() {
     return ;
   }

  
Clears the list of outputs expected from this driver
 
   public void resetOutput() {
   }

  
Runs the test but returns the result set instead of validating it (ignores any addOutput(), etc calls made before this)

Returns:
the list of (k, v) pairs returned as output from the test
 
   public abstract List<Pair<K2, V2>> run() throws IOException;

  
Runs the test and validates the results

Returns:
void if the tests passed
Throws:
java.lang.RuntimeException if they don't *
 
   public abstract void runTest() throws RuntimeException;

  
Split "key \t val" into Pair(Text(key), Text(val))

Parameters:
tabSeparatedPair
 
   public static Pair<TextTextparseTabbedPair(String tabSeparatedPair) {
 
     String keyval;
 
     if (null == tabSeparatedPair) {
       return null;
     }
 
     int split = tabSeparatedPair.indexOf('\t');
     if (-1 == split) {
       return null;
     }
 
     key = tabSeparatedPair.substring(0, split);
     val = tabSeparatedPair.substring(split + 1);
 
     return new Pair<TextText>(new Text(key), new Text(val));
   }

  
Split "val,val,val,val..." into a List of Text(val) objects.

Parameters:
commaDelimList A list of values separated by commas
 
  protected static List<TextparseCommaDelimitedList(String commaDelimList) {
    ArrayList<TextoutList = new ArrayList<Text>();
    if (null == commaDelimList) {
      return null;
    }
    int len = commaDelimList.length();
    int curPos = 0;
    int curComma = commaDelimList.indexOf(',');
    if (curComma == -1) {
      curComma = len;
    }
    while (curPos < len) {
      outList.add(new Text(
              commaDelimList.substring(curPoscurComma).trim()));
      curPos = curComma + 1;
      curComma = commaDelimList.indexOf(','curPos);
      if (curComma == -1) {
        curComma = len;
      }
    }
    return outList;
  }

  
check the outputs against the expected inputs in record

Parameters:
outputs The actual output (k, v) pairs from the Mapper
Returns:
void if they all pass
Throws:
java.lang.RuntimeException if they don't
  protected void validate(List<Pair<K2, V2>> outputsthrows RuntimeException {
    boolean success = true;
    // were we supposed to get output in the first place?
    // return false if we don't.
    if (.size() == 0 && outputs.size() > 0) {
      .error("Expected no outputs; got " + outputs.size() + " outputs.");
      success = false;
    }
    // make sure all actual outputs are in the expected set,
    // and at the proper position.
    for (int i = 0; i < outputs.size(); i++) {
      Pair<K2, V2> actual = outputs.get(i);
      success = lookupExpectedValue(actuali) && success;
    }
    // make sure all expected outputs were accounted for.
    if (.size() != outputs.size() || !success) {
      // something is unaccounted for. Figure out what.
      ArrayList<Pair<K2, V2>> actuals = new ArrayList<Pair<K2, V2>>();
      actuals.addAll(outputs);
      for (int i = 0; i < .size(); i++) {
        Pair<K2, V2> expected = .get(i);
        boolean found = false;
        for (int j = 0; j < actuals.size() && !foundj++) {
          Pair<K2, V2> actual = actuals.get(j);
          if (actual.equals(expected)) {
            // don't match against this actual output again
            actuals.remove(j);
            found = true;
          }
        }
        if (!found) {
          String expectedStr = "(null)";
          if (null != expected) {
            expectedStr = expected.toString();
          }
          .error("Missing expected output " + expectedStr + " at position "
              + i);
        }
      }
      success = false;
    }
    if (!success) {
      throw new RuntimeException();
    }
  }

  
Part of the validation system.

Parameters:
actualVal A (k, v) pair we got from the Mapper
actualPos The position of this pair in the actual output
Returns:
true if the expected val at 'actualPos' in the expected list equals actualVal
  private boolean lookupExpectedValue(Pair<K2, V2> actualValint actualPos) {
    // first: Do we have the success condition?
    if (.size() > actualPos
            && .get(actualPos).equals(actualVal)) {
      .debug("Matched expected output " + actualVal.toString()
          + " at position " + actualPos);
      return true;
    }
    // second: can we find this output somewhere else in
    // the expected list?
    boolean foundSomewhere = false;
    for (int i = 0; i < .size() && !foundSomewherei++) {
      Pair<K2, V2> expected = .get(i);
      if (expected.equals(actualVal)) {
        .error("Matched expected output "
                + actualVal.toString() + " but at incorrect position "
                + actualPos + " (expected position " + i + ")");
        foundSomewhere = true;
      }
    }
    if (!foundSomewhere) {
      .error("Received unexpected output " + actualVal.toString());
    }
    return false;
  }
  protected static void formatValueList(List valuesStringBuilder sb) {
    sb.append("(");
    if (null != values) {
      boolean first = true;
      for (Object val : values) {
        if (!first) {
          sb.append(", ");
        }
        first = false;
        sb.append(val.toString());
      }
    }
    sb.append(")");
  }

  

Returns:
The configuration object that will given to the mapper and/or reducer associated with the driver (new API only)
    return ;
  }
  
  

Parameters:
configuration The configuration object that will given to the mapper and/or reducer associated with the driver (new API only)
  public void setConfiguration(Configuration configuration) {
    this. = configuration;
  }
New to GrepCode? Check out our FAQ X