001: package org.objectweb.celtix.systest.ws.rm;
002:
003: import java.net.URL;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.xml.namespace.QName;
010: import javax.xml.soap.SOAPElement;
011: import javax.xml.soap.SOAPMessage;
012: import javax.xml.ws.BindingProvider;
013: import javax.xml.ws.Endpoint;
014: import javax.xml.ws.handler.Handler;
015: import javax.xml.ws.handler.LogicalMessageContext;
016:
017: import junit.framework.Test;
018: import junit.framework.TestSuite;
019:
020: import org.objectweb.celtix.Bus;
021: import org.objectweb.celtix.BusException;
022: import org.objectweb.celtix.bindings.AbstractBindingImpl;
023: import org.objectweb.celtix.bus.busimpl.BusConfigurationBuilder;
024: import org.objectweb.celtix.bus.configuration.wsrm.StoreInitParamType;
025: import org.objectweb.celtix.bus.configuration.wsrm.StoreType;
026: import org.objectweb.celtix.bus.ws.rm.Names;
027: import org.objectweb.celtix.bus.ws.rm.RMHandler;
028: import org.objectweb.celtix.bus.ws.rm.persistence.jdbc.RMTxStore;
029: import org.objectweb.celtix.configuration.Configuration;
030: import org.objectweb.celtix.configuration.ConfigurationBuilder;
031: import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
032: import org.objectweb.celtix.greeter_control.Greeter;
033: import org.objectweb.celtix.greeter_control.GreeterService;
034: import org.objectweb.celtix.systest.common.ClientServerSetupBase;
035: import org.objectweb.celtix.systest.common.ClientServerTestBase;
036: import org.objectweb.celtix.systest.common.TestServerBase;
037: import org.objectweb.celtix.ws.rm.persistence.RMDestinationSequence;
038: import org.objectweb.celtix.ws.rm.persistence.RMMessage;
039: import org.objectweb.celtix.ws.rm.persistence.RMSourceSequence;
040:
041: /**
042: * Tests Reliable Messaging.
043: */
044: public class PersistenceTest extends ClientServerTestBase {
045:
046: private static final String APP_NAMESPACE = "http://celtix.objectweb.org/greeter_control";
047: private static final String GREETMEONEWAY_ACTION = APP_NAMESPACE
048: + "/types/Greeter/greetMeOneWay";
049:
050: private static final QName SERVICE_NAME = new QName(APP_NAMESPACE,
051: "GreeterService");
052: private static final QName PORT_NAME = new QName(APP_NAMESPACE,
053: "GreeterPort");
054:
055: private Bus bus;
056: private MessageFlow mf;
057: private GreeterService greeterService;
058: private Greeter greeter;
059: private String cfgFileProperty;
060:
061: public static void main(String[] args) {
062: junit.textui.TestRunner.run(PersistenceTest.class);
063: }
064:
065: static class PersistenceTestServer extends TestServerBase {
066:
067: protected void run() {
068:
069: ControlImpl.setConfigFileProperty("oneway-client-crash");
070: ConfigurationBuilder builder = ConfigurationBuilderFactory
071: .getBuilder();
072: builder.buildConfiguration(
073: BusConfigurationBuilder.BUS_CONFIGURATION_URI,
074: "celtix");
075:
076: GreeterImpl implementor = new GreeterImpl();
077: String address = "http://localhost:9000/SoapContext/GreeterPort";
078: Endpoint.publish(address, implementor);
079:
080: }
081:
082: public static void main(String[] args) {
083: try {
084: PersistenceTestServer s = new PersistenceTestServer();
085: s.start();
086: } catch (Exception ex) {
087: ex.printStackTrace();
088: System.exit(-1);
089: } finally {
090: System.out.println("done!");
091: }
092: }
093: }
094:
095: public static Test suite() throws Exception {
096: TestSuite suite = new TestSuite(PersistenceTest.class);
097: return new ClientServerSetupBase(suite) {
098: public void startServers() throws Exception {
099: // special case handling for WS-Addressing system test to avoid
100: // UUID related issue when server is run as separate process
101: // via maven on Win2k
102: assertTrue("server did not launch correctly",
103: launchServer(PersistenceTestServer.class,
104: "Windows 2000".equals(System
105: .getProperty("os.name"))));
106: }
107:
108: public void setUp() throws Exception {
109:
110: RMTxStore.deleteDatabaseFiles("rmpersist", true);
111:
112: URL url = getClass().getResource(
113: "oneway-client-crash.xml");
114: assertNotNull("cannot find test resource", url);
115: configFileName = url.toString();
116: ConfigurationBuilder builder = ConfigurationBuilderFactory
117: .getBuilder();
118: builder.buildConfiguration(
119: BusConfigurationBuilder.BUS_CONFIGURATION_URI,
120: "celtix");
121:
122: super .setUp();
123:
124: }
125:
126: public void tearDown() {
127: RMTxStore.deleteDatabaseFiles("rmpersist", false);
128: }
129: };
130: }
131:
132: public void setUp() throws BusException {
133:
134: cfgFileProperty = System.getProperty("celtix.config.file");
135: URL url = getClass().getResource("oneway-client-crash.xml");
136: assertNotNull("cannot find test resource", url);
137: String configFileName = url.toString();
138: System.setProperty("celtix.config.file", configFileName);
139:
140: bus = Bus.init();
141:
142: URL wsdl = getClass().getResource("/wsdl/greeter_control.wsdl");
143: greeterService = new GreeterService(wsdl, SERVICE_NAME);
144:
145: greeter = greeterService.getPort(PORT_NAME, Greeter.class);
146:
147: BindingProvider provider = (BindingProvider) greeter;
148: AbstractBindingImpl abi = (AbstractBindingImpl) provider
149: .getBinding();
150: List<Handler> handlerChain = abi.getHandlerChain();
151: assertTrue(handlerChain.size() > 0);
152:
153: List<SOAPMessage> outboundMessages = null;
154: List<LogicalMessageContext> inboundContexts = null;
155:
156: boolean found = false;
157: for (Handler h : handlerChain) {
158: if (!found && h instanceof SOAPMessageRecorder) {
159: SOAPMessageRecorder recorder = (SOAPMessageRecorder) h;
160: outboundMessages = recorder.getOutboundMessages();
161: outboundMessages.clear();
162: found = true;
163: break;
164: }
165: }
166: assertTrue(
167: "Could not find SOAPMessageRecorder in handler chain",
168: found);
169: handlerChain = abi.getPreLogicalSystemHandlers();
170: assertTrue(handlerChain.size() > 0);
171: found = false;
172: for (Handler h : handlerChain) {
173: if (!found && h instanceof LogicalMessageContextRecorder) {
174: LogicalMessageContextRecorder recorder = (LogicalMessageContextRecorder) h;
175: inboundContexts = recorder.getInboundContexts();
176: inboundContexts.clear();
177: found = true;
178: break;
179: }
180: }
181: assertTrue(
182: "Could not find LogicalMessageContextRecorder in pre logical system handler chain",
183: found);
184:
185: mf = new MessageFlow(outboundMessages, inboundContexts);
186:
187: }
188:
189: public void tearDown() throws BusException {
190: RMHandler rmh = getRMHandler();
191: rmh.destroy();
192:
193: greeter = null;
194: bus.shutdown(true);
195:
196: if (null == cfgFileProperty) {
197: System.clearProperty("celtix.config.file");
198: } else {
199: System.setProperty("celtix.config.file", cfgFileProperty);
200: }
201: }
202:
203: public void testPopulateStore() throws Exception {
204:
205: greeter.greetMeOneWay("one");
206: greeter.greetMeOneWay("two");
207: greeter.greetMeOneWay("three");
208:
209: mf.verifyMessages(4, true);
210: String[] expectedActions = new String[] {
211: Names.WSRM_CREATE_SEQUENCE_ACTION,
212: GREETMEONEWAY_ACTION, GREETMEONEWAY_ACTION,
213: GREETMEONEWAY_ACTION };
214: mf.verifyActions(expectedActions, true);
215: mf.verifyMessageNumbers(new String[] { null, "1", "2", "3" },
216: true);
217:
218: mf.verifyMessages(5, false);
219: expectedActions = new String[] { null,
220: Names.WSRM_CREATE_SEQUENCE_RESPONSE_ACTION, null, null,
221: null };
222: mf.verifyActions(expectedActions, false);
223: mf.verifyMessageNumbers(new String[5], false);
224: boolean[] expectedAcks = new boolean[5];
225: mf.verifyAcknowledgements(expectedAcks, false);
226:
227: RMHandler rmh = getRMHandler();
228: assertNotNull(rmh);
229:
230: RMTxStore store = getStore(rmh);
231: assertNotNull(store);
232:
233: Collection<RMDestinationSequence> dss = store
234: .getDestinationSequences(rmh.getConfiguration()
235: .getParent().getId().toString());
236: assertEquals(1, dss.size());
237:
238: Collection<RMSourceSequence> sss = store.getSourceSequences(rmh
239: .getConfiguration().getParent().getId().toString());
240: assertEquals(1, sss.size());
241:
242: Collection<RMMessage> msgs = store.getMessages(sss.iterator()
243: .next().getIdentifier(), true);
244: assertEquals(3, msgs.size());
245: }
246:
247: public void testRecover() throws Exception {
248:
249: // do nothing - resends should happen in the background
250:
251: try {
252: Thread.sleep(1000 * 10);
253: } catch (InterruptedException ex) {
254: // ignore
255: }
256:
257: int n = mf.getOutboundMessages().size();
258: assertTrue(n > 0);
259: String[] expectedActions = new String[n];
260: for (int i = 0; i < n; i++) {
261: expectedActions[i] = GREETMEONEWAY_ACTION;
262: }
263: mf.verifyActions(expectedActions, true);
264: for (int i = 0; i < n; i++) {
265: SOAPElement elem = mf.getSequence(mf.getOutboundMessages()
266: .get(i));
267: int mn = Integer.parseInt(mf.getMessageNumber(elem));
268: assertTrue(mn >= 1 && mn <= 3);
269: }
270:
271: }
272:
273: private RMHandler getRMHandler() {
274: BindingProvider provider = (BindingProvider) greeter;
275: AbstractBindingImpl abi = (AbstractBindingImpl) provider
276: .getBinding();
277: List<Handler> handlerChain = abi.getPreLogicalSystemHandlers();
278: assertTrue(handlerChain.size() > 0);
279: for (Handler h : handlerChain) {
280: if (h instanceof RMHandler) {
281: return (RMHandler) h;
282: }
283: }
284: return null;
285: }
286:
287: private RMTxStore getStore(RMHandler rmh) {
288: Configuration cfg = rmh.getConfiguration();
289: StoreType s = cfg.getObject(StoreType.class, "store");
290: Map<String, String> params = new HashMap<String, String>();
291: for (StoreInitParamType p : s.getInitParam()) {
292: String name = p.getParamName();
293: String value = p.getParamValue();
294: params.put(name, value);
295: }
296: RMTxStore store = new RMTxStore();
297: store.init(params);
298: return store;
299: }
300:
301: }
|