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