/**
   * Test that a work instance can report a failure
   *
   * @throws Throwable throwable exception
   */
  @Test
  public void testFailure() throws Throwable {
    Context context = new InitialContext();

    WorkConnectionFactory wcf =
        (WorkConnectionFactory) context.lookup("java:/eis/WorkConnectionFactory");

    assertNotNull(wcf);

    WorkConnection wc = wcf.getConnection();
    DistributedWorkManager dwm = null;
    try {
      assertNotNull(wc.getWorkManager());
      assertTrue(wc.getWorkManager() instanceof javax.resource.spi.work.DistributableWorkManager);

      dwm = (DistributedWorkManager) wc.getWorkManager();
      verifyConfig(dwm);
      dwm.getStatistics().clear();
      dwm.getDistributedStatistics().clear();

      wc.doWork(new MyDistributableFailureWork());

      fail("Expected WorkException");
    } catch (WorkException we) {
      assertTrue(we instanceof WorkCompletedException);

      WorkException wce = (WorkException) we;

      assertNotNull(wce.getCause());

      assertTrue(wce.getCause() instanceof RuntimeException);

      RuntimeException re = (RuntimeException) wce.getCause();

      assertEquals("FAILURE", re.getMessage());
    } finally {
      assertNotNull(dwm);
      assertNotNull(dwm.getDistributedStatistics());
      assertEquals(1, dwm.getDistributedStatistics().getWorkFailed());

      wc.close();
    }
  }
  /**
   * Test that a work instance can be executed
   *
   * @throws Throwable throwable exception
   */
  @Test
  public void testExecuted() throws Throwable {
    Context context = new InitialContext();

    WorkConnectionFactory wcf =
        (WorkConnectionFactory) context.lookup("java:/eis/WorkConnectionFactory");

    assertNotNull(wcf);

    WorkConnection wc = wcf.getConnection();
    try {
      assertNotNull(wc.getWorkManager());
      assertTrue(wc.getWorkManager() instanceof javax.resource.spi.work.DistributableWorkManager);

      DistributedWorkManager dwm = (DistributedWorkManager) wc.getWorkManager();
      verifyConfig(dwm);
      dwm.getStatistics().clear();
      dwm.getDistributedStatistics().clear();

      wc.doWork(new MyWork());
      wc.doWork(new MyDistributableWork());

      assertNotNull(dwm.getStatistics());
      assertNotNull(dwm.getDistributedStatistics());

      log.infof("Statistics: %s", dwm.getStatistics());
      log.infof("DistributedStatistics: %s", dwm.getDistributedStatistics());

      assertEquals(1, dwm.getStatistics().getWorkSuccessful());
      assertEquals(2, dwm.getDistributedStatistics().getWorkSuccessful());

      dwm.getStatistics().clear();
      assertEquals(0, dwm.getStatistics().getWorkSuccessful());

      dwm.getDistributedStatistics().clear();
      assertEquals(0, dwm.getDistributedStatistics().getWorkSuccessful());
    } finally {
      wc.close();
    }
  }