source code formatting.
[utils] / system / general / src / test / java / org / wamblee / system / graph / component / ConnectRequiredProvidedEdgeFilterTest.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.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  * DOCUMENT ME!
36  *
37  * @author $author$
38  * @version $Revision$
39   */
40 public class ConnectRequiredProvidedEdgeFilterTest extends TestCase {
41     /**
42      * DOCUMENT ME!
43      */
44     private Application app1 = new Application("app1", "pf1.");
45
46     /**
47      * DOCUMENT ME!
48      */
49     private Application app2 = new Application("app2", "pf2.");
50
51     /**
52      * DOCUMENT ME!
53      */
54     private Environment env1 = new Environment("env1", "pf3.");
55
56     /**
57      * DOCUMENT ME!
58      */
59     private Environment env2 = new Environment("env2", "pf4.");
60
61     /**
62      * DOCUMENT ME!
63      *
64      * @param aExpected DOCUMENT ME!
65      * @param aRestriction DOCUMENT ME!
66      */
67     private void compare(Boolean[] aExpected, EdgeFilter aRestriction) {
68         List<Boolean> result = new ArrayList<Boolean>();
69
70         // order will be: 
71         //     env1, app1
72         //     env1, app2
73         //     env2, app1
74         //     env2, app2
75         for (Environment env : new Environment[] { env1, env2 }) {
76             for (Application app : new Application[] { app1, app2 }) {
77                 Node from = new RequiredInterfaceNode(app,
78                         app.getRequiredInterfaces().get(0));
79                 Node to   = new ProvidedInterfaceNode(env,
80                         env.getProvidedInterfaces().get(0));
81                 Edge edge = new DefaultEdge(from, to);
82                 result.add(aRestriction.isViolated(edge));
83             }
84         }
85
86         assertEquals(Arrays.asList(aExpected), result);
87     }
88
89     /**
90      * DOCUMENT ME!
91      */
92     public void testNoRestriction() {
93         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
94                 @Override
95                 public void run() throws Exception {
96                     EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null,
97                             null, null, null);
98                 }
99             }, IllegalArgumentException.class);
100         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
101                 @Override
102                 public void run() throws Exception {
103                     EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null,
104                             null, "x", "y");
105                 }
106             }, IllegalArgumentException.class);
107     }
108
109     /**
110      * DOCUMENT ME!
111      */
112     public void testClientServer() {
113         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1",
114                 null, "env1", null);
115         compare(new Boolean[] { false, false, true, false }, restriction);
116     }
117
118     /**
119      * DOCUMENT ME!
120      */
121     public void testNoConnectionsAtAll() {
122         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1",
123                 null, null, null);
124         compare(new Boolean[] { true, false, true, false }, restriction);
125     }
126
127     /**
128      * DOCUMENT ME!
129      */
130     public void testExplicitConfig() {
131         app1     = new Application("app1");
132         app2     = new Application("app2");
133         env1     = new Environment("env1");
134         env2     = new Environment("env2");
135
136         EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app2",
137                 "string", "env1", "datasource");
138         compare(new Boolean[] { false, false, false, true }, restriction);
139     }
140 }