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