cf6c3a822319d9e453465011d6247b24417505e1
[utils] / system / general / src / test / java / org / wamblee / system / container / DefaultInterfaceRestrictionTest.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 java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
24 import org.wamblee.system.core.Environment;
25 import org.wamblee.test.AssertionUtils;
26
27 public class DefaultInterfaceRestrictionTest extends TestCase {
28     private Application _app1 = new Application("app1", "pf1.");
29     private Application _app2 = new Application("app2", "pf2.");
30     
31     private Environment _env1 = new Environment("env1", "pf3.");
32     private Environment _env2 = new Environment("env2", "pf4.");
33  
34     
35     private void compare(Boolean[] aExpected, InterfaceRestriction aRestriction) { 
36        List<Boolean> result = new ArrayList<Boolean>();
37        
38        // order will be: 
39        //     env1, app1
40        //     env1, app2
41        //     env2, app1
42        //     env2, app2
43        for (Environment env: new Environment[] { _env1, _env2} ) { 
44            for (Application app: new Application[] { _app1, _app2} ) { 
45                result.add(aRestriction.isViolated(
46                        app, app.getRequiredInterfaces()[0], 
47                        env, 
48                        env.getProvidedInterfaces()[0]
49                        ));
50            }
51        }
52      
53        
54        assertEquals(Arrays.asList(aExpected), result);
55     }
56
57     public void testNoRestriction() { 
58         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
59             @Override
60             public void run() throws Exception {
61                 InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, null, null);
62                 
63             }
64         }, IllegalArgumentException.class);
65         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
66             @Override
67             public void run() throws Exception {
68                 InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, "x", "y");
69                 
70             }
71         }, IllegalArgumentException.class);
72         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
73             @Override
74             public void run() throws Exception {
75                 InterfaceRestriction restriction = new DefaultInterfaceRestriction("x", "y", null, null);
76                 
77             }
78         }, IllegalArgumentException.class);
79     }
80     
81     public void testClientServer() { 
82         InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, "env1", null);
83         compare(new Boolean[] { false, false, true, false}, restriction);
84     }
85     
86     public void testRequiredServer() { 
87         InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", "env1", null);
88         compare(new Boolean[] { false, false, true, false}, restriction);
89     }
90     
91     public void testClientProvided() { 
92         InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, null, "pf4.datasource");
93         compare(new Boolean[] { true, false, false, false}, restriction);
94     }
95     
96     public void testRequiredProvide() { 
97         InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", null, "pf4.datasource");
98         compare(new Boolean[] { true, false, false, false}, restriction);
99     }
100     
101     public void testExplicitConfig() { 
102         _app1 = new Application("app1");
103         _app2 = new Application("app2");
104         _env1 = new Environment("env1");
105         _env2 = new Environment("env2");
106         
107         InterfaceRestriction restriction = new DefaultInterfaceRestriction(
108                 "app2", "string", "env1", "datasource");
109         compare(new Boolean[] { false, false, false, true}, restriction);
110         
111     }
112 }