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.core;
18 import java.util.Arrays;
20 import org.easymock.classextension.EasyMock;
21 import org.easymock.classextension.IMocksControl;
22 import org.wamblee.test.EasyMockMatchers;
23 import static org.easymock.classextension.EasyMock.*;
25 import junit.framework.TestCase;
27 public class CompositeInterfaceRestrictionTest extends TestCase {
29 private Application _app = new Application();
30 private Environment _env = new Environment();
32 public void testEmpty() {
33 InterfaceRestriction restriction = new CompositeInterfaceRestriction();
34 assertFalse(restriction.isViolated(_app, _app.getRequiredInterfaces()[0],
35 _env, _env.getProvidedInterfaces()[0]));
38 private void configureRestriction(InterfaceRestriction base, boolean aResult) {
40 (Component)anyObject(),
41 (RequiredInterface)anyObject(),
42 (Component)anyObject(),
43 (ProvidedInterface)anyObject());
44 EasyMock.expectLastCall().andReturn(aResult);
47 public void testOneRestriction() {
48 IMocksControl control = EasyMock.createStrictControl();
50 InterfaceRestriction base = control.createMock(InterfaceRestriction.class);
51 InterfaceRestriction composite = new CompositeInterfaceRestriction(
52 Arrays.asList(new InterfaceRestriction[] { base } ));
54 // First let the base return false and verify the result.
56 configureRestriction(base, false);
59 assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
60 _env, _env.getProvidedInterfaces()[0]));
63 // Second let the base return true and verify the result.
65 configureRestriction(base, true);
68 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
69 _env, _env.getProvidedInterfaces()[0]));
75 public void testTwoRestrictions() {
76 IMocksControl control = EasyMock.createStrictControl();
78 InterfaceRestriction base1 = control.createMock(InterfaceRestriction.class);
79 CompositeInterfaceRestriction composite = new CompositeInterfaceRestriction(
80 Arrays.asList(new InterfaceRestriction[] { base1 } ));
81 InterfaceRestriction base2 = control.createMock(InterfaceRestriction.class);
84 // 1. base1 not violated and base 2 not violated -> not violated.
86 configureRestriction(base1, false);
87 configureRestriction(base2, false);
89 assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
90 _env, _env.getProvidedInterfaces()[0]));
94 // 2. base 1 not violated but base 2 violated -> violated
95 configureRestriction(base1, false);
96 configureRestriction(base2, true);
98 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
99 _env, _env.getProvidedInterfaces()[0]));
103 // 3. base 1 violated -> violated and base 2 not called.
104 configureRestriction(base1, true);
105 // base 2 should not be called.
107 assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0],
108 _env, _env.getProvidedInterfaces()[0]));