summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Liebig2008-03-12 10:29:44 (EDT)
committerStefan Liebig2008-03-12 10:29:44 (EDT)
commite6b755b2ecaa30c00343316e29be32bdc7da16e2 (patch)
treeb4444cbc193bd7056dbaff7b095120a95ef2e8dd
parent2b422511fd291107ed53349c9eea1d9dd18a5b19 (diff)
downloadorg.eclipse.riena-e6b755b2ecaa30c00343316e29be32bdc7da16e2.zip
org.eclipse.riena-e6b755b2ecaa30c00343316e29be32bdc7da16e2.tar.gz
org.eclipse.riena-e6b755b2ecaa30c00343316e29be32bdc7da16e2.tar.bz2
added tests for type coercion for primitive types and constraint checking
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/ExtensionInjectorTest.java173
1 files changed, 166 insertions, 7 deletions
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/ExtensionInjectorTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/ExtensionInjectorTest.java
index 0aa1897..bcecd22 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/ExtensionInjectorTest.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/ExtensionInjectorTest.java
@@ -10,22 +10,93 @@
*******************************************************************************/
package org.eclipse.riena.core.extension;
+import java.util.ArrayList;
+import java.util.Dictionary;
import java.util.HashMap;
+import java.util.Hashtable;
+import org.eclipse.riena.internal.core.config.ConfigSymbolReplace;
import org.eclipse.riena.tests.RienaTestCase;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ConfigurationPlugin;
/**
*
*/
public class ExtensionInjectorTest extends RienaTestCase {
+ /**
+ * In some test we have to sleep because of asynchronous processing.
+ */
+ private static final int SLEEP_TIME = 500;
+
public void scribble() {
BundleContext context = null;
- new ExtensionId("", Object.class).expectingMinMax(0, 1).injectInto(this).andStart();
- new ExtensionId("", Object.class).expectingExactly(1).injectInto(this).bind("update").andStart();
+ new ExtensionId("").useType(Object.class).expectingMinMax(0, 1).injectInto(this).andStart();
+ new ExtensionId("").useType(Object.class).expectingExactly(1).injectInto(this).bind("update").andStart();
new ExtensionId("").injectInto(this).doNotTrack().andStart().stop();
+ }
+
+ public void testConstructorConstraints() {
+ try {
+ new ExtensionId(null);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ }
+
+ public void testUseTypeConstraints() {
+ try {
+ new ExtensionId("id").useType(null);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ try {
+ new ExtensionId("id").useType(IData.class).useType(IData.class);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ try {
+ new ExtensionId("id").useType(String.class);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ }
+ public void testExpectingConstraints() {
+ try {
+ new ExtensionId("id").expectingMinMax(2, 1);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ try {
+ new ExtensionId("id").expectingMinMax(-1, 0);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ try {
+ new ExtensionId("id").expectingMinMax(0, 0);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
+ }
+
+ public void testInjectIntoConstraints() {
+ try {
+ new ExtensionId("id").injectInto(null);
+ fail("That should not happen!");
+ } catch (RuntimeException e) {
+ // ok
+ }
}
public void testWithKnownTypeAndMultipleData() {
@@ -35,12 +106,14 @@ public class ExtensionInjectorTest extends RienaTestCase {
addPluginXml(ExtensionInjectorTest.class, "plugin_ext2.xml");
addPluginXml(ExtensionInjectorTest.class, "plugin_ext3.xml");
ConfigurableThingMultipleData target = new ConfigurableThingMultipleData();
- new ExtensionId("core.test.extpoint", IData.class).injectInto(target).andStart();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").useType(IData.class).injectInto(target)
+ .andStart();
assertEquals(3, target.getData().length);
removeExtension("core.test.extpoint.id1");
removeExtension("core.test.extpoint.id2");
removeExtension("core.test.extpoint.id3");
removeExtensionPoint("core.test.extpoint");
+ injector.stop();
}
public void testWithUnknownTypeAndMultipleData() {
@@ -50,12 +123,13 @@ public class ExtensionInjectorTest extends RienaTestCase {
addPluginXml(ExtensionInjectorTest.class, "plugin_ext2.xml");
addPluginXml(ExtensionInjectorTest.class, "plugin_ext3.xml");
ConfigurableThingMultipleData target = new ConfigurableThingMultipleData();
- new ExtensionId("core.test.extpoint").injectInto(target).andStart();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").injectInto(target).andStart();
assertEquals(3, target.getData().length);
removeExtension("core.test.extpoint.id1");
removeExtension("core.test.extpoint.id2");
removeExtension("core.test.extpoint.id3");
removeExtensionPoint("core.test.extpoint");
+ injector.stop();
}
public void testWithKnownTypeAndSingleData() {
@@ -63,8 +137,8 @@ public class ExtensionInjectorTest extends RienaTestCase {
addPluginXml(ExtensionInjectorTest.class, "plugin.xml");
addPluginXml(ExtensionInjectorTest.class, "plugin_ext1.xml");
ConfigurableThingSingleData target = new ConfigurableThingSingleData();
- new ExtensionId("core.test.extpoint", IData.class).expectingExactly(1).injectInto(target).bind("configure")
- .andStart();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").useType(IData.class).expectingExactly(1)
+ .injectInto(target).bind("configure").andStart();
assertNotNull(target.getData());
assertTrue(target.getData().getBoolean());
assertTrue(target.getData().isBoolean());
@@ -72,6 +146,7 @@ public class ExtensionInjectorTest extends RienaTestCase {
assertEquals(String.class, target.getData().createExecutable().getClass());
removeExtension("core.test.extpoint.id1");
removeExtensionPoint("core.test.extpoint");
+ injector.stop();
}
public void testWithUnknownTypeAndSingleData() {
@@ -79,7 +154,8 @@ public class ExtensionInjectorTest extends RienaTestCase {
addPluginXml(ExtensionInjectorTest.class, "plugin.xml");
addPluginXml(ExtensionInjectorTest.class, "plugin_ext2.xml");
ConfigurableThingSingleData target = new ConfigurableThingSingleData();
- new ExtensionId("core.test.extpoint").expectingExactly(1).injectInto(target).bind("configure").andStart();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").expectingExactly(1).injectInto(target).bind(
+ "configure").andStart();
assertNotNull(target.getData());
assertFalse(target.getData().getBoolean());
assertFalse(target.getData().isBoolean());
@@ -87,6 +163,89 @@ public class ExtensionInjectorTest extends RienaTestCase {
assertEquals(HashMap.class, target.getData().createExecutable().getClass());
removeExtension("core.test.extpoint.id2");
removeExtensionPoint("core.test.extpoint");
+ injector.stop();
+ }
+
+ public void testCoerceWithUnknownTypeAndSingleData() {
+ printTestName();
+ addPluginXml(ExtensionInjectorTest.class, "plugin.xml");
+ addPluginXml(ExtensionInjectorTest.class, "plugin_ext4.xml");
+ ConfigurableThingSingleData target = new ConfigurableThingSingleData();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").expectingExactly(1).injectInto(target).bind(
+ "configure").andStart();
+ assertNotNull(target.getData());
+ assertTrue(target.getData().getBoolean());
+ assertEquals("test4", target.getData().getString());
+ assertEquals(ArrayList.class, target.getData().createExecutable().getClass());
+ assertEquals(0x20, target.getData().getByte());
+ assertEquals(2.7182818284590452d, target.getData().getDouble());
+ assertEquals(3.14159f, target.getData().getFloat());
+ assertEquals(123, target.getData().getInteger());
+ assertEquals(1234567890l, target.getData().getLong());
+ assertEquals(1, target.getData().getShort());
+ assertEquals('#', target.getData().getCharacter());
+
+ removeExtension("core.test.extpoint.id4");
+ removeExtensionPoint("core.test.extpoint");
+ injector.stop();
+ }
+
+ /**
+ * This test is brittle because it deals with asynchronous events.
+ *
+ * @throws InterruptedException
+ */
+ public void testTrackingWithKnownTypeAndMultipleData() throws InterruptedException {
+ printTestName();
+ addPluginXml(ExtensionInjectorTest.class, "plugin.xml");
+ ConfigurableThingMultipleData target = new ConfigurableThingMultipleData();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").useType(IData.class).injectInto(target)
+ .andStart();
+ assertEquals(0, target.getData().length);
+ addPluginXml(ExtensionInjectorTest.class, "plugin_ext1.xml");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(1, target.getData().length);
+ addPluginXml(ExtensionInjectorTest.class, "plugin_ext2.xml");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(2, target.getData().length);
+ addPluginXml(ExtensionInjectorTest.class, "plugin_ext3.xml");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(3, target.getData().length);
+ removeExtension("core.test.extpoint.id1");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(2, target.getData().length);
+ removeExtension("core.test.extpoint.id2");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(1, target.getData().length);
+ removeExtension("core.test.extpoint.id3");
+ Thread.sleep(SLEEP_TIME);
+ assertEquals(0, target.getData().length);
+ removeExtensionPoint("core.test.extpoint");
+ injector.stop();
}
+ public void testModifyWithUnknownTypeAndSingleData() throws ConfigurationException {
+ printTestName();
+ ServiceReference reference = getContext().getServiceReference(ConfigurationPlugin.class.getName());
+ Object service = getContext().getService(reference);
+ assertTrue("Ok, test needs rework!", service instanceof ConfigSymbolReplace);
+ ConfigSymbolReplace config = (ConfigSymbolReplace) service;
+ Dictionary properties = new Hashtable();
+ properties.put("true", "true");
+ config.updated(properties);
+ getContext().ungetService(reference);
+ addPluginXml(ExtensionInjectorTest.class, "plugin.xml");
+ addPluginXml(ExtensionInjectorTest.class, "plugin_ext5.xml");
+ ConfigurableThingSingleData target = new ConfigurableThingSingleData();
+ ExtensionInjector injector = new ExtensionId("core.test.extpoint").expectingExactly(1).injectInto(target).bind(
+ "configure").andStart(getContext());
+ assertNotNull(target.getData());
+ assertTrue(target.getData().getBoolean());
+ assertEquals("test5", target.getData().getString());
+ assertEquals(String.class, target.getData().createExecutable().getClass());
+
+ removeExtension("core.test.extpoint.id5");
+ removeExtensionPoint("core.test.extpoint");
+ injector.stop();
+ }
}