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