/
ActiveMQConnectionFactoryTest.java
713 lines (633 loc) · 35.3 KB
/
ActiveMQConnectionFactoryTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.tests.integration.jms;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.apache.activemq.artemis.api.core.BroadcastGroupConfiguration;
import org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.api.jms.JMSFactoryType;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.core.config.ha.SharedStoreMasterPolicyConfiguration;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.tests.integration.IntegrationTestLogger;
import org.apache.activemq.artemis.tests.integration.jms.serializables.TestClass1;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* ActiveMQConnectionFactoryTest
*/
public class ActiveMQConnectionFactoryTest extends ActiveMQTestBase {
private static final IntegrationTestLogger log = IntegrationTestLogger.LOGGER;
private final String groupAddress = getUDPDiscoveryAddress();
private final int groupPort = getUDPDiscoveryPort();
private ActiveMQServer liveService;
private TransportConfiguration liveTC;
@Test
public void testDefaultConstructor() throws Exception {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF);
assertFactoryParams(cf, null, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
Connection conn = null;
try {
conn = cf.createConnection();
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Assert.fail("Should throw exception");
} catch (JMSException e) {
// Ok
}
if (conn != null) {
conn.close();
}
ActiveMQConnectionFactoryTest.log.info("Got here");
testSettersThrowException(cf);
}
@Test
public void testDefaultConstructorAndSetConnectorPairs() throws Exception {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
assertFactoryParams(cf, new TransportConfiguration[]{liveTC}, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
Connection conn = cf.createConnection();
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
testSettersThrowException(cf);
conn.close();
}
@Test
public void testDiscoveryConstructor() throws Exception {
DiscoveryGroupConfiguration groupConfiguration = new DiscoveryGroupConfiguration().setBroadcastEndpointFactory(new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(groupPort));
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(groupConfiguration, JMSFactoryType.CF);
assertFactoryParams(cf, null, groupConfiguration, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
Connection conn = cf.createConnection();
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
testSettersThrowException(cf);
conn.close();
}
@Test
public void testStaticConnectorListConstructor() throws Exception {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
assertFactoryParams(cf, new TransportConfiguration[]{liveTC}, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
Connection conn = cf.createConnection();
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
testSettersThrowException(cf);
conn.close();
}
@Test
public void testStaticConnectorLiveConstructor() throws Exception {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
assertFactoryParams(cf, new TransportConfiguration[]{liveTC}, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
Connection conn = cf.createConnection();
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
testSettersThrowException(cf);
cf.close();
conn.close();
}
@Test
public void testGettersAndSetters() {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
long clientFailureCheckPeriod = RandomUtil.randomPositiveLong();
long connectionTTL = RandomUtil.randomPositiveLong();
long callTimeout = RandomUtil.randomPositiveLong();
int minLargeMessageSize = RandomUtil.randomPositiveInt();
int consumerWindowSize = RandomUtil.randomPositiveInt();
int consumerMaxRate = RandomUtil.randomPositiveInt();
int confirmationWindowSize = RandomUtil.randomPositiveInt();
int producerMaxRate = RandomUtil.randomPositiveInt();
boolean blockOnAcknowledge = RandomUtil.randomBoolean();
boolean blockOnDurableSend = RandomUtil.randomBoolean();
boolean blockOnNonDurableSend = RandomUtil.randomBoolean();
boolean autoGroup = RandomUtil.randomBoolean();
boolean preAcknowledge = RandomUtil.randomBoolean();
String loadBalancingPolicyClassName = RandomUtil.randomString();
boolean useGlobalPools = RandomUtil.randomBoolean();
int scheduledThreadPoolMaxSize = RandomUtil.randomPositiveInt();
int threadPoolMaxSize = RandomUtil.randomPositiveInt();
long retryInterval = RandomUtil.randomPositiveLong();
double retryIntervalMultiplier = RandomUtil.randomDouble();
int reconnectAttempts = RandomUtil.randomPositiveInt();
cf.setClientFailureCheckPeriod(clientFailureCheckPeriod);
cf.setConnectionTTL(connectionTTL);
cf.setCallTimeout(callTimeout);
cf.setMinLargeMessageSize(minLargeMessageSize);
cf.setConsumerWindowSize(consumerWindowSize);
cf.setConsumerMaxRate(consumerMaxRate);
cf.setConfirmationWindowSize(confirmationWindowSize);
cf.setProducerMaxRate(producerMaxRate);
cf.setBlockOnAcknowledge(blockOnAcknowledge);
cf.setBlockOnDurableSend(blockOnDurableSend);
cf.setBlockOnNonDurableSend(blockOnNonDurableSend);
cf.setAutoGroup(autoGroup);
cf.setPreAcknowledge(preAcknowledge);
cf.setConnectionLoadBalancingPolicyClassName(loadBalancingPolicyClassName);
cf.setUseGlobalPools(useGlobalPools);
cf.setScheduledThreadPoolMaxSize(scheduledThreadPoolMaxSize);
cf.setThreadPoolMaxSize(threadPoolMaxSize);
cf.setRetryInterval(retryInterval);
cf.setRetryIntervalMultiplier(retryIntervalMultiplier);
cf.setReconnectAttempts(reconnectAttempts);
Assert.assertEquals(clientFailureCheckPeriod, cf.getClientFailureCheckPeriod());
Assert.assertEquals(connectionTTL, cf.getConnectionTTL());
Assert.assertEquals(callTimeout, cf.getCallTimeout());
Assert.assertEquals(minLargeMessageSize, cf.getMinLargeMessageSize());
Assert.assertEquals(consumerWindowSize, cf.getConsumerWindowSize());
Assert.assertEquals(consumerMaxRate, cf.getConsumerMaxRate());
Assert.assertEquals(confirmationWindowSize, cf.getConfirmationWindowSize());
Assert.assertEquals(producerMaxRate, cf.getProducerMaxRate());
Assert.assertEquals(blockOnAcknowledge, cf.isBlockOnAcknowledge());
Assert.assertEquals(blockOnDurableSend, cf.isBlockOnDurableSend());
Assert.assertEquals(blockOnNonDurableSend, cf.isBlockOnNonDurableSend());
Assert.assertEquals(autoGroup, cf.isAutoGroup());
Assert.assertEquals(preAcknowledge, cf.isPreAcknowledge());
Assert.assertEquals(loadBalancingPolicyClassName, cf.getConnectionLoadBalancingPolicyClassName());
Assert.assertEquals(useGlobalPools, cf.isUseGlobalPools());
Assert.assertEquals(scheduledThreadPoolMaxSize, cf.getScheduledThreadPoolMaxSize());
Assert.assertEquals(threadPoolMaxSize, cf.getThreadPoolMaxSize());
Assert.assertEquals(retryInterval, cf.getRetryInterval());
Assert.assertEquals(retryIntervalMultiplier, cf.getRetryIntervalMultiplier(), 0.0001);
Assert.assertEquals(reconnectAttempts, cf.getReconnectAttempts());
cf.close();
}
@Test
public void testDeserializationOptions() throws Exception {
testDeserializationOptions(false, false);
}
@Test
public void testDeserializationOptionsJndi() throws Exception {
testDeserializationOptions(true, false);
}
@Test
public void testDeserializationOptionsBrowser() throws Exception {
testDeserializationOptions(false, true);
}
@Test
public void testDeserializationOptionsJndiBrowser() throws Exception {
testDeserializationOptions(true, true);
}
private void testDeserializationOptions(boolean useJndi, boolean useBrowser) throws Exception {
String qname = "SerialTestQueue";
SimpleString qaddr = new SimpleString(qname);
liveService.createQueue(qaddr, RoutingType.ANYCAST, qaddr, null, true, false);
//default ok
String blackList = null;
String whiteList = null;
Object obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof TestClass1);
//not in the white list
blackList = "java.lang";
whiteList = "some.other.package1";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
//but String always trusted
obj = receiveObjectMessage(blackList, whiteList, qname, new String("hello"), useJndi, useBrowser);
assertTrue("java.lang.String always trusted ", "hello".equals(obj));
//in the blacklist
blackList = "org.apache.activemq.artemis.tests.integration.jms.serializables";
whiteList = "org.apache.activemq.artemis.tests.integration.jms.serializables";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
//black list parent package
blackList = "org.apache.activemq.artemis";
whiteList = "org.apache.activemq.artemis.tests.integration.jms.serializables";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
//in white list
blackList = "some.other.package";
whiteList = "org.apache.activemq.artemis.tests.integration.jms.serializables";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof TestClass1);
//parent in white list
blackList = "some.other.package";
whiteList = "org.apache.activemq.artemis.tests.integration.jms";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof TestClass1);
//sub package in white list
blackList = "some.other.package";
whiteList = "org.apache.activemq.artemis.tests.integration.jms.serializables.pkg1";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
//wild card white list but black listed
blackList = "org.apache.activemq.artemis.tests.integration.jms.serializables";
whiteList = "*";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
//wild card white list and not black listed
blackList = "some.other.package";
whiteList = "*";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof TestClass1);
//wild card black list
blackList = "*";
whiteList = "*";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), useJndi, useBrowser);
assertTrue("Object is " + obj, obj instanceof JMSException);
}
@Test
public void testSystemPropertyBlackWhiteListDefault() throws Exception {
System.setProperty(ObjectInputStreamWithClassLoader.BLACKLIST_PROPERTY, "*");
System.setProperty(ObjectInputStreamWithClassLoader.WHITELIST_PROPERTY, "some.other.package");
String qname = "SerialTestQueue";
SimpleString qaddr = new SimpleString(qname);
liveService.createQueue(qaddr, RoutingType.ANYCAST, qaddr, null, true, false);
try {
String blackList = null;
String whiteList = null;
Object obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), false, false);
assertTrue("Object is " + obj, obj instanceof JMSException);
//but String always trusted
obj = receiveObjectMessage(blackList, whiteList, qname, new String("hello"), false, false);
assertTrue("java.lang.String always trusted " + obj, "hello".equals(obj));
//override
blackList = "some.other.package";
whiteList = "org.apache.activemq.artemis.tests.integration";
obj = receiveObjectMessage(blackList, whiteList, qname, new TestClass1(), false, false);
assertTrue("Object is " + obj, obj instanceof TestClass1);
//but String always trusted
obj = receiveObjectMessage(blackList, whiteList, qname, new String("hello"), false, false);
assertTrue("java.lang.String always trusted " + obj, "hello".equals(obj));
} finally {
System.clearProperty(ObjectInputStreamWithClassLoader.BLACKLIST_PROPERTY);
System.clearProperty(ObjectInputStreamWithClassLoader.WHITELIST_PROPERTY);
}
}
private Object receiveObjectMessage(String blackList,
String whiteList,
String qname,
Serializable obj,
boolean useJndi,
boolean useBrowser) throws Exception {
sendObjectMessage(qname, obj);
StringBuilder query = new StringBuilder("");
if (blackList != null) {
query.append("?");
query.append("deserializationBlackList=");
query.append(blackList);
if (whiteList != null) {
query.append("&");
query.append("deserializationWhiteList=");
query.append(whiteList);
}
} else {
if (whiteList != null) {
query.append("?deserializationWhiteList=");
query.append(whiteList);
}
}
System.out.println("query string: " + query);
ActiveMQConnectionFactory factory = null;
if (useJndi) {
Hashtable<String, Object> props = new Hashtable<>();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
props.put("connectionFactory.VmConnectionFactory", "vm://0" + query);
Context ctx = new InitialContext(props);
factory = (ActiveMQConnectionFactory) ctx.lookup("VmConnectionFactory");
} else {
factory = new ActiveMQConnectionFactory("vm://0" + query);
}
try (Connection connection = factory.createConnection()) {
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(qname);
Object result = null;
if (useBrowser) {
QueueBrowser browser = session.createBrowser(queue);
ObjectMessage objMessage = (ObjectMessage) browser.getEnumeration().nextElement();
//drain message before triggering deserialization
MessageConsumer consumer = session.createConsumer(queue);
consumer.receive(5000);
result = objMessage.getObject();
} else {
MessageConsumer consumer = session.createConsumer(queue);
ObjectMessage objMessage = (ObjectMessage) consumer.receive(5000);
assertNotNull(objMessage);
result = objMessage.getObject();
}
return result;
} catch (Exception e) {
return e;
}
}
private void sendObjectMessage(String qname, Serializable obj) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://0");
Connection connection = factory.createConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = session.createQueue(qname);
MessageProducer producer = session.createProducer(q);
ObjectMessage objMessage = session.createObjectMessage();
objMessage.setObject(obj);
producer.send(objMessage);
} finally {
connection.close();
}
}
private void testSettersThrowException(final ActiveMQConnectionFactory cf) {
String clientID = RandomUtil.randomString();
long clientFailureCheckPeriod = RandomUtil.randomPositiveLong();
long connectionTTL = RandomUtil.randomPositiveLong();
long callTimeout = RandomUtil.randomPositiveLong();
int minLargeMessageSize = RandomUtil.randomPositiveInt();
int consumerWindowSize = RandomUtil.randomPositiveInt();
int consumerMaxRate = RandomUtil.randomPositiveInt();
int confirmationWindowSize = RandomUtil.randomPositiveInt();
int producerMaxRate = RandomUtil.randomPositiveInt();
boolean blockOnAcknowledge = RandomUtil.randomBoolean();
boolean blockOnDurableSend = RandomUtil.randomBoolean();
boolean blockOnNonDurableSend = RandomUtil.randomBoolean();
boolean autoGroup = RandomUtil.randomBoolean();
boolean preAcknowledge = RandomUtil.randomBoolean();
String loadBalancingPolicyClassName = RandomUtil.randomString();
int dupsOKBatchSize = RandomUtil.randomPositiveInt();
int transactionBatchSize = RandomUtil.randomPositiveInt();
boolean useGlobalPools = RandomUtil.randomBoolean();
int scheduledThreadPoolMaxSize = RandomUtil.randomPositiveInt();
int threadPoolMaxSize = RandomUtil.randomPositiveInt();
long retryInterval = RandomUtil.randomPositiveLong();
double retryIntervalMultiplier = RandomUtil.randomDouble();
int reconnectAttempts = RandomUtil.randomPositiveInt();
boolean enableSharedClientID = true;
try {
cf.setClientID(clientID);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setClientFailureCheckPeriod(clientFailureCheckPeriod);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setConnectionTTL(connectionTTL);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setCallTimeout(callTimeout);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setMinLargeMessageSize(minLargeMessageSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setConsumerWindowSize(consumerWindowSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setConsumerMaxRate(consumerMaxRate);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setConfirmationWindowSize(confirmationWindowSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setProducerMaxRate(producerMaxRate);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setBlockOnAcknowledge(blockOnAcknowledge);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setBlockOnDurableSend(blockOnDurableSend);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setBlockOnNonDurableSend(blockOnNonDurableSend);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setAutoGroup(autoGroup);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setPreAcknowledge(preAcknowledge);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setConnectionLoadBalancingPolicyClassName(loadBalancingPolicyClassName);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setDupsOKBatchSize(dupsOKBatchSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setTransactionBatchSize(transactionBatchSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setUseGlobalPools(useGlobalPools);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setScheduledThreadPoolMaxSize(scheduledThreadPoolMaxSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setThreadPoolMaxSize(threadPoolMaxSize);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setRetryInterval(retryInterval);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setRetryIntervalMultiplier(retryIntervalMultiplier);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
try {
cf.setReconnectAttempts(reconnectAttempts);
Assert.fail("Should throw exception");
} catch (IllegalStateException e) {
// OK
}
cf.getStaticConnectors();
cf.getClientID();
cf.getClientFailureCheckPeriod();
cf.getConnectionTTL();
cf.getCallTimeout();
cf.getMinLargeMessageSize();
cf.getConsumerWindowSize();
cf.getConsumerMaxRate();
cf.getConfirmationWindowSize();
cf.getProducerMaxRate();
cf.isBlockOnAcknowledge();
cf.isBlockOnDurableSend();
cf.isBlockOnNonDurableSend();
cf.isAutoGroup();
cf.isPreAcknowledge();
cf.getConnectionLoadBalancingPolicyClassName();
cf.getDupsOKBatchSize();
cf.getTransactionBatchSize();
cf.isUseGlobalPools();
cf.getScheduledThreadPoolMaxSize();
cf.getThreadPoolMaxSize();
cf.getRetryInterval();
cf.getRetryIntervalMultiplier();
cf.getReconnectAttempts();
cf.close();
}
private void assertFactoryParams(final ActiveMQConnectionFactory cf,
final TransportConfiguration[] staticConnectors,
final DiscoveryGroupConfiguration discoveryGroupConfiguration,
final String clientID,
final long clientFailureCheckPeriod,
final long connectionTTL,
final long callTimeout,
final long callFailoverTimeout,
final int minLargeMessageSize,
final int consumerWindowSize,
final int consumerMaxRate,
final int confirmationWindowSize,
final int producerMaxRate,
final boolean blockOnAcknowledge,
final boolean blockOnDurableSend,
final boolean blockOnNonDurableSend,
final boolean autoGroup,
final boolean preAcknowledge,
final String loadBalancingPolicyClassName,
final int dupsOKBatchSize,
final int transactionBatchSize,
final long initialWaitTimeout,
final boolean useGlobalPools,
final int scheduledThreadPoolMaxSize,
final int threadPoolMaxSize,
final long retryInterval,
final double retryIntervalMultiplier,
final int reconnectAttempts) {
TransportConfiguration[] cfStaticConnectors = cf.getStaticConnectors();
if (staticConnectors == null) {
Assert.assertNull(staticConnectors);
} else {
Assert.assertEquals(staticConnectors.length, cfStaticConnectors.length);
for (int i = 0; i < staticConnectors.length; i++) {
Assert.assertEquals(staticConnectors[i], cfStaticConnectors[i]);
}
}
Assert.assertEquals(cf.getClientID(), clientID);
Assert.assertEquals(cf.getClientFailureCheckPeriod(), clientFailureCheckPeriod);
Assert.assertEquals(cf.getConnectionTTL(), connectionTTL);
Assert.assertEquals(cf.getCallTimeout(), callTimeout);
Assert.assertEquals(cf.getCallFailoverTimeout(), callFailoverTimeout);
Assert.assertEquals(cf.getMinLargeMessageSize(), minLargeMessageSize);
Assert.assertEquals(cf.getConsumerWindowSize(), consumerWindowSize);
Assert.assertEquals(cf.getConsumerMaxRate(), consumerMaxRate);
Assert.assertEquals(cf.getConfirmationWindowSize(), confirmationWindowSize);
Assert.assertEquals(cf.getProducerMaxRate(), producerMaxRate);
Assert.assertEquals(cf.isBlockOnAcknowledge(), blockOnAcknowledge);
Assert.assertEquals(cf.isBlockOnDurableSend(), blockOnDurableSend);
Assert.assertEquals(cf.isBlockOnNonDurableSend(), blockOnNonDurableSend);
Assert.assertEquals(cf.isAutoGroup(), autoGroup);
Assert.assertEquals(cf.isPreAcknowledge(), preAcknowledge);
Assert.assertEquals(cf.getConnectionLoadBalancingPolicyClassName(), loadBalancingPolicyClassName);
Assert.assertEquals(cf.getDupsOKBatchSize(), dupsOKBatchSize);
Assert.assertEquals(cf.getTransactionBatchSize(), transactionBatchSize);
Assert.assertEquals(cf.isUseGlobalPools(), useGlobalPools);
Assert.assertEquals(cf.getScheduledThreadPoolMaxSize(), scheduledThreadPoolMaxSize);
Assert.assertEquals(cf.getThreadPoolMaxSize(), threadPoolMaxSize);
Assert.assertEquals(cf.getRetryInterval(), retryInterval);
Assert.assertEquals(cf.getRetryIntervalMultiplier(), retryIntervalMultiplier, 0.00001);
Assert.assertEquals(cf.getReconnectAttempts(), reconnectAttempts);
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
startServer();
}
private void startServer() throws Exception {
liveTC = new TransportConfiguration(INVM_CONNECTOR_FACTORY);
Map<String, TransportConfiguration> connectors = new HashMap<>();
connectors.put(liveTC.getName(), liveTC);
List<String> connectorNames = new ArrayList<>();
connectorNames.add(liveTC.getName());
Configuration liveConf = createBasicConfig().addAcceptorConfiguration(new TransportConfiguration(INVM_ACCEPTOR_FACTORY)).setConnectorConfigurations(connectors).setHAPolicyConfiguration(new SharedStoreMasterPolicyConfiguration());
final long broadcastPeriod = 250;
final String bcGroupName = "bc1";
final int localBindPort = 5432;
BroadcastGroupConfiguration bcConfig1 = new BroadcastGroupConfiguration().setName(bcGroupName).setBroadcastPeriod(broadcastPeriod).setConnectorInfos(connectorNames).setEndpointFactory(new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(groupPort).setLocalBindPort(localBindPort));
List<BroadcastGroupConfiguration> bcConfigs1 = new ArrayList<>();
bcConfigs1.add(bcConfig1);
liveConf.setBroadcastGroupConfigurations(bcConfigs1);
liveService = addServer(ActiveMQServers.newActiveMQServer(liveConf, false));
liveService.start();
}
}