(no commit message)
authorErik Brakkee <erik@brakkee.org>
Sun, 20 Apr 2008 20:29:49 +0000 (20:29 +0000)
committerErik Brakkee <erik@brakkee.org>
Sun, 20 Apr 2008 20:29:49 +0000 (20:29 +0000)
system/general/src/main/java/org/wamblee/system/core/DefaultInterfaceRestriction.java [new file with mode: 0644]
system/general/src/main/java/org/wamblee/system/core/InterfaceRestriction.java [new file with mode: 0644]
system/general/src/test/java/org/wamblee/system/core/Application.java
system/general/src/test/java/org/wamblee/system/core/DefaultInterfaceRestrictionTest.java [new file with mode: 0644]
system/general/src/test/java/org/wamblee/system/core/Environment.java

diff --git a/system/general/src/main/java/org/wamblee/system/core/DefaultInterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/core/DefaultInterfaceRestriction.java
new file mode 100644 (file)
index 0000000..ece7b28
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.system.core;
+
+/**
+ * An interface restriction on the required interface of a component. 
+ * 
+ * @author Erik Brakkee
+ *
+ */
+public class DefaultInterfaceRestriction implements InterfaceRestriction {
+
+    private String _client;
+    private String _required;
+    private String _server;
+    private String _provided;
+
+    /**
+     * Constructs the restriction. If the client name and required interface
+     * name match, then the server name and interface name must also match. 
+     * Otherwise the restriction is violated. 
+     * 
+     * This can be used to explicitly connect required and provided interfaces
+     * of components. 
+     * 
+     * @param aClient
+     *            Client or null if no restriction on the client.
+     * @param aRequired
+     *            Required interface name or null if no restriction on the
+     *            required interface.
+     * @param aServer
+     *            Server or null if no restriction on the server.
+     * @param aProvided
+     *            Provided interface name or null if no restriction on the
+     *            provided interface.
+     */
+    public DefaultInterfaceRestriction(String aClient, String aRequired,
+            String aServer, String aProvided) {
+        
+        if ( aClient == null && aRequired == null ) { 
+            throw new IllegalArgumentException("No restriction on the required interface");
+        }
+        if ( aServer == null && aProvided == null ) { 
+            throw new IllegalArgumentException("No restriction on the provided interface");
+        }
+        _client = aClient;
+        _required = aRequired;
+        _server = aServer;
+        _provided = aProvided;
+    }
+
+    @Override
+    public boolean isViolated(Component aClient, RequiredInterface aRequired,
+            Component aServer, ProvidedInterface aProvided) {
+        
+        if ( _client != null && !aClient.getName().equals(_client)) { 
+            return false; 
+        }
+        if ( _required != null && !aRequired.getName().equals(_required)) { 
+            return false; 
+        }
+        
+        // The required interface matches
+        
+        if ( _server != null && !aServer.getName().equals(_server)) {
+            // Server was specified and does not match.
+            return true; 
+        }
+        if ( _provided != null && !aProvided.getName().equals(_provided)) { 
+            // provided interface was specified and doe not match. 
+            return true; 
+        }
+        
+        // The required and provided interfaces match so this is not a violation.
+        
+        return false; 
+    }
+
+}
diff --git a/system/general/src/main/java/org/wamblee/system/core/InterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/core/InterfaceRestriction.java
new file mode 100644 (file)
index 0000000..d84af1c
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+package org.wamblee.system.core;
+
+/**
+ * Interface restriction for restricting the linking of provided and required
+ * interfaces. 
+ * 
+ * @author Erik Brakkee
+ *
+ */
+public interface InterfaceRestriction {
+
+    /**
+     * Checks if connecting the given provided and required interface would violate
+     * the restriction. 
+     * 
+     * @param aClient Client component requiring an interface. 
+     * @param aRequired Required interface. 
+     * @param aServer Server component providing an interface. 
+     * @param aProvided Provided interface. 
+     */
+    boolean isViolated(Component aClient, RequiredInterface aRequired, 
+            Component aServer, ProvidedInterface aProvided); 
+}
index c162bb04400523af6b1b4f5c88efb8ad3542d948..5e771b326464676d3729ffed4d2be370ea21f6f2 100644 (file)
@@ -24,13 +24,18 @@ import org.wamblee.system.core.RequiredInterface;
 import org.wamblee.test.EventTracker;
 
 public class Application extends AbstractComponent {
-       public static RequiredInterface[] required(boolean aOptional) {
+       public static RequiredInterface[] required(boolean aOptional, String aPrefix) {
                return
                new RequiredInterface[] { 
-                       new DefaultRequiredInterface("string", String.class, aOptional), 
-                       new DefaultRequiredInterface("integer", Integer.class, aOptional)
+                       new DefaultRequiredInterface(aPrefix + "string", String.class, aOptional), 
+                       new DefaultRequiredInterface(aPrefix + "integer", Integer.class, aOptional)
        };
        }
+       
+       public static RequiredInterface[] required(boolean aOptional) {
+        return required(aOptional, "");
+    }
+    
 
        private EventTracker<String> _tracker;
        private String _string; 
@@ -38,17 +43,20 @@ public class Application extends AbstractComponent {
        private double _random; 
        
        public Application() {
-               super("application", new ProvidedInterface[0], required(false));
-               _random = Math.random();
+           this("application");
        }
        
        public Application(String aName) {
-        super(aName, new ProvidedInterface[0], required(false));
+           this(aName, "");
+    }
+       
+       public Application(String aName, String aPrefix) {
+        super(aName, new ProvidedInterface[0], required(false, aPrefix));
         _random = Math.random();
     }
        
        public Application(boolean aIsOptinal) { 
-               super("application", new ProvidedInterface[0], required(true)); 
+               super("application", new ProvidedInterface[0], required(true, "")); 
        }
        
        public Application(EventTracker<String> aTracker) { 
diff --git a/system/general/src/test/java/org/wamblee/system/core/DefaultInterfaceRestrictionTest.java b/system/general/src/test/java/org/wamblee/system/core/DefaultInterfaceRestrictionTest.java
new file mode 100644 (file)
index 0000000..81d4751
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2008 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+package org.wamblee.system.core;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.wamblee.test.AssertionUtils;
+
+import junit.framework.TestCase;
+
+public class DefaultInterfaceRestrictionTest extends TestCase {
+    private Application _app1 = new Application("app1", "pf1.");
+    private Application _app2 = new Application("app2", "pf2.");
+    
+    private Environment _env1 = new Environment("env1", "pf3.");
+    private Environment _env2 = new Environment("env2", "pf4.");
+    
+    private void compare(Boolean[] aExpected, InterfaceRestriction aRestriction) { 
+       List<Boolean> result = new ArrayList<Boolean>();
+       
+       // order will be: 
+       //     env1, app1
+       //     env1, app2
+       //     env2, app1
+       //     env2, app2
+       for (Environment env: new Environment[] { _env1, _env2} ) { 
+           for (Application app: new Application[] { _app1, _app2} ) { 
+               result.add(aRestriction.isViolated(
+                       app, app.getRequiredInterfaces()[0], 
+                       env, 
+                       env.getProvidedInterfaces()[0]
+                       ));
+           }
+       }
+     
+       
+       assertEquals(Arrays.asList(aExpected), result);
+    }
+
+    public void testNoRestriction() { 
+        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+            @Override
+            public void run() throws Exception {
+                InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, null, null);
+                
+            }
+        }, IllegalArgumentException.class);
+        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+            @Override
+            public void run() throws Exception {
+                InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, "x", "y");
+                
+            }
+        }, IllegalArgumentException.class);
+        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+            @Override
+            public void run() throws Exception {
+                InterfaceRestriction restriction = new DefaultInterfaceRestriction("x", "y", null, null);
+                
+            }
+        }, IllegalArgumentException.class);
+    }
+    
+    public void testClientServer() { 
+        InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, "env1", null);
+        compare(new Boolean[] { false, false, true, false}, restriction);
+    }
+    
+    public void testRequiredServer() { 
+        InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", "env1", null);
+        compare(new Boolean[] { false, false, true, false}, restriction);
+    }
+    
+    public void testClientProvided() { 
+        InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, null, "pf4.datasource");
+        compare(new Boolean[] { true, false, false, false}, restriction);
+    }
+    
+    public void testRequiredProvide() { 
+        InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", null, "pf4.datasource");
+        compare(new Boolean[] { true, false, false, false}, restriction);
+    }
+    
+    public void testExplicitConfig() { 
+        _app1 = new Application("app1");
+        _app2 = new Application("app2");
+        _env1 = new Environment("env1");
+        _env2 = new Environment("env2");
+        
+        InterfaceRestriction restriction = new DefaultInterfaceRestriction(
+                "app2", "string", "env1", "datasource");
+        compare(new Boolean[] { false, false, false, true}, restriction);
+        
+    }
+}
index 00d947b7504e06948eedb178dc1ef6636cfd7308..f176d4aa748e2fa3a5c731bd9e3c2960fdfccf28 100644 (file)
@@ -25,19 +25,29 @@ import org.wamblee.test.EventTracker;
 
 public class Environment extends AbstractComponent {
 
-       private static final ProvidedInterface[] provided() {
+       private static final ProvidedInterface[] provided(String aPrefix) {
                return new ProvidedInterface[] {
-                               new DefaultProvidedInterface("datasource", String.class),
-                               new DefaultProvidedInterface("integer", Integer.class) };
+                               new DefaultProvidedInterface(aPrefix + "datasource", String.class),
+                               new DefaultProvidedInterface(aPrefix + "integer", Integer.class) };
        }
 
        private EventTracker<String> _tracker;
        private double _random;
 
        public Environment() {
-               super("environment", provided(), new RequiredInterface[0]);
-               _random = Math.random();
+               this("environment");
        }
+       
+       public Environment(String aName) {
+       this(aName, "");
+    }
+       
+       public Environment(String aName, String aPrefix) {
+        super(aName, provided(aPrefix), new RequiredInterface[0]);
+        _random = Math.random();
+    }
+
+
 
        public Environment(EventTracker aTracker) {
                this();