--- /dev/null
+
+
+org.wamblee.cdi.CdiInjectorFactory
--- /dev/null
+/*
+ * 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();
+ }
+ }
+}
+/*
+ * 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;
--- /dev/null
+/*
+ * 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);
+ }
+}
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.
}
}
--- /dev/null
+/*
+ * 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();
+ }
+ }
+}
@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();