(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / graph / component / ConnectRequiredProvidedEdgeFilterTest.java
1 /*
2  * Copyright 2005-2010 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.component;
17
18 import junit.framework.TestCase;
19
20 import org.wamblee.system.container.Application;
21 import org.wamblee.system.core.Environment;
22 import org.wamblee.system.graph.DefaultEdge;
23 import org.wamblee.system.graph.Edge;
24 import org.wamblee.system.graph.EdgeFilter;
25 import org.wamblee.system.graph.Node;
26
27 import org.wamblee.test.AssertionUtils;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32
33 /**
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class ConnectRequiredProvidedEdgeFilterTest extends TestCase {
39     private Application app1 = new Application("app1", "pf1.");
40
41     private Application app2 = new Application("app2", "pf2.");
42
43     private Environment env1 = new Environment("env1", "pf3.");
44
45     private Environment env2 = new Environment("env2", "pf4.");
46
47     private void compare(Boolean[] aExpected, EdgeFilter aRestriction) {
48         List<Boolean> result = new ArrayList<Boolean>();
49
50         // order will be:
51         // env1, app1
52         // env1, app2
53         // env2, app1
54         // env2, app2
55         for (Environment env : new Environment[] { env1, env2 }) {
56             for (Application app : new Application[] { app1, app2 }) {
57                 Node from = new RequiredInterfaceNode(app, app
58                     .getRequiredInterfaces().get(0));
59                 Node to = new ProvidedInterfaceNode(env, env
60                     .getProvidedInterfaces().get(0));
61                 Edge edge = new DefaultEdge(from, to);
62                 result.add(aRestriction.isViolated(edge));
63             }
64         }
65
66         assertEquals(Arrays.asList(aExpected), result);
67     }
68
69     public void testNoRestriction() {
70         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
71             @Override
72             public void run() throws Exception {
73                 EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(
74                     null, null, null, null);
75                 ignoredVariable(restriction);
76             }
77         }, IllegalArgumentException.class);
78         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
79             @Override
80             public void run() throws Exception {
81                 EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(
82                     null, null, "x", "y");
83                 ignoredVariable(restriction);
84             }
85         }, IllegalArgumentException.class);
86     }
87
88     private static final void ignoredVariable(Object aObject) {
89         // for findbugs.
90     }
91
92     public void testClientServer() {
93         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1",
94             null, "env1", null);
95         compare(new Boolean[] { false, false, true, false }, restriction);
96     }
97
98     public void testNoConnectionsAtAll() {
99         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1",
100             null, null, null);
101         compare(new Boolean[] { true, false, true, false }, restriction);
102     }
103
104     public void testExplicitConfig() {
105         app1 = new Application("app1");
106         app2 = new Application("app2");
107         env1 = new Environment("env1");
108         env2 = new Environment("env2");
109
110         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app2",
111             "string", "env1", "datasource");
112         compare(new Boolean[] { false, false, false, true }, restriction);
113     }
114 }