Exemplo n.º 1
0
 public <S> S getService(ServiceReference<S> serviceReference) {
   expect("Service to be available: " + serviceReference.toString())
       .within(2, TimeUnit.MINUTES)
       .until(() -> bundleCtx.getService(serviceReference), notNullValue());
   return bundleCtx.getService(serviceReference);
 }
  @Override
  public List<TestClassResult> runTest(
      final ServiceReference<Object> reference, final boolean developmentMode) {
    LOGGER.info("Test OSGI Service will be run by JUnit: " + reference.toString());
    List<TestClassResult> result = new ArrayList<TestClassResult>();
    try {
      Object service = bundleContext.getService(reference);
      String[] klassNames = (String[]) reference.getProperty(Constants.OBJECTCLASS);
      if (klassNames == null) {
        LOGGER.severe("Cannot load interface names for Junit service");
        return Collections.emptyList();
      }
      Filter developmentModeFilter = null;
      if (developmentMode) {
        developmentModeFilter = new DevelopmentModeFilter();
      }
      for (String klassName : klassNames) {

        try {
          Class<?> klass = reference.getBundle().loadClass(klassName);

          if (service == null) {
            long now = new Date().getTime();
            List<TestCaseResult> testCases =
                createErrorTestCaseResults(klass, developmentMode, now);
            result.add(
                new TestClassResult(
                    klass.getName(), 0, testCases.size(), 0, 0, now, now, testCases));
          } else {
            try {
              BlockJUnit4ObjectRunner runner = new BlockJUnit4ObjectRunner(klass, service);
              if (developmentModeFilter != null) {
                try {
                  runner.filter(developmentModeFilter);
                } catch (NoTestsRemainException e) {
                  LOGGER.warning(
                      "Skipping all methods from class "
                          + klass
                          + " due to development mode. To run the tests in development mode, annotate or "
                          + "methods or the class with @TestDuringDevelopment");
                }
              }

              RunNotifier notifier = new RunNotifier();
              ExtendedResultListener extendedResultListener = new ExtendedResultListener();
              notifier.addListener(extendedResultListener);
              runner.run(notifier);
              ExtendedResult extendedResult = extendedResultListener.getResult();
              extendedResult.finishRunning();

              List<TestCaseResult> testCaseResults = new ArrayList<TestCaseResult>();

              for (FlowTestCaseResult flowTestCaseResult : extendedResult.getTestCaseResults()) {
                TestCaseResult testCaseResult =
                    new TestCaseResult(
                        flowTestCaseResult.getMethodName(),
                        flowTestCaseResult.getStartTime(),
                        flowTestCaseResult.getFinishTime(),
                        flowTestCaseResult.getFailure());
                testCaseResults.add(testCaseResult);
              }

              long runCount = extendedResult.getRunCount() + extendedResult.getIgnoreCount();
              result.add(
                  new TestClassResult(
                      klassName,
                      runCount,
                      extendedResult.getErrorCount(),
                      extendedResult.getFailureCount(),
                      extendedResult.getIgnoreCount(),
                      extendedResult.getStartTime(),
                      extendedResult.getFinishTime(),
                      testCaseResults));
            } catch (InitializationError e) {
              handleInitializationError(developmentMode, result, klassName, klass, e);
            }
          }
        } catch (ClassNotFoundException e) {
          Object testIdObject = reference.getProperty(TestRunnerConstants.SERVICE_PROPERTY_TEST_ID);
          LOGGER.log(Level.SEVERE, "Could not load the class of the test: " + testIdObject, e);
        }
      }
    } finally {
      bundleContext.ungetService(reference);
    }

    return result;
  }