View Javadoc

1   /**
2    * Copyright 2005-2011 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.kew.clientapp;
17  
18  import java.security.AccessController;
19  import java.security.PrivilegedAction;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.apache.log4j.Logger;
24  import org.junit.Ignore;
25  import org.kuali.rice.core.api.config.property.Config;
26  import org.kuali.rice.core.api.config.property.ConfigContext;
27  import org.kuali.rice.core.api.exception.RiceRuntimeException;
28  import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
29  import org.kuali.rice.core.impl.resourceloader.SpringResourceLoader;
30  import org.kuali.rice.kew.test.KEWTestCase;
31  
32  /**
33   * 
34   * Test case that confirms functionality of the KEW thin client.  All configuration for
35   * the thin client is external, so this test should be helpful as a model for setting
36   * up thin client mode. 
37   * 
38   * One potentially confusing element here is the use of a ClassLoader, but all it is really
39   * there for is to allow us to bind the thin client configuration in the ConfigContext while
40   * at the same time running a rice instance with its own config.
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  @Ignore(value="KULRICE-5061")
46  public class ThinClientTest extends KEWTestCase {
47  	private static final Logger LOG = Logger.getLogger(ThinClientTest.class);
48  
49  	/**
50  	 * configuration files used in this test 
51  	 */
52  	private static final String CONFIG_FILE = "classpath:/org/kuali/rice/kew/clientapp/thin-client-app-config.xml";
53  	private static final String SPRING_BEANS_FILE = "classpath:/org/kuali/rice/kew/clientapp/ThinClientSpringBeans.xml";
54  
55  	private static final String KIM_PRINCIPAL_NAME = "testuser1";
56  
57  	private static SpringResourceLoader thinClientResourceLoader = new SpringResourceLoader(new QName("ThinClientTest"),
58  			SPRING_BEANS_FILE, null);
59  	
60  	private ClassLoader ourClassLoader = null;
61  	private ClassLoader parentClassLoader = null;
62  	
63  	public ThinClientTest() {
64  	}
65  
66  	@Override
67  	public void setUp() throws Exception {
68  		super.setUp();
69  
70  		// put our own ClassLoader in place
71  		ourClassLoader = AccessController.doPrivileged(new DelegatingClassLoaderCreater());
72  
73  		parentClassLoader = Thread.currentThread().getContextClassLoader();
74  		Thread.currentThread().setContextClassLoader(ourClassLoader);
75  
76  		try {
77  			//Config config = new SimpleConfig(CONFIG_FILE);
78  			Config config = new JAXBConfigImpl(CONFIG_FILE);
79  			config.parseConfig();
80  			// initialize our config using properties bound to our custom classloader
81  			ConfigContext.init(ourClassLoader, config);
82  
83  			thinClientResourceLoader.start();
84  
85  		} catch (Exception e) {
86  			LOG.error("Failed to set up thin client test", e);
87  			throw new RiceRuntimeException(e);
88  		}
89  	}
90  
91  	@Override
92  	public void tearDown() throws Exception {
93  		// put the original ClassLoader back for the current thread
94  		Thread.currentThread().setContextClassLoader(parentClassLoader);
95  		// remove the reference to our config from the ConfigContext map
96  		ConfigContext.overrideConfig(ourClassLoader, null);
97  		
98  		// I can't imagine this is necessary
99  		ourClassLoader = null;
100 		thinClientResourceLoader = null;
101 
102 		super.tearDown();
103 	}
104 
105 	// commented out pending addressing KULRICE-5061
106 //	@Test
107 //	public void testThinClientServices() throws Exception {
108 //		//verify the ThinClientResourceLoader is in the GRL.
109 //		ResourceLoader rl = GlobalResourceLoader.getResourceLoader();
110 //		List<ResourceLoader> resourceLoaders = rl.getResourceLoaders();
111 //		ResourceLoader tempThinRL = rl.getResourceLoaders().get(0);
112 //		assertTrue("First resource loader should be thin", tempThinRL instanceof ThinClientResourceLoader);
113 //		ThinClientResourceLoader thinRL = (ThinClientResourceLoader)tempThinRL;
114 //
115 //		// test KIM identity service
116 //		Principal principal = thinRL.getIdentityService().getPrincipalByPrincipalName(KIM_PRINCIPAL_NAME);
117 //		assertTrue(principal.getPrincipalName().equals(KIM_PRINCIPAL_NAME));
118 //
119 //		// test KIM group service
120 //		List<Group> groups = thinRL.getGroupService().getGroupsByPrincipalId(principal.getPrincipalId());
121 //		assertNotNull(groups);
122 //		assertTrue(groups.size() > 0);
123 //
124 //		// test Workflow
125 //		RouteHeaderDTO routeHeader = new RouteHeaderDTO();
126 //		routeHeader.setDocTypeName("RiceDocument");
127 //		routeHeader = thinRL.getWorkflowDocument().createDocument(principal.getPrincipalId(), routeHeader);
128 //		assertTrue(principal.getPrincipalId().equals(routeHeader.getInitiatorPrincipalId()));
129 //	}
130 
131 	/**
132 	 * a privileged action to create a new classloader with our thread's classloader as its parent 
133 	 * 
134 	 * @author Kuali Rice Team (rice.collab@kuali.org)
135 	 *
136 	 */
137 	private class DelegatingClassLoaderCreater implements PrivilegedAction<ClassLoader> {
138 		public ClassLoader run() {
139 			return new DelegatingClassLoader(Thread.currentThread().getContextClassLoader());
140 		};
141 	}
142 
143 	/**
144 	 * a very minimal classloader to allow us to bind our thin client cofiguration 
145 	 * 
146 	 * @author Kuali Rice Team (rice.collab@kuali.org)
147 	 *
148 	 */
149 	private class DelegatingClassLoader extends ClassLoader {
150 		
151 		// silly default constructor so that Surefire doesn't get messed up
152 		// TODO: remove when http://jira.codehaus.org/browse/SUREFIRE-535 is fixed
153 		public DelegatingClassLoader() { }
154 
155 		public DelegatingClassLoader(ClassLoader parent) {
156 			super(parent);
157 		}
158 	}
159 
160 }
161