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 org.easymock.classextension.EasyMock;
23 import org.easymock.classextension.IMocksControl;
24 import static org.easymock.classextension.EasyMock.*;
25 import org.wamblee.system.container.Application;
26 import org.wamblee.system.core.Component;
27 import org.wamblee.system.core.Environment;
28 import org.wamblee.system.core.ProvidedInterface;
29 import org.wamblee.system.core.RequiredInterface;
30 import org.wamblee.system.graph.component.ProvidedInterfaceNode;
31 import org.wamblee.system.graph.component.RequiredInterfaceNode;
33 public class CompositeEdgeFilterTest extends TestCase {
34 private Application _app = new Application();
35 private Environment _env = new Environment();
37 private Edge createEdge(Component aClient, RequiredInterface aRequired,
38 Component aServer, ProvidedInterface aProvided) {
39 Node from = new RequiredInterfaceNode(aClient, aRequired);
40 Node to = new ProvidedInterfaceNode(aServer, aProvided);
41 return new DefaultEdge(from, to);
44 public void testEmpty() {
45 EdgeFilter restriction = new CompositeEdgeFilter();
46 assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
47 _env, _env.getProvidedInterfaces()[0])));
50 private void configureRestriction(EdgeFilter base, boolean aResult) {
51 base.isViolated( (Edge)EasyMock.anyObject());
52 EasyMock.expectLastCall().andReturn(aResult);
55 public void testOneRestriction() {
56 IMocksControl control = EasyMock.createStrictControl();
58 EdgeFilter base = control.createMock(EdgeFilter.class);
59 CompositeEdgeFilter composite = new CompositeEdgeFilter();
62 // First let the base return false and verify the result.
64 configureRestriction(base, false);
67 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
68 _env, _env.getProvidedInterfaces()[0])));
71 // Second let the base return true and verify the result.
73 configureRestriction(base, true);
76 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
77 _env, _env.getProvidedInterfaces()[0])));
83 public void testTwoRestrictions() {
84 IMocksControl control = EasyMock.createStrictControl();
86 EdgeFilter base1 = control.createMock(EdgeFilter.class);
87 CompositeEdgeFilter composite = new CompositeEdgeFilter();
89 EdgeFilter base2 = control.createMock(EdgeFilter.class);
92 // 1. base1 not violated and base 2 not violated -> not violated.
94 configureRestriction(base1, false);
95 configureRestriction(base2, false);
97 assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
98 _env, _env.getProvidedInterfaces()[0])));
102 // 2. base 1 not violated but base 2 violated -> violated
103 configureRestriction(base1, false);
104 configureRestriction(base2, true);
106 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
107 _env, _env.getProvidedInterfaces()[0])));
111 // 3. base 1 violated -> violated and base 2 not called.
112 configureRestriction(base1, true);
113 // base 2 should not be called.
115 assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0],
116 _env, _env.getProvidedInterfaces()[0])));