From 7be7bd6e12444ee126f8f00a890a3adb06d6f7d7 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 22 Jun 2010 20:37:43 +0000 Subject: [PATCH] --- .../java/org/wamblee/ioc/BeanKernelTest.java | 87 +++++++++++++++++++ .../test/java/org/wamblee/ioc/PairTest.java | 35 ++++++++ .../java/org/wamblee/ioc/TestBeanFactory.java | 38 ++++++++ 3 files changed, 160 insertions(+) create mode 100644 support/general/src/test/java/org/wamblee/ioc/BeanKernelTest.java create mode 100644 support/general/src/test/java/org/wamblee/ioc/PairTest.java create mode 100644 support/general/src/test/java/org/wamblee/ioc/TestBeanFactory.java diff --git a/support/general/src/test/java/org/wamblee/ioc/BeanKernelTest.java b/support/general/src/test/java/org/wamblee/ioc/BeanKernelTest.java new file mode 100644 index 00000000..73b232c7 --- /dev/null +++ b/support/general/src/test/java/org/wamblee/ioc/BeanKernelTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2005-2010 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.ioc; + + +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. + * + * @author Erik Brakkee + */ +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 { + 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 { + 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/general/src/test/java/org/wamblee/ioc/PairTest.java b/support/general/src/test/java/org/wamblee/ioc/PairTest.java new file mode 100644 index 00000000..17de2672 --- /dev/null +++ b/support/general/src/test/java/org/wamblee/ioc/PairTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2005-2010 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.ioc; + +import org.wamblee.general.Pair; + +import junit.framework.TestCase; + +/** + * Tests the pair class. + */ +public class PairTest extends TestCase { + public void testPair() { + Pair pair = new Pair(10, "hello"); + assertEquals(Integer.valueOf(10), pair.getFirst()); + assertEquals("hello", pair.getSecond()); + + Pair pair2 = new Pair(pair); + assertEquals(Integer.valueOf(10), pair2.getFirst()); + assertEquals("hello", pair2.getSecond()); + } +} diff --git a/support/general/src/test/java/org/wamblee/ioc/TestBeanFactory.java b/support/general/src/test/java/org/wamblee/ioc/TestBeanFactory.java new file mode 100644 index 00000000..8d20c26a --- /dev/null +++ b/support/general/src/test/java/org/wamblee/ioc/TestBeanFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright 2005-2010 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.ioc; + + +public class TestBeanFactory implements BeanFactory { + @Override + public Object find(String aId) { + return null; + } + + @Override + public T find(Class aClass) { + if (aClass.equals(String.class)) { + return (T) "hello"; + } + + return null; + } + + @Override + public T find(String aId, Class aClass) { + return null; + } +} -- 2.31.1