From: erik Date: Tue, 28 Mar 2006 13:37:10 +0000 (+0000) Subject: (no commit message) X-Git-Tag: BEFORE_MAVEN_MIGRATION~121 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=baec7e77039ed838bab52671931830f1623d9fc4;p=utils --- 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 index 00000000..e54de19d --- /dev/null +++ b/support/resources/test/org/wamblee/general/beankernel-wrong.properties @@ -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 index 00000000..e08cf398 --- /dev/null +++ b/support/resources/test/org/wamblee/general/beankernel.properties @@ -0,0 +1,2 @@ + +org.wamblee.beanfactory.class=org.wamblee.general.TestBeanFactory diff --git a/support/src/org/wamblee/general/BeanKernel.java b/support/src/org/wamblee/general/BeanKernel.java index 5f32f687..d05eef3d 100644 --- a/support/src/org/wamblee/general/BeanKernel.java +++ b/support/src/org/wamblee/general/BeanKernel.java @@ -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(); diff --git a/support/src/org/wamblee/general/SpringBeanFactory.java b/support/src/org/wamblee/general/SpringBeanFactory.java index 91ac1c3f..27a544b1 100644 --- a/support/src/org/wamblee/general/SpringBeanFactory.java +++ b/support/src/org/wamblee/general/SpringBeanFactory.java @@ -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 index 00000000..4a79f0ae --- /dev/null +++ b/support/test/org/wamblee/general/BeanKernelTest.java @@ -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 index 00000000..0d171a75 --- /dev/null +++ b/support/test/org/wamblee/general/PairTest.java @@ -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 pair = new Pair(10, "hello"); + assertEquals(new Integer(10), pair.getFirst()); + assertEquals("hello", pair.getSecond()); + + Pair pair2 = new Pair(pair); + assertEquals(new Integer(10), pair2.getFirst()); + assertEquals("hello", pair2.getSecond()); + + + } +} \ No newline at end of file diff --git a/support/test/org/wamblee/general/SpringBeanFactoryTest.java b/support/test/org/wamblee/general/SpringBeanFactoryTest.java index c5f2c4b1..6e34da07 100644 --- a/support/test/org/wamblee/general/SpringBeanFactoryTest.java +++ b/support/test/org/wamblee/general/SpringBeanFactoryTest.java @@ -12,36 +12,54 @@ * 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 index 00000000..dbb73309 --- /dev/null +++ b/support/test/org/wamblee/general/TestBeanFactory.java @@ -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"); + } +}