Replaced easymock by mockito.
[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 static org.mockito.Mockito.*; 
23
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;
31
32 public class CompositeEdgeFilterTest extends TestCase {
33     private Application _app = new Application(); 
34     private Environment _env = new Environment();
35     
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);
41     }
42
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))));
47     }
48     
49     private void configureRestriction(EdgeFilter base, boolean aResult) {
50         stub(base.isViolated((Edge)anyObject())).toReturn(aResult);
51     }
52     
53     public void testOneRestriction() { 
54         EdgeFilter base = mock(EdgeFilter.class);
55         CompositeEdgeFilter composite = new CompositeEdgeFilter();
56         composite.add(base);
57         
58         // First let the base return false and verify the result. 
59         
60         configureRestriction(base, false);
61         
62         assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
63                 _env, _env.getProvidedInterfaces().get(0))));
64         
65         // Second let the base return true and verify the result.
66         configureRestriction(base, true);
67         
68         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
69                 _env, _env.getProvidedInterfaces().get(0))));
70     }
71
72    
73     
74     public void testTwoRestrictions() { 
75         EdgeFilter base1 = mock(EdgeFilter.class);
76         CompositeEdgeFilter composite = new CompositeEdgeFilter();
77         composite.add(base1);
78         EdgeFilter base2 = mock(EdgeFilter.class);
79         composite.add(base2);
80         
81         // 1. base1 not violated and base 2 not violated -> not violated. 
82         
83         configureRestriction(base1, false);
84         configureRestriction(base2, false);
85         assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
86                 _env, _env.getProvidedInterfaces().get(0))));
87         
88         // 2. base 1 not violated but base 2 violated -> violated
89         configureRestriction(base1, false);
90         configureRestriction(base2, true);
91        
92         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
93                 _env, _env.getProvidedInterfaces().get(0))));
94      
95         // 3. base 1 violated -> violated and base 2 not called. 
96         configureRestriction(base1, true);
97         // base 2 should not be called.
98       
99         assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), 
100                 _env, _env.getProvidedInterfaces().get(0))));
101     }
102     
103 }