From bda62cd29b2017a2ee113122eea6d4962a9d54ea Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 20 Apr 2008 21:01:23 +0000 Subject: [PATCH] --- .../core/CompositeInterfaceRestriction.java | 71 +++++++++++ .../CompositeInterfaceRestrictionTest.java | 112 ++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 system/general/src/main/java/org/wamblee/system/core/CompositeInterfaceRestriction.java create mode 100644 system/general/src/test/java/org/wamblee/system/core/CompositeInterfaceRestrictionTest.java diff --git a/system/general/src/main/java/org/wamblee/system/core/CompositeInterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/core/CompositeInterfaceRestriction.java new file mode 100644 index 00000000..29448c16 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/core/CompositeInterfaceRestriction.java @@ -0,0 +1,71 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.core; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + + +/** + * Composite restriction that wraps a number of other restrictions. The restriction is + * violated if one of the contained restrictions is violated. + * + * @author Erik Brakkee + * + */ +public class CompositeInterfaceRestriction implements InterfaceRestriction { + + private List _restrictions; + + /** + * Constructs the restriction. + */ + public CompositeInterfaceRestriction() { + _restrictions = new ArrayList(); + } + + /** + * Constructs the restriction. + * @param aRestrictions List of contained restrictions. + */ + public CompositeInterfaceRestriction(Collection aRestrictions) { + this(); + _restrictions.addAll(aRestrictions); + } + + /** + * Adds a restriction. + * @param aRestriction Restriction. + * @return Reference to this composite restriction to allow call chaining. + */ + public CompositeInterfaceRestriction add(InterfaceRestriction aRestriction) { + _restrictions.add(aRestriction); + return this; + } + + @Override + public boolean isViolated(Component aClient, RequiredInterface aRequired, + Component aServer, ProvidedInterface aProvided) { + for (InterfaceRestriction restriction: _restrictions) { + if ( restriction.isViolated(aClient, aRequired, aServer, aProvided)) { + return true; + } + } + return false; + } + +} diff --git a/system/general/src/test/java/org/wamblee/system/core/CompositeInterfaceRestrictionTest.java b/system/general/src/test/java/org/wamblee/system/core/CompositeInterfaceRestrictionTest.java new file mode 100644 index 00000000..98f95aa4 --- /dev/null +++ b/system/general/src/test/java/org/wamblee/system/core/CompositeInterfaceRestrictionTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.core; + +import java.util.Arrays; + +import org.easymock.classextension.EasyMock; +import org.easymock.classextension.IMocksControl; +import org.wamblee.test.EasyMockMatchers; +import static org.easymock.classextension.EasyMock.*; + +import junit.framework.TestCase; + +public class CompositeInterfaceRestrictionTest extends TestCase { + + private Application _app = new Application(); + private Environment _env = new Environment(); + + public void testEmpty() { + InterfaceRestriction restriction = new CompositeInterfaceRestriction(); + assertFalse(restriction.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + } + + private void configureRestriction(InterfaceRestriction base, boolean aResult) { + base.isViolated( + (Component)anyObject(), + (RequiredInterface)anyObject(), + (Component)anyObject(), + (ProvidedInterface)anyObject()); + EasyMock.expectLastCall().andReturn(aResult); + } + + public void testOneRestriction() { + IMocksControl control = EasyMock.createStrictControl(); + + InterfaceRestriction base = control.createMock(InterfaceRestriction.class); + InterfaceRestriction composite = new CompositeInterfaceRestriction( + Arrays.asList(new InterfaceRestriction[] { base } )); + + // First let the base return false and verify the result. + + configureRestriction(base, false); + + control.replay(); + assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + control.verify(); + + // Second let the base return true and verify the result. + control.reset(); + configureRestriction(base, true); + + control.replay(); + assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + control.verify(); + } + + + + public void testTwoRestrictions() { + IMocksControl control = EasyMock.createStrictControl(); + + InterfaceRestriction base1 = control.createMock(InterfaceRestriction.class); + CompositeInterfaceRestriction composite = new CompositeInterfaceRestriction( + Arrays.asList(new InterfaceRestriction[] { base1 } )); + InterfaceRestriction base2 = control.createMock(InterfaceRestriction.class); + composite.add(base2); + + // 1. base1 not violated and base 2 not violated -> not violated. + + configureRestriction(base1, false); + configureRestriction(base2, false); + control.replay(); + assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + control.verify(); + control.reset(); + + // 2. base 1 not violated but base 2 violated -> violated + configureRestriction(base1, false); + configureRestriction(base2, true); + control.replay(); + assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + control.verify(); + control.reset(); + + // 3. base 1 violated -> violated and base 2 not called. + configureRestriction(base1, true); + // base 2 should not be called. + control.replay(); + assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], + _env, _env.getProvidedInterfaces()[0])); + control.verify(); + control.reset(); + } +} -- 2.31.1