+/*
+ * 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.Arrays;
+import java.util.List;
+
+import org.wamblee.test.AssertionUtils;
+
+import junit.framework.TestCase;
+
+public class DefaultInterfaceRestrictionTest extends TestCase {
+ private Application _app1 = new Application("app1", "pf1.");
+ private Application _app2 = new Application("app2", "pf2.");
+
+ private Environment _env1 = new Environment("env1", "pf3.");
+ private Environment _env2 = new Environment("env2", "pf4.");
+
+
+ private void compare(Boolean[] aExpected, InterfaceRestriction aRestriction) {
+ List<Boolean> result = new ArrayList<Boolean>();
+
+ // order will be:
+ // env1, app1
+ // env1, app2
+ // env2, app1
+ // env2, app2
+ for (Environment env: new Environment[] { _env1, _env2} ) {
+ for (Application app: new Application[] { _app1, _app2} ) {
+ result.add(aRestriction.isViolated(
+ app, app.getRequiredInterfaces()[0],
+ env,
+ env.getProvidedInterfaces()[0]
+ ));
+ }
+ }
+
+
+ assertEquals(Arrays.asList(aExpected), result);
+ }
+
+ public void testNoRestriction() {
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, null, null);
+
+ }
+ }, IllegalArgumentException.class);
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, "x", "y");
+
+ }
+ }, IllegalArgumentException.class);
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction("x", "y", null, null);
+
+ }
+ }, IllegalArgumentException.class);
+ }
+
+ public void testClientServer() {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, "env1", null);
+ compare(new Boolean[] { false, false, true, false}, restriction);
+ }
+
+ public void testRequiredServer() {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", "env1", null);
+ compare(new Boolean[] { false, false, true, false}, restriction);
+ }
+
+ public void testClientProvided() {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, null, "pf4.datasource");
+ compare(new Boolean[] { true, false, false, false}, restriction);
+ }
+
+ public void testRequiredProvide() {
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", null, "pf4.datasource");
+ compare(new Boolean[] { true, false, false, false}, restriction);
+ }
+
+ public void testExplicitConfig() {
+ _app1 = new Application("app1");
+ _app2 = new Application("app2");
+ _env1 = new Environment("env1");
+ _env2 = new Environment("env2");
+
+ InterfaceRestriction restriction = new DefaultInterfaceRestriction(
+ "app2", "string", "env1", "datasource");
+ compare(new Boolean[] { false, false, false, true}, restriction);
+
+ }
+}