1   /*
2    * Copyright 2008 Simon Martinelli, Rebenweg 32, 3236 Gampelen, Switzerland
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.simject.test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.fail;
21  
22  import java.lang.reflect.Proxy;
23  import java.util.logging.Logger;
24  
25  import javax.persistence.EntityManager;
26  
27  import org.junit.Test;
28  import org.simject.SimFactory;
29  import org.simject.test.dummy.TestClass;
30  import org.simject.test.dummy.TestImpl;
31  import org.simject.test.dummy.TestInterface;
32  import org.simject.test.dummy.TestNotFoundInterface;
33  import org.simject.test.dummy.TestRemoteInterface;
34  
35  /**
36   * JUnit tests
37   * 
38   * @author Simon Martinelli
39   */
40  public class SimFactoryTest {
41  
42  	/**
43  	 * Name of the resources xml file
44  	 */
45  	private static final String RESOURCES_XML = "test_resources.xml";
46  
47  	private static final Logger logger = Logger.getLogger(SimFactoryTest.class
48  			.toString());
49  
50  	/**
51  	 * Positive test using a class
52  	 * 
53  	 * @throws Exception
54  	 */
55  	@Test
56  	public void testSimFactory() {
57  
58  		final SimFactory factory = new SimFactory(RESOURCES_XML);
59  		final TestClass testClass = factory.getResource(TestClass.class);
60  
61  		logger.info(testClass.toString());
62  
63  		assertNotNull("Class not found", testClass);
64  	}
65  
66  	/**
67  	 * Positiv test using an interface
68  	 * 
69  	 * @throws Exception
70  	 */
71  	@Test
72  	public void testSimFactoryWithInterface() {
73  
74  		final SimFactory factory = new SimFactory(RESOURCES_XML);
75  		final TestInterface testInterface = factory
76  				.getResource(TestInterface.class);
77  
78  		logger.info(testInterface.toString());
79  
80  		assertNotNull("Interface not found", testInterface);
81  	}
82  
83  	/**
84  	 * Negativ test with wrong class
85  	 * 
86  	 * @throws Exception
87  	 */
88  	@Test
89  	public void testSimFactoryClassNotFound() {
90  
91  		try {
92  			final SimFactory factory = new SimFactory(RESOURCES_XML);
93  			final TestNotFoundInterface tnfi = factory
94  					.getResource(TestNotFoundInterface.class);
95  			tnfi.getClass();
96  
97  			fail();
98  		} catch (Exception e) {
99  			assertEquals(
100 					"Exception not as expected",
101 					"Resource of type org.simject.test.dummy.TestNotFoundInterface not found",
102 					e.getMessage());
103 		}
104 	}
105 
106 	/**
107 	 * Negativ test with wrong config file name
108 	 * 
109 	 * @throws Exception
110 	 */
111 	@Test
112 	public void testSimFactoryConfigFileNotFound() {
113 
114 		try {
115 			final SimFactory factory = new SimFactory("abc");
116 			final TestNotFoundInterface tnfi = factory
117 					.getResource(TestNotFoundInterface.class);
118 			tnfi.getClass();
119 
120 			fail();
121 		} catch (Exception e) {
122 			assertEquals("Exception not as expected", "META-INF/abc not found",
123 					e.getMessage());
124 		}
125 	}
126 
127 	/**
128 	 * General DI test
129 	 * 
130 	 * @throws Exception
131 	 */
132 	@Test
133 	public void testDependencyInjection() {
134 
135 		final SimFactory factory = new SimFactory(RESOURCES_XML);
136 		final TestImpl testInterfaceImpl = (TestImpl) factory
137 				.getResource(TestInterface.class);
138 
139 		assertNotNull("Implementation not found", testInterfaceImpl);
140 	}
141 
142 	/**
143 	 * Test instantiation of HttpClientProxy
144 	 */
145 	@Test
146 	public void testHttpClientProxy() {
147 		final SimFactory factory = new SimFactory(RESOURCES_XML);
148 		final Object testRemoteInterface = factory
149 				.getResource(TestRemoteInterface.class);
150 
151 		assertNotNull("Implementation not found", testRemoteInterface);
152 		if (testRemoteInterface instanceof Proxy) {
153 			// ok
154 		} else {
155 			fail("Proxy expected");
156 		}
157 	}
158 
159 	/**
160 	 * Test instantiation of EntityManager
161 	 */
162 	@Test
163 	public void testEntityManager() {
164 		final SimFactory factory = new SimFactory(RESOURCES_XML);
165 		final EntityManager entityManager = factory
166 				.getResource(EntityManager.class);
167 
168 		assertNotNull("EntityManager not found", entityManager);
169 	}
170 }