Edge filter implemented for explicitly connecting required interfaces.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 27 May 2008 17:28:25 +0000 (17:28 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 27 May 2008 17:28:25 +0000 (17:28 +0000)
trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedFilter.java [new file with mode: 0644]
trunk/system/general/src/test/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedEdgeFilterTest.java [new file with mode: 0644]

diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedFilter.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedFilter.java
new file mode 100644 (file)
index 0000000..62b1604
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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.graph.component;
+
+import org.wamblee.system.graph.Edge;
+import org.wamblee.system.graph.EdgeFilter;
+import org.wamblee.system.graph.EdgeSelector;
+import org.wamblee.system.graph.Node;
+
+/**
+ * Filter used to explicitly connect required and provided interfaces within a
+ * container.
+ * 
+ * @author Erik Brakkee
+ * 
+ */
+public class ConnectExternalProvidedProvidedFilter implements EdgeFilter {
+
+    private String _externalProvided;
+    private String _component;
+    private String _provided;
+
+    public ConnectExternalProvidedProvidedFilter(String aExternalProvided, String aComponent,
+            String aProvided) {
+        _externalProvided = aExternalProvided; 
+        _component = aComponent; 
+        _provided = aProvided; 
+        if ( _externalProvided == null ) { 
+            throw new IllegalArgumentException("External provided interface name must be specified."); 
+        }
+        if ( _component == null ) { 
+            throw new IllegalArgumentException("Component name must be specified");
+        }
+        if ( _provided == null ) { 
+            throw new IllegalArgumentException("Provided interface name of internal component must be specified");
+        }
+    }
+
+    @Override
+    public boolean isViolated(Edge aEdge) {
+        if (aEdge.getFrom() instanceof ExternalProvidedInterfaceNode
+                && aEdge.getTo() instanceof ProvidedInterfaceNode) {
+            return isViolated((ExternalProvidedInterfaceNode) aEdge.getFrom(),
+                    (ProvidedInterfaceNode) aEdge.getTo());
+        }
+        return false;
+    }
+
+    private boolean isViolated(ExternalProvidedInterfaceNode aFrom,
+            ProvidedInterfaceNode aTo) {
+        if ( !aFrom.getName().equals(_externalProvided)) { 
+            return false; // wrong provided interface.
+        }
+        if ( aTo.getComponent().getName().equals(_component) && 
+               aTo.getProvided().getName().equals(_provided) ) { 
+            return false; // ok 
+        }
+        return true; 
+    }
+}
diff --git a/trunk/system/general/src/test/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedEdgeFilterTest.java b/trunk/system/general/src/test/java/org/wamblee/system/graph/component/ConnectExternalProvidedProvidedEdgeFilterTest.java
new file mode 100644 (file)
index 0000000..7d93181
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * 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.graph.component;
+
+import junit.framework.TestCase;
+
+import org.wamblee.system.container.Application;
+import org.wamblee.system.container.Container;
+import org.wamblee.system.core.Component;
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.DefaultRequiredInterface;
+import org.wamblee.system.core.Environment;
+import org.wamblee.system.graph.DefaultEdge;
+import org.wamblee.system.graph.DefaultNode;
+import org.wamblee.system.graph.Edge;
+import org.wamblee.system.graph.EdgeFilter;
+import org.wamblee.system.graph.Node;
+
+public class ConnectExternalProvidedProvidedEdgeFilterTest extends TestCase {
+
+    private Container _container;
+    private Component _internal;
+    private String _externalInterfaceName;
+    private String _internalComponentName;
+    private String _internalInterfaceName;
+    private Edge _edge;
+
+    @Override
+    protected void setUp() throws Exception {
+        _container = new Container("container")
+                .addProvidedInterface(new DefaultProvidedInterface("string",
+                        String.class));
+        _internal = new Environment("env1");
+
+        _externalInterfaceName = _container.getProvidedInterfaces()[0]
+                .getName();
+        _internalComponentName = _internal.getName();
+        _internalInterfaceName = _internal.getProvidedInterfaces()[0].getName();
+
+        _edge = new DefaultEdge(new ExternalProvidedInterfaceNode(_container,
+                _container.getProvidedInterfaces()[0]),
+                new ProvidedInterfaceNode(_internal, _internal
+                        .getProvidedInterfaces()[0]));
+    }
+
+    public void testWrongExternal() {
+        EdgeFilter filter = new ConnectExternalProvidedProvidedFilter(
+                _externalInterfaceName + "x", _internalComponentName,
+                _internalInterfaceName);
+        assertFalse(filter.isViolated(_edge));
+    }
+
+    public void testRightExternalWrongComponent() {
+        EdgeFilter filter = new ConnectExternalProvidedProvidedFilter(
+                _externalInterfaceName, _internalComponentName + "x",
+                _internalInterfaceName);
+        assertTrue(filter.isViolated(_edge));
+    }
+
+    public void testRightExternalWrongInternal() {
+        EdgeFilter filter = new ConnectExternalProvidedProvidedFilter(
+                _externalInterfaceName, _internalComponentName,
+                _internalInterfaceName + "x");
+        assertTrue(filter.isViolated(_edge));
+    }
+    
+    public void testEverythingRight() { 
+        EdgeFilter filter = new ConnectExternalProvidedProvidedFilter(
+                _externalInterfaceName, _internalComponentName,
+                _internalInterfaceName);
+        assertFalse(filter.isViolated(_edge));
+    }
+
+
+    public void testWrongEdgeType() {
+        EdgeFilter filter = new ConnectExternalProvidedProvidedFilter(
+                _externalInterfaceName, _internalComponentName,
+                _internalInterfaceName);
+        DefaultEdge edge = new DefaultEdge(new DefaultNode("x"),
+                new DefaultNode("y"));
+        assertFalse(filter.isViolated(edge));
+    }
+
+}