Added basic graph functionality as a first step towards simplifying the container...
[utils] / system / general / src / test / java / org / wamblee / system / container / CompositeInterfaceRestrictionTest.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.container;
17
18 import static org.easymock.EasyMock.anyObject;
19
20 import java.util.Arrays;
21
22 import junit.framework.TestCase;
23
24 import org.easymock.classextension.EasyMock;
25 import org.easymock.classextension.IMocksControl;
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
31 public class CompositeInterfaceRestrictionTest extends TestCase {
32     
33     private Application _app = new Application(); 
34     private Environment _env = new Environment(); 
35
36     public void testEmpty() { 
37         InterfaceRestriction restriction = new CompositeInterfaceRestriction(); 
38         assertFalse(restriction.isViolated(_app, _app.getRequiredInterfaces()[0], 
39                 _env, _env.getProvidedInterfaces()[0]));
40     }
41     
42     private void configureRestriction(InterfaceRestriction base, boolean aResult) {
43         base.isViolated(
44                 (Component)anyObject(), 
45                 (RequiredInterface)anyObject(), 
46                 (Component)anyObject(), 
47                 (ProvidedInterface)anyObject());
48         EasyMock.expectLastCall().andReturn(aResult);
49     }
50     
51     public void testOneRestriction() { 
52         IMocksControl control = EasyMock.createStrictControl();
53       
54         InterfaceRestriction base = control.createMock(InterfaceRestriction.class);
55         InterfaceRestriction composite = new CompositeInterfaceRestriction(
56                 Arrays.asList(new InterfaceRestriction[] { base } ));
57         
58         // First let the base return false and verify the result. 
59         
60         configureRestriction(base, false);
61         
62         control.replay();
63         assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
64                 _env, _env.getProvidedInterfaces()[0]));
65         control.verify();
66         
67         // Second let the base return true and verify the result.
68         control.reset();
69         configureRestriction(base, true);
70         
71         control.replay();
72         assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
73                 _env, _env.getProvidedInterfaces()[0]));
74         control.verify();
75     }
76
77    
78     
79     public void testTwoRestrictions() { 
80         IMocksControl control = EasyMock.createStrictControl();
81         
82         InterfaceRestriction base1 = control.createMock(InterfaceRestriction.class);
83         CompositeInterfaceRestriction composite = new CompositeInterfaceRestriction(
84                 Arrays.asList(new InterfaceRestriction[] { base1 } ));
85         InterfaceRestriction base2 = control.createMock(InterfaceRestriction.class);
86         composite.add(base2);
87         
88         // 1. base1 not violated and base 2 not violated -> not violated. 
89         
90         configureRestriction(base1, false);
91         configureRestriction(base2, false);
92         control.replay();
93         assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
94                 _env, _env.getProvidedInterfaces()[0]));
95         control.verify();
96         control.reset();
97         
98         // 2. base 1 not violated but base 2 violated -> violated
99         configureRestriction(base1, false);
100         configureRestriction(base2, true);
101         control.replay();
102         assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
103                 _env, _env.getProvidedInterfaces()[0]));
104         control.verify();
105         control.reset();
106         
107         // 3. base 1 violated -> violated and base 2 not called. 
108         configureRestriction(base1, true);
109         // base 2 should not be called.
110         control.replay();
111         assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
112                 _env, _env.getProvidedInterfaces()[0]));
113         control.verify();
114         control.reset();
115     }
116 }