Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network module SPI #440

Merged
merged 8 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.eclipse.paho.client.mqttv3.internal;

import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class NetworkModuleServiceTest {

@Test
public void testValidateURI() {
NetworkModuleService.validateURI("tcp://host_literal:1883");
NetworkModuleService.validateURI("ssl://host_literal:8883");
NetworkModuleService.validateURI("ws://host_literal:80/path/to/ws");
NetworkModuleService.validateURI("wss://host_literal:443/path/to/ws");
}

public void failEmptyUri() {
try {
NetworkModuleService.validateURI("");
fail("Must fail: Empty URI");
} catch (IllegalArgumentException e) {
assertEquals("missing scheme in broker URI: ", e.getMessage());
}
}

@Test
public void failInvalidUri() {
try {
NetworkModuleService.validateURI("no URI at all");
fail("Must fail: Can't parse string to URI");
} catch (IllegalArgumentException e) {
assertEquals("Can't parse string to URI \"no URI at all\"", e.getMessage());
}
}

@Test
public void failWithPathOnTcpUri() {
try {
NetworkModuleService.validateURI("tcp://host_literal:1883/somePath");
fail("Must fail: URI path must be empty");
} catch (IllegalArgumentException e) {
assertEquals("URI path must be empty \"tcp://host_literal:1883/somePath\"", e.getMessage());
}
}

@Test(expected = IllegalArgumentException.class)
public void failWithPathOnSslUri() {
NetworkModuleService.validateURI("ssl://host_literal:1883/somePath");
}

@Test
public void failWithUnsuppurtedSchemeUri() {
try {
NetworkModuleService.validateURI("unknown://host_literal:1883");
fail("Must fail: Unknow scheme");
} catch (IllegalArgumentException e) {
assertEquals("no NetworkModule installed for scheme \"unknown\" of URI \"unknown://host_literal:1883\"", e
.getMessage());
}
}

/**
* Test for URI parsing with '_' in hostname.
*/
@Test
public void testApplyRFC3986AuthorityPatch() throws URISyntaxException {
URI uri = new URI("tcp://user:password@some_host:666/some_path");
/*
* If the following asserts trigger, then the patch may be no longer required, as Java URI class does the
* RFC3986 parsing itself.
*/
assertNull("patch no longer necessary?", uri.getUserInfo());
assertNull("patch no longer necessary?", uri.getHost());
assertEquals("patch no longer necessary?", -1, uri.getPort());

NetworkModuleService.applyRFC3986AuthorityPatch(uri);

assertEquals("wrong user info", "user:password", uri.getUserInfo());
assertEquals("wrong hostname", "some_host", uri.getHost());
assertEquals("wrong port", 666, uri.getPort());
}

@Test
public void testCreateInstance() throws MqttException {
String brokerUri = "tcp://localhost:666";
MqttConnectOptions options = new MqttConnectOptions();
int conTimeout = 234;
options.setConnectionTimeout(conTimeout);
String clientId = "";

NetworkModule result = NetworkModuleService.createInstance(brokerUri, options, clientId);

assertTrue(result instanceof TCPNetworkModule);
assertEquals(brokerUri, result.getServerURI());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,23 @@
public class MqttConnectOptionsTest {

@Test
public void testValidateURI() {
try {
MqttConnectOptions.validateURI("");
fail("Must fail: Empty URI");
} catch (IllegalArgumentException e) {
assertEquals("Unknown scheme \"null\" of URI \"\"", e.getMessage());
}

try {
MqttConnectOptions.validateURI("udp://localhost:1883");
fail("Must fail: Unknow scheme");
} catch (IllegalArgumentException e) {
assertEquals("Unknown scheme \"udp\" of URI \"udp://localhost:1883\"", e.getMessage());
}

MqttConnectOptions.validateURI("tcp://localhost:1883");

try {
MqttConnectOptions.validateURI("tcp://localhost:1883/mqtt");
fail("Must fail: URI path must be empty");
} catch (IllegalArgumentException e) {
assertEquals("URI path must be empty \"tcp://localhost:1883/mqtt\"", e.getMessage());
}

MqttConnectOptions.validateURI("tcp://localhost");

try {
MqttConnectOptions.validateURI("localhost:1883");
fail("Must fail: Unknow scheme");
} catch (IllegalArgumentException e) {
assertEquals("Unknown scheme \"localhost\" of URI \"localhost:1883\"", e.getMessage());
}

try {
MqttConnectOptions.validateURI("localhost");
fail("Must fail: URI path must be empty");
} catch (IllegalArgumentException e) {
assertEquals("URI path must be empty \"localhost\"", e.getMessage());
}

try {
MqttConnectOptions.validateURI("tcp://");
fail("Must fail: Can't parse string to URI");
} catch (IllegalArgumentException e) {
assertEquals("Can't parse string to URI \"tcp://\"", e.getMessage());
}
public void testValidateMQTTVersions() {
MqttConnectOptions connectOptions = new MqttConnectOptions();

connectOptions.setMqttVersion(MQTT_VERSION_DEFAULT);
connectOptions.setMqttVersion(MQTT_VERSION_3_1);
connectOptions.setMqttVersion(MQTT_VERSION_3_1_1);
}

@Test
public void testValidateMQTTVersions() {
public void testInvalidMQTTVersions() {
MqttConnectOptions connectOptions = new MqttConnectOptions();
try {
connectOptions.setMqttVersion(MQTT_VERSION_DEFAULT);
connectOptions.setMqttVersion(MQTT_VERSION_3_1);
connectOptions.setMqttVersion(MQTT_VERSION_3_1_1);
} catch(IllegalArgumentException e) {
fail("MQTT Versions are valid");
}

try {
connectOptions.setMqttVersion(9);
fail("MQTT Version is not valid");
} catch (IllegalArgumentException e) {
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());
}

}

}
9 changes: 0 additions & 9 deletions org.eclipse.paho.client.mqttv3/.classpath

This file was deleted.

3 changes: 3 additions & 0 deletions org.eclipse.paho.client.mqttv3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.classpath
.project
.settings/
34 changes: 0 additions & 34 deletions org.eclipse.paho.client.mqttv3/.project

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion org.eclipse.paho.client.mqttv3/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Export-Package: org.eclipse.paho.client.mqttv3;version="1.2.1.qualifier",
org.eclipse.paho.client.mqttv3.util;version="1.2.1.qualifier"
Bundle-Vendor: %bundle.provider
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: J2SE-1.4
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: javax.net;resolution:=optional,
javax.net.ssl;resolution:=optional
Loading