2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.container;
18 import static org.easymock.EasyMock.anyObject;
20 import java.util.Arrays;
22 import junit.framework.TestCase;
24 import org.easymock.classextension.EasyMock;
25 import org.easymock.classextension.IMocksControl;
26 import org.wamblee.system.core.Component;
27 import org.wamblee.system.core.Environment;
28 import org.wamblee.system.core.ProvidedInterface;
29 import org.wamblee.system.core.RequiredInterface;
31 public class CompositeInterfaceRestrictionTest extends TestCase {
33 private Application _app = new Application();
34 private Environment _env = new Environment();
36 public void testEmpty() {
37 InterfaceRestriction restriction = new CompositeInterfaceRestriction();
38 assertFalse(restriction.isViolated(_app, _app.getRequiredInterfaces()[0],
39 _env, _env.getProvidedInterfaces()[0]));
42 private void configureRestriction(InterfaceRestriction base, boolean aResult) {
44 (Component)anyObject(),
45 (RequiredInterface)anyObject(),
46 (Component)anyObject(),
47 (ProvidedInterface)anyObject());
48 EasyMock.expectLastCall().andReturn(aResult);
51 public void testOneRestriction() {
52 IMocksControl control = EasyMock.createStrictControl();
54 InterfaceRestriction base = control.createMock(InterfaceRestriction.class);
55 InterfaceRestriction composite = new CompositeInterfaceRestriction(
56 Arrays.asList(new InterfaceRestriction[] { base } ));
58 // First let the base return false and verify the result.
60 configureRestriction(base, false);
63 assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
64 _env, _env.getProvidedInterfaces()[0]));
67 // Second let the base return true and verify the result.
69 configureRestriction(base, true);
72 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
73 _env, _env.getProvidedInterfaces()[0]));
79 public void testTwoRestrictions() {
80 IMocksControl control = EasyMock.createStrictControl();
82 InterfaceRestriction base1 = control.createMock(InterfaceRestriction.class);
83 CompositeInterfaceRestriction composite = new CompositeInterfaceRestriction(
84 Arrays.asList(new InterfaceRestriction[] { base1 } ));
85 InterfaceRestriction base2 = control.createMock(InterfaceRestriction.class);
88 // 1. base1 not violated and base 2 not violated -> not violated.
90 configureRestriction(base1, false);
91 configureRestriction(base2, false);
93 assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
94 _env, _env.getProvidedInterfaces()[0]));
98 // 2. base 1 not violated but base 2 violated -> violated
99 configureRestriction(base1, false);
100 configureRestriction(base2, true);
102 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
103 _env, _env.getProvidedInterfaces()[0]));
107 // 3. base 1 violated -> violated and base 2 not called.
108 configureRestriction(base1, true);
109 // base 2 should not be called.
111 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
112 _env, _env.getProvidedInterfaces()[0]));