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.ksb.messaging;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.apache.commons.httpclient.HttpClient;
26  import org.apache.commons.httpclient.HttpConnectionManager;
27  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
28  import org.apache.commons.httpclient.SimpleHttpConnectionManager;
29  import org.apache.commons.httpclient.cookie.CookiePolicy;
30  import org.junit.Test;
31  import org.kuali.rice.core.api.config.property.Config;
32  import org.kuali.rice.core.api.config.property.ConfigContext;
33  import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
34  import org.kuali.rice.core.framework.config.property.SimpleConfig;
35  import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
36  import org.kuali.rice.ksb.api.bus.support.JavaServiceDefinition;
37  import org.kuali.rice.ksb.messaging.serviceconnectors.HttpInvokerConnector;
38  
39  /**
40   * A test which tests the RemoteResourceServiceLocatorImpl class.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class HttpInvokerConnectorConfigurationTest {
45  
46  	private static String simpleConfig = "SIMPLE_CONFIG";
47  	private static String jaxbConfig = "JAXB_CONFIG";
48  	
49  	// We want to test with both impls
50  	protected Config getConfigObject(String configType){
51  		Config cRet = null;
52  		if(simpleConfig.equals(configType)){
53  			cRet = new SimpleConfig();
54  		}else if(jaxbConfig.equals(configType)){
55  			cRet = new JAXBConfigImpl();
56  		}
57  		return cRet;
58  	}
59  	
60  	/**
61  	 * Tests the configuration and initialization of the HttpClient which is
62  	 * used for the invocation of remote service calls.
63  	 */
64  	@Test public void testHttpClientConfiguration() throws Exception {
65  		testHttpClientConfigurationImpl(simpleConfig);
66  		testHttpClientConfigurationImpl(jaxbConfig);
67  	}
68  	protected void testHttpClientConfigurationImpl(String configType) throws Exception {
69  		
70  		JavaServiceDefinition javaServiceDefinition = new JavaServiceDefinition();
71  		javaServiceDefinition.setServiceName(new QName("test", "test"));
72  		JavaServiceConfiguration configuration = JavaServiceConfiguration.fromServiceDefinition(javaServiceDefinition);
73  		
74  		// test the default configuration
75  		ConfigContext.init(getConfigObject(configType));
76  		HttpInvokerConnector httpConnector = new HttpInvokerConnector(configuration, null);
77  		HttpClient httpClient = httpConnector.getHttpClient();
78  		
79  		HttpConnectionManager connectionManager = httpClient.getHttpConnectionManager();
80  		assertTrue("Should be a multi-threaded connection manager.", connectionManager instanceof MultiThreadedHttpConnectionManager);
81  		int defaultMaxConnectionsPerHost = connectionManager.getParams().getDefaultMaxConnectionsPerHost();
82  		assertEquals(20, defaultMaxConnectionsPerHost);
83  		assertEquals(20, connectionManager.getParams().getMaxTotalConnections());
84  		assertEquals(10000, connectionManager.getParams().getConnectionTimeout());
85  		assertEquals(10000, httpClient.getParams().getConnectionManagerTimeout());
86  		assertEquals(CookiePolicy.RFC_2109, httpClient.getParams().getCookiePolicy());
87  		
88  		// re-init the core with some of these paramters changed
89  		Config config = getConfigObject(configType);		
90  		config.putProperty("http.connection-manager.max-total", "500");
91  		config.putProperty("http.connection-manager.timeout", "5000");
92  		config.putProperty("http.connection.timeout", "15000");
93  		config.putProperty("http.somerandomproperty", "thisismyproperty");
94  		config.putProperty("http.authentication.preemptive", "false");
95  		ConfigContext.init(config);
96  		
97  		httpConnector = new HttpInvokerConnector(configuration, null);
98  		httpClient = httpConnector.getHttpClient();
99  		
100 		connectionManager = httpClient.getHttpConnectionManager();
101 		assertTrue("Should be a multi-threaded connection manager.", connectionManager instanceof MultiThreadedHttpConnectionManager);
102 		defaultMaxConnectionsPerHost = connectionManager.getParams().getDefaultMaxConnectionsPerHost();
103 		assertEquals(20, defaultMaxConnectionsPerHost);
104 		assertEquals(500, connectionManager.getParams().getMaxTotalConnections());
105 		assertEquals(15000, connectionManager.getParams().getConnectionTimeout());
106 		assertEquals(5000, httpClient.getParams().getConnectionManagerTimeout());
107 		assertEquals(CookiePolicy.RFC_2109, httpClient.getParams().getCookiePolicy());
108 		assertFalse(httpClient.getParams().isAuthenticationPreemptive());
109 		
110 		// do another one that checks that booleans are working properly
111 		config = getConfigObject(configType);		
112 		config.putProperty("http.authentication.preemptive", "true");
113 		ConfigContext.init(config);
114 		
115 		httpConnector = new HttpInvokerConnector(configuration, null);
116 		httpClient = httpConnector.getHttpClient();
117 		
118 		assertTrue(httpClient.getParams().isAuthenticationPreemptive());
119 		
120 		// check setting the classname of the connection manager
121 		config = getConfigObject(configType);		
122 		config.putProperty("http.connection-manager.class", MyHttpConnectionManager.class.getName());
123 		ConfigContext.init(config);
124 		
125 		httpConnector = new HttpInvokerConnector(configuration, null);
126 		httpClient = httpConnector.getHttpClient();
127 		
128 		connectionManager = httpClient.getHttpConnectionManager();
129 		assertTrue("Should be a MyHttpConnectionManager.", connectionManager instanceof MyHttpConnectionManager);
130 		
131 		// now try setting the retry handler which expects an object that we can't pipe through our
132 		// String-based configuration.  This is an illegal parameter to configure and we should
133 		// recieve a WorkflowRuntimeException
134 		config = getConfigObject(configType);		
135 		config.putProperty("http.method.retry-handler", "badparm");
136 		ConfigContext.init(config);
137 		
138 		try {
139 			httpConnector = new HttpInvokerConnector(configuration, null);
140 			fail("An exception should have been thrown because the retry handler is an illegal parameter.");
141 		} catch (Exception e) {
142 		    //nothing to do here
143 		}
144 		
145 	}
146 	
147 	public static class MyHttpConnectionManager extends SimpleHttpConnectionManager {
148 	    // nothing extra needed
149 	}
150 	
151 }