예제 #1
0
  /**
   * Handles test fail.
   *
   * @param result Test result.
   * @param origTest Original JUnit test.
   * @param e Exception thrown from grid.
   */
  private void handleFail(TestResult result, GridJunit3SerializableTest origTest, Throwable e) {
    // Simulate that all tests were run.
    origTest.getTest().run(result);

    // For the tests suite we assume that all tests failed because
    // entire test suite execution failed and there is no way to get
    // broken tests.
    if (origTest.getTest() instanceof GridJunit3TestSuiteProxy) {
      TestSuite suite = (((TestSuite) origTest.getTest()));

      for (int j = 0; j < suite.testCount(); j++) {
        result.addError(suite.testAt(j), e);
      }
    } else if (origTest.getTest() instanceof GridJunit3TestCaseProxy) {
      result.addError(origTest.getTest(), e);
    }
  }
예제 #2
0
  /** {@inheritDoc} */
  @Override
  public GridJunit3SerializableTest execute() throws GridException {
    final GridJunit3SerializableTest testArg = argument(0);

    assert testArg != null;

    TestResult collector = new TestResult();

    collector.addListener(
        new TestListener() {
          /** */
          private GridTestPrintStream out;

          /** */
          private GridTestPrintStream err;

          /** {@inheritDoc} */
          @Override
          public void addError(Test test, Throwable e) {
            assert test instanceof TestCase : "Errors can be added only to TestCases: " + test;

            testArg.findTestCase((TestCase) test).setError(e);
          }

          /** {@inheritDoc} */
          @Override
          public void addFailure(Test test, AssertionFailedError e) {
            assert test instanceof TestCase : "Failures can be added only to TestCases: " + test;

            testArg.findTestCase((TestCase) test).setFailure(e);
          }

          /** {@inheritDoc} */
          @Override
          public void startTest(Test test) {
            GridTestPrintStreamFactory.getStdOut()
                .println("Distributed test started: " + getTestName(test));

            out = GridTestPrintStreamFactory.acquireOut();
            err = GridTestPrintStreamFactory.acquireErr();
          }

          /** {@inheritDoc} */
          @Override
          public void endTest(Test test) {
            GridJunit3SerializableTestCase testCase = testArg.findTestCase((TestCase) test);

            try {
              testCase.setStandardOut(getBytes(out));
              testCase.setStandardError(getBytes(err));
            } catch (IOException e) {
              U.error(log, "Error resetting output.", e);

              if (testCase.getError() == null) {
                testCase.setError(e);
              } else if (testCase.getFailure() == null) {
                testCase.setFailure(e);
              } else {
                // Override initial error.
                testCase.setError(e);
              }
            }

            GridTestPrintStreamFactory.releaseOut();
            GridTestPrintStreamFactory.releaseErr();

            out = null;
            err = null;

            GridTestPrintStreamFactory.getStdOut()
                .println("Distributed test finished: " + getTestName(test));
          }

          /**
           * @param out Output stream to gen bytes.
           * @return Output bytes.
           * @throws IOException If error occur.
           */
          private byte[] getBytes(GridTestPrintStream out) throws IOException {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

            out.purge(byteOut);

            return byteOut.toByteArray();
          }

          /**
           * @param test JUnit3 test.
           * @return Test name.
           */
          private String getTestName(Test test) {
            if (test instanceof TestSuite) {
              return ((TestSuite) test).getName();
            }

            return test.getClass().getName() + '.' + ((TestCase) test).getName();
          }
        });

    // Run Junits.
    testArg.getTest().run(collector);

    return testArg;
  }
예제 #3
0
  /**
   * Runs all tests belonging to this test suite on the grid.
   *
   * @param result Test result collector.
   */
  @Override
  public void run(TestResult result) {
    if (isDisabled) {
      copy.run(result);
    } else {
      GridTestRouter router = createRouter();

      Grid grid = startGrid();

      try {
        List<GridTaskFuture<?>> futs = new ArrayList<GridTaskFuture<?>>(testCount());

        List<GridJunit3SerializableTest> tests =
            new ArrayList<GridJunit3SerializableTest>(testCount());

        for (int i = 0; i < testCount(); i++) {
          Test junit = testAt(i);

          GridJunit3SerializableTest test;

          if (junit instanceof TestSuite) {
            test = new GridJunit3SerializableTestSuite((TestSuite) junit);
          } else {
            assert junit instanceof TestCase
                : "Test must be either TestSuite or TestCase: " + junit;

            test = new GridJunit3SerializableTestCase((TestCase) junit);
          }

          tests.add(test);

          if (clsLdr == null) {
            clsLdr = U.detectClassLoader(junit.getClass());
          }

          futs.add(
              grid.execute(
                  new GridJunit3Task(junit.getClass(), clsLdr),
                  new GridJunit3Argument(router, test, locTests.contains(test.getName())),
                  timeout));
        }

        for (int i = 0; i < testCount(); i++) {
          GridTaskFuture<?> fut = futs.get(i);

          GridJunit3SerializableTest origTest = tests.get(i);

          try {
            GridJunit3SerializableTest resTest = (GridJunit3SerializableTest) fut.get();

            origTest.setResult(resTest);

            origTest.getTest().run(result);
          } catch (GridException e) {
            handleFail(result, origTest, e);
          }
        }
      } finally {
        stopGrid();
      }
    }
  }