Skip to content

Commit 6d2906e

Browse files
authored
Merge pull request #440 from kiwigrid/NetworkModule_SPI
Network module SPI
2 parents 399926e + 2e26401 commit 6d2906e

31 files changed

Lines changed: 1335 additions & 495 deletions
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.eclipse.paho.client.mqttv3.internal;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
7+
import org.eclipse.paho.client.mqttv3.MqttException;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNull;
12+
import static org.junit.Assert.assertTrue;
13+
import static org.junit.Assert.fail;
14+
15+
public class NetworkModuleServiceTest {
16+
17+
@Test
18+
public void testValidateURI() {
19+
NetworkModuleService.validateURI("tcp://host_literal:1883");
20+
NetworkModuleService.validateURI("ssl://host_literal:8883");
21+
NetworkModuleService.validateURI("ws://host_literal:80/path/to/ws");
22+
NetworkModuleService.validateURI("wss://host_literal:443/path/to/ws");
23+
}
24+
25+
public void failEmptyUri() {
26+
try {
27+
NetworkModuleService.validateURI("");
28+
fail("Must fail: Empty URI");
29+
} catch (IllegalArgumentException e) {
30+
assertEquals("missing scheme in broker URI: ", e.getMessage());
31+
}
32+
}
33+
34+
@Test
35+
public void failInvalidUri() {
36+
try {
37+
NetworkModuleService.validateURI("no URI at all");
38+
fail("Must fail: Can't parse string to URI");
39+
} catch (IllegalArgumentException e) {
40+
assertEquals("Can't parse string to URI \"no URI at all\"", e.getMessage());
41+
}
42+
}
43+
44+
@Test
45+
public void failWithPathOnTcpUri() {
46+
try {
47+
NetworkModuleService.validateURI("tcp://host_literal:1883/somePath");
48+
fail("Must fail: URI path must be empty");
49+
} catch (IllegalArgumentException e) {
50+
assertEquals("URI path must be empty \"tcp://host_literal:1883/somePath\"", e.getMessage());
51+
}
52+
}
53+
54+
@Test(expected = IllegalArgumentException.class)
55+
public void failWithPathOnSslUri() {
56+
NetworkModuleService.validateURI("ssl://host_literal:1883/somePath");
57+
}
58+
59+
@Test
60+
public void failWithUnsuppurtedSchemeUri() {
61+
try {
62+
NetworkModuleService.validateURI("unknown://host_literal:1883");
63+
fail("Must fail: Unknow scheme");
64+
} catch (IllegalArgumentException e) {
65+
assertEquals("no NetworkModule installed for scheme \"unknown\" of URI \"unknown://host_literal:1883\"", e
66+
.getMessage());
67+
}
68+
}
69+
70+
/**
71+
* Test for URI parsing with '_' in hostname.
72+
*/
73+
@Test
74+
public void testApplyRFC3986AuthorityPatch() throws URISyntaxException {
75+
URI uri = new URI("tcp://user:password@some_host:666/some_path");
76+
/*
77+
* If the following asserts trigger, then the patch may be no longer required, as Java URI class does the
78+
* RFC3986 parsing itself.
79+
*/
80+
assertNull("patch no longer necessary?", uri.getUserInfo());
81+
assertNull("patch no longer necessary?", uri.getHost());
82+
assertEquals("patch no longer necessary?", -1, uri.getPort());
83+
84+
NetworkModuleService.applyRFC3986AuthorityPatch(uri);
85+
86+
assertEquals("wrong user info", "user:password", uri.getUserInfo());
87+
assertEquals("wrong hostname", "some_host", uri.getHost());
88+
assertEquals("wrong port", 666, uri.getPort());
89+
}
90+
91+
@Test
92+
public void testCreateInstance() throws MqttException {
93+
String brokerUri = "tcp://localhost:666";
94+
MqttConnectOptions options = new MqttConnectOptions();
95+
int conTimeout = 234;
96+
options.setConnectionTimeout(conTimeout);
97+
String clientId = "";
98+
99+
NetworkModule result = NetworkModuleService.createInstance(brokerUri, options, clientId);
100+
101+
assertTrue(result instanceof TCPNetworkModule);
102+
assertEquals(brokerUri, result.getServerURI());
103+
}
104+
}

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

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,23 @@
1111
public class MqttConnectOptionsTest {
1212

1313
@Test
14-
public void testValidateURI() {
15-
try {
16-
MqttConnectOptions.validateURI("");
17-
fail("Must fail: Empty URI");
18-
} catch (IllegalArgumentException e) {
19-
assertEquals("Unknown scheme \"null\" of URI \"\"", e.getMessage());
20-
}
21-
22-
try {
23-
MqttConnectOptions.validateURI("udp://localhost:1883");
24-
fail("Must fail: Unknow scheme");
25-
} catch (IllegalArgumentException e) {
26-
assertEquals("Unknown scheme \"udp\" of URI \"udp://localhost:1883\"", e.getMessage());
27-
}
28-
29-
MqttConnectOptions.validateURI("tcp://localhost:1883");
30-
31-
try {
32-
MqttConnectOptions.validateURI("tcp://localhost:1883/mqtt");
33-
fail("Must fail: URI path must be empty");
34-
} catch (IllegalArgumentException e) {
35-
assertEquals("URI path must be empty \"tcp://localhost:1883/mqtt\"", e.getMessage());
36-
}
37-
38-
MqttConnectOptions.validateURI("tcp://localhost");
39-
40-
try {
41-
MqttConnectOptions.validateURI("localhost:1883");
42-
fail("Must fail: Unknow scheme");
43-
} catch (IllegalArgumentException e) {
44-
assertEquals("Unknown scheme \"localhost\" of URI \"localhost:1883\"", e.getMessage());
45-
}
46-
47-
try {
48-
MqttConnectOptions.validateURI("localhost");
49-
fail("Must fail: URI path must be empty");
50-
} catch (IllegalArgumentException e) {
51-
assertEquals("URI path must be empty \"localhost\"", e.getMessage());
52-
}
53-
54-
try {
55-
MqttConnectOptions.validateURI("tcp://");
56-
fail("Must fail: Can't parse string to URI");
57-
} catch (IllegalArgumentException e) {
58-
assertEquals("Can't parse string to URI \"tcp://\"", e.getMessage());
59-
}
14+
public void testValidateMQTTVersions() {
15+
MqttConnectOptions connectOptions = new MqttConnectOptions();
16+
17+
connectOptions.setMqttVersion(MQTT_VERSION_DEFAULT);
18+
connectOptions.setMqttVersion(MQTT_VERSION_3_1);
19+
connectOptions.setMqttVersion(MQTT_VERSION_3_1_1);
6020
}
6121

6222
@Test
63-
public void testValidateMQTTVersions() {
23+
public void testInvalidMQTTVersions() {
6424
MqttConnectOptions connectOptions = new MqttConnectOptions();
65-
try {
66-
connectOptions.setMqttVersion(MQTT_VERSION_DEFAULT);
67-
connectOptions.setMqttVersion(MQTT_VERSION_3_1);
68-
connectOptions.setMqttVersion(MQTT_VERSION_3_1_1);
69-
} catch(IllegalArgumentException e) {
70-
fail("MQTT Versions are valid");
71-
}
7225

7326
try {
7427
connectOptions.setMqttVersion(9);
7528
fail("MQTT Version is not valid");
7629
} catch (IllegalArgumentException e) {
7730
assertEquals("An incorrect version was used \"9\". Acceptable version options are " + MQTT_VERSION_DEFAULT + ", " + MQTT_VERSION_3_1 + " and " + MQTT_VERSION_3_1_1 + ".", e.getMessage());
7831
}
79-
8032
}
81-
8233
}

org.eclipse.paho.client.mqttv3/.classpath

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.classpath
2+
.project
3+
.settings/

org.eclipse.paho.client.mqttv3/.project

Lines changed: 0 additions & 34 deletions
This file was deleted.

org.eclipse.paho.client.mqttv3/.settings/org.eclipse.jdt.core.prefs

Lines changed: 0 additions & 13 deletions
This file was deleted.

org.eclipse.paho.client.mqttv3/.settings/org.eclipse.pde.core.prefs

Lines changed: 0 additions & 3 deletions
This file was deleted.

org.eclipse.paho.client.mqttv3/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ Export-Package: org.eclipse.paho.client.mqttv3;version="1.2.1.qualifier",
1010
org.eclipse.paho.client.mqttv3.util;version="1.2.1.qualifier"
1111
Bundle-Vendor: %bundle.provider
1212
Bundle-ActivationPolicy: lazy
13-
Bundle-RequiredExecutionEnvironment: J2SE-1.4
13+
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
1414
Import-Package: javax.net;resolution:=optional,
1515
javax.net.ssl;resolution:=optional

0 commit comments

Comments
 (0)