(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 28 Mar 2006 13:37:10 +0000 (13:37 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 28 Mar 2006 13:37:10 +0000 (13:37 +0000)
support/resources/test/org/wamblee/general/beankernel-wrong.properties [new file with mode: 0644]
support/resources/test/org/wamblee/general/beankernel.properties [new file with mode: 0644]
support/src/org/wamblee/general/BeanKernel.java
support/src/org/wamblee/general/SpringBeanFactory.java
support/test/org/wamblee/general/BeanKernelTest.java [new file with mode: 0644]
support/test/org/wamblee/general/PairTest.java [new file with mode: 0644]
support/test/org/wamblee/general/SpringBeanFactoryTest.java
support/test/org/wamblee/general/TestBeanFactory.java [new file with mode: 0644]

diff --git a/support/resources/test/org/wamblee/general/beankernel-wrong.properties b/support/resources/test/org/wamblee/general/beankernel-wrong.properties
new file mode 100644 (file)
index 0000000..e54de19
--- /dev/null
@@ -0,0 +1,2 @@
+
+org.wamblee.beanfactory.class=org.wamblee.general.TestBeanFactoryBla
diff --git a/support/resources/test/org/wamblee/general/beankernel.properties b/support/resources/test/org/wamblee/general/beankernel.properties
new file mode 100644 (file)
index 0000000..e08cf39
--- /dev/null
@@ -0,0 +1,2 @@
+
+org.wamblee.beanfactory.class=org.wamblee.general.TestBeanFactory
index 5f32f68756fd5a0ceaa4f8d83eeb34a7329bcbd5..d05eef3dd6b507c8706a5d2dd865b3de4edc06c6 100644 (file)
@@ -78,7 +78,7 @@ public final class BeanKernel {
     public static BeanFactory getBeanFactory() {
         synchronized (BeanFactory.class) {
             if (BEAN_FACTORY == null) {
-                BEAN_FACTORY = lookupBeanFactory();
+                BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
             }
         }
         return BEAN_FACTORY;
@@ -89,8 +89,8 @@ public final class BeanKernel {
      * 
      * @return Bean factory.
      */
-    static BeanFactory lookupBeanFactory() {
-        InputResource resource = new ClassPathResource(BEAN_KERNEL_PROP_FILE);
+    static BeanFactory lookupBeanFactory(String aPropertyFilename) {
+        InputResource resource = new ClassPathResource(aPropertyFilename);
         InputStream is;
         try {
             is = resource.getInputStream();
index 91ac1c3f28fbb85e49bc6c9979572092266e0508..27a544b1d81da635928e7c47eb3bd02b9aac04e3 100644 (file)
@@ -32,16 +32,22 @@ public class SpringBeanFactory implements BeanFactory {
 
     /**
      * Constructs the bean factory.
-     *
+     * 
      * @param aSelector
      *            Selector to find the appropriate bean ref context.
      * @param aFactoryName
      *            Spring bean factory to use.
      */
     public SpringBeanFactory(String aSelector, String aFactoryName) {
-        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator
-                .getInstance(aSelector);
-        _factoryReference = locator.useBeanFactory(aFactoryName);
+        try {
+            BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator
+                    .getInstance(aSelector);
+            _factoryReference = locator.useBeanFactory(aFactoryName);
+        } catch (BeansException e) {
+            throw new BeanFactoryException(
+                    "Could not load bean factory: selector = '" + aSelector
+                            + "', factory = '" + aFactoryName + "'", e);
+        }
     }
 
     /*
diff --git a/support/test/org/wamblee/general/BeanKernelTest.java b/support/test/org/wamblee/general/BeanKernelTest.java
new file mode 100644 (file)
index 0000000..4a79f0a
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2005 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.general;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the bean kernel. The lookup of the bean factory itself can be tested
+ * only partially. Using a global property file for all test cases would tie all
+ * test cases together therefore no global property file is used.
+ */
+public class BeanKernelTest extends TestCase {
+
+    /**
+     * Loads the bean factory based on a property file configuration. Verifies
+     * the correct bean factory is loaded.
+     * 
+     */
+    public void testLoadBeanFactoryFromProperties() {
+        BeanFactory factory = BeanKernel
+                .lookupBeanFactory("org/wamblee/general/beankernel.properties");
+        assertTrue(factory instanceof TestBeanFactory);
+    }
+
+    /**
+     * Loads the bean factory based on a non-existing property file. 
+     * Verifies that BeanFactoryException is thrown. 
+     * 
+     */
+    public void testNonExistentPropertyFile() {
+        try {
+            BeanFactory factory = BeanKernel
+                    .lookupBeanFactory("org/wamblee/general/beankernel-nonexistent.properties");
+        } catch (BeanFactoryException e) {
+            return; // ok
+        }
+        fail();
+    }
+    
+    /**
+     * Loads the bean factory based on a property file with a non-existing
+     * bean factory defined in it.  
+     * Verifies that BeanFactoryException is thrown. 
+     * 
+     */
+    public void testNonExistentBeanFactory() {
+        try {
+            BeanFactory factory = BeanKernel
+                    .lookupBeanFactory("org/wamblee/general/beankernel-wrong.properties");
+        } catch (BeanFactoryException e) {
+            return; // ok
+        }
+        fail();
+    }
+    
+
+    /**
+     * Retrieves a bean factory throug the bean kernel. Verifies that beans can
+     * be retrieved.
+     * 
+     */
+    public void testRetrieveFactory() {
+        BeanKernel.overrideBeanFactory(new TestBeanFactory()); // bypass
+                                                                // default
+                                                                // property
+                                                                // lookup
+        BeanFactory factory = BeanKernel.getBeanFactory();
+        assertNotNull(factory);
+        assertEquals("hello", factory.find(String.class));
+    }
+
+}
diff --git a/support/test/org/wamblee/general/PairTest.java b/support/test/org/wamblee/general/PairTest.java
new file mode 100644 (file)
index 0000000..0d171a7
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005 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.general;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the pair class. 
+ */
+public class PairTest extends TestCase {
+
+    public void testPair() { 
+        Pair<Integer, String> pair = new Pair<Integer, String>(10, "hello"); 
+        assertEquals(new Integer(10), pair.getFirst());
+        assertEquals("hello", pair.getSecond());
+        
+        Pair<Integer, String> pair2 = new Pair<Integer, String>(pair);
+        assertEquals(new Integer(10), pair2.getFirst());
+        assertEquals("hello", pair2.getSecond());
+        
+        
+    }
+}
\ No newline at end of file
index c5f2c4b102b7220391b344c8ecce55ee121ab2cf..6e34da076c3ffa8374a698cd7bfe22f9f1f896e0 100644 (file)
  * 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.general;
 
-import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
-
 import junit.framework.TestCase;
 
 /**
- * Tests the spring bean factory. 
+ * Tests the spring bean factory.
  */
 public class SpringBeanFactoryTest extends TestCase {
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
+     * 
      * @see junit.framework.TestCase#setUp()
      */
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-       
+
     }
-    
-    public void testExistingBeanRefContext() { 
-        SpringBeanFactory factory = new SpringBeanFactory("org/wamblee/general/beanRefContext.xml", "test"); 
-     
+
+    public void testExistingBeanRefContext() {
+        SpringBeanFactory factory = new SpringBeanFactory(
+                "org/wamblee/general/beanRefContext.xml", "test");
+
         String value1 = factory.find(String.class);
         assertEquals("hello", value1);
-        String value2 = (String)factory.find("java.lang.String");
+        String value2 = (String) factory.find("java.lang.String");
         assertEquals("hello", value2);
         String value3 = factory.find("java.lang.String", String.class);
         assertEquals("hello", value3);
+
+        try {
+            factory.find("unknown");
+        } catch (BeanFactoryException e) {
+            return; // ok
+        }
+        fail();
+    }
+
+    public void testUnknownBeanFactory() {
+        try {
+            SpringBeanFactory factory = new SpringBeanFactory(
+                    "org/wamblee/general/beanRefContext.xml", "unknown");
+        } catch (BeanFactoryException e) {
+            return; // ok
+        }
+        fail();
     }
 }
diff --git a/support/test/org/wamblee/general/TestBeanFactory.java b/support/test/org/wamblee/general/TestBeanFactory.java
new file mode 100644 (file)
index 0000000..dbb7330
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2005 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.general;
+
+/**
+ * Test bean factory.
+ */
+public class TestBeanFactory extends SpringBeanFactory {
+
+    
+    public TestBeanFactory() { 
+        super("org/wamblee/general/beanRefContext.xml", "test");
+    }
+}