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 java.util.Arrays;
20 import junit.framework.TestCase;
22 import static org.mockito.Mockito.*;
24 import org.wamblee.system.container.Application;
25 import org.wamblee.system.core.Component;
26 import org.wamblee.system.core.Environment;
27 import org.wamblee.system.core.ProvidedInterface;
28 import org.wamblee.system.core.RequiredInterface;
29 import org.wamblee.system.graph.component.ProvidedInterfaceNode;
30 import org.wamblee.system.graph.component.RequiredInterfaceNode;
32 public class CompositeEdgeFilterTest extends TestCase {
33 private Application _app = new Application();
34 private Environment _env = new Environment();
36 private Edge createEdge(Component aClient, RequiredInterface aRequired,
37 Component aServer, ProvidedInterface aProvided) {
38 Node from = new RequiredInterfaceNode(aClient, aRequired);
39 Node to = new ProvidedInterfaceNode(aServer, aProvided);
40 return new DefaultEdge(from, to);
43 public void testEmpty() {
44 EdgeFilter restriction = new CompositeEdgeFilter();
45 assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
46 _env, _env.getProvidedInterfaces().get(0))));
49 private void configureRestriction(EdgeFilter base, boolean aResult) {
50 stub(base.isViolated((Edge)anyObject())).toReturn(aResult);
53 public void testOneRestriction() {
54 EdgeFilter base = mock(EdgeFilter.class);
55 CompositeEdgeFilter composite = new CompositeEdgeFilter();
58 // First let the base return false and verify the result.
60 configureRestriction(base, false);
62 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
63 _env, _env.getProvidedInterfaces().get(0))));
65 // Second let the base return true and verify the result.
66 configureRestriction(base, true);
68 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
69 _env, _env.getProvidedInterfaces().get(0))));
74 public void testTwoRestrictions() {
75 EdgeFilter base1 = mock(EdgeFilter.class);
76 CompositeEdgeFilter composite = new CompositeEdgeFilter();
78 EdgeFilter base2 = mock(EdgeFilter.class);
81 // 1. base1 not violated and base 2 not violated -> not violated.
83 configureRestriction(base1, false);
84 configureRestriction(base2, false);
85 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
86 _env, _env.getProvidedInterfaces().get(0))));
88 // 2. base 1 not violated but base 2 violated -> violated
89 configureRestriction(base1, false);
90 configureRestriction(base2, true);
92 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
93 _env, _env.getProvidedInterfaces().get(0))));
95 // 3. base 1 violated -> violated and base 2 not called.
96 configureRestriction(base1, true);
97 // base 2 should not be called.
99 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0),
100 _env, _env.getProvidedInterfaces().get(0))));