| 1 |
/* |
| 2 |
* Copyright 2002-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 |
| 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 test.properties; |
| 17 |
|
| 18 |
import junit.framework.TestCase; |
| 19 |
import org.apache.axis.client.Call; |
| 20 |
import org.apache.axis.client.Service; |
| 21 |
import org.apache.axis.configuration.BasicClientConfig; |
| 22 |
import org.apache.axis.configuration.BasicServerConfig; |
| 23 |
import org.apache.axis.configuration.SimpleProvider; |
| 24 |
import org.apache.axis.handlers.soap.SOAPService; |
| 25 |
import org.apache.axis.providers.java.RPCProvider; |
| 26 |
import org.apache.axis.server.AxisServer; |
| 27 |
import org.apache.axis.transport.local.LocalTransport; |
| 28 |
|
| 29 |
/** |
| 30 |
* Test scoped properties. This test confirms that MessageContext.getProperty |
| 31 |
* will correctly defer to a higher-level property scope (the Call on the |
| 32 |
* client side, the SOAPService on the server side) to obtain values for |
| 33 |
* properties that are not explicitly set in the MessageContext itself. |
| 34 |
* |
| 35 |
* @author Glen Daniels ([email protected]) |
| 36 |
*/ |
| 37 |
public class TestScopedProperties extends TestCase { |
| 38 |
public static final String PROP_NAME = "test.property"; |
| 39 |
public static final String CLIENT_VALUE = "client-side property value!"; |
| 40 |
public static final String SERVER_VALUE = "this is the server side value"; |
| 41 |
public static final String OVERRIDE_NAME = "override.property"; |
| 42 |
public static final String OVERRIDE_VALUE = "The REAL value!"; |
| 43 |
|
| 44 |
private SOAPService service; |
| 45 |
private PropertyHandler serverHandler = new PropertyHandler(); |
| 46 |
private SimpleProvider config; |
| 47 |
private AxisServer server; |
| 48 |
|
| 49 |
public TestScopedProperties(String s) { |
| 50 |
super(s); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Sets up the server side for this test. We deploy a service with |
| 55 |
* a PropertyHandler as a request handler, and PropertyHandler as the |
| 56 |
* backend class as well. We set an option on the service to a well- |
| 57 |
* known value (PROP_NAME -> PROP_VALUE), and also set an option on |
| 58 |
* the service (OVERRIDE_NAME) which we expect to be overriden by |
| 59 |
* an explicit setting in the MessageContext. |
| 60 |
*/ |
| 61 |
protected void setUp() throws Exception { |
| 62 |
config = new BasicServerConfig(); |
| 63 |
server = new AxisServer(config); |
| 64 |
|
| 65 |
// Deploy a service which contains an option that we expect to be |
| 66 |
// available by asking the MessageContext in the service method (see |
| 67 |
// PropertyHandler.java). |
| 68 |
|
| 69 |
RPCProvider provider = new RPCProvider(); |
| 70 |
service = new SOAPService(serverHandler, provider, null); |
| 71 |
service.setOption("className", PropertyHandler.class.getName()); |
| 72 |
service.setOption("allowedMethods", "*"); |
| 73 |
|
| 74 |
// Here's the interesting property. |
| 75 |
service.setOption(PROP_NAME, SERVER_VALUE); |
| 76 |
|
| 77 |
// Also set a property which we expect to be overriden by an explicit |
| 78 |
// value in the MessageContext (see PropertyHandler.invoke()). We |
| 79 |
// should never see this value. |
| 80 |
service.setOption(OVERRIDE_NAME, SERVER_VALUE); |
| 81 |
|
| 82 |
config.deployService("service", service); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Basic scoped properties test. Set up a client side service with a |
| 87 |
* PropertyHandler as the request handler, then set a property on the |
| 88 |
* Call which we expect to be available when the request handler queries |
| 89 |
* the MessageContext. Call the backend service, and make sure the |
| 90 |
* client handler, the server handler, and the result all agree on what |
| 91 |
* the values should be. |
| 92 |
*/ |
| 93 |
public void testScopedProperties() throws Exception { |
| 94 |
BasicClientConfig config = new BasicClientConfig(); |
| 95 |
PropertyHandler clientHandler = new PropertyHandler(); |
| 96 |
SOAPService clientService = new SOAPService(clientHandler, null, null); |
| 97 |
config.deployService("service", clientService); |
| 98 |
|
| 99 |
Service s = new Service(config); |
| 100 |
Call call = new Call(s); |
| 101 |
|
| 102 |
// Set a property on the Call which we expect to be available via |
| 103 |
// the MessageContext in the client-side handler. |
| 104 |
call.setProperty(PROP_NAME, CLIENT_VALUE); |
| 105 |
|
| 106 |
LocalTransport transport = new LocalTransport(server); |
| 107 |
transport.setRemoteService("service"); |
| 108 |
call.setTransport(transport); |
| 109 |
|
| 110 |
// Make the call. |
| 111 |
String result = (String)call.invoke("service", |
| 112 |
"testScopedProperty", |
| 113 |
new Object [] { }); |
| 114 |
|
| 115 |
assertEquals("Returned scoped property wasn't correct", |
| 116 |
SERVER_VALUE, |
| 117 |
result); |
| 118 |
|
| 119 |
// Confirm that both the client and server side properties were |
| 120 |
// correctly read. |
| 121 |
assertEquals("Client-side scoped property wasn't correct", |
| 122 |
CLIENT_VALUE, |
| 123 |
clientHandler.getPropVal()); |
| 124 |
assertEquals("Server-side scoped property wasn't correct", |
| 125 |
SERVER_VALUE, |
| 126 |
serverHandler.getPropVal()); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Test overriding a property that's set in the service with an explicit |
| 131 |
* setting in the MessageContext. The server-side handler will set the |
| 132 |
* OVERRIDDE_NAME property, and the "testOverrideProperty" method will |
| 133 |
* return the value it sees, which should match. |
| 134 |
*/ |
| 135 |
public void testMessageContextOverride() throws Exception { |
| 136 |
// Only the server side matters on this one, so don't bother with |
| 137 |
// special client config. |
| 138 |
Call call = new Call(new Service()); |
| 139 |
|
| 140 |
LocalTransport transport = new LocalTransport(server); |
| 141 |
transport.setRemoteService("service"); |
| 142 |
call.setTransport(transport); |
| 143 |
|
| 144 |
// Make the call. |
| 145 |
String result = (String)call.invoke("service", |
| 146 |
"testOverrideProperty", |
| 147 |
new Object [] { }); |
| 148 |
assertEquals("Overriden property value didn't match", |
| 149 |
OVERRIDE_VALUE, |
| 150 |
result); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Test of three-level client scopes (MC -> service -> Call). |
| 155 |
* |
| 156 |
* Set a property on the Call, then try the invocation. The client-side |
| 157 |
* handler should see the Call value. Then set the same property to a |
| 158 |
* different value in the client-side service object, and confirm that |
| 159 |
* when we invoke again we see the new value. |
| 160 |
*/ |
| 161 |
public void testFullClientScopes() throws Exception { |
| 162 |
Call call = new Call(new Service()); |
| 163 |
PropertyHandler clientHandler = new PropertyHandler(); |
| 164 |
SOAPService clientService = new SOAPService(clientHandler, null, null); |
| 165 |
|
| 166 |
call.setSOAPService(clientService); |
| 167 |
|
| 168 |
// Set a property on the Call which we expect to be available via |
| 169 |
// the MessageContext in the client-side handler. |
| 170 |
call.setProperty(PROP_NAME, CLIENT_VALUE); |
| 171 |
|
| 172 |
LocalTransport transport = new LocalTransport(server); |
| 173 |
transport.setRemoteService("service"); |
| 174 |
call.setTransport(transport); |
| 175 |
|
| 176 |
// First call should get the value from the Call object. |
| 177 |
call.invoke("testOverrideProperty", new Object [] { }); |
| 178 |
assertEquals("Client-side scoped property from Call wasn't correct", |
| 179 |
CLIENT_VALUE, |
| 180 |
clientHandler.getPropVal()); |
| 181 |
|
| 182 |
// Now set the same option on the client service, which should |
| 183 |
// take precedence over the value in the Call. |
| 184 |
clientService.setOption(PROP_NAME, OVERRIDE_VALUE); |
| 185 |
|
| 186 |
// Second call should now get the value from the client service. |
| 187 |
call.invoke("testOverrideProperty", new Object [] { }); |
| 188 |
assertEquals("Client-side scoped property from service wasn't correct", |
| 189 |
OVERRIDE_VALUE, |
| 190 |
clientHandler.getPropVal()); |
| 191 |
} |
| 192 |
} |