From 0100843f5f9de73d9a2409c0fa8dddc0419e9f7a Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 15 Jul 2010 13:49:11 +0000 Subject: [PATCH] --- .../org.wamblee.inject.InjectorFactory | 3 ++ .../wamblee/cdi/BeanManagerLookupTest.java | 44 +++++++++++++++ .../org/wamblee/cdi/BeanManagerSetup.java | 15 ++++++ .../wamblee/cdi/CdiInjectorFactoryTest.java | 54 +++++++++++++++++++ .../java/org/wamblee/cdi/CdiInjectorTest.java | 34 +++++++++--- .../cdi/InjectorFactoryBuilderTest.java | 46 ++++++++++++++++ .../org/wamblee/cdi/SimpleInjectorTest.java | 2 +- 7 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 support/cdi/src/main/resources/META-INF/services/org.wamblee.inject.InjectorFactory create mode 100644 support/cdi/src/test/java/org/wamblee/cdi/BeanManagerLookupTest.java create mode 100644 support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorFactoryTest.java create mode 100644 support/cdi/src/test/java/org/wamblee/cdi/InjectorFactoryBuilderTest.java diff --git a/support/cdi/src/main/resources/META-INF/services/org.wamblee.inject.InjectorFactory b/support/cdi/src/main/resources/META-INF/services/org.wamblee.inject.InjectorFactory new file mode 100644 index 00000000..99ad17c4 --- /dev/null +++ b/support/cdi/src/main/resources/META-INF/services/org.wamblee.inject.InjectorFactory @@ -0,0 +1,3 @@ + + +org.wamblee.cdi.CdiInjectorFactory diff --git a/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerLookupTest.java b/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerLookupTest.java new file mode 100644 index 00000000..8115ac49 --- /dev/null +++ b/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerLookupTest.java @@ -0,0 +1,44 @@ +/* + * 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.cdi; + +import static junit.framework.Assert.*; + +import javax.enterprise.inject.spi.BeanManager; +import javax.naming.InitialContext; + +import org.junit.Test; +import org.wamblee.support.jndi.StubInitialContextFactory; + +public class BeanManagerLookupTest { + + @Test + public void testLookupInJndi() throws Exception { + BeanManagerSetup setup = new BeanManagerSetup(); + setup.initialize(); + StubInitialContextFactory.register(); + InitialContext ctx = new InitialContext(); + ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager()); + try { + ctx = new InitialContext(); + BeanManager manager = (BeanManager) ctx.lookup(BeanManagerLookup.BEAN_MANAGER_JNDI); + assertSame(setup.getBeanManager(), manager); + } finally { + setup.shutdown(); + StubInitialContextFactory.unregister(); + } + } +} diff --git a/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerSetup.java b/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerSetup.java index 8ad67b87..cbf85bcf 100644 --- a/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerSetup.java +++ b/support/cdi/src/test/java/org/wamblee/cdi/BeanManagerSetup.java @@ -1,3 +1,18 @@ +/* + * 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.cdi; import javax.enterprise.inject.spi.BeanManager; diff --git a/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorFactoryTest.java b/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorFactoryTest.java new file mode 100644 index 00000000..76931da4 --- /dev/null +++ b/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorFactoryTest.java @@ -0,0 +1,54 @@ +/* + * 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.cdi; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.inject.Injector; +import org.wamblee.inject.InjectorFactory; + +import static junit.framework.TestCase.*; + +public class CdiInjectorFactoryTest { + + private BeanManagerSetup manager; + + @Before + public void setUp() { + manager = new BeanManagerSetup(); + manager.initialize(); + } + + @After + public void tearDown() { + manager.shutdown(); + } + + @Test + public void testGetInjector() { + InjectorFactory factory = new CdiInjectorFactory(manager.getBeanManager()); + Injector injector = factory.create(MyPojo.class); + MyPojo pojo = new MyPojo(); + injector.inject(pojo); + assertNotNull(pojo.getSingleton()); + } + + @Test(expected = IllegalArgumentException.class) + public void testBeanManagerNull() { + CdiInjectorFactory factory = new CdiInjectorFactory(null); + } +} diff --git a/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorTest.java b/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorTest.java index 53d7df91..21133fa7 100644 --- a/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorTest.java +++ b/support/cdi/src/test/java/org/wamblee/cdi/CdiInjectorTest.java @@ -17,23 +17,45 @@ package org.wamblee.cdi; import static junit.framework.Assert.*; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class CdiInjectorTest { - - @Test - public void testInject() { - fail(); + private BeanManagerSetup manager; + + @Before + public void setUp() { + manager = new BeanManagerSetup(); + manager.initialize(); + } + + @After + public void tearDown() { + manager.shutdown(); } @Test + public void testInject() { + CdiInjector injector = new CdiInjector(manager.getBeanManager(), MyPojo.class); + MyPojo pojo = new MyPojo(); + injector.inject(pojo); + assertNotNull(pojo.getSingleton()); + } + + @Test(expected = IllegalArgumentException.class) public void testWrongType() { - fail(); + CdiInjector injector = new CdiInjector(manager.getBeanManager(), String.class); + MyPojo pojo = new MyPojo(); + injector.inject(pojo); } @Test public void testNullObject() { - fail(); + CdiInjector injector = new CdiInjector(manager.getBeanManager(), MyPojo.class); + injector.inject(null); + + // ok should simply ignore. } } diff --git a/support/cdi/src/test/java/org/wamblee/cdi/InjectorFactoryBuilderTest.java b/support/cdi/src/test/java/org/wamblee/cdi/InjectorFactoryBuilderTest.java new file mode 100644 index 00000000..97c85bf1 --- /dev/null +++ b/support/cdi/src/test/java/org/wamblee/cdi/InjectorFactoryBuilderTest.java @@ -0,0 +1,46 @@ +/* + * 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.cdi; + +import static junit.framework.Assert.*; + +import javax.naming.InitialContext; + +import org.junit.Test; +import org.wamblee.inject.InjectorFactory; +import org.wamblee.inject.InjectorFactoryBuilder; +import org.wamblee.support.jndi.StubInitialContextFactory; + +public class InjectorFactoryBuilderTest { + + + + @Test + public void testCdiInjectorFactoryIsFound() throws Exception { + BeanManagerSetup setup = new BeanManagerSetup(); + setup.initialize(); + StubInitialContextFactory.register(); + InitialContext ctx = new InitialContext(); + ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager()); + try { + InjectorFactory factory = InjectorFactoryBuilder.getInjectorFactory(); + assertTrue(factory instanceof CdiInjectorFactory); + } finally { + setup.shutdown(); + StubInitialContextFactory.unregister(); + } + } +} diff --git a/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java b/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java index 829f5ceb..2a3b1242 100644 --- a/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java +++ b/support/cdi/src/test/java/org/wamblee/cdi/SimpleInjectorTest.java @@ -51,7 +51,7 @@ public class SimpleInjectorTest { @Test public void testGetSingleton() { MyPojo pojo = new MyPojo(); - SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); + SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory(BeanManagerLookup.lookup())); injector.inject(pojo); MySingleton obj = pojo.getSingleton(); -- 2.31.1