diff options
Diffstat (limited to 'plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core')
25 files changed, 3066 insertions, 0 deletions
diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AbstractServiceDescriptorsWithIdProviderTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AbstractServiceDescriptorsWithIdProviderTest.java new file mode 100644 index 00000000000..c98af6dee08 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AbstractServiceDescriptorsWithIdProviderTest.java @@ -0,0 +1,204 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.AbstractServiceIdDesc; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.AliasIdDesc; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.RegistryIdDesc; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.ServiceSetIdDesc; +import org.junit.Test; + + +/** + * @author cedric dumoulin + * + */ +public abstract class AbstractServiceDescriptorsWithIdProviderTest { + + static final String PLUGIN_XML = "resources/plugin.xml"; + + /** + * Load a RegistryDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param registryName + * The name of the registry to load. + * @return The descriptor for the registry. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + abstract public RegistryIdDesc getRegistryDesc(String extensionFileName, String registryName) throws IOException, ServiceException, DeclarationException; + + /** + * Load a ServiceSetDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param serviceSetName + * The name of the serviceSet to load. + * @return The descriptor for the ServiceSet + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + abstract public ServiceSetIdDesc getServiceSetDesc(String extensionFileName, String serviceSetName) throws IOException, ServiceException, DeclarationException; + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ExtensionServiceDescriptorsWithIdProvider#getRegistryDescriptor(java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testGetRegistryDescriptor() throws IOException, ServiceException, DeclarationException { + + String readFromFile = "resources/plugin.xml"; + RegistryIdDesc registryIdDesc = getRegistryDesc(readFromFile, "registryA"); + assertNotNull("Get registered registry", registryIdDesc); + + assertTrue("extends found", registryIdDesc.getExtends().size() > 0); + assertTrue("extends found", registryIdDesc.getExtends().contains("registryB")); + + assertTrue("extends found", registryIdDesc.getParents().size() > 0); + assertTrue("extends found", registryIdDesc.getParents().contains("registryC")); + + assertTrue("extends found", registryIdDesc.getSets().size() > 0); + assertTrue("extends found", registryIdDesc.getSets().contains("servicesSetA")); + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ExtensionServiceDescriptorsWithIdProvider#getServiceSet(java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testGetServiceSet() throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + + // Tests + assertNotNull("provider created", declarationsProvider); + ServiceSetIdDesc serviceSet = declarationsProvider.getServiceSet("fragmentA"); + + assertNotNull("Fragment exist", serviceSet); + + assertTrue("extends set", serviceSet.getExtends().size() > 0); + assertTrue("extends set", serviceSet.getExtends().contains("fragmentB")); + + } + + /** + * Test correctness of set parsing + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testSetParsing() throws IOException, ServiceException, DeclarationException { + String serviceSetName = "extendingSet"; + ServiceSetIdDesc serviceSet = getServiceSetDesc(PLUGIN_XML, serviceSetName); + + // Tests + assertNotNull("service found", serviceSet); + + assertTrue("extends set", serviceSet.getExtends().size() > 0); + assertTrue("extends set", serviceSet.getExtends().contains("setWithC")); + + String serviceAId = FakeServiceA.class.getName(); + String serviceBId = FakeServiceB.class.getName(); + assertTrue("service added", serviceSet.getServiceDescriptors().size() > 0); + assertNotNull("service added", serviceSet.getService(serviceAId)); + assertNotNull("service added", serviceSet.getService(serviceBId)); + + } + + /** + * Test correctness of service parsing + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testServiceParsing() throws IOException, ServiceException, DeclarationException { + // Indirectly read a specified service: first the set, second the service + String serviceSetName = "dependantServices"; + String serviceAId = FakeServiceA.class.getName(); + String serviceBId = FakeServiceB.class.getName(); + String serviceCId = FakeServiceC.class.getName(); + + ServiceSetIdDesc serviceSet = getServiceSetDesc(PLUGIN_XML, serviceSetName); + + AbstractServiceIdDesc serviceADesc = serviceSet.getService(serviceAId); + AbstractServiceIdDesc serviceBDesc = serviceSet.getService(serviceBId); + AbstractServiceIdDesc serviceCDesc = serviceSet.getService(serviceCId); + + // Tests + assertNotNull("service found", serviceADesc); + assertNotNull("service found", serviceBDesc); + assertNotNull("service found", serviceCDesc); + // Check dependsOn + assertTrue("dependsOn set", serviceADesc.getDependsOn().contains(serviceBId)); + assertTrue("dependsOn set", serviceBDesc.getDependsOn().contains(serviceCId)); + } + + /** + * Check if a serviceSet declared in several fragments with the same name is correctly loaded. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + * @throws ServiceException + */ + @Test + public void testLoadAlias() throws IOException, DeclarationException, ServiceException { + + // Check if specified service are in the registry + String serviceSetName = "setWithAlias"; + ServiceSetIdDesc serviceSet = getServiceSetDesc(PLUGIN_XML, serviceSetName); + + String serviceAId = FakeServiceA.class.getName(); + String aliasServiceId = "AnAlias"; + AbstractServiceIdDesc serviceADesc = serviceSet.getService(serviceAId); + AbstractServiceIdDesc abstractAliasDesc = serviceSet.getService(aliasServiceId); + + assertNotNull("service found", serviceADesc); + assertNotNull("service found", abstractAliasDesc); + assertTrue("right type", abstractAliasDesc instanceof AliasIdDesc); + + AliasIdDesc aliasIdDesc = (AliasIdDesc) abstractAliasDesc; + assertEquals("alias set", serviceAId, aliasIdDesc.getAliasedService()); + // assertTrue("alaised service added in dependencies", aliasDesc.getDependsOn().contains(serviceAId)); + } + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AllTests.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AllTests.java new file mode 100644 index 00000000000..3d2e92a18a6 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AllTests.java @@ -0,0 +1,31 @@ +/***************************************************************************** + * Copyright (c) 2010 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + + + +@RunWith(Suite.class) +@SuiteClasses({ + // {oep}.core.services + ServicesRegistryTest.class + +}) +/** + * Suite Class for all tests in the plugin + */ +public class AllTests { + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AnnotatedClass.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AnnotatedClass.java new file mode 100644 index 00000000000..c7daf4e0cd3 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/AnnotatedClass.java @@ -0,0 +1,29 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import org.eclipse.papyrus.infra.core.serviceregistry.annotations.Service; + + +/** + * @author cedric dumoulin + * + */ + +@Service +public class AnnotatedClass { + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/EclipseExtensionUtils.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/EclipseExtensionUtils.java new file mode 100644 index 00000000000..8398976a241 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/EclipseExtensionUtils.java @@ -0,0 +1,129 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.RegistryFactory; +import org.eclipse.core.runtime.spi.RegistryContributor; + + +/** + * Set of utilities helping to load eclipse extensions from predefined files. + * + * @author cedric dumoulin + * + * TODO This class is dedicated to the servicesREgistry.tests plugin. Can we make it more generic ? + * + */ +public class EclipseExtensionUtils { + + /** + * Unique instance. + */ + static final EclipseExtensionUtils instance = new EclipseExtensionUtils(); + + /** + * Create a new {@link IExtensionRegistry} and initialize it with the plugin.xml file found in the specified directory. + * Also add required plugin.xml files + * + * @param localfilepath + * @return + * @throws IOException + */ + public IExtensionRegistry createRegistry(String localfilepath) throws IOException { + + // Create the registry + IExtensionRegistry registry = RegistryFactory.createRegistry(null, this, this); + + // Load specified file + loadTestExtensionFile(registry, localfilepath); + // load regular plugin.xml in order to have the extension point definition + loadExtensionFile(registry, "org.eclipse.papyrus.infra.core.serviceregistry", "plugin.xml"); + return registry; + } + + /** + * Load the specified file (extension and extension points) into the registry. + * The file is located in the specified plugin. + * + * @param localfilepath + * @return + * @throws IOException + */ + protected void loadExtensionFile(IExtensionRegistry registry, String pluginID, String localfilepath) throws IOException { + + InputStream inputStream; + + if (Platform.isRunning()) { + // Load the specified plugin.xml file + URL url = new URL("platform:/plugin/" + pluginID + "/" + localfilepath); + + // URL url = new URL("platform", PLUGIN_ID, localfilepath ); + inputStream = url.openConnection().getInputStream(); + } + else { + // try to read locally + // System.out.println("platform not running !!"); + + URL url = new URL("file:" + "../" + pluginID + "/" + localfilepath); + // URL url = new URL("platform", PLUGIN_ID, localfilepath ); + inputStream = url.openConnection().getInputStream(); + } + + // Create a point contributor with fake ID + RegistryContributor pointContributor = new RegistryContributor(pluginID, pluginID, null, null); + + registry.addContribution(inputStream, pointContributor, false, null, null, this); + } + + /** + * Load the specified file (containing extension declarations) into the provided registry. + * The file is located in the same plugin as this class. + * + * @param localfilepath + * @return + * @throws IOException + */ + public void loadTestExtensionFile(IExtensionRegistry registry, String localfilepath) throws IOException { + + String PLUGIN_ID = "org.eclipse.papyrus.infra.core.serviceregistry.test"; + InputStream inputStream; + + if (Platform.isRunning()) { + // Load the specified plugin.xml file + URL url = new URL("platform:/plugin/" + PLUGIN_ID + "/" + localfilepath); + + // URL url = new URL("platform", PLUGIN_ID, localfilepath ); + inputStream = url.openConnection().getInputStream(); + } + else { + // try to read locally + // System.out.println("platform not running !!"); + URL url = new URL("file:" + localfilepath); + // URL url = new URL("platform", PLUGIN_ID, localfilepath ); + inputStream = url.openConnection().getInputStream(); + } + + // Create a point contributor with fake ID + RegistryContributor pointContributor = new RegistryContributor(PLUGIN_ID, PLUGIN_ID, null, null); + registry.addContribution(inputStream, pointContributor, false, null, null, this); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderFromFile.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderFromFile.java new file mode 100644 index 00000000000..1d33206dd11 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderFromFile.java @@ -0,0 +1,48 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import org.eclipse.core.runtime.IExtensionRegistry; + + +/** + * @author dumoulin + * + */ +public class ExtensionServiceDescriptorsWithIdProviderFromFile extends ExtensionServiceDescriptorsWithIdProvider { + + protected IExtensionRegistry extensionRegistry; + + /** + * + * Constructor. + * + * @param extensionRegistry + * @throws ServiceException + */ + public ExtensionServiceDescriptorsWithIdProviderFromFile(IExtensionRegistry extensionRegistry) throws ServiceException { + super(); + this.extensionRegistry = extensionRegistry; + } + + /** + * + * @return + */ + @Override + protected IExtensionRegistry getExtensionRegistry() { + return extensionRegistry; + } +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderIntegrationTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderIntegrationTest.java new file mode 100644 index 00000000000..2c00be46bd7 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderIntegrationTest.java @@ -0,0 +1,311 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +/** + * @author cedric dumoulin + * + */ +public class ExtensionServiceDescriptorsWithIdProviderIntegrationTest { + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.internal.ServicesRegistryConfigurationReaderFromExtensions#loadServicesRegistryConfiguration(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry, java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testLoadServicesSetConfiguration() throws IOException, ServiceException, DeclarationException { + + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + ServicesRegistry registry = factory.getServicesRegistry("simpleTestRegistry"); + + // String servicesSetName = "simpleTest"; + + // Check if specified service are in the registry + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceC = FakeServiceC.class; + + assertNotNull("service registered", registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceC)); + + registry.startNewServices(); + assertNotNull("service found", registry.getService(serviceA)); + assertNotNull("service found", registry.getService(serviceC)); + + registry.disposeRegistry(true); + } + + /** + * Test a set extending another set. + * * @throws IOException + * + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testLoadServicesSetWithExtends() throws IOException, ServiceException, DeclarationException { + + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + ServicesRegistry registry = factory.getServicesRegistry("extendingSetRegistry"); + + // Create a ServiceRegistry + // String servicesSetName = "extendingSet"; + + // set(A, B) --> set(C) + + // Check if specified service are in the registry + // C should be in the registry + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + Class<?> serviceC = FakeServiceC.class; + + assertNotNull("service registered", registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceB)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceC)); + + registry.startNewServices(); + assertNotNull("service found", registry.getService(serviceA)); + assertNotNull("service found", registry.getService(serviceC)); + + registry.disposeRegistry(true); + } + + /** + * Test a set extending another set. + * * @throws IOException + * + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testBadDeclaration() throws IOException, ServiceException, DeclarationException { + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + + // Create a ServiceRegistry + ServicesRegistry registry; + // String servicesSetName = "badSetExtendsIdMissing"; + try { + registry = factory.getServicesRegistry("badSetExtendsIdMissingRegistry"); + registry.startNewServices(); + fail("Exception should be thrown"); + } catch (DeclarationException e) { + // e.printStackTrace(); + } + + } + + /** + * Test a set extending another set. + * * @throws IOException + * + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testMissingBadServiceSetName() throws IOException, ServiceException, DeclarationException { + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + + // Create a ServiceRegistry + ServicesRegistry registry; + // String servicesSetName = "NonExistingName"; + try { + // Create ServiceRegistry from factory + registry = factory.getServicesRegistry("NonExistingName"); + registry.startNewServices(); + fail("Exception should be thrown"); + } catch (DeclarationException e) { + // e.printStackTrace(); + } + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.internal.ServicesRegistryConfigurationReaderFromExtensions#loadServicesRegistryConfiguration(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry, java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testServicesWithExtends() throws IOException, ServiceException, DeclarationException { + + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + ServicesRegistry registry = factory.getServicesRegistry("dependantServicesRegistry"); + + // String servicesSetName = "dependantServices"; + FakeService.getTrace().reset(); + registry.startNewServices(); + + + // check services + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + Class<?> serviceC = FakeServiceC.class; + // A and C should be started, B should not be started. + assertTrue("A started", registry.isStarted(serviceA)); + assertFalse("B not started", registry.isStarted(serviceB)); + assertTrue("C started", registry.isStarted(serviceC)); + + // Check if we can get them + assertNotNull("got service started", registry.getService(serviceC)); + assertNotNull("got service started", registry.getService(serviceA)); + + // check creation order (C then A) + // order should be C, A + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", serviceC.getSimpleName() + ",create", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",create", trace.getNameTrace(i++)); + + assertEquals("order", serviceC.getSimpleName() + ",init", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",init", trace.getNameTrace(i++)); + + assertEquals("order", serviceC.getSimpleName() + ",start", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",start", trace.getNameTrace(i++)); + + // start B + assertNotNull("got service started", registry.getService(serviceB)); + assertTrue("B started", registry.isStarted(serviceB)); + + registry.disposeRegistry(true); + } + + + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.internal.ServicesRegistryConfigurationReaderFromExtensions#loadServicesSetConfiguration(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry, java.lang.String)}. + */ + @Test + public void testLoadServicesRegistryConfiguration() { + fail("Not yet implemented"); + } + + /** + * Check if a serviceSet declared in several fragments with the same name is correctly loaded. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testLoadServicesSetFromPartialFragments() throws IOException, ServiceException, DeclarationException { + + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + ServicesRegistry registry = factory.getServicesRegistry("doubleNameRegistry"); + + // Create a ServiceRegistry + // String servicesSetName = "doubleName"; + + // Check if specified service are in the registry + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + + assertNotNull("service registered", registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceA)); + assertSame("service registered", ServiceState.registered, registry.serviceState(serviceB)); + + registry.startNewServices(); + assertNotNull("service found", registry.getService(serviceA)); + assertNotNull("service found", registry.getService(serviceB)); + + registry.disposeRegistry(true); + } + + /** + * Check if a serviceSet declared in several fragments with the same name is correctly loaded. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + * @throws ServiceException + */ + @Test + public void testLoadAlias() throws IOException, DeclarationException, ServiceException { + + // Create a declaration provider, and a factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + // Create ServiceRegistry from factory + ServicesRegistry registry = factory.getServicesRegistry("setWithAliasRegistry"); + registry.startNewServices(); + + // Check if specified service are in the registry + Class<?> serviceA = FakeServiceA.class; + String aliasService = "AnAlias"; + assertNotNull("service found", registry.getService(serviceA)); + assertNotNull("service found", registry.getService(aliasService)); + assertSame("same service", registry.getService(serviceA), registry.getService(aliasService)); + registry.disposeRegistry(true); + } + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderTest.java new file mode 100644 index 00000000000..b0337a8345b --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ExtensionServiceDescriptorsWithIdProviderTest.java @@ -0,0 +1,185 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.RegistryIdDesc; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.ServiceSetIdDesc; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +/** + * @author cedric dumoulin + * + */ +public class ExtensionServiceDescriptorsWithIdProviderTest extends AbstractServiceDescriptorsWithIdProviderTest { + + static final String PLUGIN_XML = "resources/plugin.xml"; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Load a RegistryDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param registryName + * The name of the registry to load. + * @return The descriptor for the registry. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Override + public RegistryIdDesc getRegistryDesc(String extensionFileName, String registryName) throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry(extensionFileName); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + assertNotNull("provider created", declarationsProvider); + + RegistryIdDesc registryIdDesc = declarationsProvider.getRegistryDescriptor(registryName); + + return registryIdDesc; + } + + /** + * Load a ServiceSetDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param serviceSetName + * The name of the serviceSet to load. + * @return The descriptor for the ServiceSet + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Override + public ServiceSetIdDesc getServiceSetDesc(String extensionFileName, String serviceSetName) throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry(extensionFileName); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + assertNotNull("provider created", declarationsProvider); + + ServiceSetIdDesc serviceSetIdDesc = declarationsProvider.getServiceSet(serviceSetName); + + return serviceSetIdDesc; + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ExtensionServiceDescriptorsWithIdProvider#getServiceSetFragments(java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testGetServiceSetFragments() throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + + // Tests + assertNotNull("provider created", declarationsProvider); + List<ServiceSetIdDesc> fragments = declarationsProvider.getServiceSetFragments("fragmentA"); + assertNotNull("Get fragments", fragments); + + assertTrue("fragments found", fragments.size() > 0); + // Get the first fragment and test it + ServiceSetIdDesc serviceSet = fragments.get(0); + assertNotNull("Fragment exist", serviceSet); + + assertTrue("extends set", serviceSet.getExtends().size() > 0); + assertTrue("extends set", serviceSet.getExtends().contains("fragmentB")); + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ExtensionServiceDescriptorsWithIdProvider#getServiceSetFragments(java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testGetServiceSetFragmentsWithMultiFragments() throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + + // Tests + assertNotNull("provider created", declarationsProvider); + List<ServiceSetIdDesc> fragments = declarationsProvider.getServiceSetFragments("multiFragmentA"); + assertNotNull("Get fragments", fragments); + + assertTrue("fragments found", fragments.size() > 0); + assertTrue("fragments found", fragments.size() > 1); + } + + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ExtensionServiceDescriptorsWithIdProvider#getServiceSet(java.lang.String)}. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Test + public void testGetServiceSetFromMultiFragment() throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + + // Tests + assertNotNull("provider created", declarationsProvider); + ServiceSetIdDesc serviceSet = declarationsProvider.getServiceSet("multiFragmentA"); + + assertNotNull("Fragment exist", serviceSet); + + assertTrue("extends set", serviceSet.getExtends().size() > 0); + assertTrue("extends set", serviceSet.getExtends().contains("fragmentB")); + + } + + + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeService.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeService.java new file mode 100644 index 00000000000..f9673ffbc9f --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeService.java @@ -0,0 +1,79 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + + +/** + * Fake service for testing purpose. + * + * @author cedric dumoulin + * + */ +public class FakeService implements IService { + + + static public TestTrace trace = new TestTrace(); + + static int count = 0; + + public String name = "name" + count++; + + + /** + * + * Constructor. + * + */ + public FakeService() { + trace.addTrace(name, "create"); + } + + /** + * Constructor. + * + * @param name + */ + public FakeService(String name) { + this.name = name; + trace.addTrace(name, "create"); + } + + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @return the trace + */ + static public TestTrace getTrace() { + return trace; + } + + + /** + * @param trace + * the trace to set + */ + public void setTrace(TestTrace trace) { + FakeService.trace = trace; + } + + @Override + public void init(ServicesRegistry servicesRegistry) { + trace.addTrace(name, "init", servicesRegistry); + } + + @Override + public void startService() { + trace.addTrace(name, "start"); + } + + @Override + public void disposeService() { + trace.addTrace(name, "dispose"); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceA.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceA.java new file mode 100644 index 00000000000..dfa0020ea5a --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceA.java @@ -0,0 +1,28 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + + +/** + * @author cedric dumoulin + * + */ +public class FakeServiceA extends FakeService { + + public FakeServiceA() { + super(FakeServiceA.class.getSimpleName()); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceB.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceB.java new file mode 100644 index 00000000000..af2bf447ffe --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceB.java @@ -0,0 +1,28 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + + +/** + * @author cedric dumoulin + * + */ +public class FakeServiceB extends FakeService { + + public FakeServiceB() { + super(FakeServiceB.class.getSimpleName()); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceC.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceC.java new file mode 100644 index 00000000000..da5d9e78cc4 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceC.java @@ -0,0 +1,28 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + + +/** + * @author cedric dumoulin + * + */ +public class FakeServiceC extends FakeService { + + public FakeServiceC() { + super(FakeServiceC.class.getSimpleName()); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceDescriptor.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceDescriptor.java new file mode 100644 index 00000000000..536c829082c --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceDescriptor.java @@ -0,0 +1,43 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import java.util.List; + +/** + * Service Descriptor used to create a named FakeService. + */ +public class FakeServiceDescriptor extends ServiceDescriptor { + + /** + * + * Constructor. + * Create a descriptor for a FakeService with a specified name. + * + * @param key + * @param startKind + */ + public FakeServiceDescriptor(String key, ServiceStartKind startKind) { + super(key, FakeService.class.getName(), startKind, 1); + } + + /** + * + * Constructor. + * Create a descriptor for a FakeService with a specified name. + * + * @param key + * @param startKind + * @param requiredService + */ + public FakeServiceDescriptor(String key, ServiceStartKind startKind, List<String> requiredService) { + super(key, FakeService.class.getName(), startKind, 1, requiredService); + } + + /** + * Get the type of the created service. + * + * @return + */ + static public Class<?> getServiceType() { + return FakeService.class; + } +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceFactory.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceFactory.java new file mode 100644 index 00000000000..88e3e5b757a --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeServiceFactory.java @@ -0,0 +1,70 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + + +/** + * Fake service factory for testing purpose. + * + * @author cedric dumoulin + * + */ +public class FakeServiceFactory extends FakeService implements IServiceFactory { + + + /** + * + * Constructor. + * + */ + public FakeServiceFactory() { + + super("factoryName" + count++); + } + + /** + * Constructor. + * + * @param name + */ + public FakeServiceFactory(String name) { + super(name); + } + + + /** + * @return the name + */ + @Override + public String getName() { + return name; + } + + /** + * Create the service + * + * @see org.eclipse.papyrus.infra.core.serviceregistry.IServiceFactory#createServiceInstance() + * + * @return + */ + @Override + public Object createServiceInstance() { + trace.addTrace(getName(), "createInstance"); + return new FakeCreatedService(); + } + + /** + * Pseudo service created by the factory. + * + * @author dumoulin + * + */ + public class FakeCreatedService { + + /** + * @return the name + */ + public String getName() { + return name; + } + } +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeWithRequiredServiceFactory.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeWithRequiredServiceFactory.java new file mode 100644 index 00000000000..b1d61408acd --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/FakeWithRequiredServiceFactory.java @@ -0,0 +1,54 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + + +/** + * Fake service factory for testing purpose. + * This service require the + * + * @author cedric dumoulin + * + */ +public class FakeWithRequiredServiceFactory extends FakeServiceFactory implements IServiceFactory { + + + /** + * + * Constructor. + * + */ + public FakeWithRequiredServiceFactory() { + + super("factoryName" + count++); + } + + /** + * Constructor. + * + * @param name + */ + public FakeWithRequiredServiceFactory(String name) { + super(name); + } + + + /** + * + * @see org.eclipse.papyrus.infra.core.serviceregistry.FakeService#init(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry) + * + * @param servicesRegistry + */ + @Override + public void init(ServicesRegistry servicesRegistry) { + + trace.addTrace(name, "init"); + Object foundService = null; + try { + foundService = servicesRegistry.getService("C"); + } catch (ServiceException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + trace.addTrace(name, "initEnd", foundService); + } +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA.java new file mode 100644 index 00000000000..f6e2e77a34d --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA.java @@ -0,0 +1,52 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import java.util.ArrayList; +import java.util.List; + +public class ServiceA implements IService { + + public enum TraceKind { + init, start, dispose + } + + static List<TraceKind> trace = new ArrayList<TraceKind>(); + + static List<String> nametrace = new ArrayList<String>(); + + + + static public TraceKind getEvent(int index) { + return trace.get(index); + } + + static public String getTraceName(int index) { + return nametrace.get(index); + } + + static public void resetTrace() { + trace.clear(); + nametrace.clear(); + } + + @Override + public void init(ServicesRegistry servicesRegistry) { + trace.add(TraceKind.init); + nametrace.add(this.getClass().getSimpleName()); + + } + + @Override + public void startService() { + trace.add(TraceKind.start); + nametrace.add(this.getClass().getSimpleName()); + + } + + @Override + public void disposeService() { + trace.add(TraceKind.dispose); + nametrace.add(this.getClass().getSimpleName()); + + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA10.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA10.java new file mode 100644 index 00000000000..07909845b65 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceA10.java @@ -0,0 +1,6 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + +public class ServiceA10 extends ServiceA { + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceB.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceB.java new file mode 100644 index 00000000000..fb508a0ce27 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceB.java @@ -0,0 +1,6 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + +public class ServiceB extends ServiceA { + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceC.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceC.java new file mode 100644 index 00000000000..17cf6f1cea1 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceC.java @@ -0,0 +1,6 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + +public class ServiceC extends ServiceA { + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceDescriptorsWithIdProviderCollectionTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceDescriptorsWithIdProviderCollectionTest.java new file mode 100644 index 00000000000..4a53db85975 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceDescriptorsWithIdProviderCollectionTest.java @@ -0,0 +1,115 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.RegistryIdDesc; +import org.eclipse.papyrus.infra.core.serviceregistry.servicedescriptorswithid.ServiceSetIdDesc; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class ServiceDescriptorsWithIdProviderCollectionTest extends AbstractServiceDescriptorsWithIdProviderTest { + + /** + * Load a RegistryDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param registryName + * The name of the registry to load. + * @return The descriptor for the registry. + * + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Override + public RegistryIdDesc getRegistryDesc(String extensionFileName, String registryName) throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry(extensionFileName); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + assertNotNull("provider created", declarationsProvider); + + // Create the collection + ServiceDescriptorsWithIdProviderCollection providerCollection = new ServiceDescriptorsWithIdProviderCollection(); + providerCollection.addAll(declarationsProvider); + + // Get the registry thru the collection + RegistryIdDesc registryIdDesc = providerCollection.getRegistryIdDesc(registryName); + + return registryIdDesc; + } + + /** + * Load a ServiceSetDesc from the specified declaration file. + * + * @param extensionFileName + * The name of the resource containing declarations + * @param serviceSetName + * The name of the serviceSet to load. + * @return The descriptor for the ServiceSet + * @throws IOException + * @throws ServiceException + * @throws DeclarationException + */ + @Override + public ServiceSetIdDesc getServiceSetDesc(String extensionFileName, String serviceSetName) throws IOException, ServiceException, DeclarationException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry(extensionFileName); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + assertNotNull("provider created", declarationsProvider); + + // Create the collection + ServiceDescriptorsWithIdProviderCollection providerCollection = new ServiceDescriptorsWithIdProviderCollection(); + providerCollection.addAll(declarationsProvider); + + // Get the set thru the collection + ServiceSetIdDesc serviceSetIdDesc = providerCollection.getServiceSet(serviceSetName); + return serviceSetIdDesc; + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + /** + * + */ + @Test + public void testServiceDescriptorsWithIdProviderCollection() { + ServiceDescriptorsWithIdProviderCollection providerCollection = new ServiceDescriptorsWithIdProviderCollection(); + assertNotNull("collection created", providerCollection); + } + + /** + * @throws IOException + * @throws ServiceException + * + */ + @Test + public void testAddAll() throws IOException, ServiceException { + // create provider, reading description from specified file. + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry(PLUGIN_XML); + + ExtensionServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + assertNotNull("provider created", declarationsProvider); + + // Create the collection + ServiceDescriptorsWithIdProviderCollection providerCollection = new ServiceDescriptorsWithIdProviderCollection(); + providerCollection.addAll(declarationsProvider); + assertNotNull("collection created", providerCollection); + + } + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicePojoA.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicePojoA.java new file mode 100644 index 00000000000..c2152b2b4ad --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicePojoA.java @@ -0,0 +1,6 @@ +package org.eclipse.papyrus.infra.core.serviceregistry; + + +public class ServicePojoA extends ServiceA { + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceRegistryFactoryTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceRegistryFactoryTest.java new file mode 100644 index 00000000000..c4a9028ad83 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceRegistryFactoryTest.java @@ -0,0 +1,89 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.eclipse.core.runtime.IExtensionRegistry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +/** + * @author cedric dumoulin + * + */ +public class ServiceRegistryFactoryTest { + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Integration test. + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServiceRegistryFactory#getServicesRegistry(java.lang.String)}. + * + * @throws DeclarationException + * @throws IOException + * @throws ServiceMultiException + */ + @Test + public void testGetServicesRegistryString() throws DeclarationException, ServiceException, IOException { + + + // Initialize Factory + IExtensionRegistry extensionRegistry = EclipseExtensionUtils.instance.createRegistry("resources/plugin.xml"); + + IServiceDescriptorsWithIdProvider declarationsProvider = new ExtensionServiceDescriptorsWithIdProviderFromFile(extensionRegistry); + ServiceRegistryFactory factory = new ServiceRegistryFactory(declarationsProvider); + + + String serviceA = FakeServiceA.class.getName(); + String serviceB = FakeServiceB.class.getName(); + // Create ServiceRegistry from factory + ServicesRegistry servicesRegistry = factory.getServicesRegistry("simpleTestRegistry"); + servicesRegistry.startNewServices(); + + // Check result + assertNotNull("registry created", servicesRegistry); + + assertNotNull("Get registered service", servicesRegistry.getService(serviceA)); + assertNotNull("Get registered service", servicesRegistry.getService(serviceB)); + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServiceRegistryFactory#extendsServicesRegistry(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry, java.lang.String)}. + */ + @Test + public void testExtendsServicesRegistryServicesRegistryString() { + fail("Not yet implemented"); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceTest.java new file mode 100644 index 00000000000..6d3aaa2c76d --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServiceTest.java @@ -0,0 +1,52 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.fail; + +import org.junit.Test; + + +/** + * @author cedric dumoulin + * + */ +public class ServiceTest { + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.annotations.Service#value()}. + */ + @Test + public void testValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.annotations.Service#startupKind()}. + */ + @Test + public void testStartupKind() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.annotations.Service#priority()}. + */ + @Test + public void testPriority() { + fail("Not yet implemented"); + } + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryTest.java new file mode 100644 index 00000000000..e8810aa7f9e --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryTest.java @@ -0,0 +1,909 @@ +/** + * + */ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.papyrus.infra.core.serviceregistry.FakeServiceFactory.FakeCreatedService; +import org.eclipse.papyrus.infra.core.serviceregistry.ServiceA.TraceKind; +import org.junit.Test; + + +/** + * @author dumoulin + * + */ +public class ServicesRegistryTest extends TestCase { + + /** The registry to test */ + ServicesRegistry servicesRegistry; + + ServiceDescriptor serviceADesc = new LazyServiceADescriptor(); + + ServiceDescriptor serviceA10Desc = new LazyServiceA10Descriptor(); + + ServiceDescriptor serviceBDesc = new LazyServiceBDescriptor(); + + ServiceDescriptor serviceCDesc = new ServiceCDescriptor(); + + ServiceDescriptor servicePojoADesc = new LazyServicePojoADescriptor(); + + public ServicesRegistryTest(String name) { + super(name); + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void setUp() throws Exception { + servicesRegistry = new ServicesRegistry(); + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void tearDown() throws Exception { + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#add(org.eclipse.papyrus.infra.core.serviceregistry.ServiceDescriptor)} . + * + * @throws ServiceMultiException + */ + public void testAdd() throws ServiceMultiException { + + servicesRegistry.add(serviceADesc); + servicesRegistry.add(serviceBDesc); + servicesRegistry.add(serviceCDesc); + servicesRegistry.add(servicePojoADesc); + + // Test entries creation + // try { + // assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey())); + // assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey())); + // assertFalse("serviceC stopped", servicesRegistry.isStarted(serviceCDesc.getKey())); + // assertFalse("servicePojoA stopped", servicesRegistry.isStarted(servicePojoADesc.getKey())); + // } catch (ServiceNotFoundException e) { + // fail("Service should exist." + e.getMessage()); + // } + + // Test startup + servicesRegistry.startNewServices(); + + // Test always started + try { + assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey())); + assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey())); + assertEquals("serviceC state", ServiceState.started, servicesRegistry.serviceState(serviceCDesc.getKey())); + assertTrue("serviceC started", servicesRegistry.isStarted(serviceCDesc.getKey())); + assertFalse("servicePojoA stopped", servicesRegistry.isStarted(servicePojoADesc.getKey())); + } catch (ServiceNotFoundException e) { + fail("Service should exist."); + } + + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#getService(java.lang.Object)}. + * + * @throws ServiceException + */ + public void testGetService() throws ServiceException { + servicesRegistry.add(serviceADesc); + servicesRegistry.add(serviceBDesc); + servicesRegistry.add(serviceCDesc); + servicesRegistry.add(servicePojoADesc); + + + servicesRegistry.startNewServices(); + + // Test lazy service + Object serviceA = servicesRegistry.getService(serviceADesc.getKey()); + assertNotNull("service created", serviceA); + assertEquals("right class", ServiceA.class, serviceA.getClass()); + + Object serviceA2 = servicesRegistry.getService(serviceADesc.getKey()); + assertEquals("Second retrieve get the same service", serviceA, serviceA2); + + // test startup service + Object serviceC = servicesRegistry.getService(serviceCDesc.getKey()); + assertNotNull("service created", serviceC); + assertEquals("right class", ServiceC.class, serviceC.getClass()); + + Object serviceC2 = servicesRegistry.getService(serviceCDesc.getKey()); + assertEquals("Second retrieve get the same service", serviceC, serviceC2); + + // test pojo service + Object servicePojo = servicesRegistry.getService(servicePojoADesc.getKey()); + assertNotNull("service created", servicePojo); + assertEquals("right class", ServicePojoA.class, servicePojo.getClass()); + + Object servicePojo2 = servicesRegistry.getService(servicePojoADesc.getKey()); + assertEquals("Second retrieve get the same service", servicePojo, servicePojo2); + + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#getService(java.lang.Object)}. + * + * @throws ServiceException + */ + public void testGetServiceByClass() throws ServiceException { + servicesRegistry.add(serviceADesc); + servicesRegistry.add(serviceBDesc); + servicesRegistry.add(serviceCDesc); + servicesRegistry.add(servicePojoADesc); + + + servicesRegistry.startNewServices(); + + // Test lazy service + ServiceA serviceA = servicesRegistry.getService(ServiceA.class); + assertNotNull("service created", serviceA); + assertEquals("right class", ServiceA.class, serviceA.getClass()); + + ServiceA serviceA2 = servicesRegistry.getService(ServiceA.class); + assertEquals("Second retrieve get the same service", serviceA, serviceA2); + + // test startup service + ServiceC serviceC = servicesRegistry.getService(ServiceC.class); + assertNotNull("service created", serviceC); + assertEquals("right class", ServiceC.class, serviceC.getClass()); + + // test pojo service + ServicePojoA servicePojo = servicesRegistry.getService(ServicePojoA.class); + assertNotNull("service created", servicePojo); + assertEquals("right class", ServicePojoA.class, servicePojo.getClass()); + + Object servicePojo2 = servicesRegistry.getService(ServicePojoA.class); + assertEquals("Second retrieve get the same service", servicePojo, servicePojo2); + + + } + + /** + * Try to register 2 services under the same name, but with different priorities. + * + * @throws ServiceException + */ + public void testPriority() throws ServiceException { + servicesRegistry.add(serviceADesc); + servicesRegistry.add(serviceA10Desc); + servicesRegistry.add(serviceBDesc); + + // Test creation + // try { + // assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey())); + // assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey())); + // } catch (ServiceNotFoundException e) { + // fail("Service should exist."); + // } + + servicesRegistry.startNewServices(); + + Object serviceA = servicesRegistry.getService(serviceADesc.getKey()); + + assertNotNull("Service created", serviceA); + assertEquals("right class", ServiceA10.class, serviceA.getClass()); + + + } + + /** + * Test add for a direct instance of service. + * + * @throws ServiceException + */ + public void testAddDirectInstance() throws ServiceException { + IService instanciatedService = new ServiceA(); + String key = instanciatedService.getClass().getName(); + + servicesRegistry.add(key, 1, instanciatedService); + + servicesRegistry.add(serviceCDesc); + servicesRegistry.add(serviceBDesc); + + servicesRegistry.startNewServices(); + + Object serviceA = servicesRegistry.getService(key); + + assertNotNull("Service created", serviceA); + assertEquals("right object", instanciatedService, serviceA); + assertEquals("right class", ServiceA.class, serviceA.getClass()); + + + } + + /** + * Test life cycle for directly registered services + * + * @throws ServiceException + */ + public void testCallsOrder() throws ServiceException { + // Register services as STARTUP ! + servicesRegistry.add(ServiceA.class, 1, new ServiceA()); + // Lazy - generate no trace + servicesRegistry.add(ServiceB.class, 1, new ServiceB(), ServiceStartKind.LAZY); + servicesRegistry.add(ServiceC.class, 1, new ServiceC()); + servicesRegistry.add(ServicePojoA.class, 1, new ServicePojoA()); + + + ServiceA.resetTrace(); + servicesRegistry.startNewServices(); + + // Check orders + // + int i = 0; + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + + // Now, start lazy service + ServiceB service = servicesRegistry.getService(ServiceB.class); + assertNotNull("service found", service); + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + + + // Check dispose + servicesRegistry.disposeRegistry(); + assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + + // + } + + /** + * Test life cycle for services registered wih descriptors + * + * @throws ServiceException + */ + public void testCallsOrderForDescriptors() throws ServiceException { + // Register services as STARTUP ! + // Lazy - generate no trace + servicesRegistry.add(new LazyServiceBDescriptor()); + servicesRegistry.add(new ServiceCDescriptor()); + + + ServiceA.resetTrace(); + servicesRegistry.startNewServices(); + + // Check orders + // + int i = 0; + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + + // Now, start lazy service + ServiceB service = (ServiceB) servicesRegistry.getService(ServiceB.class.getName()); + assertNotNull("service found", service); + assertEquals("service", TraceKind.init, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.start, ServiceA.getEvent(i++)); + + + // Check dispose + servicesRegistry.disposeRegistry(); + assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + // assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++)); + + // + } + + /** + * Test the start order for services of type Start and Service + * + * @throws ServiceException + */ + public void testStartDependantOrder() throws ServiceException { + + String A = "A"; + String B = "B"; + String C = "C"; + String D = "D"; + String E = "E"; + // String F = "F"; + + // Create services E --> D --> C --> B --> A + FakeService.getTrace().reset(); + + servicesRegistry.add(new ServiceDesc(E, ServiceStartKind.STARTUP, Arrays.asList(D))); + servicesRegistry.add(new ServiceDesc(D, ServiceStartKind.STARTUP, Arrays.asList(C))); + servicesRegistry.add(new ServiceDesc(C, ServiceStartKind.STARTUP, Arrays.asList(B))); + servicesRegistry.add(new ServiceDesc(B, ServiceStartKind.STARTUP, Arrays.asList(A))); + servicesRegistry.add(new ServiceDesc(A, ServiceStartKind.STARTUP)); + // servicesRegistry.add( new ServiceIdDesc( F, ServiceStartKind.STARTUP, Arrays.asList(E) )); + + // Start services + servicesRegistry.startNewServices(); + + // check services + assertTrue("service started", servicesRegistry.isStarted(A)); + assertTrue("service started", servicesRegistry.isStarted(B)); + assertNotSame("services are different", servicesRegistry.getService(A), servicesRegistry.getService(B)); + + // Get names of created services + String nameA = ((FakeService) servicesRegistry.getService(A)).getName(); + String nameB = ((FakeService) servicesRegistry.getService(B)).getName(); + String nameC = ((FakeService) servicesRegistry.getService(C)).getName(); + String nameD = ((FakeService) servicesRegistry.getService(D)).getName(); + String nameE = ((FakeService) servicesRegistry.getService(E)).getName(); + // String nameF = ((FakeService)servicesRegistry.getService(F)).getName(); + + + // check order (E and F order are not guaranteed) + // order should be A, B, C, D, E + TestTrace trace = FakeService.getTrace(); + int i = 0; + // assertEquals("order", nameF + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameD + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameE + ",create", trace.getNameTrace(i++)); + + // assertEquals("order", nameF + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameD + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameE + ",init", trace.getNameTrace(i++)); + + // assertEquals("order", nameF + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameD + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameE + ",start", trace.getNameTrace(i++)); + + } + + /** + * Start regular services, then add a new service and try to start it. + * This should work. + * + * @throws ServiceException + */ + public void testStartRegistryAndThenAddNewServiceAndStartIt() throws ServiceException { + + // Register some services + String A = "A"; + String B = "B"; + String C = "C"; + servicesRegistry.add(new ServiceFactoryDesc(C, ServiceStartKind.STARTUP)); + servicesRegistry.add(new ServiceFactoryDesc(B, ServiceStartKind.STARTUP, Arrays.asList(C))); + servicesRegistry.add(new ServiceFactoryDesc(A, ServiceStartKind.STARTUP, Arrays.asList(B))); + + // Start them + servicesRegistry.startNewServices(); + + // Register another services as pojo + IService instanciatedService = new ServiceA(); + String key = instanciatedService.getClass().getName(); + servicesRegistry.add(key, 1, instanciatedService); + + // Try to start it + servicesRegistry.startServices(key); + + // check services + assertTrue("service started", servicesRegistry.isStarted(A)); + assertTrue("service started", servicesRegistry.isStarted(B)); + assertTrue("service started", servicesRegistry.isStarted(C)); + + assertTrue("service started", servicesRegistry.isStarted(key)); + + assertEquals("get registered service", instanciatedService, servicesRegistry.getService(key)); + + } + + /** + * Test Service Factory creation + * + * @throws ServiceException + */ + public void testServiceFactoryCreation() throws ServiceException { + + String A = "A"; + String B = "B"; + String C = "C"; + + // Create services + FakeService.getTrace().reset(); + + servicesRegistry.add(new ServiceFactoryDesc(C, ServiceStartKind.STARTUP)); + servicesRegistry.add(new ServiceFactoryDesc(B, ServiceStartKind.STARTUP, Arrays.asList(C))); + servicesRegistry.add(new ServiceFactoryDesc(A, ServiceStartKind.STARTUP, Arrays.asList(B))); + + // Start services + servicesRegistry.startNewServices(); + + // check services + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass()); + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass()); + + // Get names of created services + String nameA = ((FakeCreatedService) servicesRegistry.getService(A)).getName(); + String nameB = ((FakeCreatedService) servicesRegistry.getService(B)).getName(); + String nameC = ((FakeCreatedService) servicesRegistry.getService(C)).getName(); + + + // check order + // Order should be: C, B, A + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", nameC + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",create", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",init", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",start", trace.getNameTrace(i++)); + + // next order is the order of getService() + assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++)); + } + + /** + * Test Creation of Service Factory used with class as key. + * + * @throws ServiceException + */ + public void testServiceFactoryWithClassKey() throws ServiceException { + + // + Class<?> A = ServiceA.class; + Class<?> B = ServiceB.class; + Class<?> C = ServiceC.class; + + // Specify service types as String + String serviceClassname1 = FakeServiceFactory.class.getName(); + String serviceClassname2 = FakeServiceFactory.class.getName(); + String serviceClassname3 = FakeServiceFactory.class.getName(); + + // Create services + FakeService.getTrace().reset(); + + servicesRegistry.add(new ServiceFactoryDesc(C, serviceClassname3, ServiceStartKind.STARTUP)); + servicesRegistry.add(new ServiceFactoryDesc(B, serviceClassname2, ServiceStartKind.STARTUP, Arrays.asList(C.getName()))); + servicesRegistry.add(new ServiceFactoryDesc(A, serviceClassname1, ServiceStartKind.STARTUP, Arrays.asList(B.getName()))); + + // Start services + servicesRegistry.startNewServices(); + + // check services + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass()); + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass()); + + // Get names of created services + String nameA = ((FakeCreatedService) servicesRegistry.getService(A)).getName(); + String nameB = ((FakeCreatedService) servicesRegistry.getService(B)).getName(); + String nameC = ((FakeCreatedService) servicesRegistry.getService(C)).getName(); + + + // check order (E and F order are not guaranteed) + // Order should be + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", nameC + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",create", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",init", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",start", trace.getNameTrace(i++)); + + // next order is the order of getService() + assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++)); + } + + /** + * Test Service Factory creation order. + * One of the factory try to get the required service from its init() method. + * + * @throws ServiceException + */ + public void testServiceFactoryWithRequiredInInit() throws ServiceException { + + String A = "A"; + String B = "B"; + String C = "C"; + + // Create services C --> A --> B + FakeService.getTrace().reset(); + + // FakeWithRequiredServiceFactory try to retrieve service "C" + servicesRegistry.add(new ServiceFactoryDesc(B, FakeWithRequiredServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(C))); + servicesRegistry.add(new ServiceFactoryDesc(A, FakeWithRequiredServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(B, C))); + servicesRegistry.add(new ServiceFactoryDesc(C, FakeServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1)); + + // Start services + servicesRegistry.startNewServices(); + + // check services + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass()); + assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass()); + + // Get names of created services + String nameA = ((FakeCreatedService) servicesRegistry.getService(A)).getName(); + String nameB = ((FakeCreatedService) servicesRegistry.getService(B)).getName(); + String nameC = ((FakeCreatedService) servicesRegistry.getService(C)).getName(); + + + // check order + // Order should be: C, B, A + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", nameC + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",create", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",init", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++)); + assertEquals("value", servicesRegistry.getService(C), trace.getValue(i)); + assertEquals("order", nameB + ",initEnd", trace.getNameTrace(i++)); + + assertEquals("order", nameA + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",initEnd", trace.getNameTrace(i++)); + + assertEquals("order", nameC + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameA + ",start", trace.getNameTrace(i++)); + + // next order is the order of getService() + assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++)); + // assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++)); + // assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++)); + } + + /** + * Test {@link ServicesRegistry#startServicesByClassKeys(List)}. + * Check that services are started. + * + * @throws ServiceException + */ + public void testStartService() throws ServiceException { + + String A = "A"; + String B = "B"; + String C = "C"; + String D = "D"; + String E = "E"; + + // Create services E --> D --> C --> B --> A + FakeService.getTrace().reset(); + + servicesRegistry.add(new ServiceDesc(E, ServiceStartKind.STARTUP, Arrays.asList(D))); + servicesRegistry.add(new ServiceDesc(D, ServiceStartKind.STARTUP, Arrays.asList(C))); + servicesRegistry.add(new ServiceDesc(C, ServiceStartKind.STARTUP, Arrays.asList(A, B))); + servicesRegistry.add(new ServiceDesc(B, ServiceStartKind.STARTUP, Arrays.asList(A))); + servicesRegistry.add(new ServiceDesc(A, ServiceStartKind.STARTUP)); + + // Start services + servicesRegistry.startServices(Arrays.asList(C)); + + // check services + assertTrue("service started", servicesRegistry.isStarted(A)); + assertTrue("service started", servicesRegistry.isStarted(B)); + assertTrue("service started", servicesRegistry.isStarted(C)); + + // Get names of created services + String nameA = ((FakeService) servicesRegistry.getService(A)).getName(); + String nameB = ((FakeService) servicesRegistry.getService(B)).getName(); + String nameC = ((FakeService) servicesRegistry.getService(C)).getName(); + // String nameD = ((FakeService)servicesRegistry.getService(D)).getName(); + // String nameE = ((FakeService)servicesRegistry.getService(E)).getName(); + // String nameF = ((FakeService)servicesRegistry.getService(F)).getName(); + + + // check order (E and F order are not guaranteed) + // order should be A, B, C + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", nameA + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",create", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",create", trace.getNameTrace(i++)); + // assertEquals("order", nameD + ",create", trace.getNameTrace(i++)); + // assertEquals("order", nameE + ",create", trace.getNameTrace(i++)); + + assertEquals("order", nameA + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",init", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",init", trace.getNameTrace(i++)); + // assertEquals("order", nameD + ",init", trace.getNameTrace(i++)); + // assertEquals("order", nameE + ",init", trace.getNameTrace(i++)); + + assertEquals("order", nameA + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameB + ",start", trace.getNameTrace(i++)); + assertEquals("order", nameC + ",start", trace.getNameTrace(i++)); + // assertEquals("order", nameD + ",start", trace.getNameTrace(i++)); + // assertEquals("order", nameE + ",start", trace.getNameTrace(i++)); + + } + + /* **************************************** */ + + + + /** + * Test the alias service. + * + * @throws ServiceException + */ + @Test + public void testSimpleStartServices() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + Class<?> serviceC = FakeServiceC.class; + + // services A --> B + FakeService.getTrace().reset(); + + // Create an alias to B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new ServiceDescriptor(serviceA.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(serviceB.getName()))); + registryA.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryA.add(new ServiceDescriptor(serviceC.getName(), ServiceStartKind.STARTUP, 1)); + + registryA.startNewServices(); + + + // check services + assertNotNull("got service started", registryA.getService(serviceB)); + assertNotNull("got service started", registryA.getService(serviceC)); + assertNotNull("got service started", registryA.getService(serviceA)); + + } + + /** + * Test the alias service. + * + * @throws ServiceException + */ + @Test + public void testChainWithLazyStartServices() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + Class<?> serviceC = FakeServiceC.class; + + FakeService.getTrace().reset(); + + // services A --> B (lazy) --> C + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new ServiceDescriptor(serviceA.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(serviceB.getName()))); + registryA.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.LAZY, 1, Arrays.asList(serviceC.getName()))); + registryA.add(new ServiceDescriptor(serviceC.getName(), ServiceStartKind.STARTUP, 1)); + + FakeService.getTrace().reset(); + registryA.startNewServices(); + + + // check services + // A and C should be started, B should not be started. + assertTrue("A started", registryA.isStarted(serviceA)); + assertFalse("B not started", registryA.isStarted(serviceB)); + assertTrue("C started", registryA.isStarted(serviceC)); + + // Check if we can get them + assertNotNull("got service started", registryA.getService(serviceC)); + assertNotNull("got service started", registryA.getService(serviceA)); + + // check creation order (C then A) + // order should be C, A + TestTrace trace = FakeService.getTrace(); + int i = 0; + assertEquals("order", serviceC.getSimpleName() + ",create", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",create", trace.getNameTrace(i++)); + + assertEquals("order", serviceC.getSimpleName() + ",init", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",init", trace.getNameTrace(i++)); + + assertEquals("order", serviceC.getSimpleName() + ",start", trace.getNameTrace(i++)); + assertEquals("order", serviceA.getSimpleName() + ",start", trace.getNameTrace(i++)); + + // start B + assertNotNull("got service started", registryA.getService(serviceB)); + assertTrue("B started", registryA.isStarted(serviceB)); + + + } + + + + /** + * Test the alias service. + * + * @throws ServiceException + */ + @Test + public void testAliasServices() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + Class<?> serviceC = FakeServiceC.class; + + String aliasService2 = "AnAlias"; + + // services A --> B + FakeService.getTrace().reset(); + + // Create an alias to B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new AliasDescriptor(serviceA.getName(), serviceB.getName(), 1)); + registryA.add(new AliasDescriptor(aliasService2, serviceC.getName(), 1)); + registryA.add(new ServiceDescriptor(serviceB, serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryA.add(new ServiceDescriptor(serviceC.getName(), ServiceStartKind.STARTUP, 1)); + + registryA.startNewServices(); + + + // check services + assertNotNull("got service started", registryA.getService(serviceB)); + assertNotNull("got service started", registryA.getService(serviceC)); + assertNotNull("got service started", registryA.getService(serviceA)); + assertNotNull("got service started", registryA.getService(aliasService2)); + + assertEquals("Got the same service", registryA.getService(serviceA), registryA.getService(serviceB)); + assertEquals("Got the same service", registryA.getService(serviceC), registryA.getService(aliasService2)); + + } + + /** + * General purpose descriptor. + * + * @deprecated use {@link FakeServiceDescriptor} + */ + @Deprecated + public class ServiceDesc extends ServiceDescriptor { + + + public ServiceDesc(String key, ServiceStartKind startKind) { + super(key, FakeService.class.getName(), startKind, 1); + } + + public ServiceDesc(String key, ServiceStartKind startKind, List<String> requiredService) { + super(key, FakeService.class.getName(), startKind, 1, requiredService); + } + } + + /** + * General purpose descriptor. + */ + public class ServiceFactoryDesc extends ServiceDescriptor { + + + public ServiceFactoryDesc(String key, ServiceStartKind startKind) { + super(key, FakeServiceFactory.class.getName(), startKind, 1); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + + public ServiceFactoryDesc(String key, ServiceStartKind startKind, List<String> requiredService) { + super(key, FakeServiceFactory.class.getName(), startKind, 1, requiredService); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + + /** + * Constructor with a classname and {@link ServiceDescriptor#isStartAtStartup()} = true. + * Constructor. + * + * @param key + * @param startKind + * @param requiredService + */ + public ServiceFactoryDesc(Class<?> key, String classname, ServiceStartKind startKind, List<String> requiredService) { + super(key, classname, startKind, 1, requiredService); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + + /** + * Constructor with a classname and {@link ServiceDescriptor#isStartAtStartup()} = true. + * Constructor. + * + * @param key + * @param startKind + * @param requiredService + */ + public ServiceFactoryDesc(Class<?> key, String classname, ServiceStartKind startKind) { + super(key, classname, startKind, 1); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + + public ServiceFactoryDesc(String key, String serviceClassname, ServiceStartKind serviceStartKind, int priority, List<String> requiredServices) { + super(key, serviceClassname, serviceStartKind, priority, requiredServices); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + + public ServiceFactoryDesc(String key, String serviceClassname, ServiceStartKind serviceStartKind, int priority) { + super(key, serviceClassname, serviceStartKind, priority); + setServiceTypeKind(ServiceTypeKind.serviceFactory); + } + } + + /** + * + * @author dumoulin + * + */ + public class LazyServiceADescriptor extends ServiceDescriptor { + + + public LazyServiceADescriptor() { + super(ServiceA.class.getName(), ServiceStartKind.LAZY, 1); + } + } + + public class LazyServiceA10Descriptor extends ServiceDescriptor { + + + public LazyServiceA10Descriptor() { + super(ServiceA.class.getName(), ServiceA10.class.getName(), ServiceStartKind.LAZY, 10); + } + } + + public class LazyServiceBDescriptor extends ServiceDescriptor { + + + public LazyServiceBDescriptor() { + super(ServiceB.class.getName(), ServiceStartKind.LAZY, 1); + } + } + + public class ServiceCDescriptor extends ServiceDescriptor { + + + public ServiceCDescriptor() { + super(ServiceC.class.getName(), ServiceStartKind.STARTUP, 1); + } + } + + public class LazyServicePojoADescriptor extends ServiceDescriptor { + + + public LazyServicePojoADescriptor() { + super(ServicePojoA.class.getName(), ServiceStartKind.LAZY, 1); + } + } + + public class PojoServiceDescriptor extends ServiceDescriptor { + + + public PojoServiceDescriptor() { + super(Object.class.getName(), ServiceStartKind.STARTUP, 1); + } + } + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryWithParentTest.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryWithParentTest.java new file mode 100644 index 00000000000..0cf0f8011cf --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/ServicesRegistryWithParentTest.java @@ -0,0 +1,365 @@ +/***************************************************************************** + * Copyright (c) 2012 Cedric Dumoulin. + * + * + * 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: + * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.core.serviceregistry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +/** + * Tests ServicesRegistry with a parent registry. + * + * @author cedric dumoulin + * + */ +public class ServicesRegistryWithParentTest { + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#addParentRegistry(org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry)}. + */ + @Test + public void testAddParentRegistry() { + + // Create a service registry + ServicesRegistry registry = new ServicesRegistry(); + // Add a parent + ServicesRegistry parentRegistry1 = new ServicesRegistry(); + registry.addParentRegistry(parentRegistry1); + + // Add a second parent + ServicesRegistry parentRegistry2 = new ServicesRegistry(); + registry.addParentRegistry(parentRegistry2); + + // Check if parents are set + // No way to do that. Just check the registry (but this will always succeed) + assertNotNull("registry created", registry); + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#getService(java.lang.Object)}. + * Create a registry with 2 parents. + * Add a service in each registry + * Try to get each service from the main regitry. + * + * @throws ServiceException + * + */ + @Test + public void testGetServiceObject() throws ServiceException { + + String A = "serviceA"; + String B = "serviceB"; + String C = "serviceC"; + + FakeService.getTrace().reset(); + + // Create a service registry + ServicesRegistry registry = new ServicesRegistry(); + // Add a service + registry.add(new FakeServiceDescriptor(A, ServiceStartKind.STARTUP)); + registry.startNewServices(); + + + // Create parent registry with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new FakeServiceDescriptor(B, ServiceStartKind.STARTUP)); + registryB.startNewServices(); + // Add as parent + registry.addParentRegistry(registryB); + + // Create parent registry with one service + ServicesRegistry registryC = new ServicesRegistry(); + registryC.add(new FakeServiceDescriptor(C, ServiceStartKind.STARTUP)); + registryC.startNewServices(); + // Add as parent + registry.addParentRegistry(registryC); + + // Check if we can retrieve service + + assertNotNull("got service", registry.getService(A)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(A))); + + assertNotNull("got service", registry.getService(B)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(B))); + + assertNotNull("got service", registry.getService(C)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(C))); + + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#getService(java.lang.Class)}. + * + * @throws ServiceException + */ + @Test + public void testGetServiceClassOfS() throws ServiceException { + + FakeService.getTrace().reset(); + + // Create a service registry + ServicesRegistry registry = new ServicesRegistry(); + // Add a service + registry.add(new ServiceDescriptor(FakeServiceA.class.getName(), ServiceStartKind.STARTUP, 1)); + registry.startNewServices(); + + + // Create parent registry with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(FakeServiceB.class.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + // Add as parent + registry.addParentRegistry(registryB); + + // Create parent registry with one service + ServicesRegistry registryC = new ServicesRegistry(); + registryC.add(new ServiceDescriptor(FakeServiceC.class.getName(), ServiceStartKind.STARTUP, 1)); + registryC.startNewServices(); + // Add as parent + registry.addParentRegistry(registryC); + + // Check if we can retrieve service + + assertNotNull("got service", registry.getService(FakeServiceA.class)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(FakeServiceA.class))); + + assertNotNull("got service", registry.getService(FakeServiceB.class)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(FakeServiceB.class))); + + assertNotNull("got service", registry.getService(FakeServiceC.class)); + assertTrue("Right type", FakeServiceDescriptor.getServiceType().isInstance(registry.getService(FakeServiceC.class))); + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#startRegistry()}. + * + * @throws ServiceException + */ + @Test + public void testStartRegistry() throws ServiceException { + FakeService.getTrace().reset(); + + // Create a service registry + ServicesRegistry registry = new ServicesRegistry(); + // Add a service + registry.add(new ServiceDescriptor(FakeServiceA.class.getName(), ServiceStartKind.STARTUP, 1)); + registry.startNewServices(); + + assertTrue("registry and service started", registry.isStarted(FakeServiceA.class.getName())); + } + + /** + * Test method for {@link org.eclipse.papyrus.infra.core.serviceregistry.ServicesRegistry#disposeRegistry()}. + * + * @throws ServiceMultiException + */ + @Test + public void testDisposeRegistry() throws ServiceMultiException { + FakeService.getTrace().reset(); + + // Create a service registry + ServicesRegistry registry = new ServicesRegistry(); + // Add a service + registry.add(new ServiceDescriptor(FakeServiceA.class.getName(), ServiceStartKind.STARTUP, 1)); + registry.startNewServices(); + + + // Create parent registry with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(FakeServiceB.class.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + // Add as parent + registry.addParentRegistry(registryB); + + // Create parent registry with one service + ServicesRegistry registryC = new ServicesRegistry(); + registryC.add(new ServiceDescriptor(FakeServiceC.class.getName(), ServiceStartKind.STARTUP, 1)); + registryC.startNewServices(); + // Add as parent + registry.addParentRegistry(registryC); + + // Check recursive dispose + FakeService.getTrace().reset(); + registry.disposeRegistry(); + + // + TestTrace trace = FakeService.getTrace(); + assertTrue("dispose called", trace.contains(FakeServiceA.class.getSimpleName(), "dispose")); + assertTrue("dispose called", trace.contains(FakeServiceB.class.getSimpleName(), "dispose")); + assertTrue("dispose called", trace.contains(FakeServiceC.class.getSimpleName(), "dispose")); + } + + /** + * Test the start order for services having a dependency in a parent registry + * + * @throws ServiceException + */ + @Test + public void testStartServicesWithOutsideDependencies() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + + // services A --> B + FakeService.getTrace().reset(); + + // Create parent registry B with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + + // Create a service registry A -parent-> B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new ServiceDescriptor(serviceA.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(serviceB.getName()))); + + registryA.addParentRegistry(registryB); + registryA.startNewServices(); + + + // check services + assertTrue("service started", registryA.isStarted(serviceA)); + assertTrue("service started", registryB.isStarted(serviceB)); + assertTrue("service started", registryA.isStarted(serviceB)); + + } + + /** + * Test {@link ServicesRegistry#isStarted(Object)} for services in a parent registry + * + * @throws ServiceException + */ + @Test + public void testIsStarted() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + + // services A --> B + FakeService.getTrace().reset(); + + // Create parent registry B with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + + // Create a service registry A -parent-> B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new ServiceDescriptor(serviceA.getName(), ServiceStartKind.STARTUP, 1)); + + registryA.addParentRegistry(registryB); + registryA.startNewServices(); + + + // check services + assertTrue("service started", registryA.isStarted(serviceA)); + assertTrue("service started", registryB.isStarted(serviceB)); + // Access from registryA + assertTrue("service started", registryA.isStarted(serviceB)); + + } + + /** + * Test {@link ServicesRegistry#isStarted(Object)} for services in a parent registry + * + * @throws ServiceException + */ + @Test + public void testServiceState() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + + // services A --> B + FakeService.getTrace().reset(); + + // Create parent registry B with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + + // Create a service registry A -parent-> B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new ServiceDescriptor(serviceA.getName(), ServiceStartKind.STARTUP, 1)); + + registryA.addParentRegistry(registryB); + registryA.startNewServices(); + + + // check services + assertEquals("service started", ServiceState.started, registryA.serviceState(serviceA)); + assertEquals("service started", ServiceState.started, registryB.serviceState(serviceB)); + // Access from registryA + assertEquals("service started", ServiceState.started, registryA.serviceState(serviceB)); + + } + + /** + * Test the alias service. + * + * @throws ServiceException + */ + @Test + public void testAliasServices() throws ServiceException { + + Class<?> serviceA = FakeServiceA.class; + Class<?> serviceB = FakeServiceB.class; + + // services A --> B + FakeService.getTrace().reset(); + + // Create parent registry B with one service + ServicesRegistry registryB = new ServicesRegistry(); + registryB.add(new ServiceDescriptor(serviceB.getName(), ServiceStartKind.STARTUP, 1)); + registryB.startNewServices(); + + // Create an alias to B + ServicesRegistry registryA = new ServicesRegistry(); + registryA.add(new AliasDescriptor(serviceA.getName(), serviceB.getName(), 1)); + + registryA.addParentRegistry(registryB); + registryA.startNewServices(); + + + // check services + assertNotNull("got service started", registryA.getService(serviceA)); + assertNotNull("got service started", registryB.getService(serviceB)); + + assertEquals("Got the same service", registryA.getService(serviceA), registryA.getService(serviceB)); + + } + + +} diff --git a/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/TestTrace.java b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/TestTrace.java new file mode 100644 index 00000000000..82c4943d374 --- /dev/null +++ b/plugins/infra/core/serviceregistry/org.eclipse.papyrus.infra.core.serviceregistry.tests/tests/org/eclipse/papyrus/infra/core/serviceregistry/TestTrace.java @@ -0,0 +1,193 @@ +/** + * + */ +package org.eclipse.papyrus.infra.core.serviceregistry; + +import java.util.ArrayList; +import java.util.List; + + +/** + * A class used to record traces for tests + * + * @author cedric dumoulin + * + */ +public class TestTrace { + + public List<TraceRecord> traces = new ArrayList<TraceRecord>(); + + /** + * Record a new trace. + * + * @param trace + */ + public void addTrace(String trace) { + addTrace(null, trace, null); + } + + /** + * Record a new trace. + * + * @param trace + */ + public void addTrace(String name, String trace) { + addTrace(name, trace, null); + } + + /** + * Record a new trace. + * + * @param trace + */ + public void addTrace(String name, String trace, Object value) { + traces.add(new TraceRecord(name, trace, value)); + } + + /** + * Return the name and the trace in one concatenated string ("name,trace"). + * + * @param i + * Index of the requested trace + * @return + */ + public String getNameTrace(int i) { + return traces.get(i).getNameTrace(); + } + + /** + * Get the value + * + * @param i + * @return + */ + public Object getValue(int i) { + return traces.get(i).value; + } + + /** + * Return true if the trace contains the specified events. + * + * @return + */ + public boolean contains(String name, String trace) { + + return indexOfNameTrace(name, trace) >= 0; + } + + /** + * Returns the index of the first occurrence of the specified elements + * in these lists, or -1 if this list does not contain the element. + */ + public int indexOf(String name, String trace, Object value) { + + for (int i = 0; i < traces.size(); i++) { + TraceRecord record = traces.get(i); + + if (name.equals(record.name) && trace.equals(record.trace)) { + return i; + } + } + + return -1; + } + + /** + * Returns the index of the first occurrence of the specified elements + * in these lists, or -1 if this list does not contain the element. + */ + public int indexOfNameTrace(String name, String trace) { + + for (int i = 0; i < traces.size(); i++) { + TraceRecord record = traces.get(i); + + if (name.equals(record.name) && trace.equals(record.trace)) { + return i; + } + } + + return -1; + } + + /** + * Returns the index of the first occurrence of the specified elements + * in these lists, or -1 if this list does not contain the element. + */ + public int indexOfTrace(String trace) { + + for (int i = 0; i < traces.size(); i++) { + TraceRecord record = traces.get(i); + + if (trace.equals(record.trace)) { + return i; + } + } + + return -1; + } + + /** + * Returns the index of the first occurrence of the specified elements + * in these lists, or -1 if this list does not contain the element. + */ + public int indexOfName(String name) { + + for (int i = 0; i < traces.size(); i++) { + TraceRecord record = traces.get(i); + + if (name.equals(record.name)) { + return i; + } + } + + return -1; + } + + /** + * Reset the trace. + */ + public void reset() { + traces.clear(); + } + + + /** + * @return the traces + */ + public List<TraceRecord> getTraces() { + return traces; + } + + /** + * A Record of the trace. + */ + public class TraceRecord { + public String name; + public String trace; + public Object value; + + /** + * Constructor. + * + * @param name + * @param trace + * @param value + */ + public TraceRecord(String name, String trace, Object value) { + this.name = name; + this.trace = trace; + this.value = value; + } + + /** + * + * @return + */ + public String getNameTrace() { + // TODO Auto-generated method stub + return name + "," + trace; + } + + } + +} |