(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / graph / CompositeEdgeFilterTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.system.graph;
17
18 import junit.framework.TestCase;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
21
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;
29
30 /**
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class CompositeEdgeFilterTest extends TestCase {
36     private Application app = new Application();
37
38     private Environment env = new Environment();
39
40     private Edge createEdge(Component aClient, RequiredInterface aRequired,
41         Component aServer, ProvidedInterface aProvided) {
42         Node from = new RequiredInterfaceNode(aClient, aRequired);
43         Node to = new ProvidedInterfaceNode(aServer, aProvided);
44
45         return new DefaultEdge(from, to);
46     }
47
48     public void testEmpty() {
49         EdgeFilter restriction = new CompositeEdgeFilter();
50         assertFalse(restriction.isViolated(createEdge(app, app
51             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
52             .get(0))));
53     }
54
55     private void configureRestriction(EdgeFilter aBase, boolean aResult) {
56         stub(aBase.isViolated((Edge) anyObject())).toReturn(aResult);
57     }
58
59     public void testOneRestriction() {
60         EdgeFilter base = mock(EdgeFilter.class);
61         CompositeEdgeFilter composite = new CompositeEdgeFilter();
62         composite.add(base);
63
64         // First let the base return false and verify the result.
65         configureRestriction(base, false);
66
67         assertFalse(composite.isViolated(createEdge(app, app
68             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
69             .get(0))));
70
71         // Second let the base return true and verify the result.
72         configureRestriction(base, true);
73
74         assertTrue(composite.isViolated(createEdge(app, app
75             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
76             .get(0))));
77     }
78
79     public void testTwoRestrictions() {
80         EdgeFilter base1 = mock(EdgeFilter.class);
81         CompositeEdgeFilter composite = new CompositeEdgeFilter();
82         composite.add(base1);
83
84         EdgeFilter base2 = mock(EdgeFilter.class);
85         composite.add(base2);
86
87         // 1. base1 not violated and base 2 not violated -> not violated.
88         configureRestriction(base1, false);
89         configureRestriction(base2, false);
90         assertFalse(composite.isViolated(createEdge(app, app
91             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
92             .get(0))));
93
94         // 2. base 1 not violated but base 2 violated -> violated
95         configureRestriction(base1, false);
96         configureRestriction(base2, true);
97
98         assertTrue(composite.isViolated(createEdge(app, app
99             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
100             .get(0))));
101
102         // 3. base 1 violated -> violated and base 2 not called.
103         configureRestriction(base1, true);
104         // base 2 should not be called.
105         assertTrue(composite.isViolated(createEdge(app, app
106             .getRequiredInterfaces().get(0), env, env.getProvidedInterfaces()
107             .get(0))));
108     }
109 }