View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.test;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.kuali.rice.core.config.Config;
25  import org.kuali.rice.core.config.ConfigContext;
26  import org.kuali.rice.core.exception.RiceRuntimeException;
27  import org.kuali.rice.core.lifecycle.Lifecycle;
28  import org.kuali.rice.core.ojb.BaseOjbConfigurer;
29  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
30  import org.kuali.rice.core.resourceloader.ResourceLoader;
31  import org.kuali.rice.core.resourceloader.SpringResourceLoader;
32  import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
33  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
34  import org.kuali.rice.ksb.messaging.resourceloader.KSBResourceLoaderFactory;
35  import org.kuali.rice.ksb.server.TestClient1;
36  import org.kuali.rice.ksb.server.TestClient2;
37  import org.kuali.rice.ksb.service.KSBServiceLocator;
38  import org.kuali.rice.test.RiceTestCase;
39  import org.mortbay.jetty.webapp.WebAppClassLoader;
40  import org.springframework.context.ApplicationContext;
41  
42  
43  public abstract class KSBTestCase extends RiceTestCase {
44      
45      protected static final String MOCK_JAVA_SECURITY_MANAGEMENT_SERVICE_BEAN_ID = "testJavaSecurityManagementService";
46  
47      private TestClient1 testClient1;
48      private TestClient2 testClient2;
49      private ResourceLoader springContextResourceLoader;
50  
51      @Override
52      public void setUp() throws Exception {
53          // because we're stopping and starting so many times we need to clear
54          // the core before another set of RLs get put in the core. This is 
55          // because we are sometimes using the GRL to fetch a specific servers
56          // spring file out for testing purposes.
57          ConfigContext.destroy();
58          
59  		// Turn off http keep-alive. Repeated jetty start/stop using same port
60  		// results sockets held by client not to close properly, resulting in
61          // cxf test failures.
62  		System.setProperty("http.keepAlive", "false");
63  
64          super.setUp();
65          if (startClient1() || startClient2()) {
66              ((Runnable) KSBResourceLoaderFactory.getRemoteResourceLocator()).run();
67          }
68          // new SQLDataLoader("classpath:db/DefaultTestData.sql", ";").runSql();
69      }
70  
71      @Override
72      protected String getModuleName() {
73          return "ksb";
74      }
75      
76      protected List<String> getPerTestTablesNotToClear() {
77          return new ArrayList<String>();
78      }
79  
80      @Override
81      protected List<Lifecycle> getPerTestLifecycles() {
82          List<Lifecycle> lifecycles = super.getSuiteLifecycles();
83          if (this.disableJta()) {
84              System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, "RiceNoJtaOJB.properties");
85              this.springContextResourceLoader = new SpringResourceLoader(new QName("ksbtestharness"), "KSBTestHarnessNoJtaSpring.xml");
86          } else {
87              this.springContextResourceLoader = new SpringResourceLoader(new QName("ksbtestharness"), "KSBTestHarnessSpring.xml");
88          }
89  
90          lifecycles.add(this.springContextResourceLoader);
91          if (startClient1()) {
92              this.testClient1 = new TestClient1();
93              lifecycles.add(this.testClient1);
94          }
95          if (startClient2()) {
96              this.testClient2 = new TestClient2();
97              lifecycles.add(this.testClient2);
98          }
99          return lifecycles;
100     }
101 
102     public boolean startClient1() {
103         return false;
104     }
105 
106     public boolean startClient2() {
107         return false;
108     }
109 
110     public TestClient1 getTestClient1() {
111         return this.testClient1;
112     }
113 
114     public TestClient2 getTestClient2() {
115         return this.testClient2;
116     }
117 
118     public static boolean verifyServiceCallsViaBam(QName serviceName, String methodName, boolean serverInvocation) throws Exception {
119         BAMService bamService = KSBServiceLocator.getBAMService();
120         List<BAMTargetEntry> bamCalls = null;
121         if (methodName == null) {
122             bamCalls = bamService.getCallsForService(serviceName);
123         } else {
124             bamCalls = bamService.getCallsForService(serviceName, methodName);
125         }
126 
127         if (bamCalls.size() == 0) {
128             return false;
129         }
130         for (BAMTargetEntry bamEntry : bamCalls) {
131             if (bamEntry.getServerInvocation() && serverInvocation) {
132                 return true;
133             } else if (!serverInvocation) {
134                 return true;
135             }
136         }
137         return false;
138     }
139 
140     public static Object getServiceFromWebAppResourceLoader(String serviceName) {
141         for (Map.Entry<ClassLoader, Config> configEntry : ConfigContext.getConfigs()) {
142             if (configEntry.getKey() instanceof WebAppClassLoader) {
143                 ClassLoader old = Thread.currentThread().getContextClassLoader();
144                 // to make GRL select services from correct classloader
145                 Thread.currentThread().setContextClassLoader(configEntry.getKey());
146                 try {
147                     return GlobalResourceLoader.getService(serviceName);
148                 } finally {
149                     Thread.currentThread().setContextClassLoader(old);
150                 }
151             }
152         }
153         throw new RiceRuntimeException("Couldn't find service " + serviceName + " in WebApp Resource Loader");
154     }
155 
156     public static Object getServiceFromTestClient1SpringContext(String serviceName) {
157         for (Map.Entry<ClassLoader, Config> configEntry : ConfigContext.getConfigs()) {
158             if (configEntry.getKey() instanceof WebAppClassLoader) {
159                 ClassLoader old = Thread.currentThread().getContextClassLoader();
160                 // to make GRL select services from correct classloader
161                 Thread.currentThread().setContextClassLoader(configEntry.getKey());
162                 try {
163                     // TestClient1SpringContext found in web.xml of TestClient1
164                     ApplicationContext appContext = (ApplicationContext) ConfigContext.getCurrentContextConfig().getObject("TestClient1SpringContext");
165 
166                     return appContext.getBean(serviceName);
167                 } finally {
168                     Thread.currentThread().setContextClassLoader(old);
169                 }
170             }
171         }
172         throw new RiceRuntimeException("Couldn't find service " + serviceName + " in TestClient1 Spring Context");
173     }
174 
175     public ResourceLoader getSpringContextResourceLoader() {
176         return this.springContextResourceLoader;
177     }
178 
179     public void setSpringContextResourceLoader(ResourceLoader testHarnessResourceLoader) {
180         this.springContextResourceLoader = testHarnessResourceLoader;
181     }
182 
183     protected boolean disableJta() {
184         return false;
185     }
186 }