source code formatting.
[utils] / system / general / src / test / java / org / wamblee / system / graph / CompositeEdgeFilterTest.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;
17
18 import junit.framework.TestCase;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
21
22 import org.wamblee.system.container.Application;
23 import org.wamblee.system.core.Component;
24 import org.wamblee.system.core.Environment;
25 import org.wamblee.system.core.ProvidedInterface;
26 import org.wamblee.system.core.RequiredInterface;
27 import org.wamblee.system.graph.component.ProvidedInterfaceNode;
28 import org.wamblee.system.graph.component.RequiredInterfaceNode;
29
30
31 /**
32  * DOCUMENT ME!
33  *
34  * @author $author$
35  * @version $Revision$
36   */
37 public class CompositeEdgeFilterTest extends TestCase {
38     /**
39      * DOCUMENT ME!
40      */
41     private Application app = new Application();
42
43     /**
44      * DOCUMENT ME!
45      */
46     private Environment env = new Environment();
47
48     /**
49      * DOCUMENT ME!
50      *
51      * @param aClient DOCUMENT ME!
52      * @param aRequired DOCUMENT ME!
53      * @param aServer DOCUMENT ME!
54      * @param aProvided DOCUMENT ME!
55      *
56      * @return DOCUMENT ME!
57      */
58     private Edge createEdge(Component aClient, RequiredInterface aRequired,
59         Component aServer, ProvidedInterface aProvided) {
60         Node from = new RequiredInterfaceNode(aClient, aRequired);
61         Node to   = new ProvidedInterfaceNode(aServer, aProvided);
62
63         return new DefaultEdge(from, to);
64     }
65
66     /**
67      * DOCUMENT ME!
68      */
69     public void testEmpty() {
70         EdgeFilter restriction = new CompositeEdgeFilter();
71         assertFalse(restriction.isViolated(createEdge(app,
72                     app.getRequiredInterfaces().get(0), env,
73                     env.getProvidedInterfaces().get(0))));
74     }
75
76     /**
77      * DOCUMENT ME!
78      *
79      * @param base DOCUMENT ME!
80      * @param aResult DOCUMENT ME!
81      */
82     private void configureRestriction(EdgeFilter base, boolean aResult) {
83         stub(base.isViolated((Edge) anyObject())).toReturn(aResult);
84     }
85
86     /**
87      * DOCUMENT ME!
88      */
89     public void testOneRestriction() {
90         EdgeFilter          base      = mock(EdgeFilter.class);
91         CompositeEdgeFilter composite = new CompositeEdgeFilter();
92         composite.add(base);
93
94         // First let the base return false and verify the result. 
95         configureRestriction(base, false);
96
97         assertFalse(composite.isViolated(createEdge(app,
98                     app.getRequiredInterfaces().get(0), env,
99                     env.getProvidedInterfaces().get(0))));
100
101         // Second let the base return true and verify the result.
102         configureRestriction(base, true);
103
104         assertTrue(composite.isViolated(createEdge(app,
105                     app.getRequiredInterfaces().get(0), env,
106                     env.getProvidedInterfaces().get(0))));
107     }
108
109     /**
110      * DOCUMENT ME!
111      */
112     public void testTwoRestrictions() {
113         EdgeFilter          base1     = mock(EdgeFilter.class);
114         CompositeEdgeFilter composite = new CompositeEdgeFilter();
115         composite.add(base1);
116
117         EdgeFilter base2 = mock(EdgeFilter.class);
118         composite.add(base2);
119
120         // 1. base1 not violated and base 2 not violated -> not violated. 
121         configureRestriction(base1, false);
122         configureRestriction(base2, false);
123         assertFalse(composite.isViolated(createEdge(app,
124                     app.getRequiredInterfaces().get(0), env,
125                     env.getProvidedInterfaces().get(0))));
126
127         // 2. base 1 not violated but base 2 violated -> violated
128         configureRestriction(base1, false);
129         configureRestriction(base2, true);
130
131         assertTrue(composite.isViolated(createEdge(app,
132                     app.getRequiredInterfaces().get(0), env,
133                     env.getProvidedInterfaces().get(0))));
134
135         // 3. base 1 violated -> violated and base 2 not called. 
136         configureRestriction(base1, true);
137         // base 2 should not be called.
138         assertTrue(composite.isViolated(createEdge(app,
139                     app.getRequiredInterfaces().get(0), env,
140                     env.getProvidedInterfaces().get(0))));
141     }
142 }