2 * Copyright 2008 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.graph;
18 import junit.framework.TestCase;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import org.wamblee.system.container.Application;
23 import org.wamblee.system.core.Component;
24 import org.wamblee.system.core.Environment;
25 import org.wamblee.system.core.ProvidedInterface;
26 import org.wamblee.system.core.RequiredInterface;
27 import org.wamblee.system.graph.component.ProvidedInterfaceNode;
28 import org.wamblee.system.graph.component.RequiredInterfaceNode;
37 public class CompositeEdgeFilterTest extends TestCase {
41 private Application app = new Application();
46 private Environment env = new Environment();
51 * @param aClient DOCUMENT ME!
52 * @param aRequired DOCUMENT ME!
53 * @param aServer DOCUMENT ME!
54 * @param aProvided DOCUMENT ME!
56 * @return DOCUMENT ME!
58 private Edge createEdge(Component aClient, RequiredInterface aRequired,
59 Component aServer, ProvidedInterface aProvided) {
60 Node from = new RequiredInterfaceNode(aClient, aRequired);
61 Node to = new ProvidedInterfaceNode(aServer, aProvided);
63 return new DefaultEdge(from, to);
69 public void testEmpty() {
70 EdgeFilter restriction = new CompositeEdgeFilter();
71 assertFalse(restriction.isViolated(createEdge(app,
72 app.getRequiredInterfaces().get(0), env,
73 env.getProvidedInterfaces().get(0))));
79 * @param base DOCUMENT ME!
80 * @param aResult DOCUMENT ME!
82 private void configureRestriction(EdgeFilter base, boolean aResult) {
83 stub(base.isViolated((Edge) anyObject())).toReturn(aResult);
89 public void testOneRestriction() {
90 EdgeFilter base = mock(EdgeFilter.class);
91 CompositeEdgeFilter composite = new CompositeEdgeFilter();
94 // First let the base return false and verify the result.
95 configureRestriction(base, false);
97 assertFalse(composite.isViolated(createEdge(app,
98 app.getRequiredInterfaces().get(0), env,
99 env.getProvidedInterfaces().get(0))));
101 // Second let the base return true and verify the result.
102 configureRestriction(base, true);
104 assertTrue(composite.isViolated(createEdge(app,
105 app.getRequiredInterfaces().get(0), env,
106 env.getProvidedInterfaces().get(0))));
112 public void testTwoRestrictions() {
113 EdgeFilter base1 = mock(EdgeFilter.class);
114 CompositeEdgeFilter composite = new CompositeEdgeFilter();
115 composite.add(base1);
117 EdgeFilter base2 = mock(EdgeFilter.class);
118 composite.add(base2);
120 // 1. base1 not violated and base 2 not violated -> not violated.
121 configureRestriction(base1, false);
122 configureRestriction(base2, false);
123 assertFalse(composite.isViolated(createEdge(app,
124 app.getRequiredInterfaces().get(0), env,
125 env.getProvidedInterfaces().get(0))));
127 // 2. base 1 not violated but base 2 violated -> violated
128 configureRestriction(base1, false);
129 configureRestriction(base2, true);
131 assertTrue(composite.isViolated(createEdge(app,
132 app.getRequiredInterfaces().get(0), env,
133 env.getProvidedInterfaces().get(0))));
135 // 3. base 1 violated -> violated and base 2 not called.
136 configureRestriction(base1, true);
137 // base 2 should not be called.
138 assertTrue(composite.isViolated(createEdge(app,
139 app.getRequiredInterfaces().get(0), env,
140 env.getProvidedInterfaces().get(0))));