Skip to content

Commit abf6fe8

Browse files
authored
Sonar fixes, Doc Fixes and Resolution for Issue #418 (#430)
* Fixing Javadoc Errors * SonarQube Fix: Performance - Method invokes inefficient Number constructor; use static valueOf instead * SonarQube: Security - Array is stored directly * Fixing null pointer issues with new clone method * Issue #418 - Fixing Topic matching for sport/# and sport Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent 635a8c6 commit abf6fe8

2 files changed

Lines changed: 218 additions & 172 deletions

File tree

  • org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test
  • org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/MqttTopicTest.java

Lines changed: 90 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,85 +19,102 @@
1919
import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities;
2020
import org.eclipse.paho.client.mqttv3.test.utilities.Utility;
2121
import org.junit.AfterClass;
22+
import org.junit.Assert;
2223
import org.junit.BeforeClass;
2324
import org.junit.Test;
2425

25-
2626
/**
2727
* Tests mqtt topic wildcards
2828
*/
2929
public class MqttTopicTest {
3030

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+
103120
}

0 commit comments

Comments
 (0)