1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.kuali.student.common.test.spring; |
17 |
|
|
18 |
|
import java.lang.reflect.Field; |
19 |
|
import java.util.EventListener; |
20 |
|
import java.util.HashMap; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import javax.jws.WebService; |
24 |
|
import javax.servlet.Servlet; |
25 |
|
import javax.xml.namespace.QName; |
26 |
|
|
27 |
|
import org.junit.internal.runners.InitializationError; |
28 |
|
import org.junit.internal.runners.JUnit4ClassRunner; |
29 |
|
import org.junit.runner.notification.RunNotifier; |
30 |
|
import org.kuali.student.common.ws.beans.JaxWsClientFactory; |
31 |
|
import org.mortbay.jetty.Server; |
32 |
|
import org.mortbay.jetty.servlet.Context; |
33 |
|
import org.mortbay.jetty.servlet.ServletHandler; |
34 |
|
import org.mortbay.jetty.servlet.ServletHolder; |
35 |
|
import org.mortbay.jetty.servlet.ServletMapping; |
36 |
|
import org.springframework.web.context.ContextLoaderListener; |
37 |
|
|
|
|
| 89.7% |
Uncovered Elements: 14 (136) |
Complexity: 27 |
Complexity Density: 0.27 |
|
38 |
|
public class ServiceTestClassRunner extends JUnit4ClassRunner { |
39 |
|
private final Class<?> testImplClass; |
40 |
|
private Server server; |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
42 |
371
|
public ServiceTestClassRunner(Class<?> klass) throws InitializationError {... |
43 |
371
|
super(klass); |
44 |
371
|
testImplClass = klass; |
45 |
|
|
46 |
|
} |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@see |
56 |
|
|
|
|
| 81.6% |
Uncovered Elements: 7 (38) |
Complexity: 8 |
Complexity Density: 0.29 |
|
57 |
236
|
@Override... |
58 |
|
protected Object createTest() throws Exception { |
59 |
236
|
Object instance = super.createTest(); |
60 |
236
|
try { |
61 |
236
|
for (Field f : instance.getClass().getDeclaredFields()) { |
62 |
544
|
if (f.isAnnotationPresent(Client.class)) { |
63 |
236
|
Client a = f.getAnnotation(Client.class); |
64 |
236
|
String port = System.getProperty("ks.test.port"); |
65 |
|
|
66 |
236
|
Class<?> serviceImplClass = Class.forName(a.value()); |
67 |
236
|
String serviceName = ""; |
68 |
236
|
String serviceNamespaceURI = ""; |
69 |
236
|
String serviceWsdlLocation = "http://localhost:" + port |
70 |
236
|
+ "/Service/Service" + (a.secure() ? "Secure" : "") |
71 |
|
+ "?wsdl"; |
72 |
236
|
String serviceAddress = "http://localhost:" + port |
73 |
236
|
+ "/Service/Service" + (a.secure() ? "Secure" : ""); |
74 |
|
|
75 |
236
|
if (serviceImplClass.isAnnotationPresent(WebService.class)) { |
76 |
236
|
WebService wsAnnotation = serviceImplClass |
77 |
|
.getAnnotation(WebService.class); |
78 |
236
|
serviceName = wsAnnotation.serviceName(); |
79 |
236
|
serviceNamespaceURI = wsAnnotation.targetNamespace(); |
80 |
|
} |
81 |
|
|
82 |
236
|
Class<?> clientImplClass = Class |
83 |
|
.forName("org.kuali.student.common.ws.beans.JaxWsClientFactoryBean"); |
84 |
|
|
85 |
236
|
if (a.secure()) { |
86 |
0
|
try { |
87 |
0
|
clientImplClass = Class |
88 |
|
.forName("org.kuali.student.common.cxf.security.SecureJaxWsProxyFactoryBean"); |
89 |
|
} catch (ClassNotFoundException cnfe) { |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
236
|
JaxWsClientFactory clientFactory = (JaxWsClientFactory) clientImplClass |
94 |
|
.newInstance(); |
95 |
|
|
96 |
236
|
clientFactory.setServiceEndpointInterface(f.getType()); |
97 |
236
|
clientFactory.setServiceName(new QName(serviceNamespaceURI, |
98 |
|
serviceName)); |
99 |
236
|
clientFactory.setWsdlLocation(serviceWsdlLocation); |
100 |
236
|
clientFactory.setAddress(serviceAddress); |
101 |
|
|
102 |
236
|
f.setAccessible(true); |
103 |
236
|
f.set(instance, clientFactory.getObject()); |
104 |
|
} |
105 |
|
} |
106 |
|
} catch (Exception e) { |
107 |
0
|
throw new RuntimeException(e); |
108 |
|
} |
109 |
236
|
return instance; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
|
|
| 92.9% |
Uncovered Elements: 6 (85) |
Complexity: 15 |
Complexity Density: 0.23 |
|
116 |
15
|
private void startServer() {... |
117 |
15
|
try { |
118 |
|
|
119 |
15
|
if (System.getProperty("ks.test.default.port")==null||System.getProperty("ks.test.default.port").isEmpty()){ |
120 |
15
|
System.setProperty("ks.test.port", "9191"); |
121 |
|
} else { |
122 |
0
|
System.setProperty("ks.test.port", System.getProperty("ks.test.default.port")); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
15
|
for (Field f : testImplClass.getDeclaredFields()) { |
128 |
34
|
if (f.isAnnotationPresent(Client.class)) { |
129 |
15
|
Client a = f.getAnnotation(Client.class); |
130 |
15
|
if (a.secure()) { |
131 |
0
|
System.setProperty("ks.test.serviceImplSecure", a |
132 |
|
.value()); |
133 |
|
} else { |
134 |
15
|
System.setProperty("ks.test.serviceImplClass", a |
135 |
|
.value()); |
136 |
|
} |
137 |
15
|
if(a.port()!=null&&!a.port().isEmpty()){ |
138 |
1
|
System.setProperty("ks.test.port", a.port()); |
139 |
|
} |
140 |
15
|
if(!"".equals(a.additionalContextFile())){ |
141 |
13
|
System.setProperty("ks.test.additionalContextFile", a.additionalContextFile()); |
142 |
|
}else{ |
143 |
2
|
System.setProperty("ks.test.additionalContextFile", "classpath:*noSuchContextFile.xml"); |
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
15
|
if (System.getProperty("ks.test.serviceImplSecure") == null) { |
151 |
13
|
System.setProperty("ks.test.serviceImplSecure", System |
152 |
|
.getProperty("ks.test.serviceImplClass")); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
15
|
if (testImplClass |
157 |
|
.isAnnotationPresent(PersistenceFileLocation.class)) { |
158 |
12
|
PersistenceFileLocation a = testImplClass |
159 |
|
.getAnnotation(PersistenceFileLocation.class); |
160 |
12
|
System.setProperty("ks.test.persistenceLocation", a.value()); |
161 |
|
} else { |
162 |
3
|
System.setProperty("ks.test.persistenceLocation", |
163 |
|
"classpath:META-INF/persistence.xml"); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
15
|
Daos daos = testImplClass.getAnnotation(Daos.class); |
168 |
|
|
169 |
15
|
String daoImpls = ""; |
170 |
15
|
if (daos != null) { |
171 |
12
|
int i = 1; |
172 |
12
|
for (Dao dao : daos.value()) { |
173 |
14
|
daoImpls += dao.value() + "|" + dao.testDataFile() +"|" + dao.testSqlFile() ; |
174 |
14
|
if (i < daos.value().length) { |
175 |
2
|
daoImpls += ","; |
176 |
|
} |
177 |
14
|
i++; |
178 |
|
} |
179 |
|
} |
180 |
15
|
System.setProperty("ks.test.daoImplClasses", daoImpls); |
181 |
|
|
182 |
15
|
server = new Server(Integer.valueOf(System |
183 |
|
.getProperty("ks.test.port"))); |
184 |
|
|
185 |
15
|
Context context = new Context(); |
186 |
15
|
ServletHandler servletHandler = new ServletHandler(); |
187 |
15
|
ServletHolder servletHolder = new ServletHolder(); |
188 |
15
|
ServletMapping servletMapping = new ServletMapping(); |
189 |
15
|
ContextLoaderListener contextLoaderListener = new ContextLoaderListener(); |
190 |
|
|
191 |
|
|
192 |
15
|
Class<?> servletClass; |
193 |
15
|
String wsEngine; |
194 |
15
|
try{ |
195 |
15
|
servletClass = Class.forName("org.apache.cxf.transport.servlet.CXFServlet"); |
196 |
15
|
wsEngine = "cxf"; |
197 |
|
} catch (ClassNotFoundException e) { |
198 |
0
|
servletClass = Class.forName("com.sun.xml.ws.transport.http.servlet.WSSpringServlet"); |
199 |
0
|
wsEngine = "jaxws"; |
200 |
|
} |
201 |
|
|
202 |
15
|
Servlet servlet = (Servlet) servletClass.newInstance(); |
203 |
|
|
204 |
15
|
servletHolder.setName("Service"); |
205 |
15
|
servletHolder.setServlet(servlet); |
206 |
15
|
servletHandler.setServlets(new ServletHolder[] { servletHolder }); |
207 |
|
|
208 |
15
|
servletMapping.setPathSpec("/*"); |
209 |
15
|
servletMapping.setServletName("Service"); |
210 |
15
|
servletHandler |
211 |
|
.setServletMappings(new ServletMapping[] { servletMapping }); |
212 |
|
|
213 |
15
|
context.setContextPath("/Service"); |
214 |
15
|
context.setResourceBase("src/test/resources"); |
215 |
|
|
216 |
15
|
Map<String, String> initParams = new HashMap<String, String>(); |
217 |
|
|
218 |
|
|
219 |
15
|
String contextConfigLocation = "classpath:META-INF/" + wsEngine + "-context.xml"; |
220 |
15
|
if(!"".equals(daoImpls)){ |
221 |
|
|
222 |
12
|
contextConfigLocation +="\nclasspath:META-INF/default-dao-context-test.xml"; |
223 |
|
} |
224 |
|
|
225 |
15
|
initParams.put("contextConfigLocation", contextConfigLocation); |
226 |
15
|
initParams.put("log4jConfigLocation", "log4j.properties"); |
227 |
15
|
context.setInitParams(initParams); |
228 |
15
|
context |
229 |
|
.setEventListeners(new EventListener[] { contextLoaderListener }); |
230 |
15
|
context.setServletHandler(servletHandler); |
231 |
|
|
232 |
15
|
server.setHandler(context); |
233 |
|
|
234 |
15
|
server.start(); |
235 |
|
} catch (Exception e) { |
236 |
2
|
throw new RuntimeException(e); |
237 |
|
} |
238 |
|
|
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
@see |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
246 |
15
|
@Override... |
247 |
|
public void run(RunNotifier notifier) { |
248 |
15
|
startServer(); |
249 |
13
|
super.run(notifier); |
250 |
13
|
stopServer(); |
251 |
|
} |
252 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
253 |
13
|
private void stopServer() {... |
254 |
13
|
try { |
255 |
13
|
server.stop(); |
256 |
|
} catch (Exception e) { |
257 |
0
|
throw new RuntimeException(e); |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
|
} |