PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 1ms

/testability-explorer/src/test/java/com/google/test/metric/TestabilityRunnerTest.java

http://testability-explorer.googlecode.com/
Java | 109 lines | 61 code | 16 blank | 32 comment | 0 complexity | 06bcd73263ff8fb04857674a258de699 MD5 | raw file
  1package com.google.test.metric;
  2
  3import com.google.classpath.ClassPath;
  4import com.google.classpath.ClassPathFactory;
  5import com.google.test.metric.report.ReportGenerator;
  6import com.google.test.metric.report.TextReportGenerator;
  7
  8import java.io.ByteArrayOutputStream;
  9import java.io.PrintStream;
 10import java.util.Arrays;
 11import java.util.List;
 12
 13public class TestabilityRunnerTest extends AutoFieldClearTestCase {
 14  /**
 15   * Directories to be used for testing that contains class files, for testing.
 16   * These are included in subversion so that any checkout will have a consistent
 17   * environment for testing.
 18   */
 19  public static final String CLASSES_FOR_TEST = "src/test/classes";
 20
 21  /**
 22   * Directory root that contains one class with no external
 23   * dependencies.
 24   */
 25  public static final String CLASS_NO_EXTERNAL_DEPS = CLASSES_FOR_TEST + "/root1";
 26
 27  /**
 28   * Directory root containing classes that extend from, and reference, external
 29   * classes outside of this directory.
 30   */
 31  public static final String CLASSES_EXTERNAL_DEPS_AND_SUPERCLASSES = CLASSES_FOR_TEST + "/root2";
 32
 33  /**
 34   * Directory root containing classes extending from Object that reference
 35   * external classes outside of this directory.
 36   */
 37  public static final String CLASSES_EXTERNAL_DEPS_NO_SUPERCLASSES = CLASSES_FOR_TEST + "/root3";
 38
 39  private ByteArrayOutputStream out = new ByteArrayOutputStream();
 40  private ByteArrayOutputStream err = new ByteArrayOutputStream();
 41  private List<String> allEntryList = Arrays.asList("");
 42  private ReportGenerator report = new TextReportGenerator(new PrintStream(out), new CostModel(), 0, 0, 0);
 43  private RegExpWhiteList whiteList = new RegExpWhiteList("java.");
 44  private PrintStream errStream = new PrintStream(err);
 45
 46  public void testClassesNotInClasspath() throws Exception {
 47    JavaTestabilityRunner runner = runnerFor(CLASSES_EXTERNAL_DEPS_AND_SUPERCLASSES);
 48    runner.run();
 49    assertTrue(out.toString().length() > 0);
 50    final String errStr = err.toString();
 51    assertTrue(errStr, errStr.length() > 0);
 52    assertTrue(errStr, errStr.startsWith("WARNING: can not analyze class "));
 53    assertTrue(errStr, errStr.contains("'com.google.test.metric.ClassInfoTest'"));
 54    assertTrue(errStr, errStr.contains("'com.google.test.metric.x.SelfTest'"));
 55    assertTrue(errStr, errStr.contains("'com.google.test.metric.ClassRepositoryTestCase'"));
 56  }
 57
 58  /*
 59   * The given classpath contains some classes from this project, but not all.
 60   * There are many references to classes that will not be in this test's -cp
 61   * classpath. This test verifies that when the ClassRepository encounters a
 62   * ClassNotFoundException, it continues nicely and prints the values for the
 63   * classes that it <em>does</em> find.
 64   */
 65  public void testIncompleteClasspath() throws Exception {
 66    JavaTestabilityRunner runner = runnerFor(CLASSES_EXTERNAL_DEPS_AND_SUPERCLASSES);
 67    runner.run();
 68    assertTrue(out.toString(), out.toString().length() > 0);
 69    assertTrue(err.toString(), err.toString().length() > 0);
 70  }
 71
 72  /*
 73   * Tries calculating the cost for classes that reference other classes not in
 74   * the classpath.
 75   */
 76  public void testForWarningWhenClassesRecurseToIncludeClassesOutOfClasspath()
 77      throws Exception {
 78    JavaTestabilityRunner runner = runnerFor(CLASSES_EXTERNAL_DEPS_NO_SUPERCLASSES);
 79    runner.run();
 80
 81    assertTrue(out.toString(), out.toString().length() > 0);
 82    assertTrue(err.toString(), err.toString().length() > 0);
 83    assertTrue(err.toString(), err.toString().startsWith("WARNING: class not found: "));
 84  }
 85
 86  /*
 87   * Tries calculating the cost for classes that extend from another class,
 88   * which does not exist in the classpath.
 89   */
 90  public void testForWarningWhenClassExtendsFromClassOutOfClasspath()
 91      throws Exception {
 92    JavaTestabilityRunner runner = runnerFor(CLASSES_EXTERNAL_DEPS_AND_SUPERCLASSES);
 93    runner.run();
 94
 95    assertTrue(out.toString().length() > 0);
 96    assertTrue(err.toString().length() > 0);
 97    assertTrue(err.toString().startsWith("WARNING: can not analyze class "));
 98  }
 99
100
101  private JavaTestabilityRunner runnerFor(String path) {
102    ClassPath classPath = new ClassPathFactory().createFromPaths(path, "core/" + path);
103    ClassRepository classRepository = new JavaClassRepository(classPath);
104    MetricComputer computer = new MetricComputer(classRepository, errStream, whiteList, 0);
105    return new JavaTestabilityRunner(report, classPath, classRepository, computer,
106        allEntryList, whiteList, new PrintStream(err));
107  }
108
109}