From: erik Date: Wed, 4 Jun 2008 20:08:00 +0000 (+0000) Subject: provided interface now sets its own unique id. X-Git-Tag: wamblee-utils-0.2@603~88 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=6222acf41ea4bd49fe8c9e76e1709e472171d7f3;p=utils provided interface now sets its own unique id. --- diff --git a/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java b/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java index 10b8f618..d3a40bfe 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.UUID; /** @@ -43,7 +44,7 @@ public class DefaultProvidedInterface implements ProvidedInterface { public DefaultProvidedInterface(String aName, Class[] aInterfaces) { _name = aName; _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length); - _uniqueId = null; + _uniqueId = UUID.randomUUID().toString(); } @Override @@ -55,11 +56,6 @@ public class DefaultProvidedInterface implements ProvidedInterface { public Class[] getInterfaceTypes() { return _interfaces; } - - @Override - public void setUniqueId(String aId) { - _uniqueId = aId; - } @Override public String getUniqueId() { diff --git a/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultScope.java b/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultScope.java index 51949ca1..18f26cc8 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultScope.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/core/DefaultScope.java @@ -81,10 +81,8 @@ public class DefaultScope implements Scope { @Override synchronized public void publishInterface(ProvidedInterface aInterface, Object aImplementation) { - String id = UUID.randomUUID().toString(); - _provided.put(id, new ProvidedInterfaceImplementation(aInterface, + _provided.put(aInterface.getUniqueId(), new ProvidedInterfaceImplementation(aInterface, aImplementation)); - aInterface.setUniqueId(id); } @Override diff --git a/trunk/system/general/src/main/java/org/wamblee/system/core/ProvidedInterface.java b/trunk/system/general/src/main/java/org/wamblee/system/core/ProvidedInterface.java index fe7f7d26..aa2621f7 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/core/ProvidedInterface.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/core/ProvidedInterface.java @@ -36,17 +36,9 @@ public interface ProvidedInterface { * @return Service type. */ Class[] getInterfaceTypes(); - - /** - * Sets a unique id of the provided interface to identify it within a given scope. - * Will be called by the container as part of calling {@link #publish(Object, Scope)}. - * @param aId Unique id. - */ - void setUniqueId(String aId); /** - * Gets the unique if of the provided interface. This is set by the container - * using {@link #setUniqueId(String)}. + * Gets the unique if of the provided interface. */ String getUniqueId(); diff --git a/trunk/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java b/trunk/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java index cc18db2e..83abf3c2 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java @@ -201,7 +201,6 @@ public class ContainerTest extends TestCase { return; } fail(); - } public void testDuplicateComponent() { @@ -216,6 +215,7 @@ public class ContainerTest extends TestCase { fail(); } + public void testInconsistentHierarchy() { try { Component comp = new Application(); @@ -527,74 +527,79 @@ public class ContainerTest extends TestCase { Thread.currentThread()).toArray(new String[0])); } - - public void testNonUniqueRequiredInterface() { + + public void testNonUniqueRequiredInterface() { final Container container = new Container("top"); - container.addRequiredInterface(new DefaultRequiredInterface("i", Integer.class)); - container.addRequiredInterface(new DefaultRequiredInterface("x", String.class)); - container.addRequiredInterface(new DefaultRequiredInterface("y", String.class)); - + container.addRequiredInterface(new DefaultRequiredInterface("i", + Integer.class)); + container.addRequiredInterface(new DefaultRequiredInterface("x", + String.class)); + container.addRequiredInterface(new DefaultRequiredInterface("y", + String.class)); + Application app = new Application("1"); container.addComponent(app); - - AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { @Override public void run() throws Exception { - container.start(); + container.start(); } }, SystemAssemblyException.class); - - container.connectExternalRequired("1", app.getRequiredInterfaces()[0].getName(), - "y"); - + + container.connectExternalRequired("1", app.getRequiredInterfaces()[0] + .getName(), "y"); + ProvidedInterface i = new DefaultProvidedInterface("i", Integer.class); ProvidedInterface x = new DefaultProvidedInterface("x", String.class); ProvidedInterface y = new DefaultProvidedInterface("y", String.class); - + Scope externalScope = new DefaultScope(new ProvidedInterface[0]); - + externalScope.publishInterface(i, 100); externalScope.publishInterface(x, "x-value"); externalScope.publishInterface(y, "y-value"); - + container.getRequiredInterfaces()[0].setProvider(i); container.getRequiredInterfaces()[1].setProvider(x); container.getRequiredInterfaces()[2].setProvider(y); - + Scope runtime = container.start(externalScope); - + assertEquals("y-value", app.getString()); - + } - - public void testNonUniqueProvidedInterface() { - - final Container container = new Container("top").addProvidedInterface(new DefaultProvidedInterface("external", String.class)); + + public void testNonUniqueProvidedInterface() { + + final Container container = new Container("top") + .addProvidedInterface(new DefaultProvidedInterface("external", + String.class)); Environment env1 = new Environment("env1"); Environment env2 = new Environment("env2"); - - - container.addComponent(env1); + + container.addComponent(env1); container.addComponent(env2); - - AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { @Override public void run() throws Exception { - container.start(); + container.start(); } }, SystemAssemblyException.class); - + // now choose env2 - - container.connectExternalProvided(container.getProvidedInterfaces()[0].getName(), - env2.getName(), env2.getProvidedInterfaces()[0].getName()); - + + container.connectExternalProvided(container.getProvidedInterfaces()[0] + .getName(), env2.getName(), env2.getProvidedInterfaces()[0] + .getName()); + Scope scope = container.start(); - + // check the value of the provided interface of the container - - String value = scope.getInterfaceImplementation(container.getProvidedInterfaces()[0], String.class); + + String value = scope.getInterfaceImplementation(container + .getProvidedInterfaces()[0], String.class); assertNotNull(value); assertEquals(value, env2.getString()); assertFalse(value.equals(env1.getString())); diff --git a/trunk/system/general/src/test/java/org/wamblee/system/core/DefaultScopeTest.java b/trunk/system/general/src/test/java/org/wamblee/system/core/DefaultScopeTest.java index 8458c326..d07ccd45 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/core/DefaultScopeTest.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/core/DefaultScopeTest.java @@ -22,10 +22,10 @@ public class DefaultScopeTest extends TestCase { public void testLookup() { ProvidedInterface provider = new DefaultProvidedInterface("x", Integer.class); Scope scope = new DefaultScope(new ProvidedInterface[0]); - - assertNull(provider.getUniqueId()); + scope.publishInterface(provider, 100); assertNotNull(provider.getUniqueId()); + assertEquals(100, scope.getInterfaceImplementation(provider, Integer.class).intValue()); } public void testNestedLookup() {