@After
 public void tearDown() throws Exception {
   if (null != greeter) {
     ((java.io.Closeable) greeter).close();
     assertTrue("Failed to stop greeter.", control.stopGreeter(null));
     greeterBus.shutdown(true);
     greeterBus = null;
   }
   if (null != control) {
     assertTrue("Failed to stop greeter", control.stopGreeter(null));
     ((java.io.Closeable) control).close();
     controlBus.shutdown(true);
   }
 }
  private void setupGreeter(String cfgResource, boolean useDecoupledEndpoint)
      throws NumberFormatException, MalformedURLException {

    SpringBusFactory bf = new SpringBusFactory();

    controlBus = bf.createBus();
    BusFactory.setDefaultBus(controlBus);

    ControlService cs = new ControlService();
    control = cs.getControlPort();
    updateAddressPort(control, PORT);

    assertTrue("Failed to start greeter", control.startGreeter(cfgResource));

    greeterBus = bf.createBus(cfgResource);
    BusFactory.setDefaultBus(greeterBus);
    LOG.fine("Initialised greeter bus with configuration: " + cfgResource);

    if (null == comparator) {
      comparator = new PhaseComparator();
    }
    if (null == inPhases) {
      inPhases = new ArrayList<Phase>();
      inPhases.addAll(greeterBus.getExtension(PhaseManager.class).getInPhases());
      Collections.sort(inPhases, comparator);
    }
    if (null == preLogicalPhase) {
      preLogicalPhase = getPhase(Phase.PRE_LOGICAL);
    }

    GreeterService gs = new GreeterService();

    greeter = gs.getGreeterPort();
    updateAddressPort(greeter, PORT);
    LOG.fine("Created greeter client.");

    if (!useDecoupledEndpoint) {
      return;
    }

    // programatically configure decoupled endpoint that is guaranteed to
    // be unique across all test cases
    decoupledEndpointPort++;
    decoupledEndpoint =
        "http://localhost:"
            + allocatePort("decoupled-" + decoupledEndpointPort)
            + "/decoupled_endpoint";

    Client c = ClientProxy.getClient(greeter);
    HTTPConduit hc = (HTTPConduit) (c.getConduit());
    HTTPClientPolicy cp = hc.getClient();
    cp.setDecoupledEndpoint(decoupledEndpoint);

    LOG.fine("Using decoupled endpoint: " + cp.getDecoupledEndpoint());
  }
  private void testFail(FaultLocation location, boolean usingAddressing, boolean robust)
      throws PingMeFault {
    // System.out.print("Test interceptor failing in phase: " + location.getPhase());

    control.setFaultLocation(location);

    // oneway reports a plain fault (although server sends a soap fault)

    boolean expectOnewayFault =
        robust || comparator.compare(preLogicalPhase, getPhase(location.getPhase())) > 0;

    try {
      greeter.greetMeOneWay("oneway");
      if (expectOnewayFault) {
        fail("Oneway operation unexpectedly succeded for phase " + location.getPhase());
      }
    } catch (WebServiceException ex) {
      if (!expectOnewayFault) {
        fail("Oneway operation unexpectedly failed.");
      }
      assertEquals(FAULT_MESSAGE, ex.getMessage());
    }

    String expectedMsg = getExpectedInterceptorFaultMessage(location.getPhase());
    try {
      greeter.greetMe("cxf");
      fail("Twoway operation unexpectedly succeded.");
    } catch (WebServiceException ex) {
      Throwable cause = ex.getCause();
      SoapFault sf = (SoapFault) cause;

      assertEquals(expectedMsg, sf.getReason());
      assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
    }

    try {
      greeter.pingMe();
      fail("Expected PingMeFault not thrown.");
    } catch (WebServiceException ex) {
      Throwable cause = ex.getCause();
      SoapFault sf = (SoapFault) cause;
      assertEquals(expectedMsg, sf.getReason());
      assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
    }
  }
  private void testWithAddressingAnonymousReplies(boolean robust) throws Exception {
    setupGreeter("org/apache/cxf/systest/interceptor/addr.xml", false);

    control.setRobustInOnlyMode(robust);

    // all interceptors pass
    testInterceptorsPass(robust);

    // test failure in phases before Phase.PRE_LOGICAL
    FaultLocation location =
        new org.apache.cxf.greeter_control.types.ObjectFactory().createFaultLocation();
    location.setAfter(MAPAggregator.class.getName());

    // test failure occuring before and after logical addressing interceptor
    // won't get a fault in case of oneways non-robust for the latter (partial response already
    // sent)
    testInterceptorFail(inPhases, location, robust);
  }
  private void testWithoutAddressing(boolean robust) throws Exception {

    setupGreeter("org/apache/cxf/systest/interceptor/no-addr.xml", false);

    control.setRobustInOnlyMode(robust);

    // all interceptors pass
    testInterceptorsPass(robust);

    // behaviour is identicial for all phases
    FaultLocation location =
        new org.apache.cxf.greeter_control.types.ObjectFactory().createFaultLocation();

    // test failure occuring before and after logical addressing interceptor
    // won't get a fault in case of oneways non-robust for the latter (partial response already
    // sent)
    testInterceptorFail(inPhases, location, robust);
  }