/* * 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.graph; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import junit.framework.TestCase; import org.wamblee.system.container.Application; import org.wamblee.system.core.Component; import org.wamblee.system.core.Environment; import org.wamblee.system.core.ProvidedInterface; import org.wamblee.system.core.RequiredInterface; import org.wamblee.system.graph.component.ProvidedInterfaceNode; import org.wamblee.system.graph.component.RequiredInterfaceNode; public class CompositeEdgeFilterTest extends TestCase { private Application _app = new Application(); private Environment _env = new Environment(); private Edge createEdge(Component aClient, RequiredInterface aRequired, Component aServer, ProvidedInterface aProvided) { Node from = new RequiredInterfaceNode(aClient, aRequired); Node to = new ProvidedInterfaceNode(aServer, aProvided); return new DefaultEdge(from, to); } public void testEmpty() { EdgeFilter restriction = new CompositeEdgeFilter(); assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); } private void configureRestriction(EdgeFilter base, boolean aResult) { stub(base.isViolated((Edge)anyObject())).toReturn(aResult); } public void testOneRestriction() { EdgeFilter base = mock(EdgeFilter.class); CompositeEdgeFilter composite = new CompositeEdgeFilter(); composite.add(base); // First let the base return false and verify the result. configureRestriction(base, false); assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); // Second let the base return true and verify the result. configureRestriction(base, true); assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); } public void testTwoRestrictions() { EdgeFilter base1 = mock(EdgeFilter.class); CompositeEdgeFilter composite = new CompositeEdgeFilter(); composite.add(base1); EdgeFilter base2 = mock(EdgeFilter.class); composite.add(base2); // 1. base1 not violated and base 2 not violated -> not violated. configureRestriction(base1, false); configureRestriction(base2, false); assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); // 2. base 1 not violated but base 2 violated -> violated configureRestriction(base1, false); configureRestriction(base2, true); assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); // 3. base 1 violated -> violated and base 2 not called. configureRestriction(base1, true); // base 2 should not be called. assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces().get(0), _env, _env.getProvidedInterfaces().get(0)))); } }