diff options
author | mkuppe | 2009-02-25 14:12:14 +0000 |
---|---|---|
committer | mkuppe | 2009-02-25 14:12:14 +0000 |
commit | d255394a8d4a304a6e0e022299d9de07cf4e1dda (patch) | |
tree | 40d9e3497e99436cbe3a9fea149cb3ff43b1a276 | |
parent | 9d4af68b1d32670d9e07e1b7edcb28d01032fd18 (diff) | |
download | org.eclipse.ecf-d255394a8d4a304a6e0e022299d9de07cf4e1dda.tar.gz org.eclipse.ecf-d255394a8d4a304a6e0e022299d9de07cf4e1dda.tar.xz org.eclipse.ecf-d255394a8d4a304a6e0e022299d9de07cf4e1dda.zip |
NEW - bug 254832: [Discovery][SLP] Provider may cache discovered services for lifetime
https://bugs.eclipse.org/bugs/show_bug.cgi?id=254832
NEW - bug 257861: Split org.eclipse.ecf.discovery.IDiscoveryContainerAdapter into "Locator" and a "Advertiser"
https://bugs.eclipse.org/bugs/show_bug.cgi?id=257861
7 files changed, 338 insertions, 170 deletions
diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/META-INF/MANIFEST.MF b/tests/bundles/org.eclipse.ecf.tests.discovery/META-INF/MANIFEST.MF index b1c9a16ed..099c6186c 100755 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/META-INF/MANIFEST.MF +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/META-INF/MANIFEST.MF @@ -14,7 +14,8 @@ Eclipse-LazyStart: true Export-Package: org.eclipse.ecf.tests.discovery, org.eclipse.ecf.tests.discovery.identity, org.eclipse.ecf.tests.discovery.listener -Import-Package: org.osgi.framework, +Import-Package: org.eclipse.equinox.concurrent.future;version="1.0.0", + org.osgi.framework, org.osgi.util.tracker;version="1.3.0" Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: CDC-1.1/Foundation-1.1, diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/AbstractDiscoveryTest.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/AbstractDiscoveryTest.java index c118743db..5f3cb07ea 100755 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/AbstractDiscoveryTest.java +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/AbstractDiscoveryTest.java @@ -13,59 +13,47 @@ package org.eclipse.ecf.tests.discovery; import java.net.URI; import java.util.Comparator; import java.util.Properties; +import java.util.Random; + +import junit.framework.TestCase; -import org.eclipse.ecf.core.ContainerCreateException; -import org.eclipse.ecf.core.ContainerFactory; -import org.eclipse.ecf.core.IContainer; import org.eclipse.ecf.core.identity.IDFactory; -import org.eclipse.ecf.discovery.IDiscoveryContainerAdapter; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; import org.eclipse.ecf.discovery.IServiceInfo; import org.eclipse.ecf.discovery.ServiceInfo; import org.eclipse.ecf.discovery.ServiceProperties; import org.eclipse.ecf.discovery.identity.IServiceID; import org.eclipse.ecf.discovery.identity.ServiceIDFactory; -import junit.framework.TestCase; - -public class AbstractDiscoveryTest extends TestCase { +public abstract class AbstractDiscoveryTest extends TestCase { + private final Random random; protected IServiceInfo serviceInfo; - protected IServiceInfo serviceInfo2; - protected IServiceInfo serviceInfo3; - protected String containerUnderTest; - protected long waitTimeForProvider = 1000; - protected Comparator comparator = new ServiceInfoComparator(); protected String protocol = DiscoveryTestHelper.PROTOCOL; protected String scope = DiscoveryTestHelper.SCOPE; protected String namingAuthority = DiscoveryTestHelper.NAMINGAUTHORITY; - protected IContainer container = null; - protected IDiscoveryContainerAdapter discoveryContainer = null; - protected int eventsToExpect = 1; + protected Comparator comparator = new ServiceInfoComparator(); + + protected String containerUnderTest; + protected IDiscoveryLocator discoveryLocator = null; + protected IDiscoveryAdvertiser discoveryAdvertiser = null; public AbstractDiscoveryTest(String name) { super(); this.containerUnderTest = name; + this.random = new Random(); } - protected IDiscoveryContainerAdapter getAdapter(Class clazz) { - final IDiscoveryContainerAdapter adapter = (IDiscoveryContainerAdapter) container.getAdapter(clazz); - assertNotNull("Adapter must not be null", adapter); - return adapter; - } + protected abstract IDiscoveryLocator getDiscoveryLocator(); - protected IContainer getContainer(String containerUnderTest) throws ContainerCreateException { - return ContainerFactory.getDefault().createContainer(containerUnderTest); - } + protected abstract IDiscoveryAdvertiser getDiscoveryAdvertiser(); protected IServiceID createServiceID(String serviceType, String serviceName) throws Exception { - return ServiceIDFactory.getDefault().createServiceID(discoveryContainer.getServicesNamespace(), serviceType, serviceName); + return ServiceIDFactory.getDefault().createServiceID(discoveryLocator.getServicesNamespace(), serviceType, serviceName); } - protected void setWaitTimeForProvider(long aWaitTimeForProvider) { - this.waitTimeForProvider = aWaitTimeForProvider + (aWaitTimeForProvider * 1 / 2); - } - protected void setComparator(Comparator comparator) { this.comparator = comparator; } @@ -91,40 +79,25 @@ public class AbstractDiscoveryTest extends TestCase { assertNotNull(containerUnderTest); assertTrue(containerUnderTest.startsWith("ecf.discovery.")); - container = getContainer(containerUnderTest); - discoveryContainer = getAdapter(IDiscoveryContainerAdapter.class); - assertEquals("Container and DiscoveryContainer must be same", container, discoveryContainer); - - assertNotNull(container); - assertNotNull(discoveryContainer); + discoveryLocator = getDiscoveryLocator(); + discoveryAdvertiser = getDiscoveryAdvertiser(); + assertNotNull(discoveryLocator); + assertNotNull(discoveryAdvertiser); final Properties props = new Properties(); final URI uri = DiscoveryTestHelper.createDefaultURI(); - IServiceID serviceID = (IServiceID) IDFactory.getDefault().createID(discoveryContainer.getServicesNamespace(), new Object[] {getServiceType(), DiscoveryTestHelper.getHost()}); + IServiceID serviceID = (IServiceID) IDFactory.getDefault().createID(discoveryLocator.getServicesNamespace(), new Object[] {getServiceType(), DiscoveryTestHelper.getHost()}); assertNotNull(serviceID); final ServiceProperties serviceProperties = new ServiceProperties(props); - serviceProperties.setPropertyString(DiscoveryTest.class.getName() + "servicePropertiesString", "serviceProperties"); - serviceProperties.setProperty(DiscoveryTest.class.getName() + "servicePropertiesIntegerMax", new Integer(Integer.MIN_VALUE)); - serviceProperties.setProperty(DiscoveryTest.class.getName() + "servicePropertiesIntegerMin", new Integer(Integer.MAX_VALUE)); - serviceProperties.setProperty(DiscoveryTest.class.getName() + "servicePropertiesBoolean", new Boolean(false)); - serviceProperties.setPropertyBytes(DiscoveryTest.class.getName() + "servicePropertiesByte", new byte[]{-127, -126, -125, 0, 1, 2, 3, 'a', 'b', 'c', 'd', 126, 127}); + serviceProperties.setPropertyString(getClass() + "testIdentifier", Long.toString(random.nextLong())); + serviceProperties.setPropertyString(getName() + "servicePropertiesString", "serviceProperties"); + serviceProperties.setProperty(getName() + "servicePropertiesIntegerMax", new Integer(Integer.MIN_VALUE)); + serviceProperties.setProperty(getName() + "servicePropertiesIntegerMin", new Integer(Integer.MAX_VALUE)); + serviceProperties.setProperty(getName() + "servicePropertiesBoolean", new Boolean(false)); + serviceProperties.setPropertyBytes(getName() + "servicePropertiesByte", new byte[]{-127, -126, -125, 0, 1, 2, 3, 'a', 'b', 'c', 'd', 126, 127}); serviceInfo = new ServiceInfo(uri, serviceID, 1, 1, serviceProperties); assertNotNull(serviceInfo); - - IServiceID serviceID2 = (IServiceID) IDFactory.getDefault().createID(discoveryContainer.getServicesNamespace(), new Object[] {"_service._ecf._tests2._fooProtocol.fooScope._fooNA", DiscoveryTestHelper.getHost()}); - assertNotNull(serviceID); - final ServiceProperties serviceProperties2 = new ServiceProperties(props); - serviceProperties2.setPropertyString("serviceProperties2", "serviceProperties2"); - serviceInfo2 = new ServiceInfo(uri, serviceID2, 2, 2, serviceProperties2); - assertNotNull(serviceInfo2); - - IServiceID serviceID3 = (IServiceID) IDFactory.getDefault().createID(discoveryContainer.getServicesNamespace(), new Object[] {"_service._ecf._tests3._barProtocol.barScope._barNA", DiscoveryTestHelper.getHost()}); - assertNotNull(serviceID); - final ServiceProperties serviceProperties3 = new ServiceProperties(props); - serviceProperties3.setPropertyString("serviceProperties3", "serviceProperties3"); - serviceInfo3 = new ServiceInfo(uri, serviceID3, 3, 3, serviceProperties3); - assertNotNull(serviceInfo3); } } diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/Activator.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/Activator.java index 6cf5c3d80..fdc16fc45 100755 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/Activator.java +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/Activator.java @@ -10,9 +10,12 @@ *****************************************************************************/ package org.eclipse.ecf.tests.discovery; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; import org.eclipse.ecf.discovery.service.IDiscoveryService; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; /** @@ -26,7 +29,8 @@ public class Activator implements BundleActivator { // The shared instance private static Activator plugin; - private ServiceTracker tracker; + private ServiceTracker locatorTracker; + private ServiceTracker advertiserTracker; private BundleContext context; @@ -37,8 +41,10 @@ public class Activator implements BundleActivator { public void start(BundleContext aContext) throws Exception { plugin = this; context = aContext; - tracker = new ServiceTracker(aContext, IDiscoveryService.class.getName(), null); - tracker.open(); + locatorTracker = new ServiceTracker(aContext, IDiscoveryLocator.class.getName(), null); + locatorTracker.open(); + advertiserTracker = new ServiceTracker(aContext, IDiscoveryAdvertiser.class.getName(), null); + advertiserTracker.open(); } /* @@ -46,9 +52,13 @@ public class Activator implements BundleActivator { * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { - if (tracker != null) { - tracker.close(); - tracker = null; + if (locatorTracker != null) { + locatorTracker.close(); + locatorTracker = null; + } + if (advertiserTracker != null) { + advertiserTracker.close(); + advertiserTracker = null; } plugin = null; } @@ -62,13 +72,28 @@ public class Activator implements BundleActivator { return plugin; } - /** - * @return Tracker for all IDiscoveryServices - */ - public ServiceTracker getDiscoveryServiceTracker() { - return tracker; + public IDiscoveryLocator getDiscoveryLocator(String containerUnderTest) { + final ServiceReference[] serviceReferences = locatorTracker.getServiceReferences(); + for(int i = 0; i < serviceReferences.length; i++) { + ServiceReference sr = serviceReferences[i]; + if(containerUnderTest.equals(sr.getProperty(IDiscoveryService.CONTAINER_NAME))) { + return (IDiscoveryLocator) locatorTracker.getService(sr); + } + } + return null; } + public IDiscoveryAdvertiser getDiscoveryAdvertiser(String containerUnderTest) { + final ServiceReference[] serviceReferences = advertiserTracker.getServiceReferences(); + for(int i = 0; i < serviceReferences.length; i++) { + ServiceReference sr = serviceReferences[i]; + if(containerUnderTest.equals(sr.getProperty(IDiscoveryService.CONTAINER_NAME))) { + return (IDiscoveryAdvertiser) advertiserTracker.getService(sr); + } + } + return null; + } + public BundleContext getContext() { return context; } diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryContainerTest.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryContainerTest.java new file mode 100644 index 000000000..6c9339c0e --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryContainerTest.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2009 Markus Alexander Kuppe. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Markus Alexander Kuppe (ecf-dev_eclipse.org <at> lemmster <dot> de) - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.discovery; + +import org.eclipse.ecf.core.ContainerCreateException; +import org.eclipse.ecf.core.ContainerFactory; +import org.eclipse.ecf.core.IContainer; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; + +public abstract class DiscoveryContainerTest extends DiscoveryTest { + protected IContainer container = null; + + public DiscoveryContainerTest(String name) { + super(name); + } + + protected IDiscoveryLocator getDiscoveryLocator() { + final IDiscoveryLocator adapter = (IDiscoveryLocator) container.getAdapter(IDiscoveryLocator.class); + assertNotNull("Adapter must not be null", adapter); + return adapter; + } + + protected IDiscoveryAdvertiser getDiscoveryAdvertiser() { + final IDiscoveryAdvertiser adapter = (IDiscoveryAdvertiser) container.getAdapter(IDiscoveryAdvertiser.class); + assertNotNull("Adapter must not be null", adapter); + return adapter; + } + + protected IContainer getContainer(String containerUnderTest) throws ContainerCreateException { + return ContainerFactory.getDefault().createContainer(containerUnderTest); + } + + /* (non-Javadoc) + * @see org.eclipse.ecf.tests.discovery.DiscoveryTest#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + container.disconnect(); + container.dispose(); + } + + /* (non-Javadoc) + * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#setUp() + */ + protected void setUp() throws Exception { + container = getContainer(containerUnderTest); + assertNotNull(container); + container.connect(null, null); + super.setUp(); + } + +} diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryServiceTest.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryServiceTest.java index 816ddecb0..878a3ad5e 100755 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryServiceTest.java +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryServiceTest.java @@ -11,58 +11,34 @@ package org.eclipse.ecf.tests.discovery; -import org.eclipse.ecf.core.ContainerCreateException; -import org.eclipse.ecf.core.IContainer; -import org.eclipse.ecf.discovery.IDiscoveryContainerAdapter; -import org.eclipse.ecf.discovery.service.IDiscoveryService; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; public abstract class DiscoveryServiceTest extends DiscoveryTest { - private static final String NO_PROVIDER_REGISTERED = "No discovery provider by that name seems to be registered"; - - /** - * @param name - */ public DiscoveryServiceTest(String name) { super(name); } /* (non-Javadoc) - * @see org.eclipse.ecf.tests.discovery.DiscoveryTest#getAdapter(java.lang.Class) + * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#setUp() */ - protected IDiscoveryContainerAdapter getAdapter(Class notNeeded) { - final ServiceTracker serviceTracker = Activator.getDefault().getDiscoveryServiceTracker(); - assertNotNull(NO_PROVIDER_REGISTERED, serviceTracker); - final ServiceReference[] serviceReferences = serviceTracker.getServiceReferences(); - assertNotNull(NO_PROVIDER_REGISTERED, serviceReferences); - for(int i = 0; i < serviceReferences.length; i++) { - ServiceReference sr = serviceReferences[i]; - if(containerUnderTest.equals(sr.getProperty(IDiscoveryService.CONTAINER_NAME))) { - return (IDiscoveryContainerAdapter) serviceTracker.getService(sr); - } - } - fail(NO_PROVIDER_REGISTERED); - return null; + protected void setUp() throws Exception { + super.setUp(); + discoveryLocator.purgeCache(); } /* (non-Javadoc) - * @see org.eclipse.ecf.tests.discovery.DiscoveryTest#getContainer(java.lang.String) + * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#getDiscoveryLocator() */ - protected IContainer getContainer(String containerUnderTest) - throws ContainerCreateException { - IContainer container = (IContainer) getAdapter(null); - // we might get a pre used container but the tests expect a fresh instance - container.disconnect(); - return container; + protected IDiscoveryLocator getDiscoveryLocator() { + return Activator.getDefault().getDiscoveryLocator(containerUnderTest); } - - /* - * (non-Javadoc) - * @see junit.framework.TestCase#tearDown() + + /* (non-Javadoc) + * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#getDiscoveryLocator() */ - protected void tearDown() throws Exception { - discoveryContainer.unregisterService(serviceInfo); + protected IDiscoveryAdvertiser getDiscoveryAdvertiser() { + return Activator.getDefault().getDiscoveryAdvertiser(containerUnderTest); } } diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTest.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTest.java index 3653ed310..767476302 100755 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTest.java +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTest.java @@ -13,6 +13,7 @@ package org.eclipse.ecf.tests.discovery; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.ecf.core.ContainerConnectException; import org.eclipse.ecf.core.util.ECFRuntimeException; import org.eclipse.ecf.discovery.IServiceEvent; @@ -20,23 +21,31 @@ import org.eclipse.ecf.discovery.IServiceInfo; import org.eclipse.ecf.discovery.identity.IServiceTypeID; import org.eclipse.ecf.tests.discovery.listener.TestServiceListener; import org.eclipse.ecf.tests.discovery.listener.TestServiceTypeListener; +import org.eclipse.equinox.concurrent.future.IFuture; public abstract class DiscoveryTest extends AbstractDiscoveryTest { + protected long waitTimeForProvider = 1000; + protected int eventsToExpect = 1; + public DiscoveryTest(String name) { super(name); } + protected void setWaitTimeForProvider(long aWaitTimeForProvider) { + this.waitTimeForProvider = aWaitTimeForProvider + (aWaitTimeForProvider * 1 / 2); + } + protected void registerService(IServiceInfo serviceInfo) throws Exception { assertNotNull(serviceInfo); - assertNotNull(discoveryContainer); - discoveryContainer.registerService(serviceInfo); + assertNotNull(discoveryAdvertiser); + discoveryAdvertiser.registerService(serviceInfo); } protected void unregisterService(IServiceInfo serviceInfo) throws Exception { assertNotNull(serviceInfo); - assertNotNull(discoveryContainer); - discoveryContainer.unregisterService(serviceInfo); + assertNotNull(discoveryAdvertiser); + discoveryAdvertiser.unregisterService(serviceInfo); } /* @@ -44,15 +53,13 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { - discoveryContainer.unregisterService(serviceInfo); - container.disconnect(); - container.dispose(); + discoveryAdvertiser.unregisterService(serviceInfo); super.tearDown(); } protected void registerService() { try { - discoveryContainer.registerService(serviceInfo); + discoveryAdvertiser.registerService(serviceInfo); } catch (final ECFRuntimeException e) { fail("IServiceInfo may be valid with this IDCA"); } @@ -60,7 +67,7 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { protected void unregisterService() { try { - discoveryContainer.unregisterService(serviceInfo); + discoveryAdvertiser.unregisterService(serviceInfo); } catch (final ECFRuntimeException e) { fail("unregistering of " + serviceInfo + " should just work"); } @@ -80,12 +87,11 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { } protected void addServiceListener(TestServiceListener serviceListener) { - discoveryContainer.addServiceListener(serviceListener); + discoveryLocator.addServiceListener(serviceListener); addListenerRegisterAndWait(serviceListener, serviceInfo); - discoveryContainer.removeServiceListener(serviceListener); + discoveryLocator.removeServiceListener(serviceListener); assertNotNull("Test listener didn't receive discovery", serviceListener.getEvent()); assertEquals("Test listener received more than expected discovery event", eventsToExpect, serviceListener.getEvent().length); - assertTrue("Container mismatch", serviceListener.getEvent()[eventsToExpect - 1].getLocalContainerID().equals(container.getConnectedID())); assertTrue("IServiceInfo mismatch", comparator.compare(((IServiceEvent) serviceListener.getEvent()[eventsToExpect - 1]).getServiceInfo(), serviceInfo) == 0); } @@ -95,9 +101,8 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testGetServiceInfo() throws ContainerConnectException { - container.connect(null, null); registerService(); - final IServiceInfo info = discoveryContainer.getServiceInfo(serviceInfo.getServiceID()); + final IServiceInfo info = discoveryLocator.getServiceInfo(serviceInfo.getServiceID()); assertTrue("IServiceInfo should match, expected:\n" + serviceInfo + " but:\n" + info, comparator.compare(info, serviceInfo) == 0); } @@ -107,10 +112,16 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testGetServiceTypes() throws ContainerConnectException { - container.connect(null, null); registerService(); - final IServiceTypeID[] serviceTypeIDs = discoveryContainer.getServiceTypes(); - assertTrue(serviceTypeIDs.length > 0); + final IServiceTypeID[] serviceTypeIDs = discoveryLocator.getServiceTypes(); + assertTrue(serviceTypeIDs.length >= 1); + for (int i = 0; i < serviceTypeIDs.length; i++) { + IServiceTypeID iServiceTypeId = serviceTypeIDs[i]; + if(serviceInfo.getServiceID().getServiceTypeID().equals(iServiceTypeId)) { + return; + } + } + fail("Self registered service not found"); } /** @@ -119,10 +130,16 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testGetServices() throws ContainerConnectException { - container.connect(null, null); registerService(); - final IServiceInfo[] services = discoveryContainer.getServices(); + final IServiceInfo[] services = discoveryLocator.getServices(); assertTrue(services.length >= 1); + for (int i = 0; i < services.length; i++) { + IServiceInfo iServiceInfo = services[i]; + if(comparator.compare(iServiceInfo, serviceInfo) == 0) { + return; + } + } + fail("Self registered service not found"); } /** @@ -131,10 +148,16 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testGetServicesIServiceTypeID() throws ContainerConnectException { - container.connect(null, null); registerService(); - final IServiceInfo serviceInfos[] = discoveryContainer.getServices(serviceInfo.getServiceID().getServiceTypeID()); - assertTrue(serviceInfos.length > 0); + final IServiceInfo serviceInfos[] = discoveryLocator.getServices(serviceInfo.getServiceID().getServiceTypeID()); + assertTrue(serviceInfos.length >= 1); + for (int i = 0; i < serviceInfos.length; i++) { + IServiceInfo iServiceInfo = serviceInfos[i]; + if(comparator.compare(iServiceInfo, serviceInfo) == 0) { + return; + } + } + fail("Self registered service not found"); } /** @@ -143,9 +166,8 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testRegisterService() throws ContainerConnectException { - container.connect(null, null); registerService(); - final IServiceInfo[] services = discoveryContainer.getServices(); + final IServiceInfo[] services = discoveryLocator.getServices(); assertTrue(services.length >= 1); for (int i = 0; i < services.length; i++) { final IServiceInfo service = services[i]; @@ -164,7 +186,7 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { public void testUnregisterService() throws ContainerConnectException { testRegisterService(); unregisterService(); - final IServiceInfo[] services = discoveryContainer.getServices(); + final IServiceInfo[] services = discoveryLocator.getServices(); for (int i = 0; i < services.length; i++) { final IServiceInfo service = services[i]; if (comparator.compare(service, serviceInfo) == 0) { @@ -179,8 +201,9 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testAddServiceListenerIServiceListener() throws ContainerConnectException { - container.connect(null, null); - assertTrue("No Services must be registerd at this point", discoveryContainer.getServices().length == 0); + IServiceInfo[] services = discoveryLocator.getServices(); + assertTrue("No Services must be registerd at this point " + (services.length == 0 ? "" : services[0].toString()), services.length == 0); + final TestServiceListener tsl = new TestServiceListener(eventsToExpect); addServiceListener(tsl); } @@ -191,17 +214,16 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testAddServiceListenerIServiceTypeIDIServiceListener() throws ContainerConnectException { - container.connect(null, null); - assertTrue("No Services must be registerd at this point", discoveryContainer.getServices().length == 0); + IServiceInfo[] services = discoveryLocator.getServices(); + assertTrue("No Services must be registerd at this point " + (services.length == 0 ? "" : services[0].toString()), services.length == 0); final TestServiceListener tsl = new TestServiceListener(eventsToExpect); - discoveryContainer.addServiceListener(serviceInfo.getServiceID().getServiceTypeID(), tsl); + discoveryLocator.addServiceListener(serviceInfo.getServiceID().getServiceTypeID(), tsl); addListenerRegisterAndWait(tsl, serviceInfo); - discoveryContainer.removeServiceListener(serviceInfo.getServiceID().getServiceTypeID(), tsl); + discoveryLocator.removeServiceListener(serviceInfo.getServiceID().getServiceTypeID(), tsl); assertNotNull("Test listener didn't receive discovery", tsl.getEvent()); assertEquals("Test listener received more than expected discovery event", eventsToExpect, tsl.getEvent().length); - assertTrue("Container mismatch", tsl.getEvent()[eventsToExpect - 1].getLocalContainerID().equals(container.getConnectedID())); assertTrue("IServiceInfo mismatch", comparator.compare(((IServiceEvent) tsl.getEvent()[eventsToExpect - 1]).getServiceInfo(), serviceInfo) == 0); } @@ -211,11 +233,11 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testAddServiceTypeListener() throws ContainerConnectException { - container.connect(null, null); - assertTrue("No Services must be registerd at this point", discoveryContainer.getServices().length == 0); + IServiceInfo[] services = discoveryLocator.getServices(); + assertTrue("No Services must be registerd at this point " + (services.length == 0 ? "" : services[0].toString()), services.length == 0); final TestServiceTypeListener testTypeListener = new TestServiceTypeListener(eventsToExpect); - discoveryContainer.addServiceTypeListener(testTypeListener); + discoveryLocator.addServiceTypeListener(testTypeListener); synchronized (testTypeListener) { // register a service which we expect the test listener to get notified of @@ -228,11 +250,10 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { } } - discoveryContainer.removeServiceTypeListener(testTypeListener); + discoveryLocator.removeServiceTypeListener(testTypeListener); assertNotNull("Test listener didn't receive discovery", testTypeListener.getEvent()); assertEquals("Test listener received more than expected discovery event", eventsToExpect, testTypeListener.getEvent().length); - assertTrue("Container mismatch", testTypeListener.getEvent()[eventsToExpect - 1].getLocalContainerID().equals(container.getConnectedID())); } @@ -242,7 +263,6 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testRemoveServiceListenerIServiceListener() throws ContainerConnectException { - container.connect(null, null); final TestServiceListener serviceListener = new TestServiceListener(eventsToExpect); addServiceListener(serviceListener); //TODO reregister and verify the listener doesn't receive any events any longer. @@ -254,7 +274,6 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testRemoveServiceListenerIServiceTypeIDIServiceListener() throws ContainerConnectException { - container.connect(null, null); final TestServiceListener serviceListener = new TestServiceListener(eventsToExpect); addServiceListener(serviceListener); //TODO reregister and verify the listener doesn't receive any events any longer. @@ -266,11 +285,10 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { * @throws ContainerConnectException */ public void testRemoveServiceTypeListener() throws ContainerConnectException { - container.connect(null, null); - assertTrue("No Services must be registerd at this point", discoveryContainer.getServices().length == 0); + assertTrue("No Services must be registerd at this point", discoveryLocator.getServices().length == 0); final TestServiceTypeListener testTypeListener = new TestServiceTypeListener(eventsToExpect); - discoveryContainer.addServiceTypeListener(testTypeListener); + discoveryLocator.addServiceTypeListener(testTypeListener); synchronized (testTypeListener) { // register a service which we expect the test listener to get notified of @@ -283,13 +301,96 @@ public abstract class DiscoveryTest extends AbstractDiscoveryTest { } } - discoveryContainer.removeServiceTypeListener(testTypeListener); + discoveryLocator.removeServiceTypeListener(testTypeListener); assertNotNull("Test listener didn't receive discovery", testTypeListener.getEvent()); assertEquals("Test listener received more than expected discovery event", eventsToExpect, testTypeListener.getEvent().length); - assertTrue("Container mismatch", testTypeListener.getEvent()[eventsToExpect - 1].getLocalContainerID().equals(container.getConnectedID())); //TODO reregister and verify the listener doesn't receive any events any longer. } + /** + * Test method for + * {@link org.eclipse.ecf.discovery.IDiscoveryLocator#getAsyncServiceInfo(org.eclipse.ecf.discovery.identity.IServiceID)}. + * @throws InterruptedException + * @throws OperationCanceledException + * @throws ContainerConnectException + */ + public void testGetAsyncServiceInfo() throws OperationCanceledException, InterruptedException, ContainerConnectException { + registerService(); + final IFuture aFuture = discoveryLocator.getAsyncServiceInfo(serviceInfo.getServiceID()); + final Object object = aFuture.get(); + assertTrue(object instanceof IServiceInfo); + final IServiceInfo info = (IServiceInfo) object; + assertTrue("IServiceInfo should match, expected:\n" + serviceInfo + " but:\n" + info, comparator.compare(info, serviceInfo) == 0); + } + + /** + * Test method for + * {@link org.eclipse.ecf.discovery.IDiscoveryLocator#getAsyncServices()}. + * @throws ContainerConnectException + * @throws InterruptedException + * @throws OperationCanceledException + */ + public void testGetAsyncServices() throws ContainerConnectException, OperationCanceledException, InterruptedException { + registerService(); + final IFuture aFuture = discoveryLocator.getAsyncServices(); + final Object object = aFuture.get(); + assertTrue(object instanceof IServiceInfo[]); + final IServiceInfo[] services = (IServiceInfo[]) object; + assertTrue("Found: " + services.length, services.length == eventsToExpect); + for (int i = 0; i < services.length; i++) { + IServiceInfo iServiceInfo = services[i]; + if(comparator.compare(iServiceInfo, serviceInfo) == 0) { + return; + } + } + fail("Self registered service not found"); + } + + /** + * Test method for + * {@link org.eclipse.ecf.discovery.IDiscoveryLocator#getAsyncServices(org.eclipse.ecf.discovery.identity.IServiceTypeID)}. + * @throws ContainerConnectException + * @throws InterruptedException + * @throws OperationCanceledException + */ + public void testGetAsyncServicesIServiceTypeID() throws ContainerConnectException, OperationCanceledException, InterruptedException { + registerService(); + final IFuture aFuture = discoveryLocator.getAsyncServices(serviceInfo.getServiceID().getServiceTypeID()); + final Object object = aFuture.get(); + assertTrue(object instanceof IServiceInfo[]); + final IServiceInfo[] services = (IServiceInfo[]) object; + assertTrue("Found: " + services.length, services.length == eventsToExpect); + for (int i = 0; i < services.length; i++) { + IServiceInfo iServiceInfo = services[i]; + if(comparator.compare(iServiceInfo, serviceInfo) == 0) { + return; + } + } + fail("Self registered service not found"); + } + + /** + * Test method for + * {@link org.eclipse.ecf.discovery.IDiscoveryLocator#getAsyncServiceTypes()}. + * @throws ContainerConnectException + * @throws InterruptedException + * @throws OperationCanceledException + */ + public void testGetAsyncServiceTypes() throws ContainerConnectException, OperationCanceledException, InterruptedException { + registerService(); + final IFuture aFuture = discoveryLocator.getAsyncServiceTypes(); + final Object object = aFuture.get(); + assertTrue(object instanceof IServiceTypeID[]); + final IServiceTypeID[] services = (IServiceTypeID[]) object; + assertTrue("Found: " + services.length, services.length == eventsToExpect); + for (int i = 0; i < services.length; i++) { + IServiceTypeID iServiceTypeId = services[i]; + if(serviceInfo.getServiceID().getServiceTypeID().equals(iServiceTypeId)) { + return; + } + } + fail("Self registered service not found"); + } } diff --git a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTestsWithoutRegister.java b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTestsWithoutRegister.java index 3ca669bd0..07402b5f3 100644 --- a/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTestsWithoutRegister.java +++ b/tests/bundles/org.eclipse.ecf.tests.discovery/src/org/eclipse/ecf/tests/discovery/DiscoveryTestsWithoutRegister.java @@ -12,18 +12,49 @@ package org.eclipse.ecf.tests.discovery; import org.eclipse.core.runtime.AssertionFailedException; import org.eclipse.ecf.core.ContainerConnectException; +import org.eclipse.ecf.core.ContainerCreateException; +import org.eclipse.ecf.core.ContainerFactory; +import org.eclipse.ecf.core.IContainer; import org.eclipse.ecf.core.identity.Namespace; import org.eclipse.ecf.core.util.ECFRuntimeException; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; import org.eclipse.ecf.discovery.identity.IServiceID; import org.eclipse.ecf.discovery.identity.ServiceIDFactory; public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTest { - + protected IContainer container = null; + public DiscoveryTestsWithoutRegister(String name) { super(name); } + protected IDiscoveryLocator getDiscoveryLocator() { + final IDiscoveryLocator adapter = (IDiscoveryLocator) container.getAdapter(IDiscoveryLocator.class); + assertNotNull("Adapter must not be null", adapter); + return adapter; + } + + protected IDiscoveryAdvertiser getDiscoveryAdvertiser() { + final IDiscoveryAdvertiser adapter = (IDiscoveryAdvertiser) container.getAdapter(IDiscoveryAdvertiser.class); + assertNotNull("Adapter must not be null", adapter); + return adapter; + } + + protected IContainer getContainer(String containerUnderTest) throws ContainerCreateException { + return ContainerFactory.getDefault().createContainer(containerUnderTest); + } + + /* (non-Javadoc) + * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#setUp() + */ + protected void setUp() throws Exception { + container = getContainer(containerUnderTest); + assertNotNull(container); + super.setUp(); + } + /* (non-Javadoc) * @see junit.framework.TestCase#tearDown() */ @@ -70,7 +101,7 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes */ public void testGetServiceInfoWithNull() { try { - discoveryContainer.getServiceInfo(null); + discoveryLocator.getServiceInfo(null); } catch (final AssertionFailedException e) { return; } @@ -79,11 +110,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServices(org.eclipse.ecf.discovery.identity.IServiceTypeID)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#getServices(org.eclipse.ecf.discovery.identity.IServiceTypeID)}. */ public void testGetServicesIServiceTypeIDWithNull() { try { - discoveryContainer.getServices(null); + discoveryLocator.getServices(null); } catch (final AssertionFailedException e) { return; } @@ -92,12 +123,12 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#unregisterService(org.eclipse.ecf.discovery.IServiceInfo)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#unregisterService(org.eclipse.ecf.discovery.IServiceInfo)}. */ public void testUnregisterServiceWithNull() { testConnect(); try { - discoveryContainer.unregisterService(null); + discoveryAdvertiser.unregisterService(null); } catch (final ECFRuntimeException e) { fail("null must cause AssertionFailedException"); } catch (final AssertionFailedException e) { @@ -120,11 +151,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#addServiceTypeListener(org.eclipse.ecf.discovery.IServiceTypeListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#addServiceTypeListener(org.eclipse.ecf.discovery.IServiceTypeListener)}. */ public void testAddServiceTypeListenerWithNull() { try { - discoveryContainer.addServiceTypeListener(null); + discoveryLocator.addServiceTypeListener(null); } catch (final AssertionFailedException e) { return; } @@ -151,11 +182,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServicesNamespace()}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#getServicesNamespace()}. */ public void testGetServicesNamespace() { testConnect(); - final Namespace namespace = discoveryContainer.getServicesNamespace(); + final Namespace namespace = discoveryLocator.getServicesNamespace(); assertNotNull(namespace); final IServiceID serviceID = ServiceIDFactory.getDefault().createServiceID(namespace, serviceInfo.getServiceID().getServiceTypeID()); assertNotNull("It must be possible to obtain a IServiceID", serviceID); @@ -163,11 +194,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#removeServiceTypeListener(org.eclipse.ecf.discovery.IServiceTypeListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#removeServiceTypeListener(org.eclipse.ecf.discovery.IServiceTypeListener)}. */ public void testRemoveServiceTypeListenerWithNull() { try { - discoveryContainer.removeServiceTypeListener(null); + discoveryLocator.removeServiceTypeListener(null); } catch (final AssertionFailedException e) { return; } @@ -177,12 +208,12 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#registerService(org.eclipse.ecf.discovery.IServiceInfo)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#registerService(org.eclipse.ecf.discovery.IServiceInfo)}. */ public void testRegisterServiceWithNull() { testConnect(); try { - discoveryContainer.registerService(null); + discoveryAdvertiser.registerService(null); } catch (final ECFRuntimeException e) { fail("null must cause AssertionFailedException"); } catch (final AssertionFailedException e) { @@ -193,11 +224,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#addServiceListener(org.eclipse.ecf.discovery.IServiceListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#addServiceListener(org.eclipse.ecf.discovery.IServiceListener)}. */ public void testAddServiceListenerIServiceListenerWithNull() { try { - discoveryContainer.addServiceListener(null); + discoveryLocator.addServiceListener(null); } catch (final AssertionFailedException e) { return; } @@ -206,11 +237,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#addServiceListener(org.eclipse.ecf.discovery.identity.IServiceTypeID, org.eclipse.ecf.discovery.IServiceListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#addServiceListener(org.eclipse.ecf.discovery.identity.IServiceTypeID, org.eclipse.ecf.discovery.IServiceListener)}. */ public void testAddServiceListenerIServiceTypeIDIServiceListenerWithNull() { try { - discoveryContainer.addServiceListener(null, null); + discoveryLocator.addServiceListener(null, null); } catch (final AssertionFailedException e) { return; } @@ -219,11 +250,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#removeServiceListener(org.eclipse.ecf.discovery.IServiceListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#removeServiceListener(org.eclipse.ecf.discovery.IServiceListener)}. */ public void testRemoveServiceListenerIServiceListenerWithNull() { try { - discoveryContainer.removeServiceListener(null); + discoveryLocator.removeServiceListener(null); } catch (final AssertionFailedException e) { return; } @@ -232,11 +263,11 @@ public abstract class DiscoveryTestsWithoutRegister extends AbstractDiscoveryTes /** * Test method for - * {@link org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#removeServiceListener(org.eclipse.ecf.discovery.identity.IServiceTypeID, org.eclipse.ecf.discovery.IServiceListener)}. + * {@link org.eclipse.ecf.discovery.IdiscoveryLocatorAdapter#removeServiceListener(org.eclipse.ecf.discovery.identity.IServiceTypeID, org.eclipse.ecf.discovery.IServiceListener)}. */ public void testRemoveServiceListenerIServiceTypeIDIServiceListenerWithNull() { try { - discoveryContainer.removeServiceListener(null, null); + discoveryLocator.removeServiceListener(null, null); } catch (final AssertionFailedException e) { return; } |