/
StompV12Test.java
2399 lines (1709 loc) · 80.6 KB
/
StompV12Test.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.stomp.v12;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.protocol.stomp.Stomp;
import org.apache.activemq.artemis.core.protocol.stomp.StompConnection;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.tests.integration.IntegrationTestLogger;
import org.apache.activemq.artemis.tests.integration.stomp.StompTestBase;
import org.apache.activemq.artemis.tests.integration.stomp.util.ClientStompFrame;
import org.apache.activemq.artemis.tests.integration.stomp.util.StompClientConnection;
import org.apache.activemq.artemis.tests.integration.stomp.util.StompClientConnectionFactory;
import org.apache.activemq.artemis.tests.integration.stomp.util.StompClientConnectionV11;
import org.apache.activemq.artemis.tests.integration.stomp.util.StompClientConnectionV12;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/**
* Testing Stomp version 1.2 functionalities
*/
@RunWith(Parameterized.class)
public class StompV12Test extends StompTestBase {
private static final transient IntegrationTestLogger log = IntegrationTestLogger.LOGGER;
public static final String CLIENT_ID = "myclientid";
private StompClientConnectionV12 conn;
private URI v10Uri;
private URI v11Uri;
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{"ws+v12.stomp"}, {"tcp+v12.stomp"}});
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
v10Uri = new URI(uri.toString().replace("v12", "v10"));
v11Uri = new URI(uri.toString().replace("v12", "v11"));
conn = (StompClientConnectionV12) StompClientConnectionFactory.createClientConnection(uri);
}
@Override
@After
public void tearDown() throws Exception {
try {
boolean connected = conn != null && conn.isConnected();
log.debug("Connection 1.2 : " + connected);
if (connected) {
conn.disconnect();
}
} finally {
super.tearDown();
conn.closeTransport();
}
}
@Test
public void testConnection() throws Exception {
server.getSecurityStore().setSecurityEnabled(true);
StompClientConnection connection = StompClientConnectionFactory.createClientConnection(v10Uri);
connection.connect(defUser, defPass);
Assert.assertTrue(connection.isConnected());
Assert.assertEquals("1.0", connection.getVersion());
connection.disconnect();
connection = StompClientConnectionFactory.createClientConnection(uri);
connection.connect(defUser, defPass);
Assert.assertTrue(connection.isConnected());
Assert.assertEquals("1.2", connection.getVersion());
connection.disconnect();
connection = StompClientConnectionFactory.createClientConnection(uri);
connection.connect();
Assert.assertFalse(connection.isConnected());
//new way of connection
StompClientConnectionV11 conn = (StompClientConnectionV11) StompClientConnectionFactory.createClientConnection(v11Uri);
conn.connect1(defUser, defPass);
Assert.assertTrue(conn.isConnected());
conn.disconnect();
}
@Test
public void testConnectionAsInSpec() throws Exception {
StompClientConnection conn = StompClientConnectionFactory.createClientConnection(v10Uri);
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.2");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
ClientStompFrame reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("1.2", reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
//need 1.2 client
conn = StompClientConnectionFactory.createClientConnection(uri);
frame = conn.createFrame(Stomp.Commands.STOMP);
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.2");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("1.2", reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
}
@Test
public void testNegotiation() throws Exception {
StompClientConnection conn = StompClientConnectionFactory.createClientConnection(v10Uri);
// case 1 accept-version absent. It is a 1.0 connect
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
ClientStompFrame reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
//reply headers: version, session, server
Assert.assertEquals(null, reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
// case 2 accept-version=1.0, result: 1.0
conn = StompClientConnectionFactory.createClientConnection(v11Uri);
frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
//reply headers: version, session, server
Assert.assertEquals("1.0", reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
// case 3 accept-version=1.1, result: 1.1
conn = StompClientConnectionFactory.createClientConnection(v11Uri);
frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.1");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
//reply headers: version, session, server
Assert.assertEquals("1.1", reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
// case 4 accept-version=1.0,1.1,1.3, result 1.2
conn = StompClientConnectionFactory.createClientConnection(v11Uri);
frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.3");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
//reply headers: version, session, server
Assert.assertEquals("1.1", reply.getHeader(Stomp.Headers.Error.VERSION));
conn.disconnect();
// case 5 accept-version=1.3, result error
conn = StompClientConnectionFactory.createClientConnection(v11Uri);
frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.3");
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1");
frame.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser);
frame.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.ERROR, reply.getCommand());
conn.disconnect();
System.out.println("Got error frame " + reply);
}
@Test
public void testSendAndReceive() throws Exception {
conn.connect(defUser, defPass);
ClientStompFrame response = send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World 1!");
Assert.assertNull(response);
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World 2!", true);
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
ClientStompFrame frame = newConn.receiveFrame();
System.out.println("received " + frame);
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
Assert.assertEquals("a-sub", frame.getHeader(Stomp.Headers.Message.SUBSCRIPTION));
//'auto' ack mode doesn't require 'ack' header
Assert.assertNull(frame.getHeader(Stomp.Headers.Subscribe.ACK_MODE));
Assert.assertEquals(getQueuePrefix() + getQueueName(), frame.getHeader(Stomp.Headers.Subscribe.DESTINATION));
Assert.assertEquals("Hello World 1!", frame.getBody());
frame = newConn.receiveFrame();
System.out.println("received " + frame);
//unsub
unsubscribe(newConn, "a-sub");
newConn.disconnect();
}
@Test
public void testHeaderContentType() throws Exception {
conn.connect(defUser, defPass);
send(conn, getQueuePrefix() + getQueueName(), "application/xml", "Hello World 1!");
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(v11Uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
ClientStompFrame frame = newConn.receiveFrame();
System.out.println("received " + frame);
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
Assert.assertEquals("application/xml", frame.getHeader(Stomp.Headers.CONTENT_TYPE));
//unsub
unsubscribe(newConn, "a-sub");
newConn.disconnect();
}
@Test
public void testHeaderContentLength() throws Exception {
conn.connect(defUser, defPass);
String body = "Hello World 1!";
String cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.addHeader(Stomp.Headers.CONTENT_LENGTH, cLen)
.setBody(body + "extra");
conn.sendFrame(frame);
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
frame = newConn.receiveFrame();
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
Assert.assertEquals(body, frame.getBody());
Assert.assertEquals(cLen, frame.getHeader(Stomp.Headers.CONTENT_LENGTH));
//send again without content-length header
frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.setBody(body + "extra");
conn.sendFrame(frame);
//receive again. extra should received.
frame = newConn.receiveFrame();
Assert.assertEquals(body + "extra", frame.getBody());
//although sender didn't send the content-length header,
//the server should add it anyway
Assert.assertEquals((body + "extra").getBytes(StandardCharsets.UTF_8).length, Integer.valueOf(frame.getHeader(Stomp.Headers.CONTENT_LENGTH)).intValue());
//unsub
unsubscribe(newConn, "a-sub");
newConn.disconnect();
}
//test that repetitive headers
@Test
public void testHeaderRepetitive() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setAutoCreateQueues(false);
addressSettings.setAutoCreateAddresses(false);
server.getAddressSettingsRepository().addMatch("#", addressSettings);
conn.connect(defUser, defPass);
String body = "Hello World!";
String cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.Subscribe.DESTINATION, "aNonexistentQueue")
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.addHeader(Stomp.Headers.CONTENT_LENGTH, cLen)
.addHeader("foo", "value1")
.addHeader("foo", "value2")
.addHeader("foo", "value3");
frame.setBody(body);
conn.sendFrame(frame);
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub", null, null, true);
frame = newConn.receiveFrame();
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
Assert.assertEquals(body, frame.getBody());
System.out.println("received: " + frame);
Assert.assertEquals("value1", frame.getHeader("foo"));
//unsub
unsubscribe(newConn, "a-sub", true);
newConn.disconnect();
//should get error
body = "Hello World!";
cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, "aNonexistentQueue")
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.addHeader(Stomp.Headers.CONTENT_LENGTH, cLen)
.addHeader(Stomp.Headers.RECEIPT_REQUESTED, "1234")
.setBody(body);
ClientStompFrame reply = conn.sendFrame(frame);
// TODO this is broken because queue auto-creation is always on
Assert.assertEquals(Stomp.Responses.ERROR, reply.getCommand());
}
//padding shouldn't be trimmed
@Test
public void testHeadersPadding() throws Exception {
conn.connect(defUser, defPass);
String body = "<p>Hello World!</p>";
String cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.addHeader(Stomp.Headers.CONTENT_LENGTH, cLen)
.addHeader(" header1", "value1 ")
.addHeader(" header2", "value2 ")
.addHeader("header3 ", " value3")
.addHeader(" header4 ", " value4 ")
.addHeader(" header 5 ", " value 5 ")
.addHeader("header6", "\t value\t 6 \t")
.setBody(body);
conn.sendFrame(frame);
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
frame = newConn.receiveFrame();
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
Assert.assertEquals(body, frame.getBody());
System.out.println("received: " + frame);
Assert.assertEquals(null, frame.getHeader("header1"));
Assert.assertEquals("value1 ", frame.getHeader(" header1"));
Assert.assertEquals("value2 ", frame.getHeader(" header2"));
Assert.assertEquals(" value3", frame.getHeader("header3 "));
Assert.assertEquals(" value4 ", frame.getHeader(" header4 "));
Assert.assertEquals(" value 5 ", frame.getHeader(" header 5 "));
Assert.assertEquals("\t value\t 6 \t", frame.getHeader("header6"));
//unsub
unsubscribe(newConn, "a-sub");
newConn.disconnect();
}
/**
* Since 1.2 the CR ('\r') needs to be escaped.
*/
@Test
public void testHeaderEncoding() throws Exception {
conn.connect(defUser, defPass);
String body = "Hello World 1!";
String cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
String hKey = "\\rspecial-header\\\\\\n\\c\\r\\n";
String hVal = "\\c\\\\\\ngood\\n\\r";
ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND)
.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName())
.addHeader(Stomp.Headers.CONTENT_TYPE, "application/xml")
.addHeader(Stomp.Headers.CONTENT_LENGTH, cLen)
.addHeader(hKey, hVal)
.setBody(body);
System.out.println("key: |" + hKey + "| val: |" + hVal + "|");
conn.sendFrame(frame);
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
frame = newConn.receiveFrame();
System.out.println("received " + frame);
Assert.assertEquals(Stomp.Responses.MESSAGE, frame.getCommand());
String value = frame.getHeader("\r" + "special-header" + "\\" + "\n" + ":" + "\r\n");
Assert.assertEquals(":" + "\\" + "\n" + "good\n\r", value);
//unsub
unsubscribe(newConn, "a-sub");
newConn.disconnect();
}
/**
* In 1.2, undefined escapes must cause a fatal protocol error.
*/
@Test
public void testHeaderUndefinedEscape() throws Exception {
conn.connect(defUser, defPass);
ClientStompFrame frame = conn.createFrame("SEND");
String body = "Hello World 1!";
String cLen = String.valueOf(body.getBytes(StandardCharsets.UTF_8).length);
frame.addHeader("destination", getQueuePrefix() + getQueueName());
frame.addHeader("content-type", "text/plain");
frame.addHeader("content-length", cLen);
String hKey = "undefined-escape";
String hVal = "is\\ttab";
frame.addHeader(hKey, hVal);
System.out.println("key: |" + hKey + "| val: |" + hVal + "|");
frame.setBody(body);
conn.sendFrame(frame);
ClientStompFrame error = conn.receiveFrame();
System.out.println("received " + error);
String desc = "Should have received an ERROR for undefined escape sequence";
Assert.assertNotNull(desc, error);
Assert.assertEquals(desc, "ERROR", error.getCommand());
waitDisconnect(conn);
Assert.assertFalse("Should be disconnected in STOMP 1.2 after ERROR", conn.isConnected());
}
@Test
public void testHeartBeat() throws Exception {
StompClientConnection conn = StompClientConnectionFactory.createClientConnection(uri);
//no heart beat at all if heat-beat absent
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass);
ClientStompFrame reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Thread.sleep(5000);
Assert.assertEquals(0, conn.getFrameQueueSize());
conn.disconnect();
//no heart beat for (0,0)
conn = StompClientConnectionFactory.createClientConnection(uri);
frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "0,0")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("0,30000", reply.getHeader(Stomp.Headers.Connect.HEART_BEAT));
Thread.sleep(5000);
Assert.assertEquals(0, conn.getFrameQueueSize());
conn.disconnect();
//heart-beat (1,0), should receive a min client ping accepted by server
conn = StompClientConnectionFactory.createClientConnection(uri);
frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "1,0")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.2");
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("0,500", reply.getHeader(Stomp.Headers.Connect.HEART_BEAT));
Thread.sleep(2000);
//now server side should be disconnected because we didn't send ping for 2 sec
//send will fail
try {
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World");
Assert.fail("connection should have been destroyed by now");
} catch (IOException e) {
//ignore
}
//heart-beat (1,0), start a ping, then send a message, should be ok.
conn = StompClientConnectionFactory.createClientConnection(uri);
frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "1,0")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("0,500", reply.getHeader(Stomp.Headers.Connect.HEART_BEAT));
conn.startPinger(500);
Thread.sleep(2000);
//send will be ok
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World");
conn.stopPinger();
conn.disconnect();
}
//server ping
@Test
public void testHeartBeat2() throws Exception {
//heart-beat (1,1)
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "1,1")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
ClientStompFrame reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("500,500", reply.getHeader(Stomp.Headers.Connect.HEART_BEAT));
conn.disconnect();
//heart-beat (500,1000)
conn = (StompClientConnectionV12) StompClientConnectionFactory.createClientConnection(uri);
frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "500,1000")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
reply = conn.sendFrame(frame);
Assert.assertEquals(Stomp.Responses.CONNECTED, reply.getCommand());
Assert.assertEquals("1000,500", reply.getHeader(Stomp.Headers.Connect.HEART_BEAT));
conn.startPinger(500);
Thread.sleep(10000);
//now check the frame size
int size = conn.getServerPingNumber();
System.out.println("ping received: " + size);
Assert.assertTrue("size: " + size, size > 5);
//now server side should be disconnected because we didn't send ping for 2 sec
//send will be ok
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World");
conn.disconnect();
}
@Test
public void testSendWithHeartBeatsAndReceive() throws Exception {
StompClientConnection newConn = null;
try {
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT);
frame.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "500,1000")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
conn.sendFrame(frame);
conn.startPinger(500);
for (int i = 0; i < 10; i++) {
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World " + i + "!");
Thread.sleep(500);
}
// subscribe
newConn = StompClientConnectionFactory.createClientConnection(uri);
newConn.connect(defUser, defPass);
subscribe(newConn, "a-sub");
int cnt = 0;
frame = newConn.receiveFrame();
while (frame != null) {
cnt++;
Thread.sleep(500);
frame = newConn.receiveFrame(5000);
}
Assert.assertEquals(10, cnt);
// unsub
unsubscribe(newConn, "a-sub");
} finally {
if (newConn != null)
newConn.disconnect();
conn.disconnect();
}
}
@Test
public void testSendAndReceiveWithHeartBeats() throws Exception {
conn.connect(defUser, defPass);
for (int i = 0; i < 10; i++) {
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World " + i + "!");
Thread.sleep(500);
}
//subscribe
StompClientConnection newConn = StompClientConnectionFactory.createClientConnection(v11Uri);
try {
ClientStompFrame frame = newConn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "500,1000")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1");
newConn.sendFrame(frame);
newConn.startPinger(500);
Thread.sleep(500);
subscribe(newConn, "a-sub");
int cnt = 0;
frame = newConn.receiveFrame();
while (frame != null) {
cnt++;
Thread.sleep(500);
frame = newConn.receiveFrame(5000);
}
Assert.assertEquals(10, cnt);
// unsub
unsubscribe(newConn, "a-sub");
} finally {
newConn.disconnect();
}
}
@Test
public void testSendWithHeartBeatsAndReceiveWithHeartBeats() throws Exception {
StompClientConnection newConn = null;
try {
ClientStompFrame frame = conn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "500,1000")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
conn.sendFrame(frame);
conn.startPinger(500);
for (int i = 0; i < 10; i++) {
send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World " + i + "!");
Thread.sleep(500);
}
// subscribe
newConn = StompClientConnectionFactory.createClientConnection(uri);
frame = newConn.createFrame(Stomp.Commands.CONNECT)
.addHeader(Stomp.Headers.Connect.HOST, "127.0.0.1")
.addHeader(Stomp.Headers.Connect.LOGIN, this.defUser)
.addHeader(Stomp.Headers.Connect.PASSCODE, this.defPass)
.addHeader(Stomp.Headers.Connect.HEART_BEAT, "500,1000")
.addHeader(Stomp.Headers.ACCEPT_VERSION, "1.0,1.1,1.2");
newConn.sendFrame(frame);
newConn.startPinger(500);
Thread.sleep(500);
subscribe(newConn, "a-sub");
int cnt = 0;
frame = newConn.receiveFrame();
while (frame != null) {
cnt++;
Thread.sleep(500);
frame = newConn.receiveFrame(5000);
}
Assert.assertEquals(10, cnt);
// unsub
unsubscribe(newConn, "a-sub");
} finally {
if (newConn != null)
newConn.disconnect();
conn.disconnect();
}
}
@Test
public void testNack() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client");
sendJmsMessage(getName());
ClientStompFrame frame = conn.receiveFrame();
String messageID = frame.getHeader(Stomp.Headers.Message.MESSAGE_ID);
nack(conn, messageID);
unsubscribe(conn, "sub1");
conn.disconnect();
//Nack makes the message be dropped.
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
Assert.assertNull(message);
}
@Test
public void testNackWithWrongSubId() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client");
sendJmsMessage(getName());
ClientStompFrame frame = conn.receiveFrame();
String messageID = frame.getHeader(Stomp.Headers.Subscribe.ACK_MODE);
nack(conn, messageID + "0");
ClientStompFrame error = conn.receiveFrame();
Assert.assertEquals(Stomp.Responses.ERROR, error.getCommand());
waitDisconnect(conn);
Assert.assertFalse("Should be disconnected in STOMP 1.2 after ERROR", conn.isConnected());
//message should be still there
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
Assert.assertNotNull(message);
}
@Test
public void testNackWithWrongMessageId() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client");
sendJmsMessage(getName());
ClientStompFrame frame = conn.receiveFrame();
Assert.assertNotNull(frame);
Assert.assertNotNull(frame.getHeader(Stomp.Headers.Subscribe.ACK_MODE));
nack(conn, "someother");
ClientStompFrame error = conn.receiveFrame();
System.out.println("Receiver error: " + error);
waitDisconnect(conn);
Assert.assertFalse("Should be disconnected in STOMP 1.2 after ERROR", conn.isConnected());
//message should still there
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
Assert.assertNotNull(message);
}
@Test
public void testAck() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client");
sendJmsMessage(getName());
ClientStompFrame frame = conn.receiveFrame();
String messageID = frame.getHeader(Stomp.Headers.Subscribe.ACK_MODE);
Assert.assertNotNull(messageID);
ack(conn, messageID);
unsubscribe(conn, "sub1");
conn.disconnect();
//Nack makes the message be dropped.
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
Assert.assertNull(message);
}
@Test
public void testAckNoIDHeader() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client-individual");
sendJmsMessage(getName());
ClientStompFrame frame = conn.receiveFrame();
String messageID = frame.getHeader(Stomp.Headers.Subscribe.ACK_MODE);
Assert.assertNotNull(messageID);
ClientStompFrame ackFrame = conn.createFrame(Stomp.Commands.ACK);
conn.sendFrame(ackFrame);
frame = conn.receiveFrame();
Assert.assertEquals(Stomp.Responses.ERROR, frame.getCommand());
waitDisconnect(conn);
Assert.assertFalse("Should be disconnected in STOMP 1.2 after ERROR", conn.isConnected());
//message still there.
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(1000);
Assert.assertNotNull(message);
}
@Test
public void testAckWithWrongMessageId() throws Exception {
conn.connect(defUser, defPass);
subscribe(conn, "sub1", "client");
sendJmsMessage(getName());