-
Notifications
You must be signed in to change notification settings - Fork 37
/
AbstractJaxWsClientTest.java
701 lines (604 loc) · 23.6 KB
/
AbstractJaxWsClientTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* Copyright (c) 2010-2024 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.rt.server.jaxws.consumer;
import static org.junit.Assert.*;
import java.lang.reflect.Proxy;
import java.net.SocketTimeoutException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.EchoRequest;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.EchoResponse;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.GetHeaderRequest;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.GetHeaderResponse;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.SetHeaderRequest;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.SetHeaderResponse;
import org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.SleepRequest;
import org.eclipse.scout.jaxws.consumer.jaxwspingtestservice.JaxWsPingTestServicePortType;
import org.eclipse.scout.jaxws.consumer.jaxwspingtestservice.PingRequest;
import org.eclipse.scout.jaxws.consumer.jaxwspingtestservice.PingResponse;
import org.eclipse.scout.rt.platform.BEANS;
import org.eclipse.scout.rt.platform.holders.Holder;
import org.eclipse.scout.rt.platform.job.IFuture;
import org.eclipse.scout.rt.platform.job.Jobs;
import org.eclipse.scout.rt.platform.util.SleepUtil;
import org.eclipse.scout.rt.server.context.ServerRunContexts;
import org.eclipse.scout.rt.server.jaxws.implementor.JaxWsImplementorSpecifics;
import org.eclipse.scout.rt.testing.platform.runner.RunWithSubject;
import org.eclipse.scout.rt.testing.server.runner.RunWithServerSession;
import org.eclipse.scout.rt.testing.server.runner.ServerTestRunner;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Abstract test for web service consumers. <br>
* <b>Note:</b> Subclasses are responsible for exposing {@link JaxWsConsumerTestServiceProvider}.
*
* @since 6.0.300
*/
@RunWith(ServerTestRunner.class)
@RunWithServerSession(JaxWsConsumerTestServerSession.class)
@RunWithSubject("default")
public abstract class AbstractJaxWsClientTest {
private static final String X_SCOUT_JAX_WS_TEST_HEADER = "X-Scout-JaxWsTestHeader";
@BeforeClass
public static void startupWsProvider() {
BEANS.get(JaxWsTestProviderInstaller.class).install();
}
@AfterClass
public static void stopWsProvider() {
BEANS.get(JaxWsTestProviderInstaller.class).uninstall();
}
/*
* ************************************************************
* Test setup
* ************************************************************/
@Test
public void testSetupEcho() {
final String messageSent = "Test Message";
EchoRequest req = new EchoRequest();
req.setMessage(messageSent);
EchoResponse response = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort()
.echo(req);
assertNotNull(response);
assertEquals(messageSent, response.getMessage());
}
@Test
public void testSetupGetHeaderNoHeaderSet() {
GetHeaderRequest req = new GetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
GetHeaderResponse resp = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort()
.getHeader(req);
assertNotNull(resp);
assertFalse(resp.isHeaderSet());
assertNull(resp.getHeaderValue());
}
@Test
public void testSetupGetHeaderHeaderSet() {
GetHeaderRequest req = new GetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
final String headerValueSent = "test header value";
GetHeaderResponse resp = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.withHttpRequestHeader(X_SCOUT_JAX_WS_TEST_HEADER, headerValueSent)
.getPort()
.getHeader(req);
assertNotNull(resp);
assertTrue(resp.isHeaderSet());
assertEquals(headerValueSent, resp.getHeaderValue());
}
@Test
public void testSetupSetHeader() {
final String expectedHeader = "test header value";
SetHeaderRequest req = new SetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
req.setHeaderValue(expectedHeader);
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
SetHeaderResponse resp = port.setHeader(req);
assertNotNull(resp);
assertEquals("ok", resp.getMessage());
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, expectedHeader);
}
@Test
public void testSetupSleep() {
final int sleepTimeMillis = 500;
SleepRequest req = new SleepRequest();
req.setMillis(sleepTimeMillis);
final JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
final long start = System.currentTimeMillis();
try {
port.sleep(req);
}
finally {
long dt = System.currentTimeMillis() - start;
if (dt < sleepTimeMillis) {
fail("Operation took less than " + sleepTimeMillis);
}
}
}
@Test
public void testSetupSleepWithReadTimeout() {
final int sleepTimeMillis = 5000;
SleepRequest req = new SleepRequest();
req.setMillis(sleepTimeMillis);
final JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.withReadTimeout(1, TimeUnit.SECONDS)
.getPort();
try {
port.sleep(req);
fail("invocation is expected to be cancelled");
}
catch (WebServiceException e) {
if (!isTimeoutCause(e)) {
throw e;
}
}
}
/*
* ************************************************************
* Test use same port multiple times
* ************************************************************/
@Test
public void testSamePortMultipleTimesEcho() {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
for (int i = 0; i < 10; i++) {
assertSendEcho(port, i);
}
}
@Test
public void testSamePortMultipleTimesGetHeader() {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
GetHeaderRequest req = new GetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
// 1. no HTTP headers set
GetHeaderResponse resp = port.getHeader(req);
assertNotNull(resp);
assertFalse(resp.isHeaderSet());
assertNull(resp.getHeaderValue());
// 2. add HTTP header
final String headerValueSent = "test header value";
BEANS.get(JaxWsImplementorSpecifics.class).setHttpRequestHeader(((BindingProvider) port).getRequestContext(), X_SCOUT_JAX_WS_TEST_HEADER, headerValueSent);
resp = port.getHeader(req);
assertNotNull(resp);
assertTrue(resp.isHeaderSet());
assertEquals(headerValueSent, resp.getHeaderValue());
// 3. do not change HTTP headers
resp = port.getHeader(req);
assertNotNull(resp);
assertTrue(resp.isHeaderSet());
assertEquals(headerValueSent, resp.getHeaderValue());
// 4. remove HTTP header
BEANS.get(JaxWsImplementorSpecifics.class).removeHttpRequestHeader(((BindingProvider) port).getRequestContext(), X_SCOUT_JAX_WS_TEST_HEADER);
resp = port.getHeader(req);
assertNotNull(resp);
assertFalse(resp.isHeaderSet());
assertNull(resp.getHeaderValue());
}
@Test
public void testSamePortMultipleTimesSetHeader() {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
// 1. invoke echo without header
EchoRequest echoReq = new EchoRequest();
echoReq.setMessage("test message");
port.echo(echoReq);
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, null);
// 2. set header
final String expectedHeader = "test header value";
SetHeaderRequest req = new SetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
req.setHeaderValue(expectedHeader);
SetHeaderResponse resp = port.setHeader(req);
assertNotNull(resp);
assertEquals("ok", resp.getMessage());
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, expectedHeader);
// 3. invoke echo without header
echoReq.setMessage("test message");
port.echo(echoReq);
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, null);
}
@Test
public void testSamePortMultipleTimesSleepWithReadTimeoutCheckResponseCode() {
final JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.withReadTimeout(500, TimeUnit.MILLISECONDS)
.getPort();
// 1. invoke echo, response code 200 expected
EchoRequest echoReq = new EchoRequest();
echoReq.setMessage("test message");
port.echo(echoReq);
assertHttpResponseCode(port, 200);
// 2. invoke sleep
SleepRequest req = new SleepRequest();
req.setMillis(5000);
try {
port.sleep(req);
fail("invocation is expected to be cancelled");
}
catch (WebServiceException e) {
if (!isTimeoutCause(e)) {
throw e;
}
}
assertHttpResponseCode(port, 0);
// 3. invoke echo again, response code 200 expected
port.echo(echoReq);
assertHttpResponseCode(port, 200);
}
protected boolean isTimeoutCause(WebServiceException e) {
return e.getCause() instanceof SocketTimeoutException;
}
@Test
public void testSamePortMultipleTimesSleepWithReadTimeoutCheckResponseHeaders() {
final JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.withReadTimeout(500, TimeUnit.MILLISECONDS)
.getPort();
// 1. invoke set header
final String testHeaderValue = "test header value";
SetHeaderRequest headerReq = new SetHeaderRequest();
headerReq.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
headerReq.setHeaderValue(testHeaderValue);
port.setHeader(headerReq);
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, testHeaderValue);
// 2. invoke sleep
SleepRequest req = new SleepRequest();
req.setMillis(5000);
try {
port.sleep(req);
fail("invocation is expected to be cancelled");
}
catch (WebServiceException e) {
if (!isTimeoutCause(e)) {
throw e;
}
}
assertHttpResponseCode(port, 0);
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, null);
// 3. invoke echo
EchoRequest echoReq = new EchoRequest();
echoReq.setMessage("test message");
port.echo(echoReq);
assertHttpResponseCode(port, 200);
assertHttpResponseHeader(port, X_SCOUT_JAX_WS_TEST_HEADER, null);
}
/*
* ************************************************************
* Test acquire port in same transaction multiple times
* ************************************************************/
@Test
public void testAcquirePortInSameTransactionMultipleTimesEcho() {
JaxWsConsumerTestServicePortType port0 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port0, 0);
JaxWsConsumerTestServicePortType port1 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
assertSamePort(port0, port1);
}
else {
assertDifferentPort(port0, port1);
}
assertSendEcho(port1, 1);
}
@Test
public void testAcquirePortInSameTransactionMultipleTimesGetHeader() {
GetHeaderRequest req = new GetHeaderRequest();
req.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
// 1. add HTTP header on port0
JaxWsConsumerTestServicePortType port0 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
final String headerValueSent = "test header value";
BEANS.get(JaxWsImplementorSpecifics.class).setHttpRequestHeader(((BindingProvider) port0).getRequestContext(), X_SCOUT_JAX_WS_TEST_HEADER, headerValueSent);
GetHeaderResponse resp = port0.getHeader(req);
assertNotNull(resp);
assertTrue(resp.isHeaderSet());
assertEquals(headerValueSent, resp.getHeaderValue());
// 2. acquire port1 and do not set a header
JaxWsConsumerTestServicePortType port1 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
assertSamePort(port0, port1);
}
else {
assertDifferentPort(port0, port1);
}
resp = port1.getHeader(req);
assertNotNull(resp);
assertFalse(resp.isHeaderSet());
assertNull(resp.getHeaderValue());
}
@Test
public void testAcquirePortInSameTransactionMultipleTimesSleepWithReadTimeoutCheckResponseHeaders() {
// 1. invoke set header on port0
final JaxWsConsumerTestServicePortType port0 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
final String testHeaderValue = "test header value";
SetHeaderRequest headerReq = new SetHeaderRequest();
headerReq.setHeaderName(X_SCOUT_JAX_WS_TEST_HEADER);
headerReq.setHeaderValue(testHeaderValue);
port0.setHeader(headerReq);
assertHttpResponseHeader(port0, X_SCOUT_JAX_WS_TEST_HEADER, testHeaderValue);
// 2. invoke sleep on port1
final JaxWsConsumerTestServicePortType port1 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.withReadTimeout(500, TimeUnit.MILLISECONDS)
.getPort();
if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
assertSamePort(port0, port1);
}
else {
assertDifferentPort(port0, port1);
}
SleepRequest req = new SleepRequest();
req.setMillis(5000);
try {
port1.sleep(req);
fail("invocation is expected to be cancelled");
}
catch (WebServiceException e) {
if (!isTimeoutCause(e)) {
throw e;
}
}
assertHttpResponseCode(port1, 0);
assertHttpResponseHeader(port1, X_SCOUT_JAX_WS_TEST_HEADER, null);
// 3. invoke echo on port1
EchoRequest echoReq = new EchoRequest();
echoReq.setMessage("test message");
port1.echo(echoReq);
assertHttpResponseCode(port1, 200);
assertHttpResponseHeader(port1, X_SCOUT_JAX_WS_TEST_HEADER, null);
}
@Test
public void testAcquirePortConcurrentlyInDifferentTransactions() throws InterruptedException {
final CountDownLatch txn1InitLatch = new CountDownLatch(1);
final CountDownLatch txn2InitLatch = new CountDownLatch(1);
final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
Jobs.schedule(
() -> {
JaxWsConsumerTestServicePortType port0 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port0, 0);
txn1PortHolder.setValue(port0);
txn1InitLatch.countDown();
txn2InitLatch.await();
JaxWsConsumerTestServicePortType port1 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSamePort(port0, port1);
assertSendEcho(port1, 1);
}, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
txn1InitLatch.await();
Jobs.schedule(
() -> {
JaxWsConsumerTestServicePortType port0 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port0, 0);
txn2PortHolder.setValue(port0);
txn2InitLatch.countDown();
JaxWsConsumerTestServicePortType port1 = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSamePort(port0, port1);
assertSendEcho(port1, 1);
}, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
txn2InitLatch.await();
assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
/*
* ************************************************************
* Test acquire port in different transactions
* ************************************************************/
@Test
public void testAcquirePortInDifferentTransactions() {
final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
// This test case expects at most one port in the pool. It is guaranteed by discarding all pooled entries.
BEANS.get(JaxWsConsumerTestClient.class).discardAllPoolEntries();
ServerRunContexts.copyCurrent().run(
() -> {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port, 0);
txn1PortHolder.setValue(port);
});
ServerRunContexts.copyCurrent().run(
() -> {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port, 0);
txn2PortHolder.setValue(port);
});
if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
assertSamePort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
else {
assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
}
/**
* Canceling a running web service invocation invalidates the port. This test verifies, that the canceled port is not
* put back into the pool.
*/
@Test
public void testAcquirePortInDifferentTransactionsCancelFirstOne() throws InterruptedException {
final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
// This test case expects at most one port in the pool. It is guaranteed by discarding all pooled entries.
BEANS.get(JaxWsConsumerTestClient.class).discardAllPoolEntries();
final CountDownLatch requestRunningLatch = new CountDownLatch(1);
final IFuture<Void> future = Jobs.schedule(
() -> {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
txn1PortHolder.setValue(port);
SleepRequest req = new SleepRequest();
req.setMillis(1000);
requestRunningLatch.countDown();
port.sleep(req);
}, Jobs
.newInput()
.withRunContext(ServerRunContexts.copyCurrent())
.withExceptionHandling(null, true));
requestRunningLatch.await();
SleepUtil.sleepSafe(50, TimeUnit.MILLISECONDS);
Jobs.schedule(() -> {
future.cancel(true);
}, Jobs
.newInput()
.withRunContext(ServerRunContexts.copyCurrent()));
try {
future.awaitDone();
}
catch (WebServiceRequestCancelledException e) { // NOSONAR
// expected
}
assertTrue(future.isCancelled());
ServerRunContexts.copyCurrent().run(
() -> {
JaxWsConsumerTestServicePortType port = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(port, 0);
txn2PortHolder.setValue(port);
});
assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
/*
* ************************************************************
* Test invoke different web services in same transaction
* ************************************************************/
@Test
public void testDifferentPortsInSameTransaction() {
JaxWsConsumerTestServicePortType echoPort = BEANS
.get(JaxWsConsumerTestClient.class)
.newInvocationContext()
.getPort();
assertSendEcho(echoPort, 0);
JaxWsPingTestServicePortType pingPort = BEANS
.get(JaxWsPingTestClient.class)
.newInvocationContext()
.getPort();
assertNotSame(echoPort, pingPort);
PingRequest pingRequest = new PingRequest();
pingRequest.setMessage("ping");
PingResponse pingResponse = pingPort.ping(pingRequest);
assertNotNull(pingResponse);
assertEquals("ping", pingResponse.getMessage());
}
/*
* ************************************************************
* Utility methods
* ************************************************************/
protected void assertHttpResponseHeader(JaxWsConsumerTestServicePortType port, String headerName, String expectedHeader) {
List<String> responseHeader = BEANS.get(JaxWsImplementorSpecifics.class).getHttpResponseHeader(((BindingProvider) port).getResponseContext(), headerName);
if (expectedHeader == null) {
assertEquals(Collections.emptyList(), responseHeader);
}
else {
assertEquals(Collections.singletonList(expectedHeader), responseHeader);
}
}
protected void assertHttpResponseCode(JaxWsConsumerTestServicePortType port, int expectedResponseCode) {
Integer httpResponseCode = BEANS.get(JaxWsImplementorSpecifics.class).getHttpResponseCode(((BindingProvider) port).getResponseContext());
assertEquals(Integer.valueOf(expectedResponseCode), httpResponseCode);
}
protected void assertSendEcho(JaxWsConsumerTestServicePortType port, int i) {
final String messageSent = "Test Message " + i;
EchoRequest req = new EchoRequest();
req.setMessage(messageSent);
EchoResponse response = port.echo(req);
assertNotNull(response);
assertEquals(messageSent, response.getMessage());
}
protected void assertSamePort(Object port0, Object port1) {
assertPorts(true, port0, port1);
}
protected void assertDifferentPort(Object port0, Object port1) {
assertPorts(false, port0, port1);
}
protected void assertPorts(boolean expectedSame, Object port0, Object port1) {
assertNotNull(port0);
assertNotNull(port1);
// extract Socut's JAX-WS invocation handler, i.e. InvocationContext.P_InvocationHandler<PORT>
assertTrue(Proxy.isProxyClass(port0.getClass()));
assertTrue(Proxy.isProxyClass(port1.getClass()));
Object ih0 = Proxy.getInvocationHandler(port0);
Object ih1 = Proxy.getInvocationHandler(port1);
// dereference actual port
assertTrue(ih0 instanceof IPortWrapper<?>);
assertTrue(ih1 instanceof IPortWrapper<?>);
Object p0 = ((IPortWrapper<?>) ih0).getPort();
Object p1 = ((IPortWrapper<?>) ih1).getPort();
if (expectedSame) {
assertSame(p0, p1);
}
else {
assertNotSame(p0, p1);
}
}
}