-
Notifications
You must be signed in to change notification settings - Fork 574
/
RemoteTxAttrServlet.java
843 lines (745 loc) · 39.2 KB
/
RemoteTxAttrServlet.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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*******************************************************************************
* Copyright (c) 2019, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.ejbcontainer.remote.client.web;
import static javax.transaction.Status.STATUS_COMMITTED;
import static javax.transaction.Status.STATUS_NO_TRANSACTION;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Properties;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.EJBTransactionRequiredException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.annotation.WebServlet;
import javax.transaction.UserTransaction;
import org.junit.Test;
import org.omg.CORBA.BAD_PARAM;
import com.ibm.websphere.ejbcontainer.test.tools.FATHelper;
import com.ibm.websphere.ejbcontainer.test.tools.FATTransactionHelper;
import com.ibm.ws.ejbcontainer.remote.server.shared.TxAttrEJB;
import com.ibm.ws.ejbcontainer.remote.server.shared.TxAttrEJBHome;
import com.ibm.ws.ejbcontainer.remote.server.shared.TxAttrRemote;
import componenttest.annotation.ExpectedFFDC;
import componenttest.annotation.SkipForRepeat;
import componenttest.app.FATServlet;
import componenttest.rules.repeater.EE7FeatureReplacementAction;
import componenttest.rules.repeater.EE8FeatureReplacementAction;
import componenttest.rules.repeater.JakartaEE10Action;
import componenttest.rules.repeater.JakartaEE9Action;
import test.TestRemoteInterface;
/**
* Tests variations of looking up remote enterprise beans located on a different server
* and whether the EJB container performs the correct action for each of the possible TX
* attribute values that can be assigned to a method of an EJB when making remote EJB
* calls between two different servers.
*/
@WebServlet("/RemoteTxAttrServlet")
public class RemoteTxAttrServlet extends FATServlet {
private static final long serialVersionUID = -5671511025293075382L;
private static final Logger logger = Logger.getLogger(RemoteTxAttrServlet.class.getName());
private static final Integer IIOPPort = Integer.getInteger("bvt.prop.IIOP");
private static final Integer IIOPSecurePort = Integer.getInteger("bvt.prop.IIOP.secure");
private static Context defaultContext;
private static Context providerContext;
private static Context remoteContext;
// Name of module... for lookup.
private static final String CorbaName = "corbaname::localhost:" + IIOPPort;
private static final String CorbaNameNS = CorbaName + "/NameService";
private static final String CorbaNameSecure = "corbaname::localhost:" + IIOPSecurePort;
private static final String App = "RemoteServerApp";
private static final String Module = "RemoteServerEJB";
private static final String TxAttrBean = "TxAttrBean";
private static final String TxAttrCompBean = "TxAttrCompBean";
private static final String TestRemoteSingleton = "TestRemoteSingletonBean";
private static final String TestRemoteStateful = "TestRemoteStatefulBean";
private static final String TestRemoteStateless = "TestRemoteStatelessBean";
private static final String TxAttrBeanJndi = "ejb/global/" + App + "/" + Module + "/" + TxAttrBean;
private static final String TxAttrCompBeanJndi = "ejb/global/" + App + "/" + Module + "/" + TxAttrCompBean;
private static final String TestRemoteSingletonJndi = "ejb/global/" + App + "/" + Module + "/" + TestRemoteSingleton;
private static final String TestRemoteStatefulJndi = "ejb/global/" + App + "/" + Module + "/" + TestRemoteStateful;
private static final String TestRemoteStatelessJndi = "ejb/global/" + App + "/" + Module + "/" + TestRemoteStateless;
private static final String TxAttrRemote_Escape_5c = "com%5c.ibm%5c.ws%5c.ejbcontainer%5c.remote%5c.server%5c.shared%5c.TxAttrRemote";
private static final String TxAttrRemote_Escape_5C = "com%5C.ibm%5C.ws%5C.ejbcontainer%5C.remote%5C.server%5C.shared%5C.TxAttrRemote";
private static final String TxAttrRemote_Escape_Slash = "com\\.ibm\\.ws\\.ejbcontainer\\.remote\\.server\\.shared\\.TxAttrRemote";
private TxAttrRemote txAttrBean;
@Resource
private UserTransaction userTran;
/**
* Provides the default InitialContext.
*
* @return an InitialContext with no properties
* @throws NamingException if a NamingException is encountered
*/
private static Context getDefaultContext() throws NamingException {
if (defaultContext == null) {
defaultContext = new InitialContext();
}
return defaultContext;
}
/**
* Provides InitialContext created with Context.PROVIDER_URL for the remote system.
*
* @return an InitialContext with Context.PROVIDER_URL
* @throws NamingException if a NamingException is encountered
*/
private static Context getProviderContext() throws NamingException {
if (providerContext == null) {
logger.info("creating provider context with provider.url = corbaloc::127.0.0.1:" + IIOPPort + "/NameService");
Properties p = new Properties();
// TODO: remove or enable if testProviderUrlContextLookup is ever supported
// p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ws.jndi.iiop.InitialContextFactoryImpl");
p.put(Context.PROVIDER_URL, "corbaloc::127.0.0.1:" + IIOPPort);
providerContext = new InitialContext(p);
}
return providerContext;
}
/**
* Provides the Context of the remote system NameService
*
* @return an InitialContext with Context.PROVIDER_URL
* @throws NamingException if a NamingException is encountered
*/
private static Context getRemoteContext() throws NamingException {
if (remoteContext == null) {
logger.info("creating remote context with provider.url = corbaloc::127.0.0.1:" + IIOPPort + "/NameService");
Context context = new InitialContext();
remoteContext = (Context) context.lookup("corbaname::localhost:" + IIOPPort + "/NameService");
}
return remoteContext;
}
/**
* Provides an instance of the TxAttrBean. The first call to this method
* will perform a lookup using the default InitialContext and full corbaname.
*/
private TxAttrRemote getTxAttrBean() throws NamingException {
if (txAttrBean == null) {
// lookup the bean using default context with corbaname (including NameService)
String jndiName = CorbaNameNS + "#" + TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
txAttrBean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
}
return txAttrBean;
}
public static <T> T lookupRemoteBean(String jndiName, Class<T> interfaceClass) throws NamingException {
return lookupRemoteBean(getDefaultContext(), jndiName, interfaceClass);
}
public static <T> T lookupRemoteBean(Context context, String jndiName, Class<T> interfaceClass) throws NamingException {
logger.info("lookupRemoteBean: JNDI = " + jndiName + ", interface = " + interfaceClass.getName());
Object remoteObj = context.lookup(jndiName);
logger.info("lookupRemoteBean: found = " + ((remoteObj == null) ? "null" : remoteObj.getClass().getName()));
T remoteBean = interfaceClass.cast(PortableRemoteObject.narrow(remoteObj, interfaceClass));
logger.info("lookupRemoteBean: returning = " + ((remoteBean == null) ? remoteBean : remoteBean.getClass().getName()));
return remoteBean;
}
/**
* Test looking up a remote EJB located on a different server using the
* default InitialContext with the full corbaname (not including NameService).
*
* corbaname::localhost:<IIOPPort>#ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test
public void testDefaultContextLookupWithCorbaname() throws Exception {
// lookup the bean using default context with corbaname
String jndiName = CorbaName + "#" + TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
TxAttrRemote bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
// lookup the bean using default context with corbaname using %5c escape
jndiName = CorbaName + "#" + TxAttrBeanJndi + "!" + TxAttrRemote_Escape_5c;
bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
assertTrue("Container did not begin global transaction for TX REQUIRED", bean.txRequired());
// lookup the bean using default context with corbaname using %5C escape
jndiName = CorbaName + "#" + TxAttrBeanJndi + "!" + TxAttrRemote_Escape_5C;
bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
assertTrue("Container did not begin global transaction for TX REQUIRED", bean.txRequired());
// lookup the bean using default context with corbaname using \\ escape
jndiName = CorbaName + "#" + TxAttrBeanJndi + "!" + TxAttrRemote_Escape_Slash;
bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
assertTrue("Container did not begin global transaction for TX REQUIRED", bean.txRequired());
}
/**
* Test looking up a remote EJB located on a different server using the
* default InitialContext with the full corbaname (including NameService).
*
* corbaname::localhost:<IIOPPort>/NameService#ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test
public void testDefaultContextLookupWithCorbanameNameService() throws Exception {
// lookup the bean using default context with corbaname (including NameService)
String jndiName = CorbaNameNS + "#" + TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
TxAttrRemote bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test looking up a remote EJB located on a different server using the
* default InitialContext with the corbaname, not including NameService or interface.
*
* corbaname::localhost:<IIOPPort>#ejb/global/<App>/<Module>/<Bean>
*/
// @Test - TODO: enable or remove test once determined if this should be supported
public void testDefaultContextLookupWithCorbanameNoInterface() throws Exception {
// lookup the bean using default context with corbaname, minus interface
String jndiName = CorbaName + "#" + TxAttrBeanJndi;
TxAttrRemote bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test looking up a remote EJB located on a different server using an InitialContext
* with java.naming.provider.url and the lookup name without corbaname prefix.
*
* java.naming.provider.url : corbaloc::127.0.0.1:<IIOPPort>
* ejb/global/<App>/<Module>/<Bean>!<interface>
*/
// @Test - not supported
public void testProviderUrlContextLookup() throws Exception {
// lookup the bean using context with provider URL
String jndiName = TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
TxAttrRemote bean = lookupRemoteBean(getProviderContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test looking up a remote EJB located on a different server using the
* context of the remote server NameService and the lookup name without corbaname prefix.
*
* remote NameService : corbaloc::127.0.0.1:<IIOPPort>
* ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test
public void testRemoteContextLookup() throws Exception {
// lookup the bean using context of remote NameService
String jndiName = TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
TxAttrRemote bean = lookupRemoteBean(getRemoteContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test looking up a remote EJB located on a different server using the
* default InitialContext with the full corbaname (including NameService)
* and escaping the single period in the interface.
*
* An escape is required when the interface contains a single period, so also
* verify the lookup fails without the escape.
*
* corbaname::localhost:<IIOPPort>/NameService#ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test
public void testDefaultContextLookupWithEscape() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatelessJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
assertEquals("Incorrect beanName returned", "TestRemoteStatelessBean", bean.getBeanName());
// Also confirm that the same lookup without an escape fails
jndiName = CorbaNameNS + "#" + TestRemoteStatelessJndi + "!test.TestRemoteInterface";
try {
bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
fail("lookup did not fail : " + bean);
} catch (BAD_PARAM ex) {
logger.info("Excpected exception occurred : " + ex);
}
}
/**
* Test looking up a remote EJB located on a different server using the
* default InitialContext with the full corbaname (not including NameService)
* using he secure IIOP port.
*
* Note: Use of the secure port (iiops) is not currently supported, and will
* fail if it is the only JNDI lookup attempted. However, the lookup will
* succeed if a previous lookup is performed using the iiop port, though this
* is not supported and will likely fail in the future.
*
* corbaname::localhost:<IIOPSecurePort>#ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test //- requires additional security configuration
@SkipForRepeat({ EE7FeatureReplacementAction.ID, EE8FeatureReplacementAction.ID, JakartaEE9Action.ID, JakartaEE10Action.ID })
public void testDefaultContextLookupWithSecurePort() throws Exception {
// first, lookup the bean using default context with corbaname with iiop port.
// this is required or use of the secure port below will fail.
String jndiName = CorbaName + "#" + TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
TxAttrRemote bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// lookup the bean using default context with corbaname on secure IIOP port
// Note: not a supported scenario; likely to fail in the future.
jndiName = CorbaNameSecure + "#" + TxAttrBeanJndi + "!" + TxAttrRemote.class.getName();
bean = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrRemote.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test looking up a remote EJBHome located on a different server using the
* default InitialContext with the full corbaname (not including NameService).
*
* corbaname::localhost:<IIOPPort>#ejb/global/<App>/<Module>/<Bean>!<interface>
*/
@Test
public void testDefaultContextLookupWithEJBHome() throws Exception {
// lookup the bean using default context with corbaname of EJBHome
String jndiName = CorbaName + "#" + TxAttrCompBeanJndi + "!" + TxAttrEJBHome.class.getName();
TxAttrEJBHome home = lookupRemoteBean(getDefaultContext(), jndiName, TxAttrEJBHome.class);
assertNotNull("Remote home is null", home);
// Create an instance of the bean
TxAttrEJB bean = home.create();
assertNotNull("Remote bean is null", bean);
// verify the bean works
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
}
/**
* Test that a remote reference to an EJB may be passed as a parameter to a remote
* EJB located on a different server.
*/
@Test
public void testPassingRemoteInterfaceParameter() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatelessJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; remote reference may be passed as parameter
assertTrue("Remote reference not passed as parameter correctly", bean.verifyRemoteBean(bean));
}
/**
* Test that method calls to a remote Singleton session bean on a different server
* are routed to the same bean instance (state is preserved).
*/
@Test
public void testSingletonBeanState() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteSingletonJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; remote state is preserved
assertEquals("Remote state is not correct", 8, bean.increment(8));
assertEquals("Remote state is not correct", (8 + 27), bean.increment(27));
}
/**
* Test that method calls to a remote Stateful session bean on a different server
* are routed to the same bean instance (state is preserved).
*/
@Test
public void testStatefulBeanState() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatefulJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; remote state is preserved
assertEquals("Remote state is not correct", 42, bean.increment(42));
assertEquals("Remote state is not correct", (42 + 27), bean.increment(27));
}
/**
* Test that method calls to a remote Stateless session bean on a different server
* are routed to any bean instance (state is not preserved).
*/
@Test
public void testStatelessBeanState() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatelessJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; remote state is not preserved
assertEquals("Remote state is not correct", 14, bean.increment(14));
assertEquals("Remote state is not correct", 35, bean.increment(35));
}
/**
* Test that asynchronous method calls to a remote Singleton session bean on a different
* server are routed properly and the result may be retrieved.
*/
@Test
public void testSingletonAsyncMethod() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteSingletonJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; async result may be retrieved
Future<String> nameFuture = bean.asynchMethodReturn();
assertEquals("Asynchronus method value incorrect", TestRemoteSingleton, nameFuture.get(60, TimeUnit.SECONDS));
}
/**
* Test that asynchronous method calls to a remote Stateful session bean on a different
* server are routed properly and the result may be retrieved.
*/
@Test
public void testStatefulAsyncMethod() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatefulJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; async result may be retrieved
Future<String> nameFuture = bean.asynchMethodReturn();
assertEquals("Asynchronus method value incorrect", TestRemoteStateful, nameFuture.get(60, TimeUnit.SECONDS));
}
/**
* Test that asynchronous method calls to a remote Stateless session bean on a different
* server are routed properly and the result may be retrieved.
*/
@Test
public void testStatelessAsyncMethod() throws Exception {
// lookup the bean using default context with corbaname, with escape for period (.)
String jndiName = CorbaNameNS + "#" + TestRemoteStatelessJndi + "!test%5c.TestRemoteInterface";
TestRemoteInterface bean = lookupRemoteBean(getDefaultContext(), jndiName, TestRemoteInterface.class);
assertNotNull("Remote bean is null", bean);
// verify the bean works; async result may be retrieved
Future<String> nameFuture = bean.asynchMethodReturn();
assertEquals("Asynchronus method value incorrect", TestRemoteStateless, nameFuture.get(60, TimeUnit.SECONDS));
}
/**
* Test the Required transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While the thread is currently not associated with a transaction context, call a method
* that has a transaction attribute of REQUIRED and verify the container began a global
* transaction. Verify container completed global transaction prior to returning to caller
* of method.
*/
@Test
public void testRequiredAttrib() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
boolean global = bean.txRequired();
assertTrue("Container did not begin global transaction for TX REQUIRED", global);
assertFalse("Container did not complete global transaction for TX REQUIRED", FATTransactionHelper.isTransactionGlobal());
}
/**
* Test the Required transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* While thread is currently associated with a transaction context, call a remote method
* that has a transaction attribute of REQUIRED and verify the container throws a
* EJBTransactionRequiredException indicating the global transaction has not been
* propagated to the remote server.
*/
@Test
@ExpectedFFDC({ "com.ibm.websphere.csi.CSITransactionRequiredException" })
public void testRequiredAttribInGlobalTrans() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
// call TX REQUIRED method
try {
boolean global = bean.txRequired(tid);
fail("Expected exception did not occur; Container used caller's global transaction for TX REQUIRED : " + global);
} catch (EJBTransactionRequiredException ex) {
logger.info("Expected exception occurred calling REQUIRED method : " + ex);
}
// Verify global tran still active.
assertTrue("container did not complete caller's transaction for TX REQUIRED", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
/**
* Test the RequiresNew transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* While thread is currently associated with a transaction context, call a method that
* has a transaction attribute of REQUIRES_NEW and verify the container begins a new global
* transaction. Verify container completes global transaction prior to returning to caller
* of method. Verify caller's global transaction is still active when container returns to
* caller.
*/
@Test
public void testRequiresNewAttribIfClientTranExists() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
// call TX REQUIRES NEW method
boolean global = bean.txRequiresNew(tid);
assertTrue("Container began new global transaction for TX REQUIRES NEW", global);
// Verify global tran still active.
assertTrue("container did not complete caller's transaction for TX REQUIRES NEW", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
/**
* Test the RequiresNew transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While thread is currently not associated with a transaction context, call a method that
* has a transaction attribute of REQUIRES NEW and verify the container began a global
* transaction. Verify container completed global transaction prior to returning to caller
* of method.
*/
@Test
public void testRequiresNewAttribOnGlobalInt() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
boolean global = bean.txRequiresNew();
assertTrue("Container did not begin global transaction for TX REQUIRES NEW", global);
assertFalse("container did not complete global transaction for TX REQUIRES NEW", FATTransactionHelper.isTransactionGlobal());
}
/**
* Test the Mandatory transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While thread is currently not associated with a transaction context, call a method that
* has a transaction attribute of Mandatory and verify the container throws a
* javax.ejb.EJBTransactionRequiredException.
*/
@Test
@ExpectedFFDC({ "com.ibm.websphere.csi.CSITransactionRequiredException" })
public void testMandatoryAttribThrowsExcp() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
bean.txMandatory();
fail("The container did NOT throw the expected EJBTransactionRequiredException for TX Mandatory");
} catch (EJBTransactionRequiredException ex) {
logger.info("Container threw expected EJBTransactionRequiredException for TX Mandatory");
}
}
/**
* Test the Mandatory transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* While thread is currently associated with a transaction context, call a method that
* has a transaction attribute of Mandatory and verify the container throws a
* javax.ejb.EJBTransactionRequiredException.
*/
@Test
@ExpectedFFDC({ "com.ibm.websphere.csi.CSITransactionRequiredException" })
public void testMandatoryAttribInGlobalTrans() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
// call TX Mandatory method
try {
boolean global = bean.txMandatory(tid);
fail("Expected exception did not occur; Container used caller's global transaction for TX Mandatory : " + global);
} catch (EJBTransactionRequiredException ex) {
logger.info("Expected exception occurred calling MANDATORY method : " + ex);
}
// Verify global tran still active.
assertTrue("Container completed caller's transaction for TX Mandatory", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
/**
* Test the Never transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While thread is currently not associated with a transaction context, call a method that
* has a transaction attribute of Never and verify the container begins a local transaction.
*/
@Test
public void testNever() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
boolean local = bean.txNever();
assertTrue("container did not begin a local transaction for TX Never", local);
}
/**
* Test the Never transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* Used to verify when a method with a NEVER transaction attribute is called while
* the thread is currently associated with a global transaction the container throws
* a javax.ejb.EJBException.
*/
@Test
@ExpectedFFDC({ "com.ibm.websphere.csi.CSIException" })
public void testNeverException() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
// call TX NEVER method
try {
bean.txNever(tid);
fail("Container did not throw a javax.ejb.EJBException as expected for TX Never when transaction already existed.");
} catch (EJBException ex) {
logger.info("container threw expected EJBException for TX Never when transaction already existed.");
}
// Verify global tran still active.
assertTrue("container did not complete caller's transaction for TX NEVER", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
/**
* Test the NotSupported transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While thread is currently not associated with a transaction context, call a method that
* has a transaction attribute of NotSupported and verify the container began a local
* transaction.
*/
@Test
public void testNotSupported() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
boolean local = bean.txNotSupported();
assertTrue("container did not begin a local transaction for TX NotSupported", local);
}
/**
* Test the NotSupported transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* While thread is currently associated with a transaction context, call a method
* that has a transaction attribute of NotSupported and verify the container began a
* local transaction.
*/
@Test
public void testNotSupportedGlobalTransExists() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
boolean local = bean.txNotSupported();
assertTrue("container did not begin a local transaction for TX NotSupported", local);
// Verify global tran still active.
assertTrue("container did not complete caller's transaction for TX NotSupported", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
/**
* Test the Supports transaction attribute for a remote EJB on a different server
* when a transaction does not exist on the calling thread.
*
* While thread is currently not associated with a transaction context, call a method that
* has a transaction attribute of Supports and verify the container began a local transaction.
*/
@Test
public void testSupportsAttrib() throws Exception {
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
boolean local = bean.txSupports();
assertTrue("container did not begin a local transaction for TX Supports", local);
}
/**
* Test the Supports transaction attribute for a remote EJB on a different server
* when a transaction does exist on the calling thread.
*
* While thread is currently associated with a transaction context, call a method that
* has a transaction attribute of Supports and verify the container executes in caller's
* global transaction. Verify container does not complete the caller's global
* transaction prior to returning to caller of method.
*/
@Test
@ExpectedFFDC({ "com.ibm.websphere.csi.CSITransactionRequiredException" })
public void testSupportsAttribOnGlobalTrans() throws Exception {
byte[] tid = null;
UserTransaction userTran = null;
TxAttrRemote bean = getTxAttrBean();
assertNotNull("Remote bean is null", bean);
try {
// Begin a global transaction
userTran = FATHelper.lookupUserTransaction();
userTran.begin();
tid = FATTransactionHelper.getTransactionId();
logger.info("user global transaction was started");
// call TX Supports method
try {
boolean global = bean.txSupports(tid);
fail("Expected exception did not occur; Container used caller's global transaction for TX Supports : " + global);
} catch (EJBTransactionRequiredException ex) {
logger.info("Expected exception occurred calling SUPPORTS method : " + ex);
}
// Verify global tran still active.
assertTrue("container did not complete caller's transaction for TX Supports", FATTransactionHelper.isSameTransactionId(tid));
userTran.commit();
tid = null;
logger.info("user global transaction committed");
} finally {
if (tid != null || (userTran != null && userTran.getStatus() != STATUS_NO_TRANSACTION && userTran.getStatus() != STATUS_COMMITTED)) {
userTran.rollback();
}
}
}
}