Ejemplo n.º 1
0
  /**
   * Tests the API using the supplied tester instance. First the API is tested with non-null
   * parameters, then with null parameters and finally the failure of the API is tested.
   *
   * @param inTester the tester to test the API invocation.
   * @param <R> the return type of the API.
   * @throws Exception if there were unexpected errors
   */
  private static <R> void testAPI(final WSTester<R> inTester) throws Exception {
    // Test a regular invocation.
    R value = inTester.setReturnValue(false);
    verifyEquals(value, inTester.invokeApi(false));
    inTester.verifyInputParams(false);

    // Test invocation with nulls
    resetServiceParameters();
    value = inTester.setReturnValue(true);
    verifyEquals(value, inTester.invokeApi(true));
    inTester.verifyInputParams(true);

    // Test a failure
    resetServiceParameters();
    I18NException failure = new I18NException(new I18NMessage0P(Messages.LOGGER, "test"));
    getMockSAService().setFailure(failure);
    inTester.setReturnValue(false);
    ConnectionException e =
        new ExpectedFailure<ConnectionException>() {
          @Override
          protected void run() throws Exception {
            inTester.invokeApi(false);
          }
        }.getException();
    assertNotNull(e.getCause());
    assertEquals(failure, e.getCause());
    // Verify that input parameters were received even when failure occured.
    inTester.verifyInputParams(false);

    // Test service interruption
    verifyInvocationCannotBeInterrupted(inTester);
  }
Ejemplo n.º 2
0
 private static <R> void verifyInvocationCannotBeInterrupted(final WSTester<R> inTester)
     throws Exception {
   resetServiceParameters();
   getMockSAService().setSleep(true);
   inTester.setReturnValue(false);
   final Semaphore sema = new Semaphore(0);
   final AtomicReference<Exception> interruptFailure = new AtomicReference<Exception>();
   Thread t =
       new Thread() {
         @Override
         public void run() {
           sema.release();
           try {
             inTester.invokeApi(false);
           } catch (Exception ex) {
             interruptFailure.set(ex);
           }
         }
       };
   t.start();
   // Wait for the thread to be started
   sema.acquire();
   // Interrupt it as soon as it is found started
   t.interrupt();
   // wait for it to end
   t.join();
   // verify that we are not able to interrupt it
   assertNull("API invocation got interrupted!", interruptFailure.get());
 }