/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

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