Exemple #1
0
 public static void runInMain(Class<?> testClass, String[] args)
     throws InitializationError, NoTestsRemainException {
   JUnitCore core = new JUnitCore();
   core.addListener(new TextListener(System.out));
   SLTestRunner suite = new SLTestRunner(testClass);
   if (args.length > 0) {
     suite.filter(new NameFilter(args[0]));
   }
   Result r = core.run(suite);
   if (!r.wasSuccessful()) {
     System.exit(1);
   }
 }
  /**
   * Tests are created based on the files that exist in the directory specified in the passed in
   * annotation. Each test must have a source file and an expected output file. Optionally, each
   * test can also include an input file. Source files have an ".sl" extension. Expected output have
   * a ".output" extension. Input files have an ".input" extension. All these files must share the
   * same base name to be correctly grouped. For example: "test1_assnCount.sl",
   * "test1_assnCount.output" and "test1_assnCount.input" would all be used to create a single test
   * called "test1_assnCount".
   *
   * <p>This method iterates over the files in the directory and creates a new InstrumentTestCase
   * for each group of related files. Each file is also expected to end with an identified at the
   * end of the base name to indicate what visitor needs to be attached. Currently, visitors are
   * hard coded to work on specific lines, so the code here is not currently generalizable.
   *
   * @param c The annotation containing the directory with tests
   * @return A list of {@link InstrumentTestCase}s to run.
   * @throws IOException If the directory is invalid.
   * @throws InitializationError If no directory is provided.
   * @see #runChild(InstrumentTestCase, RunNotifier)
   */
  protected static List<InstrumentTestCase> createTests(final Class<?> c)
      throws IOException, InitializationError {
    SLInstrumentTestSuite suite = c.getAnnotation(SLInstrumentTestSuite.class);
    if (suite == null) {
      throw new InitializationError(
          String.format(
              "@%s annotation required on class '%s' to run with '%s'.",
              SLInstrumentTestSuite.class.getSimpleName(),
              c.getName(),
              SLInstrumentTestRunner.class.getSimpleName()));
    }

    String[] paths = suite.value();

    Path root = SLTestRunner.getRootViaResourceURL(c, paths);
    if (root == null) {
      for (String path : paths) {
        root = FileSystems.getDefault().getPath(path);
        if (Files.exists(root)) {
          break;
        }
      }
    }
    if (root == null && paths.length > 0) {
      throw new FileNotFoundException(paths[0]);
    }

    final Path rootPath = root;

    final List<InstrumentTestCase> testCases = new ArrayList<>();

    // Scaffolding in place for future automation
    Files.walkFileTree(
        rootPath,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs)
              throws IOException {
            String sourceName = sourceFile.getFileName().toString();
            if (sourceName.endsWith(SOURCE_SUFFIX)) {
              String baseName =
                  sourceName.substring(0, sourceName.length() - SOURCE_SUFFIX.length());

              Path inputFile = sourceFile.resolveSibling(baseName + INPUT_SUFFIX);
              String testInput = "";
              if (Files.exists(inputFile)) {
                testInput = readAllLines(inputFile);
              }

              Path outputFile = sourceFile.resolveSibling(baseName + OUTPUT_SUFFIX);
              String expectedOutput = "";
              if (Files.exists(outputFile)) {
                expectedOutput = readAllLines(outputFile);
              }

              testCases.add(
                  new InstrumentTestCase(
                      c, baseName, sourceName, sourceFile, testInput, expectedOutput));
            }
            return FileVisitResult.CONTINUE;
          }
        });

    return testCases;
  }