Coverage Report - org.kuali.student.common.test.spring.ServiceTestClassRunner
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceTestClassRunner
0%
0/109
0%
0/40
6.6
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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  
 public class ServiceTestClassRunner extends JUnit4ClassRunner {
 39  
         private final Class<?> testImplClass;
 40  
         private Server server;
 41  
 
 42  
         public ServiceTestClassRunner(Class<?> klass) throws InitializationError {
 43  0
                 super(klass);
 44  0
                 testImplClass = klass;
 45  
 
 46  0
         }
 47  
 
 48  
         /**
 49  
          * Creates a client and injects it into the test class based on the
 50  
          * &#064;Client annotations
 51  
          */
 52  
         /*
 53  
          * (non-Javadoc)
 54  
          * 
 55  
          * @see org.junit.internal.runners.JUnit4ClassRunner#createTest()
 56  
          */
 57  
         @Override
 58  
         protected Object createTest() throws Exception {
 59  0
                 Object instance = super.createTest();
 60  
                 try {
 61  0
                         for (Field f : instance.getClass().getDeclaredFields()) {
 62  0
                                 if (f.isAnnotationPresent(Client.class)) {
 63  0
                                         Client a = f.getAnnotation(Client.class);
 64  0
                                         String port = System.getProperty("ks.test.port");
 65  
                                         
 66  0
                                         Class<?> serviceImplClass = Class.forName(a.value());
 67  0
                                         String serviceName = "";
 68  0
                                         String serviceNamespaceURI = "";
 69  0
                                         String serviceWsdlLocation = "http://localhost:" + port
 70  
                                                         + "/Service/Service" + (a.secure() ? "Secure" : "")
 71  
                                                         + "?wsdl";
 72  0
                                         String serviceAddress = "http://localhost:" + port
 73  
                                                         + "/Service/Service" + (a.secure() ? "Secure" : "");
 74  
 
 75  0
                                         if (serviceImplClass.isAnnotationPresent(WebService.class)) {
 76  0
                                                 WebService wsAnnotation = serviceImplClass
 77  
                                                                 .getAnnotation(WebService.class);
 78  0
                                                 serviceName = wsAnnotation.serviceName();
 79  0
                                                 serviceNamespaceURI = wsAnnotation.targetNamespace();
 80  
                                         }
 81  
 
 82  0
                                         Class<?> clientImplClass = Class
 83  
                                                         .forName("org.kuali.student.common.ws.beans.JaxWsClientFactoryBean");
 84  
 
 85  0
                                         if (a.secure()) {
 86  
                                                 try {
 87  0
                                                         clientImplClass = Class
 88  
                                                                         .forName("org.kuali.student.common.cxf.security.SecureJaxWsProxyFactoryBean");
 89  0
                                                 } catch (ClassNotFoundException cnfe) {
 90  0
                                                 }
 91  
                                         }
 92  
 
 93  0
                                         JaxWsClientFactory clientFactory = (JaxWsClientFactory) clientImplClass
 94  
                                                         .newInstance();
 95  
 
 96  0
                                         clientFactory.setServiceEndpointInterface(f.getType());
 97  0
                                         clientFactory.setServiceName(new QName(serviceNamespaceURI,
 98  
                                                         serviceName));
 99  0
                                         clientFactory.setWsdlLocation(serviceWsdlLocation);
 100  0
                                         clientFactory.setAddress(serviceAddress);
 101  
 
 102  0
                                         f.setAccessible(true);
 103  0
                                         f.set(instance, clientFactory.getObject());
 104  
                                 }
 105  
                         }
 106  0
                 } catch (Exception e) {
 107  0
                         throw new RuntimeException(e);
 108  0
                 }
 109  0
                 return instance;
 110  
         }
 111  
 
 112  
         /**
 113  
          * Start a server based on the service Impl &#064;WebService annotaions and
 114  
          * the test class &#064;
 115  
          */
 116  
         private void startServer() {
 117  
                 try {
 118  
                         // Set the default Port
 119  0
                         if (System.getProperty("ks.test.default.port")==null||System.getProperty("ks.test.default.port").isEmpty()){
 120  0
                                 System.setProperty("ks.test.port", "9191");
 121  
                         } else {
 122  0
                                 System.setProperty("ks.test.port", System.getProperty("ks.test.default.port"));
 123  
                         }
 124  
 
 125  
                         // Grab the client annotation and set the service implementation and
 126  
                         // port as system properties
 127  0
                         for (Field f : testImplClass.getDeclaredFields()) {
 128  0
                                 if (f.isAnnotationPresent(Client.class)) {
 129  0
                                         Client a = f.getAnnotation(Client.class);
 130  0
                                         if (a.secure()) {
 131  0
                                                 System.setProperty("ks.test.serviceImplSecure", a
 132  
                                                                 .value());
 133  
                                         } else {
 134  0
                                                 System.setProperty("ks.test.serviceImplClass", a
 135  
                                                                 .value());
 136  
                                         }
 137  0
                                         if(a.port()!=null&&!a.port().isEmpty()){
 138  0
                                                 System.setProperty("ks.test.port", a.port());
 139  
                                         }
 140  0
                                         if(!"".equals(a.additionalContextFile())){
 141  0
                                                 System.setProperty("ks.test.additionalContextFile", a.additionalContextFile());
 142  
                                         }else{
 143  0
                                                 System.setProperty("ks.test.additionalContextFile", "classpath:*noSuchContextFile.xml");
 144  
                                         }
 145  
                                 }
 146  
                         }
 147  
 
 148  
                         // If no secure client defined, set secure service endpoint impl
 149  
                         // to be same as non-secure endpoint impl
 150  0
                         if (System.getProperty("ks.test.serviceImplSecure") == null) {
 151  0
                                 System.setProperty("ks.test.serviceImplSecure", System
 152  
                                                 .getProperty("ks.test.serviceImplClass"));
 153  
                         }
 154  
 
 155  
                         // Grab the persistence context loacation or set a default value
 156  0
                         if (testImplClass
 157  
                                         .isAnnotationPresent(PersistenceFileLocation.class)) {
 158  0
                                 PersistenceFileLocation a = testImplClass
 159  
                                                 .getAnnotation(PersistenceFileLocation.class);
 160  0
                                 System.setProperty("ks.test.persistenceLocation", a.value());
 161  0
                         } else {
 162  0
                                 System.setProperty("ks.test.persistenceLocation",
 163  
                                                 "classpath:META-INF/persistence.xml");
 164  
                         }
 165  
 
 166  
                         // Grab the Dao information and pass it to a System variable
 167  0
                         Daos daos = testImplClass.getAnnotation(Daos.class);
 168  
 
 169  0
                         String daoImpls = "";
 170  0
                         if (daos != null) {
 171  0
                                 int i = 1;
 172  0
                                 for (Dao dao : daos.value()) {
 173  0
                                         daoImpls += dao.value() + "|" + dao.testDataFile() +"|" + dao.testSqlFile()  ;
 174  0
                                         if (i < daos.value().length) {
 175  0
                                                 daoImpls += ",";
 176  
                                         }
 177  0
                                         i++;
 178  
                                 }
 179  
                         }
 180  0
                         System.setProperty("ks.test.daoImplClasses", daoImpls);
 181  
                         
 182  0
                         server = new Server(Integer.valueOf(System
 183  
                                         .getProperty("ks.test.port")));
 184  
 
 185  0
                         Context context = new Context();
 186  0
                         ServletHandler servletHandler = new ServletHandler();
 187  0
                         ServletHolder servletHolder = new ServletHolder();
 188  0
                         ServletMapping servletMapping = new ServletMapping();
 189  0
                         ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
 190  
 
 191  
                         
 192  
             Class<?> servletClass;
 193  
                         String wsEngine;
 194  
                         try{
 195  0
                             servletClass = Class.forName("org.apache.cxf.transport.servlet.CXFServlet");                    
 196  0
                             wsEngine = "cxf";
 197  0
                         } catch (ClassNotFoundException e) {
 198  0
                     servletClass = Class.forName("com.sun.xml.ws.transport.http.servlet.WSSpringServlet");
 199  0
                                 wsEngine = "jaxws";
 200  0
                         }
 201  
 
 202  0
                         Servlet servlet = (Servlet) servletClass.newInstance();
 203  
 
 204  0
                         servletHolder.setName("Service");
 205  0
                         servletHolder.setServlet(servlet);
 206  0
                         servletHandler.setServlets(new ServletHolder[] { servletHolder });
 207  
 
 208  0
                         servletMapping.setPathSpec("/*");
 209  0
                         servletMapping.setServletName("Service");
 210  0
                         servletHandler
 211  
                                         .setServletMappings(new ServletMapping[] { servletMapping });
 212  
 
 213  0
                         context.setContextPath("/Service");
 214  0
                         context.setResourceBase("src/test/resources");
 215  
 
 216  0
                         Map<String, String> initParams = new HashMap<String, String>();
 217  
                         
 218  
                         //Set the context config location
 219  0
                         String contextConfigLocation = "classpath:META-INF/" + wsEngine + "-context.xml";
 220  0
                         if(!"".equals(daoImpls)){
 221  
                                 //If there are Daos defined, add the dao context file
 222  0
                                 contextConfigLocation +="\nclasspath:META-INF/default-dao-context-test.xml";
 223  
                         }
 224  
                         
 225  0
                         initParams.put("contextConfigLocation", contextConfigLocation);
 226  0
                         initParams.put("log4jConfigLocation", "log4j.properties");
 227  0
                         context.setInitParams(initParams);
 228  0
                         context
 229  
                                         .setEventListeners(new EventListener[] { contextLoaderListener });
 230  0
                         context.setServletHandler(servletHandler);
 231  
 
 232  0
                         server.setHandler(context);
 233  
 
 234  0
                         server.start();
 235  0
                 } catch (Exception e) {
 236  0
                         throw new RuntimeException(e);
 237  0
                 }
 238  
 
 239  0
         }
 240  
 
 241  
         /*
 242  
          * (non-Javadoc)
 243  
          * 
 244  
          * @see org.junit.internal.runners.JUnit4ClassRunner#run(org.junit.runner.notification.RunNotifier)
 245  
          */
 246  
         @Override
 247  
         public void run(RunNotifier notifier) {
 248  0
                 startServer();
 249  0
                 super.run(notifier);
 250  0
                 stopServer();
 251  0
         }
 252  
 
 253  
         private void stopServer() {
 254  
                 try {
 255  0
                         server.stop();
 256  0
                 } catch (Exception e) {
 257  0
                         throw new RuntimeException(e);
 258  0
                 }
 259  0
         }
 260  
 
 261  
 }