001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.bus.test;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.kuali.bus.services.KSBServiceLocator;
026: import org.kuali.rice.config.Config;
027: import org.kuali.rice.core.Core;
028: import org.kuali.rice.exceptions.RiceRuntimeException;
029: import org.kuali.rice.lifecycle.Lifecycle;
030: import org.kuali.rice.resourceloader.GlobalResourceLoader;
031: import org.kuali.rice.resourceloader.ResourceLoader;
032: import org.kuali.rice.resourceloader.SpringResourceLoader;
033: import org.kuali.rice.test.RiceTestCase;
034: import org.mortbay.jetty.webapp.WebAppClassLoader;
035: import org.springframework.context.ApplicationContext;
036:
037: import edu.iu.uis.eden.messaging.bam.BAMService;
038: import edu.iu.uis.eden.messaging.bam.BAMTargetEntry;
039: import edu.iu.uis.eden.messaging.resourceloading.KSBResourceLoaderFactory;
040: import edu.iu.uis.eden.server.TestClient1;
041: import edu.iu.uis.eden.server.TestClient2;
042:
043: public class KSBTestCase extends RiceTestCase {
044:
045: private TestClient1 testClient1;
046: private TestClient2 testClient2;
047: private ResourceLoader springContextResourceLoader;
048:
049: @Override
050: public void setUp() throws Exception {
051: // because we're stopping and starting so many times we need to clear the core before
052: // another set of RLs get put in the core. This is because we are sometimes using
053: // the GRL to fetch a specific servers spring file out for testing purposes.
054: Core.destroy();
055: super .setUp();
056: if (startClient1() || startClient2()) {
057: ((Runnable) KSBResourceLoaderFactory
058: .getRemoteResourceLocator()).run();
059: }
060: // new SQLDataLoader("classpath:db/DefaultTestData.sql", ";").runSql();
061: }
062:
063: @Override
064: protected List<String> getConfigLocations() {
065: return Arrays
066: .asList(new String[] { "classpath:META-INF/ksb-test-config.xml" });
067: }
068:
069: @Override
070: protected String getDerbySQLFileLocation() {
071: return "classpath:db/derby/bus.sql";
072: }
073:
074: @Override
075: protected String getModuleName() {
076: return "ksb";
077: }
078:
079: @Override
080: protected List<String> getTablesToClear() {
081: List<String> tables = new ArrayList<String>();
082: tables.add("EN_MSG_QUE_T");
083: tables.add("EN_MSG_PAYLOAD_T");
084: tables.add("EN_BAM_T");
085: tables.add("EN_BAM_PARAM_T");
086: tables.add("EN_SERVICE_DEF_DUEX_T");
087: return tables;
088: }
089:
090: @Override
091: protected List<Lifecycle> getPerTestLifecycles() {
092: List<Lifecycle> lifecycles = super .getPerTestLifecycles();
093: this .springContextResourceLoader = new SpringResourceLoader(
094: new QName("ksbtestharness"), "KSBTestHarnessSpring.xml");
095: lifecycles.add(this .springContextResourceLoader);
096: if (startClient1()) {
097: this .testClient1 = new TestClient1();
098: lifecycles.add(this .testClient1);
099: }
100: if (startClient2()) {
101: this .testClient2 = new TestClient2();
102: lifecycles.add(this .testClient2);
103: }
104: return lifecycles;
105: }
106:
107: public boolean startClient1() {
108: return false;
109: }
110:
111: public boolean startClient2() {
112: return false;
113: }
114:
115: public TestClient1 getTestClient1() {
116: return this .testClient1;
117: }
118:
119: public TestClient2 getTestClient2() {
120: return this .testClient2;
121: }
122:
123: public static boolean verifyServiceCallsViaBam(QName serviceName,
124: String methodName, boolean serverInvocation)
125: throws Exception {
126: BAMService bamService = KSBServiceLocator.getBAMService();
127: List<BAMTargetEntry> bamCalls = null;
128: if (methodName == null) {
129: bamCalls = bamService.getCallsForService(serviceName);
130: } else {
131: bamCalls = bamService.getCallsForService(serviceName,
132: methodName);
133: }
134:
135: if (bamCalls.size() == 0) {
136: return false;
137: }
138: for (BAMTargetEntry bamEntry : bamCalls) {
139: if (bamEntry.getServerInvocation() && serverInvocation) {
140: return true;
141: } else if (!serverInvocation) {
142: return true;
143: }
144: }
145: return false;
146: }
147:
148: public static Object getServiceFromWebAppResourceLoader(
149: String serviceName) {
150: Map<ClassLoader, Config> configs = Core.getCONFIGS();
151: for (Map.Entry<ClassLoader, Config> configEntry : configs
152: .entrySet()) {
153: if (configEntry.getKey() instanceof WebAppClassLoader) {
154: ClassLoader old = Thread.currentThread()
155: .getContextClassLoader();
156: // to make GRL select services from correct classloader
157: Thread.currentThread().setContextClassLoader(
158: configEntry.getKey());
159: try {
160: return GlobalResourceLoader.getService(serviceName);
161: } finally {
162: Thread.currentThread().setContextClassLoader(old);
163: }
164: }
165: }
166: throw new RiceRuntimeException("Couldn't find service "
167: + serviceName + " in WebApp Resource Loader");
168: }
169:
170: public static Object getServiceFromTestClient1SpringContext(
171: String serviceName) {
172: Map<ClassLoader, Config> configs = Core.getCONFIGS();
173: for (Map.Entry<ClassLoader, Config> configEntry : configs
174: .entrySet()) {
175: if (configEntry.getKey() instanceof WebAppClassLoader) {
176: ClassLoader old = Thread.currentThread()
177: .getContextClassLoader();
178: // to make GRL select services from correct classloader
179: Thread.currentThread().setContextClassLoader(
180: configEntry.getKey());
181: try {
182: // TestClient1SpringContext found in web.xml of TestClient1
183: ApplicationContext appContext = (ApplicationContext) Core
184: .getCurrentContextConfig().getObject(
185: "TestClient1SpringContext");
186:
187: return appContext.getBean(serviceName);
188: } finally {
189: Thread.currentThread().setContextClassLoader(old);
190: }
191: }
192: }
193: throw new RiceRuntimeException("Couldn't find service "
194: + serviceName + " in TestClient1 Spring Context");
195: }
196:
197: public ResourceLoader getSpringContextResourceLoader() {
198: return this .springContextResourceLoader;
199: }
200:
201: public void setSpringContextResourceLoader(
202: ResourceLoader testHarnessResourceLoader) {
203: this.springContextResourceLoader = testHarnessResourceLoader;
204: }
205:
206: }
|