View Javadoc

1   package org.kuali.student.core.test;
2   
3   import java.util.Map;
4   
5   import org.junit.After;
6   import org.junit.AfterClass;
7   import org.junit.Assert;
8   import org.junit.Before;
9   import org.junit.BeforeClass;
10  import org.kuali.rice.core.config.Config;
11  import org.kuali.rice.core.config.ConfigContext;
12  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
13  import org.kuali.rice.core.resourceloader.ResourceLoader;
14  import org.kuali.rice.core.web.jetty.JettyServer;
15  import org.kuali.rice.test.TransactionalLifecycle;
16  import org.mortbay.jetty.webapp.WebAppClassLoader;
17  
18  /**
19   * A base test class which adds the following capabilities to the test harness:
20   * 
21   * <li>
22   *   <ol>Automatically starts an embedded Jetty server which loads the application.</ol>
23   *   <ol>Starts a transaction before the test begins and rolls the transaction back after 
24   *       the test completes so that nothing is written to the database.</ol>
25   * </li>
26   * 
27   * @author Eric Westfall
28   */
29  public class BaseCase extends Assert {
30  
31  	private static final int DEFAULT_PORT = 8090;
32  	private static final String CONTEXT_NAME = "/ks-test-dev";
33  	private static final String WEBAPP_ROOT = "/src/main/webapp";
34  	
35  	private static JettyServer server;
36  	
37  	private TransactionalLifecycle transactionalLifecycle;
38  	
39  	@Before
40  	public void setUp() throws Exception {
41  		transactionalLifecycle = new TransactionalLifecycle();
42  		transactionalLifecycle.start();
43  	}
44  	
45  	@After
46  	public void tearDown() throws Exception {
47  		try {
48  			if (transactionalLifecycle != null) {
49  				transactionalLifecycle.stop();
50  			}
51  		} finally {
52  			transactionalLifecycle = null;
53  		}
54  	}
55  	
56  	@BeforeClass
57  	public static void startJettyServer() throws Exception {
58  	    System.setProperty("kew.bootstrap.spring.file", "SpringBeans.xml");
59  	    
60  		BaseCase.server = new JettyServer(DEFAULT_PORT, CONTEXT_NAME, WEBAPP_ROOT);
61  		server.setFailOnContextFailure(true);
62  		server.setTestMode(true);
63  		server.start();
64  		// establish the GlobalResourceLoader and ConfigContext for the classloader of the test harness
65  		addWebappsToContext();
66  	}
67  	
68      /**
69       * Adds all ResourceLoaders registered to WebAppClassLoaders to the GlobalResourceLoader.
70       * Overrides the current context config with the Config registered to the (last) WebAppClassLoader
71       */
72      public static void addWebappsToContext() {
73          for (Map.Entry<ClassLoader, Config> configEntry : ConfigContext.getConfigs()) {
74              if (configEntry.getKey() instanceof WebAppClassLoader) {
75                  ResourceLoader rl = GlobalResourceLoader.getResourceLoader(configEntry.getKey());
76                  if (rl == null) {
77                      Assert.fail("didn't find resource loader for workflow test harness web app");
78                  }
79                  ConfigContext.overrideConfig(Thread.currentThread().getContextClassLoader(), configEntry.getValue());
80                  GlobalResourceLoader.addResourceLoader(rl);
81              }
82          }
83      }
84  	
85  	@AfterClass
86  	public static void stopJettyServer() throws Exception {
87  		try {
88  			if (server != null) {
89  				server.stop();
90  			}
91  		} finally {
92  			server = null;
93  		}
94  	}
95  	
96  }