--- /dev/null
+
+org.wamblee.beanfactory.class=org.wamblee.general.TestBeanFactoryBla
--- /dev/null
+
+org.wamblee.beanfactory.class=org.wamblee.general.TestBeanFactory
public static BeanFactory getBeanFactory() {
synchronized (BeanFactory.class) {
if (BEAN_FACTORY == null) {
- BEAN_FACTORY = lookupBeanFactory();
+ BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
}
}
return BEAN_FACTORY;
*
* @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();
/**
* 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);
+ }
}
/*
--- /dev/null
+/*
+ * 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));
+ }
+
+}
--- /dev/null
+/*
+ * 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
* 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();
}
}
--- /dev/null
+/*
+ * 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");
+ }
+}