|
19 | 19 | import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities; |
20 | 20 | import org.eclipse.paho.client.mqttv3.test.utilities.Utility; |
21 | 21 | import org.junit.AfterClass; |
| 22 | +import org.junit.Assert; |
22 | 23 | import org.junit.BeforeClass; |
23 | 24 | import org.junit.Test; |
24 | 25 |
|
25 | | - |
26 | 26 | /** |
27 | 27 | * Tests mqtt topic wildcards |
28 | 28 | */ |
29 | 29 | public class MqttTopicTest { |
30 | 30 |
|
31 | | - static final Class<?> cclass = MqttTopicTest.class; |
32 | | - private static final String className = cclass.getName(); |
33 | | - private static final Logger log = Logger.getLogger(className); |
34 | | - |
35 | | - @BeforeClass |
36 | | - public static void setUpBeforeClass() throws Exception { |
37 | | - String methodName = Utility.getMethodName(); |
38 | | - LoggingUtilities.banner(log, cclass, methodName); |
39 | | - } |
40 | | - |
41 | | - @AfterClass |
42 | | - public static void tearDownAfterClass() throws Exception { |
43 | | - String methodName = Utility.getMethodName(); |
44 | | - LoggingUtilities.banner(log, cclass, methodName); |
45 | | - } |
46 | | - |
47 | | - |
48 | | - @Test |
49 | | - public void testValidTopicFilterWildcards() throws Exception { |
50 | | - String methodName = Utility.getMethodName(); |
51 | | - LoggingUtilities.banner(log, cclass, methodName); |
52 | | - String[] topics = new String[] { |
53 | | - "+", "+/+", "+/foo", |
54 | | - "+/tennis/#", |
55 | | - "foo/+", "foo/+/bar", |
56 | | - "/+", "/+/sport/+/player1", |
57 | | - "#", "/#", |
58 | | - "sport/#", |
59 | | - "sport/tennis/#" |
60 | | - }; |
61 | | - |
62 | | - for(String topic:topics){ |
63 | | - MqttTopic.validate(topic, true); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - |
68 | | - @Test(expected = IllegalArgumentException.class) |
69 | | - public void testInvalidTopicFilterWildcards1() throws Exception { |
70 | | - String methodName = Utility.getMethodName(); |
71 | | - LoggingUtilities.banner(log, cclass, methodName); |
72 | | - MqttTopic.validate("sport/tennis#", true); |
73 | | - } |
74 | | - |
75 | | - @Test(expected = IllegalArgumentException.class) |
76 | | - public void testInvalidTopicFilterWildcards2() throws Exception { |
77 | | - String methodName = Utility.getMethodName(); |
78 | | - LoggingUtilities.banner(log, cclass, methodName); |
79 | | - MqttTopic.validate("sport/tennis/#/ranking", true); |
80 | | - } |
81 | | - |
82 | | - @Test(expected = IllegalArgumentException.class) |
83 | | - public void testInvalidTopicFilterWildcards3() throws Exception { |
84 | | - String methodName = Utility.getMethodName(); |
85 | | - LoggingUtilities.banner(log, cclass, methodName); |
86 | | - MqttTopic.validate("sport+", true); |
87 | | - } |
88 | | - |
89 | | - @Test(expected = IllegalArgumentException.class) |
90 | | - public void testInvalidTopicFilterWildcards4() throws Exception { |
91 | | - String methodName = Utility.getMethodName(); |
92 | | - LoggingUtilities.banner(log, cclass, methodName); |
93 | | - MqttTopic.validate("sport/+aa", true); |
94 | | - } |
95 | | - |
96 | | - @Test(expected = IllegalArgumentException.class) |
97 | | - public void testInvalidTopicFilterWildcards5() throws Exception { |
98 | | - String methodName = Utility.getMethodName(); |
99 | | - LoggingUtilities.banner(log, cclass, methodName); |
100 | | - MqttTopic.validate("sport/#/ball/+/aa", true); |
101 | | - } |
102 | | - |
| 31 | + static final Class<?> cclass = MqttTopicTest.class; |
| 32 | + private static final String className = cclass.getName(); |
| 33 | + private static final Logger log = Logger.getLogger(className); |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void setUpBeforeClass() throws Exception { |
| 37 | + String methodName = Utility.getMethodName(); |
| 38 | + LoggingUtilities.banner(log, cclass, methodName); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterClass |
| 42 | + public static void tearDownAfterClass() throws Exception { |
| 43 | + String methodName = Utility.getMethodName(); |
| 44 | + LoggingUtilities.banner(log, cclass, methodName); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testValidTopicFilterWildcards() throws Exception { |
| 49 | + String methodName = Utility.getMethodName(); |
| 50 | + LoggingUtilities.banner(log, cclass, methodName); |
| 51 | + String[] topics = new String[] { "+", "+/+", "+/foo", "+/tennis/#", "foo/+", "foo/+/bar", "/+", |
| 52 | + "/+/sport/+/player1", "#", "/#", "sport/#", "sport/tennis/#" }; |
| 53 | + |
| 54 | + for (String topic : topics) { |
| 55 | + MqttTopic.validate(topic, true); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testMatchedTopicFilterWildcards() throws Exception { |
| 61 | + String methodName = Utility.getMethodName(); |
| 62 | + LoggingUtilities.banner(log, cclass, methodName); |
| 63 | + String[][] matchingTopics = new String[][] { { "sport/tennis/player1/#", "sport/tennis/player1" }, |
| 64 | + { "sport/tennis/player1/#", "sport/tennis/player1/ranking" }, |
| 65 | + { "sport/tennis/player1/#", "sport/tennis/player1/score/wimbledon" }, { "sport/#", "sport" }, |
| 66 | + { "#", "sport/tennis/player1" } }; |
| 67 | + |
| 68 | + for (String[] pair : matchingTopics) { |
| 69 | + Assert.assertTrue(pair[0] + " should match " + pair[1], MqttTopic.isMatched(pair[0], pair[1])); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testNonMatchedTopicFilterWildcards() throws Exception { |
| 75 | + String methodName = Utility.getMethodName(); |
| 76 | + LoggingUtilities.banner(log, cclass, methodName); |
| 77 | + String[][] matchingTopics = new String[][] { { "sport/tennis/player1/#", "sport/tennis/player2" }, |
| 78 | + { "sport1/#", "sport2" }, { "sport/tennis1/player/#", "sport/tennis2/player" } }; |
| 79 | + |
| 80 | + for (String[] pair : matchingTopics) { |
| 81 | + Assert.assertFalse(pair[0] + " should NOT match " + pair[1], MqttTopic.isMatched(pair[0], pair[1])); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expected = IllegalArgumentException.class) |
| 86 | + public void testInvalidTopicFilterWildcards1() throws Exception { |
| 87 | + String methodName = Utility.getMethodName(); |
| 88 | + LoggingUtilities.banner(log, cclass, methodName); |
| 89 | + MqttTopic.validate("sport/tennis#", true); |
| 90 | + } |
| 91 | + |
| 92 | + @Test(expected = IllegalArgumentException.class) |
| 93 | + public void testInvalidTopicFilterWildcards2() throws Exception { |
| 94 | + String methodName = Utility.getMethodName(); |
| 95 | + LoggingUtilities.banner(log, cclass, methodName); |
| 96 | + MqttTopic.validate("sport/tennis/#/ranking", true); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected = IllegalArgumentException.class) |
| 100 | + public void testInvalidTopicFilterWildcards3() throws Exception { |
| 101 | + String methodName = Utility.getMethodName(); |
| 102 | + LoggingUtilities.banner(log, cclass, methodName); |
| 103 | + MqttTopic.validate("sport+", true); |
| 104 | + } |
| 105 | + |
| 106 | + @Test(expected = IllegalArgumentException.class) |
| 107 | + public void testInvalidTopicFilterWildcards4() throws Exception { |
| 108 | + String methodName = Utility.getMethodName(); |
| 109 | + LoggingUtilities.banner(log, cclass, methodName); |
| 110 | + MqttTopic.validate("sport/+aa", true); |
| 111 | + } |
| 112 | + |
| 113 | + @Test(expected = IllegalArgumentException.class) |
| 114 | + public void testInvalidTopicFilterWildcards5() throws Exception { |
| 115 | + String methodName = Utility.getMethodName(); |
| 116 | + LoggingUtilities.banner(log, cclass, methodName); |
| 117 | + MqttTopic.validate("sport/#/ball/+/aa", true); |
| 118 | + } |
| 119 | + |
103 | 120 | } |
0 commit comments