Add null pointer checks and associated test cases.
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
index ed8a11b..54465bf 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
@@ -28,6 +28,9 @@
public abstract int hashCode();
public static IPAddress<?> of(String ip) {
+ if (ip == null) {
+ throw new NullPointerException("String ip must not be null");
+ }
if (ip.indexOf('.') != -1)
return IPv4Address.of(ip);
else if (ip.indexOf(':') != -1)
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
index d0632c5..654e72c 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
@@ -10,6 +10,9 @@
public abstract IPVersion getIpVersion();
public static IPAddressWithMask<?> of(String ip) {
+ if (ip == null) {
+ throw new NullPointerException("String ip must not be null");
+ }
if (ip.indexOf('.') != -1)
return IPv4AddressWithMask.of(ip);
else if (ip.indexOf(':') != -1)
@@ -18,6 +21,7 @@
throw new IllegalArgumentException("IP Address not well formed: " + ip);
}
+ @Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(value.toString());
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
index 970a65c..87167fb 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
@@ -49,6 +49,9 @@
}
public static IPv4Address of(final byte[] address) {
+ if (address == null) {
+ throw new NullPointerException("Address must not be null");
+ }
if (address.length != LENGTH) {
throw new IllegalArgumentException(
"Invalid byte array length for IPv4Address address: " + address.length);
@@ -67,6 +70,9 @@
}
public static IPv4Address of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int start = 0;
int shift = 24;
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
index 1f23f2d..9b60c6a 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
@@ -22,10 +22,19 @@
}
public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
+ if (value == null) {
+ throw new NullPointerException("Value must not be null");
+ }
+ if (mask == null) {
+ throw new NullPointerException("Mask must not be null");
+ }
return new IPv4AddressWithMask(value, mask);
}
public static IPv4AddressWithMask of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int slashPos;
String ip = string;
int maskBits = 32;
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
index 531dadf..63ae700 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
@@ -53,6 +53,9 @@
}
public static IPv6Address of(final byte[] address) {
+ if (address == null) {
+ throw new NullPointerException("Address must not be null");
+ }
if (address.length != LENGTH) {
throw new IllegalArgumentException(
"Invalid byte array length for IPv6 address: " + address.length);
@@ -98,6 +101,9 @@
private final static Pattern colonPattern = Pattern.compile(":");
public static IPv6Address of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
IPv6Builder builder = new IPv6Builder();
String[] parts = colonPattern.split(string, -1);
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
index 8376e5e..7259c7f 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
@@ -16,11 +16,20 @@
}
public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
+ if (value == null) {
+ throw new NullPointerException("Value must not be null");
+ }
+ if (mask == null) {
+ throw new NullPointerException("Mask must not be null");
+ }
return new IPv6AddressWithMask(value, mask);
}
public static IPv6AddressWithMask of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int slashPos;
String ip = string;
int maskBits = 128;
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
new file mode 100644
index 0000000..25fc943
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
@@ -0,0 +1,54 @@
+package org.projectfloodlight.openflow.types;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Most tests are in IPv4AddressTest and IPv6AddressTest
+ * Just exception testing here
+ * @author gregor
+ *
+ */
+public class IPAddressTest {
+ @Test
+ public void testOfException() {
+ try {
+ IPAddress.of("Foobar");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPAddressWithMask.of("Foobar");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPAddress.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddress.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
+
+}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
index d9c6681..975c7d3 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
@@ -2,6 +2,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -193,4 +194,57 @@
assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
}
}
+
+ @Test
+ public void testOfExceptions() {
+ // We check if the message of a caught NPE is set to a useful message
+ // as a hacky way of verifying that we got an NPE thrown by use rather
+ // than one the JVM created for a null access.
+ try {
+ String s = null;
+ IPv4Address.of(s);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = null;
+ IPv4Address.of(b);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = new byte[3];
+ IPv4Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ byte[] b = new byte[5];
+ IPv4Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPv4AddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
index 1243c61..706fb25 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
@@ -2,6 +2,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -181,4 +182,54 @@
assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
}
}
+
+ @Test
+ public void testOfExceptions() throws Exception {
+ try {
+ IPv6AddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ String s = null;
+ IPv6Address.of(s);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = null;
+ IPv6Address.of(b);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = new byte[7];
+ IPv6Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ byte[] b = new byte[9];
+ IPv6Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
}