Commit 66a2d087 authored by George Nash's avatar George Nash

Add OCEndpointParseException

This adds OCEndpointParseException that will be thrown
if stringToEndpoint fails to parse the passed in string.

Initially I would have liked to use the java.text.ParseException
but it expects the errorOffset value which is the position where
the parse error occured in the source string. We don't have a
mechanism for discovering the errorOffset so a regular exception
with a error message has been used instead.

Additionally updated the NullPointerException code for setDi
to use the C/C++ abstraction macros for Java method calls.

Change-Id: I76442508c0b5b6c84d8c5ba303f0d982d550e5b6
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parent 42483dd5
...@@ -10,7 +10,13 @@ public class OCEndpointTest { ...@@ -10,7 +10,13 @@ public class OCEndpointTest {
public void testStringToEndpoint() { public void testStringToEndpoint() {
String[] uri = new String[1]; String[] uri = new String[1];
// IPV4 with port and uri // IPV4 with port and uri
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps://10.211.55.3:56789/a/light", uri); OCEndpoint ep = null;
try {
ep = OCEndpointUtil.stringToEndpoint("coaps://10.211.55.3:56789/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4); assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4);
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED); assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
...@@ -21,7 +27,12 @@ public class OCEndpointTest { ...@@ -21,7 +27,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPV6 // IPV6
ep = OCEndpointUtil.stringToEndpoint("coap://[ff02::158]", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coap://[ff02::158]", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -33,7 +44,12 @@ public class OCEndpointTest { ...@@ -33,7 +44,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPV6 with uri // IPV6 with uri
ep = OCEndpointUtil.stringToEndpoint("coaps://[ff02::158]/a/light", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coaps://[ff02::158]/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -45,7 +61,12 @@ public class OCEndpointTest { ...@@ -45,7 +61,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPV6 with port and uri // IPV6 with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps://[fe80::12]:2439/a/light", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coaps://[fe80::12]:2439/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -62,7 +83,13 @@ public class OCEndpointTest { ...@@ -62,7 +83,13 @@ public class OCEndpointTest {
public void testStringToEndpoint_dns_lookup() { public void testStringToEndpoint_dns_lookup() {
String[] uri = new String[1]; String[] uri = new String[1];
// dns lookup // dns lookup
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org", uri); OCEndpoint ep = null;
try {
ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) || assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6)); OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
...@@ -73,7 +100,12 @@ public class OCEndpointTest { ...@@ -73,7 +100,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// dns lookup with uri // dns lookup with uri
ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org/alpha", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org/alpha", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) || assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6)); OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
...@@ -84,7 +116,12 @@ public class OCEndpointTest { ...@@ -84,7 +116,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// dns lookup with port and uri // dns lookup with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps://openconnectivity.org:3456/alpha", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coaps://openconnectivity.org:3456/alpha", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) || assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6)); OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
...@@ -100,7 +137,13 @@ public class OCEndpointTest { ...@@ -100,7 +137,13 @@ public class OCEndpointTest {
public void testStringToEndpoint_tcp() { public void testStringToEndpoint_tcp() {
String[] uri = new String[1]; String[] uri = new String[1];
// IPv4 over tcp and uri // IPv4 over tcp and uri
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://10.211.55.3/a/light", uri); OCEndpoint ep = null;
try {
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://10.211.55.3/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4); assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4);
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED); assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
...@@ -111,7 +154,12 @@ public class OCEndpointTest { ...@@ -111,7 +154,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPv4 over tcp and port // IPv4 over tcp and port
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://1.2.3.4:2568", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://1.2.3.4:2568", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, (ep.getFlags() & OCTransportFlags.IPV4)); assertEquals(OCTransportFlags.IPV4, (ep.getFlags() & OCTransportFlags.IPV4));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -122,7 +170,12 @@ public class OCEndpointTest { ...@@ -122,7 +170,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp // IPv6 over tcp
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://[ff02::158]", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://[ff02::158]", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -134,7 +187,12 @@ public class OCEndpointTest { ...@@ -134,7 +187,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp with uri // IPv6 over tcp with uri
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[ff02::158]/a/light", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[ff02::158]/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -146,7 +204,12 @@ public class OCEndpointTest { ...@@ -146,7 +204,12 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep); OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp with port and uri // IPv6 over tcp with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[fe80::12]:2439/a/light", uri); try {
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[fe80::12]:2439/a/light", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6)); assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED)); assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
...@@ -163,7 +226,13 @@ public class OCEndpointTest { ...@@ -163,7 +226,13 @@ public class OCEndpointTest {
public void testStringToEndpoint_tcp_and_dns_lookup() { public void testStringToEndpoint_tcp_and_dns_lookup() {
String[] uri = new String[1]; String[] uri = new String[1];
// dns lookup over tcp with port // dns lookup over tcp with port
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://openconnectivity.org:3456", uri); OCEndpoint ep = null;
try {
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://openconnectivity.org:3456", uri);
} catch (OCEndpointParseException e) {
e.printStackTrace();
fail("stringToEndpoint threw exception when it was not expected.");
}
assertNotNull(ep); assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) || assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6)); OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
...@@ -200,4 +269,32 @@ public class OCEndpointTest { ...@@ -200,4 +269,32 @@ public class OCEndpointTest {
assertEquals(e.getMessage(), "OCUuid cannot be null."); assertEquals(e.getMessage(), "OCUuid cannot be null.");
} }
} }
@Test
public void test_throw_parse_exception_stringToEndpoint() {
String[] uri = new String[1];
OCEndpoint ep = null;
try {
ep = OCEndpointUtil.stringToEndpoint("", uri);
fail("The call stringToEndpoint should throw an exception");
} catch (OCEndpointParseException e) {
assertEquals("The \"\" string cannot be parsed.", e.getMessage());
}
try {
// will fail does not have `://`
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp", uri);
fail("The call stringToEndpoint should throw an exception");
} catch (OCEndpointParseException e) {
assertEquals("The \"coaps+tcp\" string cannot be parsed.", e.getMessage());
}
try {
ep = OCEndpointUtil.stringToEndpoint("foobar", uri);
fail("The call stringToEndpoint should throw an exception");
} catch (OCEndpointParseException e) {
assertEquals("The \"foobar\" string cannot be parsed.", e.getMessage());
}
assertNull(ep);
}
} }
package org.iotivity;
public class OCEndpointParseException extends Exception {
private static final long serialVersionUID = -7550359826963961621L;
public OCEndpointParseException(String message) {
super(message);
}
}
...@@ -67,40 +67,20 @@ void jni_free_endpoint(oc_endpoint_t *endpoint) { ...@@ -67,40 +67,20 @@ void jni_free_endpoint(oc_endpoint_t *endpoint) {
/* The `oc_endpoint_t *endpoint` parameter is jarg1, the name is generated by SWIG. */ /* The `oc_endpoint_t *endpoint` parameter is jarg1, the name is generated by SWIG. */
if(jarg1 == NULL) { if(jarg1 == NULL) {
OC_DBG("JNI: OCEndpoint cannot be null.\n"); OC_DBG("JNI: OCEndpoint cannot be null.\n");
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException"); jclass clazz = JCALL1(FindClass, jenv, "java/lang/NullPointerException");
(*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null."); JCALL2(ThrowNew, jenv, clazz, "OCEndpoint cannot be null.");
return; return;
} }
/* The `oc_uuid_t *di` parameter is jarg2, the name is generated by SWIG. */ /* The `oc_uuid_t *di` parameter is jarg2, the name is generated by SWIG. */
if(jarg2 == NULL) { if(jarg2 == NULL) {
OC_DBG("JNI: OCUuid cannot be null.\n"); OC_DBG("JNI: OCUuid cannot be null.\n");
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException"); jclass clazz = JCALL1(FindClass, jenv, "java/lang/NullPointerException");
(*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null."); JCALL2(ThrowNew, jenv, clazz, "OCUuid cannot be null.");
return; return;
} }
$action $action
} }
%rename(setDi) oc_endpoint_set_di; %rename(setDi) oc_endpoint_set_di;
/*
%inline %{
void jni_endpoint_set_di(oc_endpoint_t *endpoint, oc_uuid_t *di) {
if(endpoint == NULL) {
OC_DBG("JNI: OCEndpoint cannot be null.\n", __func__);
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
(*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null.");
return;
}
if(di == NULL) {
OC_DBG("JNI: OCUuid cannot be null.\n", __func__);
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
(*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null.");
return;
}
oc_endpoint_set_di(endpoint, di);
}
%}
*/
%ignore oc_endpoint_to_string; %ignore oc_endpoint_to_string;
%typemap(jni) jobject toString "jobject"; %typemap(jni) jobject toString "jobject";
...@@ -144,6 +124,23 @@ SWIGEXPORT jobject JNICALL Java_org_iotivity_OCEndpointUtilJNI_toString(JNIEnv * ...@@ -144,6 +124,23 @@ SWIGEXPORT jobject JNICALL Java_org_iotivity_OCEndpointUtilJNI_toString(JNIEnv *
%apply oc_string_t *INPUT { oc_string_t *endpoint_str }; %apply oc_string_t *INPUT { oc_string_t *endpoint_str };
%apply oc_string_t *OUTPUT { oc_string_t *uri }; %apply oc_string_t *OUTPUT { oc_string_t *uri };
%javaexception("OCEndpointParseException") jni_string_to_endpoint {
$action
if(result == NULL) {
OC_DBG("JNI: String can not be parsed.");
jclass cls_OCEndpointParseException = JCALL1(FindClass, jenv, "org/iotivity/OCEndpointParseException");
assert(cls_OCEndpointParseException);
oc_string_t exception_message_part1;
oc_concat_strings(&exception_message_part1, "The \"", oc_string(*arg1));
oc_string_t exception_message;
oc_concat_strings(&exception_message, oc_string(exception_message_part1), "\" string cannot be parsed.");
JCALL2(ThrowNew, jenv, cls_OCEndpointParseException, ((char *)oc_string(exception_message)));
oc_free_string(&exception_message_part1);
oc_free_string(&exception_message);
return $null;
}
}
/* TODO figure out a clean way to return the uri param not as an array value */ /* TODO figure out a clean way to return the uri param not as an array value */
%ignore oc_string_to_endpoint; %ignore oc_string_to_endpoint;
%rename(stringToEndpoint) jni_string_to_endpoint; %rename(stringToEndpoint) jni_string_to_endpoint;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment