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 static org.mockito.Matchers.*;
19 import static org.mockito.Mockito.*;
20 import junit.framework.TestCase;
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;
30 public class CompositeEdgeFilterTest extends TestCase {
31 private Application _app = new Application();
32 private Environment _env = new Environment();
34 private Edge createEdge(Component aClient, RequiredInterface aRequired,
35 Component aServer, ProvidedInterface aProvided) {
36 Node from = new RequiredInterfaceNode(aClient, aRequired);
37 Node to = new ProvidedInterfaceNode(aServer, aProvided);
38 return new DefaultEdge(from, to);
41 public void testEmpty() {
42 EdgeFilter restriction = new CompositeEdgeFilter();
43 assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
44 _env, _env.getProvidedInterfaces().get(0))));
47 private void configureRestriction(EdgeFilter base, boolean aResult) {
48 stub(base.isViolated((Edge)anyObject())).toReturn(aResult);
51 public void testOneRestriction() {
52 EdgeFilter base = mock(EdgeFilter.class);
53 CompositeEdgeFilter composite = new CompositeEdgeFilter();
56 // First let the base return false and verify the result.
58 configureRestriction(base, false);
60 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
61 _env, _env.getProvidedInterfaces().get(0))));
63 // Second let the base return true and verify the result.
64 configureRestriction(base, true);
66 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
67 _env, _env.getProvidedInterfaces().get(0))));
72 public void testTwoRestrictions() {
73 EdgeFilter base1 = mock(EdgeFilter.class);
74 CompositeEdgeFilter composite = new CompositeEdgeFilter();
76 EdgeFilter base2 = mock(EdgeFilter.class);
79 // 1. base1 not violated and base 2 not violated -> not violated.
81 configureRestriction(base1, false);
82 configureRestriction(base2, false);
83 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
84 _env, _env.getProvidedInterfaces().get(0))));
86 // 2. base 1 not violated but base 2 violated -> violated
87 configureRestriction(base1, false);
88 configureRestriction(base2, true);
90 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
91 _env, _env.getProvidedInterfaces().get(0))));
93 // 3. base 1 violated -> violated and base 2 not called.
94 configureRestriction(base1, true);
95 // base 2 should not be called.
97 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
98 _env, _env.getProvidedInterfaces().get(0))));