7889c35723f25e62926c8530b4bc38f6eb6d6b87
[utils] / system / general / src / test / java / org / wamblee / system / graph / CompositeEdgeFilterTest.java
1 /*
2  * Copyright 2008 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 java.util.Arrays;
19
20 import junit.framework.TestCase;
21
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;
32
33 public class CompositeEdgeFilterTest extends TestCase {
34     private Application _app = new Application(); 
35     private Environment _env = new Environment();
36     
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);
42     }
43
44     public void testEmpty() { 
45         EdgeFilter restriction = new CompositeEdgeFilter(); 
46         assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
47                 _env, _env.getProvidedInterfaces().get(0))));
48     }
49     
50     private void configureRestriction(EdgeFilter base, boolean aResult) {
51         base.isViolated( (Edge)EasyMock.anyObject());
52         EasyMock.expectLastCall().andReturn(aResult);
53     }
54     
55     public void testOneRestriction() { 
56         IMocksControl control = EasyMock.createStrictControl();
57       
58         EdgeFilter base = control.createMock(EdgeFilter.class);
59         CompositeEdgeFilter composite = new CompositeEdgeFilter();
60         composite.add(base);
61         
62         // First let the base return false and verify the result. 
63         
64         configureRestriction(base, false);
65         
66         control.replay();
67         assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
68                 _env, _env.getProvidedInterfaces().get(0))));
69         control.verify();
70         
71         // Second let the base return true and verify the result.
72         control.reset();
73         configureRestriction(base, true);
74         
75         control.replay();
76         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
77                 _env, _env.getProvidedInterfaces().get(0))));
78         control.verify();
79     }
80
81    
82     
83     public void testTwoRestrictions() { 
84         IMocksControl control = EasyMock.createStrictControl();
85         
86         EdgeFilter base1 = control.createMock(EdgeFilter.class);
87         CompositeEdgeFilter composite = new CompositeEdgeFilter();
88         composite.add(base1);
89         EdgeFilter base2 = control.createMock(EdgeFilter.class);
90         composite.add(base2);
91         
92         // 1. base1 not violated and base 2 not violated -> not violated. 
93         
94         configureRestriction(base1, false);
95         configureRestriction(base2, false);
96         control.replay();
97         assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
98                 _env, _env.getProvidedInterfaces().get(0))));
99         control.verify();
100         control.reset();
101         
102         // 2. base 1 not violated but base 2 violated -> violated
103         configureRestriction(base1, false);
104         configureRestriction(base2, true);
105         control.replay();
106         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
107                 _env, _env.getProvidedInterfaces().get(0))));
108         control.verify();
109         control.reset();
110         
111         // 3. base 1 violated -> violated and base 2 not called. 
112         configureRestriction(base1, true);
113         // base 2 should not be called.
114         control.replay();
115         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
116                 _env, _env.getProvidedInterfaces().get(0))));
117         control.verify();
118         control.reset();
119     }
120     
121 }