Exemple #1
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;
  }