@Override
  protected void tearDown() throws Exception {
    NMR nmr = OsgiSupport.getReference(bundleContext, NMR.class);
    assertNotNull(nmr);

    ServiceMixSupport.unregisterEndpoints(nmr, endpointService1, endpointService2);

    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    registry.unregister(resolverMock, resolverProps);
    assertFalse(registry.getKeys().get(0).equals(resolverMock));

    super.tearDown();
  }
  private void setExchangeThroughNMR() {
    NMR nmr = OsgiSupport.getReference(bundleContext, NMR.class);
    assertNotNull(nmr);

    ExchangeImpl exchange = new ExchangeImpl(Pattern.InOut);
    exchange.setSource(((ChannelImpl) endpointService1.getChannel()).getEndpoint());
    exchange.getIn(true).setBody(new StringSource("<Hello/>"));

    exchange.setOperation(operation);
    exchange.setProperty(MessageExchangeImpl.INTERFACE_NAME_PROP, service2Interface);

    Map<String, Object> props = new HashMap<String, Object>();
    props.put(Endpoint.ENDPOINT_NAME, "JustDummyEndpointName");
    exchange.setTarget(nmr.getEndpointRegistry().lookup(props));

    assertEquals(endpointService2.getQueue().size(), 0);
    assertEquals(endpointService1.getQueue().size(), 0);

    assertTrue(nmr.createChannel().sendSync(exchange));

    assertEquals(endpointService2.getQueue().size(), 1);
    assertEquals(endpointService1.getQueue().size(), 0);

    List<Exchange> exchanges = new ArrayList<Exchange>();
    for (MessageExchange messageExchangeIt : mockInterceptor.getExchanges()) {
      Exchange innerExchange = ((MessageExchangeImpl) messageExchangeIt).getInternalExchange();
      if (innerExchange == exchange) {
        exchanges.add(innerExchange);
      }
    }
    assertEquals(2, exchanges.size());
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    NMR nmr = OsgiSupport.getReference(bundleContext, NMR.class);
    assertNotNull(nmr);

    endpointService1 = ServiceMixSupport.createAndRegisterEndpoint(nmr, service1, null);
    endpointService2 =
        ServiceMixSupport.createAndRegisterEndpoint(
            nmr, service2, new ExchangeProcessorImpl(service2.toString()));

    mockInterceptor = new MockInterceptor();

    Dictionary<String, String> interceptorProps = new Hashtable<String, String>();
    interceptorProps.put("role", "consumer,provider");
    interceptorProps.put("scope", "request,response");

    addRegistrationToCancel(
        bundleContext.registerService(
            Interceptor.class.getCanonicalName(), mockInterceptor, interceptorProps));
    Thread.sleep(500);

    reset(resolverMock);
  }
  @SuppressWarnings("unchecked")
  public void test5NoSuitableEndpointFound() throws Exception {
    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    registry.register(resolverMock, resolverProps);

    EndpointDescription endpointMock = createMock(EndpointDescription.class);
    ServiceDescription serviceMock = createMock(ServiceDescription.class);

    List<EndpointDescription> endpoints = new ArrayList<EndpointDescription>();
    endpoints.add(endpointMock);

    expect(resolverMock.getEndpointsFor(service2Interface, null)).andReturn(endpoints).once();
    expect(endpointMock.getServiceName()).andReturn(new QName("dummy", "dummy")).times(2);
    expect(endpointMock.getAddress()).andReturn("non-existing-one").once();
    expect(endpointMock.getName()).andReturn("endpoint-name").once();

    replay(resolverMock);
    replay(endpointMock);
    replay(serviceMock);

    try {
      setExchangeThroughNMR();
      fail("The routing of exchange must fail");
    } catch (RuntimeException e) {
      // expected result
    }

    verify(resolverMock);
    verify(endpointMock);
    verify(serviceMock);
  }
  /** @throws Exception */
  public void test3SuccessfullResolverSequence() throws Exception {
    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    registry.register(resolverMock, resolverProps);

    EndpointDescription endpointMock = createMock(EndpointDescription.class);
    ServiceDescription<?> serviceMock = createMock(ServiceDescription.class);

    List<EndpointDescription> endpoints = new ArrayList<EndpointDescription>();
    endpoints.add(endpointMock);

    expect(resolverMock.getEndpointsFor(service2Interface, null)).andReturn(endpoints).once();
    expect(endpointMock.getMetadata()).andReturn(null);
    expect(endpointMock.getServiceName()).andReturn(service2).once();

    replay(resolverMock);
    replay(endpointMock);
    replay(serviceMock);

    setExchangeThroughNMR();

    verify(resolverMock);
    verify(endpointMock);
    verify(serviceMock);
  }
  public void test7ServiceResolverNoPolicyMatching() throws Exception {
    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    ServiceResolver resolver = registry.getKeys().get(0);
    assertNotNull("Couldn't find the ServiceResolver service", resolver);

    Collection<EndpointDescription> c =
        resolver.getEndpointsFor(
            new QName("http://service.dynamic.samples.swordfish.eclipse.org/", "FlightService"));
    assertNotNull("No endpoints resolved. ", c);
    assertEquals(c.size(), 1);
    assertEquals(
        c.iterator().next().getServiceName(),
        new QName("http://service.dynamic.samples.swordfish.eclipse.org/", "FlightServiceImpl"));
  }
  public void test4FailedNoServiceDescription() throws Exception {
    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    registry.register(resolverMock, resolverProps);

    List<EndpointDescription> endpointsEmpty = new ArrayList<EndpointDescription>();

    expect(resolverMock.getEndpointsFor(service2Interface, null)).andReturn(endpointsEmpty).once();
    replay(resolverMock);

    try {
      setExchangeThroughNMR();
      fail("The routing of exchange must fail");
    } catch (RuntimeException e) {
      // expected result
    }
    verify(resolverMock);
  }
  /**
   * The testcase sends the messageExchange through the nmr. Checks if the serviceResolver was
   * actually invoked inside the nmr
   */
  public void test2SuccessfullResolverUsingStub() throws Exception {
    ServiceDescriptionStub stubDescr = new ServiceDescriptionStub(service2);
    EndpointDescriptionStub stubEndp = new EndpointDescriptionStub(stubDescr);

    List<EndpointDescription> stubEndpoints = new ArrayList<EndpointDescription>();
    stubEndpoints.add(stubEndp);

    ServiceResolverRegistry registry =
        OsgiSupport.getReference(bundleContext, ServiceResolverRegistry.class);
    registry.register(resolverMock, resolverProps);

    expect(resolverMock.getEndpointsFor(service2Interface, null)).andStubReturn(stubEndpoints);
    replay(resolverMock);

    setExchangeThroughNMR();

    verify(resolverMock);
  }