96fd009a4e915c7a771d63b8e859c97e08fc15d7
[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 static org.mockito.Matchers.*;
19 import static org.mockito.Mockito.*;
20 import junit.framework.TestCase;
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 public class CompositeEdgeFilterTest extends TestCase {
31     private Application app = new Application(); 
32     private Environment env = new Environment();
33     
34     private Edge createEdge(Component aClient, RequiredInterface aRequired, 
35             Component aServer, ProvidedInterface aProvided) { 
36         Node from = new RequiredInterfaceNode(aClient, aRequired);
37         Node to = new ProvidedInterfaceNode(aServer, aProvided);
38         return new DefaultEdge(from, to);
39     }
40
41     public void testEmpty() { 
42         EdgeFilter restriction = new CompositeEdgeFilter(); 
43         assertFalse(restriction.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
44                 env, env.getProvidedInterfaces().get(0))));
45     }
46     
47     private void configureRestriction(EdgeFilter base, boolean aResult) {
48         stub(base.isViolated((Edge)anyObject())).toReturn(aResult);
49     }
50     
51     public void testOneRestriction() { 
52         EdgeFilter base = mock(EdgeFilter.class);
53         CompositeEdgeFilter composite = new CompositeEdgeFilter();
54         composite.add(base);
55         
56         // First let the base return false and verify the result. 
57         
58         configureRestriction(base, false);
59         
60         assertFalse(composite.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
61                 env, env.getProvidedInterfaces().get(0))));
62         
63         // Second let the base return true and verify the result.
64         configureRestriction(base, true);
65         
66         assertTrue(composite.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
67                 env, env.getProvidedInterfaces().get(0))));
68     }
69
70    
71     
72     public void testTwoRestrictions() { 
73         EdgeFilter base1 = mock(EdgeFilter.class);
74         CompositeEdgeFilter composite = new CompositeEdgeFilter();
75         composite.add(base1);
76         EdgeFilter base2 = mock(EdgeFilter.class);
77         composite.add(base2);
78         
79         // 1. base1 not violated and base 2 not violated -> not violated. 
80         
81         configureRestriction(base1, false);
82         configureRestriction(base2, false);
83         assertFalse(composite.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
84                 env, env.getProvidedInterfaces().get(0))));
85         
86         // 2. base 1 not violated but base 2 violated -> violated
87         configureRestriction(base1, false);
88         configureRestriction(base2, true);
89        
90         assertTrue(composite.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
91                 env, env.getProvidedInterfaces().get(0))));
92      
93         // 3. base 1 violated -> violated and base 2 not called. 
94         configureRestriction(base1, true);
95         // base 2 should not be called.
96       
97         assertTrue(composite.isViolated(createEdge(app, app.getRequiredInterfaces().get(0), 
98                 env, env.getProvidedInterfaces().get(0))));
99     }
100     
101 }