--- /dev/null
+/*
+ * 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<InterfaceRestriction> _restrictions;
+
+ /**
+ * Constructs the restriction.
+ */
+ public CompositeInterfaceRestriction() {
+ _restrictions = new ArrayList<InterfaceRestriction>();
+ }
+
+ /**
+ * Constructs the restriction.
+ * @param aRestrictions List of contained restrictions.
+ */
+ public CompositeInterfaceRestriction(Collection<InterfaceRestriction> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+}