Esempio n. 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);
    }
  }
Esempio n. 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;
  }