summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Liebig2008-02-26 09:00:24 (EST)
committerStefan Liebig2008-02-26 09:00:24 (EST)
commitc2cd31f497157aea4d6f3ecdef8b5eab861144b6 (patch)
tree5abb6ee50062eaced6a22e292add44225ea563c7
parentad4b5f6d290c518a16c190299acbf0b5ee8da0f4 (diff)
downloadorg.eclipse.riena-c2cd31f497157aea4d6f3ecdef8b5eab861144b6.zip
org.eclipse.riena-c2cd31f497157aea4d6f3ecdef8b5eab861144b6.tar.gz
org.eclipse.riena-c2cd31f497157aea4d6f3ecdef8b5eab861144b6.tar.bz2
good old test
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ContainerModelTest.java49
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/util/IterTest.java146
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ReflectionUtilsTest.java254
3 files changed, 449 insertions, 0 deletions
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ContainerModelTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ContainerModelTest.java
new file mode 100644
index 0000000..04b80d3
--- /dev/null
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ContainerModelTest.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2008 compeople AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * compeople AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.riena.core.util;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ */
+public class ContainerModelTest extends TestCase {
+
+ private static final String INITIALIZE_METHOD_NAME = "initialize"; //$NON-NLS-1$
+
+ /*
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ // just load the class once
+ ContainerModel.isClient();
+ }
+
+ public void testOnClient() throws ClassNotFoundException {
+ ReflectionUtils.invokeHidden(ContainerModel.class, INITIALIZE_METHOD_NAME);
+ assertTrue(ContainerModel.isClient());
+ }
+
+ public void testOnServerOk() {
+ System.setProperty(ContainerModel.RIENA_CONTAINER_TYPE, "server"); //$NON-NLS-1$
+ ReflectionUtils.invokeHidden(ContainerModel.class, INITIALIZE_METHOD_NAME);
+ assertTrue(ContainerModel.isServer());
+ }
+
+ public void testOnServerFail() {
+ System.setProperty(ContainerModel.RIENA_CONTAINER_TYPE, "Xerver"); //$NON-NLS-1$
+ ReflectionUtils.invokeHidden(ContainerModel.class, INITIALIZE_METHOD_NAME);
+ assertFalse(ContainerModel.isServer());
+ }
+
+}
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/IterTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/IterTest.java
new file mode 100644
index 0000000..30c9963
--- /dev/null
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/IterTest.java
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2008 compeople AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * compeople AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.riena.core.util;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Scanner;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+import junit.framework.TestCase;
+
+import org.eclipse.riena.core.util.Iter;
+
+/**
+ * Test the Iter class
+ */
+public class IterTest extends TestCase {
+
+ /**
+ * Nomen est omen!
+ */
+ public void testIteratorTyped() {
+ Scanner scanner = new Scanner("this is a test");
+ String result = "";
+ for (String s : Iter.able(scanner)) {
+ result = result + s;
+ }
+ assertEquals("thisisatest", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testIteratorUntyped() {
+ UntypedIteratorReturner utr = new UntypedIteratorReturner(new String[] { "1", "2" });
+ String result = "";
+ for (String s : Iter.able(utr.getIterator(), String.class)) {
+ result = result + s;
+ }
+ assertEquals("12", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testNullIterator() {
+ String result = "";
+ for (String s : Iter.able((Iterator<String>) null)) {
+ result = result + s;
+ }
+ assertEquals("", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testEnumerationTyped() {
+ Vector<String> v = new Vector<String>();
+ v.add("1");
+ v.add("2");
+ String result = "";
+ for (String o : Iter.able(v.elements())) {
+ result = result + o;
+ }
+ assertEquals("12", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testEnumerationUntyped() {
+ StringTokenizer st = new StringTokenizer("this is a test");
+ String result = "";
+ for (String str : Iter.able(st, String.class)) {
+ result = result + str;
+ }
+ assertEquals("thisisatest", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testNullEnumeration() {
+ String result = "";
+ for (Object o : Iter.able((Enumeration<Object>) null)) {
+ result = result + o;
+ }
+ assertEquals("", result);
+ }
+
+ /**
+ * Nomen est omen!
+ */
+ public void testAndNowSomethingCompletlyDifferent() throws IOException {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ Enumeration<URL> enumeration = classLoader.getResources("META-INF/hivemodule.xml");
+ for (URL jarUrl : Iter.able(enumeration)) {
+ System.out.println(jarUrl);
+ }
+ }
+
+ private static class UntypedIteratorReturner {
+ private String[] strings;
+
+ /**
+ * @param strings
+ */
+ public UntypedIteratorReturner(String[] strings) {
+ this.strings = strings;
+ }
+
+ /**
+ * Note: It is intended that the iterator is untyped!!!!
+ *
+ * @return
+ */
+ public Iterator getIterator() {
+ return new Iterator() {
+ private int i = 0;
+
+ public boolean hasNext() {
+ return i < strings.length;
+ }
+
+ public Object next() {
+ return strings[i++];
+ }
+
+ public void remove() {
+ }
+
+ };
+ }
+ }
+}
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ReflectionUtilsTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ReflectionUtilsTest.java
new file mode 100644
index 0000000..0d07414
--- /dev/null
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/util/ReflectionUtilsTest.java
@@ -0,0 +1,254 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2008 compeople AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * compeople AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.riena.core.util;
+
+import java.awt.Dimension;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the ReflectionUtils class.
+ */
+public class ReflectionUtilsTest extends TestCase {
+
+ /**
+ * Test creating an instance #1
+ */
+ public void testNewInstance1() {
+ Object o = ReflectionUtils.newInstance("java.lang.String", "Hello!");
+ assertTrue(o instanceof String);
+ assertEquals(o, "Hello!");
+ }
+
+ /**
+ * Test creating an instance #2
+ */
+ public void testNewInstance2() {
+ Object o = ReflectionUtils.newInstance("java.lang.String");
+ assertTrue(o instanceof String);
+ assertEquals(o, "");
+ }
+
+ /**
+ * Test creating an instance #3
+ */
+ public void testNewInstance3() {
+ Object o = ReflectionUtils.newInstance("java.awt.Dimension", new Integer(2), new Integer(2));
+ assertTrue(o instanceof Dimension);
+ Dimension dim = (Dimension) o;
+ assertEquals(new Dimension(2, 2), dim);
+ }
+
+ /**
+ * Test creating an instance #4
+ */
+ public void testNewInstance4() {
+ String s = ReflectionUtils.newInstance(String.class, "Hello!");
+ assertEquals(s, "Hello!");
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws MalformedURLException
+ *
+ * @throws Throwable
+ */
+ public void testInvokeHiddenInstance() throws MalformedURLException {
+
+ URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+ URL url = new URL("file:c:\\");
+ ReflectionUtils.invokeHidden(sysloader, "addURL", url);
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws MalformedURLException
+ *
+ * @throws Throwable
+ */
+ public void testInvokeHiddenInstanceWithMalformedURLException() {
+
+ Object object = new Thrower();
+ try {
+ ReflectionUtils.invokeHidden(object, "setUrl", MalformedURLException.class, "filez://murks.at");
+ fail();
+ } catch (MalformedURLException e) {
+ // ok
+ }
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testInvokeHiddenClass() {
+
+ ReflectionUtils.invokeHidden(TestClass.class, "setString", new Object[] { "hihi" });
+ String out = (String) ReflectionUtils.invokeHidden(TestClass.class, "getString");
+ assertEquals(out, "hihi");
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testInvokeHiddenClassWithIOException() {
+
+ try {
+ ReflectionUtils.invokeHidden(Thrower.class, "throwIOException", IOException.class);
+ fail();
+ } catch (IOException e) {
+ assertEquals("Yippie!", e.getMessage());
+ }
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testSetGetHiddenInstance() {
+
+ String s = "Hallo";
+ ReflectionUtils.setHidden(s, "count", new Integer(2));
+ Integer count = (Integer) ReflectionUtils.getHidden(s, "count");
+ assertEquals(2, count.intValue());
+ assertEquals(s, "Ha");
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testSetGetHiddenClass() {
+
+ ReflectionUtils.setHidden(TestClass.class, "string", "hallo");
+ String out = (String) ReflectionUtils.getHidden(TestClass.class, "string");
+ assertEquals(out, "hallo");
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testSetGetHiddenDeepField() {
+
+ TestTestClass ttc = new TestTestClass("Hallo");
+ ReflectionUtils.setHidden(ttc, "_string", "hurz");
+ String hurz = (String) ReflectionUtils.getHidden(ttc, "_string");
+ assertEquals("hurz", hurz);
+ }
+
+ /**
+ * Nomen est omen!
+ *
+ * @throws Throwable
+ */
+ public void testSetGetHiddenDeepClassField() throws Throwable {
+
+ ReflectionUtils.setHidden(TestTestClass.class, "string", "hallo");
+ String out = (String) ReflectionUtils.getHidden(TestTestClass.class, "string");
+ assertEquals(out, "hallo");
+ }
+
+ /**
+ * Nomen est Omen!
+ */
+ public void testNewLazyInstanceByClass() {
+ HaeshMaep.created = false;
+ Map<String, String> map = ReflectionUtils.newLazyInstance(Map.class, HaeshMaep.class);
+ assertFalse(HaeshMaep.created);
+ map.put("hi", "there");
+ assertTrue(HaeshMaep.created);
+ }
+
+ /**
+ * Nomen est Omen!
+ */
+ public void testNewLazyInstanceByString() {
+ HaeshMaep.created = false;
+ Map<String, String> map = ReflectionUtils.newLazyInstance(Map.class, HaeshMaep.class.getName());
+ assertFalse(HaeshMaep.created);
+ map.put("hi", "there");
+ assertTrue(HaeshMaep.created);
+ }
+
+ private static class TestClass {
+ private static String string;
+ private String _string;
+
+ private TestClass(String str) {
+ _string = str;
+ }
+
+ private static void setString(String string) {
+ TestClass.string = string;
+ }
+
+ private static String getString() {
+ return string;
+ }
+
+ }
+
+ private static final class TestTestClass extends TestClass {
+ private static String stringString;
+
+ private TestTestClass(String str) {
+ super(str);
+ }
+
+ private static void setString(String string) {
+ TestTestClass.stringString = string;
+ }
+
+ private static String getString() {
+ return stringString;
+ }
+ }
+
+ private static class Thrower {
+
+ private void setUrl(String url) throws MalformedURLException {
+ new URL(url);
+ }
+
+ private static String throwIOException() throws IOException {
+ throw new IOException("Yippie!");
+ }
+ }
+
+ public static class HaeshMaep<K, V> extends HashMap<K, V> {
+
+ protected static boolean created;
+
+ /**
+ *
+ */
+ public HaeshMaep() {
+ super();
+ created = true;
+ }
+
+ }
+} \ No newline at end of file