+/*
+ * 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.adapters;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.RequiredInterfaceComparator;
+import org.wamblee.test.AssertionUtils;
+
+public class SetterConfigurationTest extends AdapterTestCase {
+
+ public void testOneSetter() {
+ SetterConfiguration config = new SetterConfiguration(X5.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ assertEquals(1, required.size());
+ assertEquals("value", required.get(0).getName());
+
+ ProvidedInterface provided = new DefaultProvidedInterface("janse", String.class);
+ required.get(0).setProvider(provided);
+ provided.publish("hello", _scope);
+
+ X5 obj = new X5();
+ assertNull(obj.getValue());
+ config.invoke(_scope, obj);
+ assertEquals("hello", obj.getValue());
+ }
+
+ public void testMultipleSetters() {
+ SetterConfiguration config = new SetterConfiguration(X6.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ Collections.sort(required, new RequiredInterfaceComparator());
+ assertEquals(2, required.size());
+ assertEquals("host", required.get(0).getName());
+ assertEquals("port", required.get(1).getName());
+
+ ProvidedInterface provided0 = new DefaultProvidedInterface("janse", String.class);
+ required.get(0).setProvider(provided0);
+ provided0.publish("hello", _scope);
+
+ ProvidedInterface provided1 = new DefaultProvidedInterface("port", Integer.class);
+ required.get(1).setProvider(provided1);
+ provided1.publish(10, _scope);
+
+ X6 obj = new X6();
+ assertNull(obj.getHost());
+ assertNull(obj.getPort());
+
+ config.invoke(_scope, obj);
+ assertEquals("hello", obj.getHost());
+ assertEquals(10, obj.getPort().intValue());
+ }
+
+ public void testInvokeWrongType() {
+ final SetterConfiguration config = new SetterConfiguration(X5.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ assertEquals(1, required.size());
+ assertEquals("value", required.get(0).getName());
+
+ ProvidedInterface provided = new DefaultProvidedInterface("janse", String.class);
+ required.get(0).setProvider(provided);
+ provided.publish("hello", _scope);
+
+ final X6 obj = new X6();
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ config.invoke(_scope, obj);
+ }
+ }, IllegalArgumentException.class);
+ }
+
+ public void testSetExplicitValue() {
+ SetterConfiguration config = new SetterConfiguration(X5.class);
+ config.values("value").setValue(0, "bladibla");
+
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ assertEquals(0, required.size());
+
+ X5 obj = new X5();
+ assertNull(obj.getValue());
+ config.invoke(_scope, obj);
+ assertEquals("bladibla", obj.getValue());
+ }
+
+ public void testClear() {
+ SetterConfiguration config = new SetterConfiguration(X6.class);
+ config.clear();
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ Collections.sort(required, new RequiredInterfaceComparator());
+ assertEquals(0, required.size());
+
+ X6 obj = new X6();
+ assertNull(obj.getHost());
+ assertNull(obj.getPort());
+
+ config.invoke(_scope, obj);
+
+ assertNull(obj.getHost());
+ assertNull(obj.getPort());
+ }
+
+ public void testAdd() {
+ SetterConfiguration config = new SetterConfiguration(X6.class);
+ config.clear().add("host");
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ Collections.sort(required, new RequiredInterfaceComparator());
+ assertEquals(1, required.size());
+ assertEquals("host", required.get(0).getName());
+
+ ProvidedInterface provided0 = new DefaultProvidedInterface("janse", String.class);
+ required.get(0).setProvider(provided0);
+ provided0.publish("hello", _scope);
+
+ X6 obj = new X6();
+ assertNull(obj.getHost());
+ assertNull(obj.getPort());
+
+ config.invoke(_scope, obj);
+ assertEquals("hello", obj.getHost());
+ assertNull(obj.getPort());
+ }
+
+ public void testAddNonExisting() {
+ final SetterConfiguration config = new SetterConfiguration(X6.class);
+ config.clear();
+
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ config.add("bladibla");
+ }
+ }, IllegalArgumentException.class);
+ }
+
+ public void testRemove() {
+ SetterConfiguration config = new SetterConfiguration(X6.class);
+ config.remove("port");
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ Collections.sort(required, new RequiredInterfaceComparator());
+ assertEquals(1, required.size());
+ assertEquals("host", required.get(0).getName());
+
+ ProvidedInterface provided0 = new DefaultProvidedInterface("janse", String.class);
+ required.get(0).setProvider(provided0);
+ provided0.publish("hello", _scope);
+
+ X6 obj = new X6();
+ assertNull(obj.getHost());
+ assertNull(obj.getPort());
+
+ config.invoke(_scope, obj);
+ assertEquals("hello", obj.getHost());
+ assertNull(obj.getPort());
+ }
+
+ public void testRemoveNonExisting() {
+ final SetterConfiguration config = new SetterConfiguration(X6.class);
+
+ AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+ @Override
+ public void run() throws Exception {
+ config.remove("bladibla");
+ }
+ }, IllegalArgumentException.class);
+ }
+}