001: package org.objectweb.celtix.bus.ws.rm;
002:
003: import java.io.IOException;
004: import java.math.BigInteger;
005: import java.util.ArrayList;
006: import java.util.Collection;
007:
008: import junit.framework.TestCase;
009:
010: import org.easymock.classextension.EasyMock;
011: import org.easymock.classextension.IMocksControl;
012: import org.objectweb.celtix.Bus;
013: import org.objectweb.celtix.bindings.AbstractClientBinding;
014: import org.objectweb.celtix.bus.configuration.wsrm.SourcePolicyType;
015: import org.objectweb.celtix.bus.ws.addressing.AddressingPropertiesImpl;
016: import org.objectweb.celtix.buslifecycle.BusLifeCycleManager;
017: import org.objectweb.celtix.configuration.Configuration;
018: import org.objectweb.celtix.context.ObjectMessageContext;
019: import org.objectweb.celtix.context.ObjectMessageContextImpl;
020: import org.objectweb.celtix.ws.addressing.AddressingProperties;
021: import org.objectweb.celtix.ws.rm.Identifier;
022: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
023: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement.AcknowledgementRange;
024: import org.objectweb.celtix.ws.rm.SequenceType;
025: import org.objectweb.celtix.ws.rm.persistence.RMMessage;
026: import org.objectweb.celtix.ws.rm.persistence.RMSourceSequence;
027: import org.objectweb.celtix.ws.rm.persistence.RMStore;
028: import org.objectweb.celtix.ws.rm.policy.RMAssertionType;
029: import org.objectweb.celtix.ws.rm.wsdl.SequenceFault;
030:
031: import static org.easymock.classextension.EasyMock.expect;
032: import static org.easymock.classextension.EasyMock.expectLastCall;
033: import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND;
034:
035: public class RMSourceTest extends TestCase {
036:
037: private Bus bus;
038: private BusLifeCycleManager lcm;
039: private RMHandler handler;
040: private RMSource s;
041: private Configuration cfg;
042: private IMocksControl control;
043:
044: public void setUp() {
045: control = EasyMock.createNiceControl();
046: handler = control.createMock(RMHandler.class);
047: s = createSource(handler);
048: }
049:
050: public void testRMSourceConstructor() {
051: assertNotNull(s.getRetransmissionQueue());
052: assertNull(s.getCurrent());
053: }
054:
055: public void testGetSourcePolicies() {
056: SourcePolicyType sp = null;
057: handler.getConfiguration();
058: expectLastCall().andReturn(cfg);
059: cfg.getObject(SourcePolicyType.class, "sourcePolicies");
060: expectLastCall().andReturn(sp);
061:
062: control.replay();
063: assertNotNull(s.getSourcePolicies());
064: control.verify();
065:
066: control.reset();
067: sp = control.createMock(SourcePolicyType.class);
068: handler.getConfiguration();
069: expectLastCall().andReturn(cfg);
070: cfg.getObject(SourcePolicyType.class, "sourcePolicies");
071: expectLastCall().andReturn(sp);
072: control.replay();
073: assertNotNull(s.getSourcePolicies());
074: control.verify();
075: }
076:
077: public void testGetSequenceTerminationPolicies() {
078: SourcePolicyType sp = null;
079: handler.getConfiguration();
080: expectLastCall().andReturn(cfg);
081: cfg.getObject(SourcePolicyType.class, "sourcePolicies");
082: expectLastCall().andReturn(sp);
083:
084: control.replay();
085: assertNotNull(s.getSequenceTerminationPolicy());
086: control.verify();
087: }
088:
089: public void testSequenceAccess() throws IOException, SequenceFault {
090: RMStore store = control.createMock(RMStore.class);
091: expect(handler.getStore()).andReturn(store).times(3);
092: store.createSourceSequence(EasyMock.isA(SourceSequence.class));
093: expectLastCall().times(2);
094: store.removeSourceSequence(EasyMock.isA(Identifier.class));
095:
096: control.replay();
097: Identifier sid = s.generateSequenceIdentifier();
098: SourceSequence seq = new SourceSequence(sid);
099: assertNull(s.getCurrent());
100: s.addSequence(seq);
101: assertNotNull(s.getSequence(sid));
102: assertNull(s.getCurrent());
103: SourceSequence anotherSeq = new SourceSequence(s
104: .generateSequenceIdentifier());
105: s.addSequence(anotherSeq);
106: assertNull(s.getCurrent());
107: assertEquals(2, s.getAllSequences().size());
108: s.removeSequence(seq);
109: assertEquals(1, s.getAllSequences().size());
110: assertNull(s.getSequence(sid));
111: control.verify();
112: }
113:
114: public void testCurrent() {
115: Identifier sid = s.generateSequenceIdentifier();
116: SourceSequence seq = new SourceSequence(sid);
117: assertNull(s.getCurrent());
118: Identifier inSid = s.generateSequenceIdentifier();
119: assertNull(s.getCurrent(inSid));
120: s.setCurrent(seq);
121: assertNotNull(s.getCurrent());
122: assertSame(seq, s.getCurrent());
123: assertNull(s.getCurrent(inSid));
124: s.setCurrent(inSid, seq);
125: assertNotNull(s.getCurrent(inSid));
126: assertSame(seq, s.getCurrent(inSid));
127: assertNull(s.getCurrent(sid));
128: }
129:
130: public void testAddUnacknowledged() {
131: ObjectMessageContext ctx = new ObjectMessageContextImpl();
132: AddressingProperties maps = new AddressingPropertiesImpl();
133: ctx.put(CLIENT_ADDRESSING_PROPERTIES_OUTBOUND, maps);
134: RMPropertiesImpl rmps = new RMPropertiesImpl();
135: SequenceType seq = control.createMock(SequenceType.class);
136: Identifier sid = control.createMock(Identifier.class);
137: rmps.setSequence(seq);
138: RMContextUtils.storeRMProperties(ctx, rmps, true);
139: SourceSequence ss = control.createMock(SourceSequence.class);
140: RMMessage msg = control.createMock(RMMessage.class);
141: AbstractClientBinding binding = control
142: .createMock(AbstractClientBinding.class);
143: handler.getBinding();
144: expectLastCall().andReturn(binding);
145: ObjectMessageContext clonedCtx = new ObjectMessageContextImpl();
146: binding.createObjectContext();
147: expectLastCall().andReturn(clonedCtx);
148: msg.getContext();
149: expectLastCall().andReturn(ctx);
150: seq.getIdentifier();
151: expectLastCall().andReturn(sid);
152: RMStore store = control.createMock(RMStore.class);
153: handler.getStore();
154: expectLastCall().andReturn(store);
155: store.persistOutgoing(ss, msg);
156: expectLastCall();
157:
158: control.replay();
159: s.addUnacknowledged(ss, msg);
160: control.verify();
161: }
162:
163: public void testSetAcknowledged() throws NoSuchMethodException,
164: IOException {
165: RMStore store = control.createMock(RMStore.class);
166: expect(handler.getStore()).andReturn(store);
167: store.createSourceSequence(EasyMock.isA(SourceSequence.class));
168: expectLastCall();
169:
170: control.replay();
171:
172: Identifier sid1 = s.generateSequenceIdentifier();
173: Identifier sid2 = s.generateSequenceIdentifier();
174: SourceSequence seq = new SourceSequence(sid1, null, null,
175: BigInteger.TEN, true);
176: seq.setSource(s);
177: s.addSequence(seq);
178:
179: SequenceAcknowledgement ack = RMUtils.getWSRMFactory()
180: .createSequenceAcknowledgement();
181: ack.setIdentifier(sid1);
182: s.setAcknowledged(ack);
183: assertSame(ack, seq.getAcknowledgement());
184: ack.setIdentifier(sid2);
185: s.setAcknowledged(ack);
186:
187: control.verify();
188:
189: control.reset();
190:
191: ack = RMUtils.getWSRMFactory().createSequenceAcknowledgement();
192: ack.setIdentifier(sid1);
193: AcknowledgementRange range = RMUtils.getWSRMFactory()
194: .createSequenceAcknowledgementAcknowledgementRange();
195:
196: range.setLower(BigInteger.ONE);
197: range.setUpper(BigInteger.TEN);
198: ack.getAcknowledgementRange().add(range);
199:
200: RMProxy proxy = control.createMock(RMProxy.class);
201: handler.getProxy();
202: expectLastCall().andReturn(proxy);
203: proxy.terminateSequence(seq);
204: expectLastCall();
205:
206: control.replay();
207: s.setAcknowledged(ack);
208: control.verify();
209:
210: control.reset();
211:
212: handler.getProxy();
213: expectLastCall().andReturn(proxy);
214: proxy.terminateSequence(seq);
215: expectLastCall().andThrow(
216: new IOException("can't terminate sequence"));
217:
218: control.replay();
219: s.setAcknowledged(ack);
220: control.verify();
221:
222: }
223:
224: public void testShutdown() {
225: s.shutdown();
226: }
227:
228: public void testRestore() {
229: RMStore store = control.createMock(RMStore.class);
230: EasyMock.expect(handler.getStore()).andReturn(store);
231: Configuration c = control.createMock(Configuration.class);
232: EasyMock.expect(handler.getConfiguration()).andReturn(c);
233: Configuration pc = control.createMock(Configuration.class);
234: EasyMock.expect(c.getParent()).andReturn(pc);
235: EasyMock.expect(pc.getId()).andReturn("endpoint");
236: Identifier id = RMUtils.getWSRMFactory().createIdentifier();
237: id.setValue("source1");
238: SourceSequence ss = new SourceSequence(id);
239: Collection<RMSourceSequence> sss = new ArrayList<RMSourceSequence>();
240: sss.add(ss);
241: EasyMock.expect(store.getSourceSequences("endpoint"))
242: .andReturn(sss);
243: EasyMock.expect(handler.getStore()).andReturn(store);
244: Collection<RMMessage> msgs = new ArrayList<RMMessage>();
245: EasyMock.expect(store.getMessages(id, true)).andReturn(msgs);
246: control.replay();
247: s.restore();
248: assertEquals(1, s.getAllSequences().size());
249: control.verify();
250: }
251:
252: private RMSource createSource(RMHandler h) {
253: bus = control.createMock(Bus.class);
254: h.getBus();
255: expectLastCall().andReturn(bus);
256: lcm = control.createMock(BusLifeCycleManager.class);
257: bus.getLifeCycleManager();
258: expectLastCall().andReturn(lcm);
259: cfg = control.createMock(Configuration.class);
260: h.getConfiguration();
261: expectLastCall().andReturn(cfg);
262: cfg.getObject(RMAssertionType.class, "rmAssertion");
263: expectLastCall().andReturn(null);
264:
265: control.replay();
266:
267: RMSource src = new RMSource(h);
268: control.verify();
269: control.reset();
270: return src;
271: }
272: }
|