> resultHandler) {
+ Objects.requireNonNull(name, "name");
+ Objects.requireNonNull(resultHandler, "resultHandler");
if (clusterManager == null) {
getLocalCounter(name, resultHandler);
} else {
@@ -92,6 +103,7 @@ public class SharedDataImpl implements SharedData {
* Return a {@code Map} with the specific {@code name}. All invocations of this method with the same value of {@code name}
* are guaranteed to return the same {@code Map} instance.
*/
+ @SuppressWarnings("unchecked")
public LocalMap getLocalMap(String name) {
LocalMap map = (LocalMap) localMaps.get(name);
if (map == null) {
diff --git a/vertx-core/src/test/java/io/vertx/test/core/AsynchronousLockTest.java b/vertx-core/src/test/java/io/vertx/test/core/AsynchronousLockTest.java
index 752951f99..59f1277db 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/AsynchronousLockTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/AsynchronousLockTest.java
@@ -20,6 +20,9 @@ import io.vertx.core.Vertx;
import io.vertx.core.shareddata.Lock;
import org.junit.Test;
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Tim Fox
*/
@@ -29,6 +32,15 @@ public class AsynchronousLockTest extends VertxTestBase {
return vertx;
}
+ @Test
+ public void testIllegalArguments() throws Exception {
+ assertNullPointerException(() -> getVertx().sharedData().getLock(null, ar -> {}));
+ assertNullPointerException(() -> getVertx().sharedData().getLock("foo", null));
+ assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout(null, 1, ar -> {}));
+ assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout("foo", 1, null));
+ assertIllegalArgumentException(() -> getVertx().sharedData().getLockWithTimeout("foo", -1, ar -> {}));
+ }
+
@Test
public void testAcquire() {
getVertx().sharedData().getLock("foo", ar -> {
diff --git a/vertx-core/src/test/java/io/vertx/test/core/BufferTest.java b/vertx-core/src/test/java/io/vertx/test/core/BufferTest.java
index b8d4e18cc..11a16a049 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/BufferTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/BufferTest.java
@@ -16,16 +16,33 @@
package io.vertx.test.core;
+import io.netty.buffer.ByteBuf;
import io.vertx.core.buffer.Buffer;
import org.junit.Test;
-import static org.junit.Assert.*;
+import java.nio.ByteBuffer;
+
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertIndexOutOfBoundsException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
/**
* @author Tim Fox
*/
public class BufferTest {
+ @Test
+ public void testConstructorArguments() throws Exception {
+ assertIllegalArgumentException(() -> Buffer.buffer(-1));
+ assertNullPointerException(() -> Buffer.buffer((byte[]) null));
+ assertNullPointerException(() -> Buffer.buffer((String) null));
+ assertNullPointerException(() -> Buffer.buffer((ByteBuf) null));
+ assertNullPointerException(() -> Buffer.buffer(null, "UTF-8"));
+ assertNullPointerException(() -> Buffer.buffer("", null));
+ }
+
//https://github.com/vert-x/vert.x/issues/561
@Test
public void testSetGetInt() throws Exception {
@@ -53,6 +70,8 @@ public class BufferTest {
assertTrue(TestUtils.byteArraysEqual(bytes, b.getBytes()));
b.appendBuffer(toAppend);
assertEquals(b.length(), 2 * bytes.length);
+
+ assertNullPointerException(() -> b.appendBuffer(null));
}
@Test
@@ -68,6 +87,8 @@ public class BufferTest {
b.appendBytes(bytes);
assertEquals(b.length(), 2 * bytes.length);
+
+ assertNullPointerException(() -> b.appendBytes(null));
}
@Test
@@ -85,6 +106,8 @@ public class BufferTest {
b.appendBytes(bytes, 1, len);
assertEquals(b.length(), 2 * len);
+
+ assertNullPointerException(() -> b.appendBytes(null, 1, len));
}
@Test
@@ -103,6 +126,8 @@ public class BufferTest {
b.appendBuffer(src, 1, len);
assertEquals(b.length(), 2 * len);
+
+ assertNullPointerException(() -> b.appendBuffer(null, 1, len));
}
@Test
@@ -141,6 +166,10 @@ public class BufferTest {
b.appendString(str);
assertEquals(b.length(), sb.length);
assertTrue(str.equals(b.toString("UTF-8")));
+
+ assertNullPointerException(() -> b.appendString(null));
+ assertNullPointerException(() -> b.appendString(null, "UTF-8"));
+ assertNullPointerException(() -> b.appendString("", null));
}
@Test
@@ -154,218 +183,66 @@ public class BufferTest {
byte[] bytes = TestUtils.randomByteArray(bytesLen);
Buffer b = Buffer.buffer(bytes);
- try {
- b.getByte(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getByte(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getByte(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getByte(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getByte(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
+ assertIndexOutOfBoundsException(() -> b.getByte(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getByte(-1));
+ assertIndexOutOfBoundsException(() -> b.getByte(-100));
+ assertIndexOutOfBoundsException(() -> b.getInt(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getInt(-1));
+ assertIndexOutOfBoundsException(() -> b.getInt(-100));
+ assertIndexOutOfBoundsException(() -> b.getLong(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getLong(-1));
+ assertIndexOutOfBoundsException(() -> b.getLong(-100));
+ assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getFloat(-1));
+ assertIndexOutOfBoundsException(() -> b.getFloat(-100));
+ assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getDouble(-1));
+ assertIndexOutOfBoundsException(() -> b.getDouble(-100));
+ assertIndexOutOfBoundsException(() -> b.getShort(bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getShort(-1));
+ assertIndexOutOfBoundsException(() -> b.getShort(-100));
+ assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 1, bytesLen + 1));
+ assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 100, bytesLen + 100));
+ assertIndexOutOfBoundsException(() -> b.getBytes(-1, -1));
+ assertIndexOutOfBoundsException(() -> b.getBytes(-100, -100));
+ assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen));
+ assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1));
+ assertIllegalArgumentException(() -> b.getString(2, 1));
+ assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen, "UTF-8"));
+ assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1, "UTF-8"));
+ assertIllegalArgumentException(() -> b.getString(2, 1, "UTF-8"));
+ }
- try {
- b.getInt(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getInt(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getInt(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getInt(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getInt(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- try {
- b.getLong(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getLong(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getLong(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getLong(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getLong(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- try {
- b.getFloat(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getFloat(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getFloat(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getFloat(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getFloat(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- try {
- b.getDouble(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getDouble(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getDouble(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getDouble(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getDouble(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
-
- try {
- b.getShort(bytesLen);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getShort(bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getShort(bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getShort(-1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getShort(-100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- try {
- b.getBytes(bytesLen + 1, bytesLen + 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getBytes(bytesLen + 100, bytesLen + 100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getBytes(-1, -1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
- try {
- b.getBytes(-100, -100);
- fail();
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
+ @Test
+ public void testSetOutOfBounds() throws Exception {
+ Buffer b = Buffer.buffer(numSets);
+ assertIndexOutOfBoundsException(() -> b.setByte(-1, (byte) 0));
+ assertIndexOutOfBoundsException(() -> b.setInt(-1, 0));
+ assertIndexOutOfBoundsException(() -> b.setLong(-1, 0));
+ assertIndexOutOfBoundsException(() -> b.setDouble(-1, 0));
+ assertIndexOutOfBoundsException(() -> b.setFloat(-1, 0));
+ assertIndexOutOfBoundsException(() -> b.setShort(-1, (short) 0));
+ assertIndexOutOfBoundsException(() -> b.setBuffer(-1, b));
+ assertIndexOutOfBoundsException(() -> b.setBuffer(0, b, -1, 0));
+ assertIllegalArgumentException(() -> b.setBuffer(0, b, 0, -1));
+ assertIndexOutOfBoundsException(() -> b.setBytes(-1, TestUtils.randomByteArray(1)));
+ assertIndexOutOfBoundsException(() -> b.setBytes(-1, TestUtils.randomByteArray(1), -1, 0));
+ assertIllegalArgumentException(() -> b.setBytes(-1, TestUtils.randomByteArray(1), 0, -1));
+ assertIndexOutOfBoundsException(() -> b.setString(-1, ""));
+ assertIndexOutOfBoundsException(() -> b.setString(-1, "", "UTF-8"));
}
@Test
@@ -594,6 +471,7 @@ public class BufferTest {
@Test
public void testSetBytesBuffer() throws Exception {
testSetBytesBuffer(Buffer.buffer(150));
+ assertNullPointerException(() -> Buffer.buffer(150).setBytes(0, (ByteBuffer) null));
}
@Test
@@ -631,6 +509,9 @@ public class BufferTest {
b.setBytes(b.length(), bytes, 1, len);
assertEquals(b.length(), 2 * len + 1);
+
+ assertNullPointerException(() -> Buffer.buffer(150).setBytes(0, (byte[]) null));
+ assertNullPointerException(() -> Buffer.buffer(150).setBytes(0, null, 1, len));
}
@@ -652,6 +533,9 @@ public class BufferTest {
b.setBuffer(b.length(), src, 1, len);
assertEquals(b.length(), 2 * len + 1);
+
+ assertNullPointerException(() -> b.setBuffer(1, null));
+ assertNullPointerException(() -> b.setBuffer(1, null, 0, len));
}
@Test
@@ -674,6 +558,9 @@ public class BufferTest {
assertEquals(str, str2);
+ assertNullPointerException(() -> Buffer.buffer(150).setString(0, null));
+ assertNullPointerException(() -> Buffer.buffer(150).setString(0, null, "UTF-8"));
+
//TODO setString with encoding
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/ClusterWideMapTest.java b/vertx-core/src/test/java/io/vertx/test/core/ClusterWideMapTest.java
index 8510136c6..c465d4e70 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/ClusterWideMapTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/ClusterWideMapTest.java
@@ -25,6 +25,9 @@ import org.junit.Test;
import java.io.Serializable;
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Tim Fox
*/
@@ -416,16 +419,21 @@ public class ClusterWideMapTest extends VertxTestBase {
testMapReplaceIfPresent(new SomeSerializableObject("foo"), new SomeSerializableObject("bar"), new SomeSerializableObject("quux"));
}
+ @Test
+ public void testGetMapWithNullName() throws Exception {
+ assertNullPointerException(() -> getVertx().sharedData().getClusterWideMap(null, ar -> {}));
+ }
+
+ @Test
+ public void testGetMapWithNullResultHandler() throws Exception {
+ assertNullPointerException(() -> getVertx().sharedData().getClusterWideMap("foo", null));
+ }
+
@Test
public void testPutNullKey() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.put(null, "foo", ar2 -> {});
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.put(null, "foo", ar2 -> {}));
+ testComplete();
}));
await();
}
@@ -433,13 +441,8 @@ public class ClusterWideMapTest extends VertxTestBase {
@Test
public void testPutNullValue() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.put("foo", null, ar2 -> {});
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.put("foo", null, ar2 -> {}));
+ testComplete();
}));
await();
}
@@ -447,13 +450,8 @@ public class ClusterWideMapTest extends VertxTestBase {
@Test
public void testPutInvalidKey() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.put(new SomeObject(), "foo", ar2 -> {});
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.put(new SomeObject(), "foo", ar2 -> {}));
+ testComplete();
}));
await();
}
@@ -461,13 +459,8 @@ public class ClusterWideMapTest extends VertxTestBase {
@Test
public void testPutInvalidValue() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.put("foo", new SomeObject(), ar2 -> {});
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.put("foo", new SomeObject(), ar2 -> {}));
+ testComplete();
}));
await();
}
@@ -475,14 +468,8 @@ public class ClusterWideMapTest extends VertxTestBase {
@Test
public void testPutIfAbsentInvalidKey() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.putIfAbsent(new SomeObject(), "foo", ar2 -> {
- });
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.putIfAbsent(new SomeObject(), "foo", ar2 -> {}));
+ testComplete();
}));
await();
}
@@ -490,14 +477,8 @@ public class ClusterWideMapTest extends VertxTestBase {
@Test
public void testPutIfAbsentInvalidValue() {
getVertx().sharedData().getClusterWideMap("foo", onSuccess(map -> {
- try {
- map.putIfAbsent("foo", new SomeObject(), ar2 -> {
- });
- fail("Should throw Exception");
- } catch (IllegalArgumentException e) {
- // OK
- testComplete();
- }
+ assertIllegalArgumentException(() -> map.putIfAbsent("foo", new SomeObject(), ar2 -> {}));
+ testComplete();
}));
await();
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/DNSTest.java b/vertx-core/src/test/java/io/vertx/test/core/DNSTest.java
index 7e0dbccda..e9934e8ac 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/DNSTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/DNSTest.java
@@ -29,6 +29,9 @@ import org.junit.Test;
import java.net.InetSocketAddress;
import java.util.List;
+import static io.vertx.test.core.TestUtils.assertIllegalStateException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Norman Maurer
* @author Tim Fox
@@ -37,6 +40,23 @@ public class DNSTest extends VertxTestBase {
private FakeDNSServer dnsServer;
+ @Test
+ public void testIllegalArguments() throws Exception {
+ DnsClient dns = prepareDns(FakeDNSServer.testResolveAAAA("::1"));
+
+ assertNullPointerException(() -> dns.lookup(null, ar -> {}));
+ assertNullPointerException(() -> dns.lookup4(null, ar -> {}));
+ assertNullPointerException(() -> dns.lookup6(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveA(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveAAAA(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveCNAME(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveMX(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveTXT(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolvePTR(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveNS(null, ar -> {}));
+ assertNullPointerException(() -> dns.resolveSRV(null, ar -> {}));
+ }
+
@Test
public void testResolveA() throws Exception {
final String ip = "10.0.0.1";
@@ -265,12 +285,7 @@ public class DNSTest extends VertxTestBase {
class MyVerticle extends AbstractVerticle {
@Override
public void start() {
- try {
- vertx.createDnsClient(1234, "localhost");
- fail("Should throw exception");
- } catch (IllegalStateException e) {
- // OK
- }
+ assertIllegalStateException(() -> vertx.createDnsClient(1234, "localhost"));
testComplete();
}
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/DatagramTest.java b/vertx-core/src/test/java/io/vertx/test/core/DatagramTest.java
index ecf680865..3855d9dc4 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/DatagramTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/DatagramTest.java
@@ -32,7 +32,10 @@ import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
+
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertIllegalStateException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
/**
* @author Norman Maurer
@@ -63,6 +66,33 @@ public class DatagramTest extends VertxTestBase {
super.tearDown();
}
+ @Test
+ public void testDatagramSocket() throws Exception {
+ peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
+
+ assertNullPointerException(() -> peer1.send((Buffer) null, 1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send(Buffer.buffer(), -1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send(Buffer.buffer(), 65536, "127.0.0.1", ar -> {}));
+
+ assertNullPointerException(() -> peer1.send((String) null, 1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send("", -1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send("", 65536, "127.0.0.1", ar -> {}));
+
+ assertNullPointerException(() -> peer1.send((String) null, "UTF-8", 1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send("", "UTF-8", -1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.send("", "UTF-8", 65536, "127.0.0.1", ar -> {}));
+ assertNullPointerException(() -> peer1.send("", null, 1, "127.0.0.1", ar -> {}));
+
+ assertIllegalArgumentException(() -> peer1.sender(-1, "127.0.0.1"));
+ assertIllegalArgumentException(() -> peer1.sender(65536, "127.0.0.1"));
+ assertNullPointerException(() -> peer1.sender(1, null));
+
+ assertIllegalArgumentException(() -> peer1.listen(-1, "127.0.0.1", ar -> {}));
+ assertIllegalArgumentException(() -> peer1.listen(65536, "127.0.0.1", ar -> {}));
+ assertNullPointerException(() -> peer1.listen(1, null, ar -> {}));
+ assertNullPointerException(() -> peer1.listen(1, "127.0.0.1", null));
+ }
+
@Test
public void testSendReceive() {
peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
@@ -320,35 +350,15 @@ public class DatagramTest extends VertxTestBase {
int rand = TestUtils.randomPositiveInt();
assertEquals(options, options.setSendBufferSize(rand));
assertEquals(rand, options.getSendBufferSize());
- try {
- options.setSendBufferSize(0);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
- try {
- options.setSendBufferSize(-123);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> options.setSendBufferSize(0));
+ assertIllegalArgumentException(() -> options.setSendBufferSize(-123));
assertEquals(NetworkOptions.DEFAULT_RECEIVE_BUFFER_SIZE, options.getReceiveBufferSize());
rand = TestUtils.randomPositiveInt();
assertEquals(options, options.setReceiveBufferSize(rand));
assertEquals(rand, options.getReceiveBufferSize());
- try {
- options.setReceiveBufferSize(0);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
- try {
- options.setReceiveBufferSize(-123);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> options.setReceiveBufferSize(0));
+ assertIllegalArgumentException(() -> options.setReceiveBufferSize(-123));
assertFalse(options.isReuseAddress());
assertEquals(options, options.setReuseAddress(true));
@@ -358,18 +368,8 @@ public class DatagramTest extends VertxTestBase {
rand = 23;
assertEquals(options, options.setTrafficClass(rand));
assertEquals(rand, options.getTrafficClass());
- try {
- options.setTrafficClass(-1);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
- try {
- options.setTrafficClass(256);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> options.setTrafficClass(-1));
+ assertIllegalArgumentException(() -> options.setTrafficClass(256));
assertFalse(options.isBroadcast());
assertEquals(options, options.setBroadcast(true));
@@ -383,12 +383,7 @@ public class DatagramTest extends VertxTestBase {
rand = TestUtils.randomPositiveInt();
assertEquals(options, options.setMulticastTimeToLive(rand));
assertEquals(rand, options.getMulticastTimeToLive());
- try {
- options.setMulticastTimeToLive(-1);
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> options.setMulticastTimeToLive(-1));
assertNull(options.getMulticastNetworkInterface());
String randString = TestUtils.randomUnicodeString(100);
@@ -488,12 +483,7 @@ public class DatagramTest extends VertxTestBase {
class MyVerticle extends AbstractVerticle {
@Override
public void start() {
- try {
- peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
- fail("Should throw exception");
- } catch (IllegalStateException e) {
- // OK
- }
+ assertIllegalStateException(() -> peer1 = vertx.createDatagramSocket(new DatagramSocketOptions()));
testComplete();
}
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/FileSystemTest.java b/vertx-core/src/test/java/io/vertx/test/core/FileSystemTest.java
index 599fb04e9..d626944e1 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/FileSystemTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/FileSystemTest.java
@@ -51,6 +51,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
import static io.vertx.test.core.TestUtils.assertNullPointerException;
/**
@@ -149,6 +150,24 @@ public class FileSystemTest extends VertxTestBase {
assertNullPointerException(() -> vertx.fileSystem().existsSync(null));
assertNullPointerException(() -> vertx.fileSystem().fsProps(null, h -> {}));
assertNullPointerException(() -> vertx.fileSystem().fsPropsSync(null));
+
+ String fileName = "some-file.dat";
+ AsyncFile asyncFile = vertx.fileSystem().openSync(testDir + pathSep + fileName, new OpenOptions());
+
+ assertNullPointerException(() -> asyncFile.write(null));
+ assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(1));
+ assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(0));
+ assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(-1));
+ assertNullPointerException(() -> asyncFile.write(null, 0, h -> {}));
+ assertNullPointerException(() -> asyncFile.write(Buffer.buffer(), 0, null));
+ assertIllegalArgumentException(() -> asyncFile.write(Buffer.buffer(), -1, h -> {}));
+
+ assertNullPointerException(() -> asyncFile.read(null, 0, 0, 0, h -> {}));
+ assertNullPointerException(() -> asyncFile.read(Buffer.buffer(), 0, 0, 0, null));
+
+ assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), -1, 0, 0, h -> {}));
+ assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, -1, 0, h -> {}));
+ assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, 0, -1, h -> {}));
}
@Test
@@ -1254,6 +1273,7 @@ public class FileSystemTest extends VertxTestBase {
}
@Test
+ @SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {
String fileName1 = "some-file.dat";
String fileName2 = "some-other-file.dat";
diff --git a/vertx-core/src/test/java/io/vertx/test/core/LocalEventBusTest.java b/vertx-core/src/test/java/io/vertx/test/core/LocalEventBusTest.java
index ae341857f..1f4b95592 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/LocalEventBusTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/LocalEventBusTest.java
@@ -56,6 +56,10 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.Consumer;
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertIllegalStateException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Tim Fox
*/
@@ -80,6 +84,40 @@ public class LocalEventBusTest extends EventBusTestBase {
super.tearDown();
}
+ @Test
+ public void testDeliveryOptions() {
+ DeliveryOptions options = new DeliveryOptions();
+
+ assertIllegalArgumentException(() -> options.setSendTimeout(0));
+ assertIllegalArgumentException(() -> options.setSendTimeout(-1));
+ assertNullPointerException(() -> options.addHeader(null, ""));
+ assertNullPointerException(() -> options.addHeader("", null));
+ }
+
+ @Test
+ public void testArgumentValidation() throws Exception {
+ assertNullPointerException(() -> eb.send(null, ""));
+ assertNullPointerException(() -> eb.send(null, "", handler -> {}));
+ assertNullPointerException(() -> eb.send(null, "", new DeliveryOptions()));
+ assertNullPointerException(() -> eb.send("", "", (DeliveryOptions) null));
+ assertNullPointerException(() -> eb.send(null, "", new DeliveryOptions(), handler -> {}));
+ assertNullPointerException(() -> eb.send("", "", null, handler -> {}));
+ assertNullPointerException(() -> eb.publish(null, ""));
+ assertNullPointerException(() -> eb.publish(null, "", new DeliveryOptions()));
+ assertNullPointerException(() -> eb.publish("", "", null));
+ assertNullPointerException(() -> eb.consumer(null));
+ assertNullPointerException(() -> eb.localConsumer(null));
+ assertNullPointerException(() -> eb.sender(null));
+ assertNullPointerException(() -> eb.sender(null, new DeliveryOptions()));
+ assertNullPointerException(() -> eb.publisher("", null));
+ assertNullPointerException(() -> eb.publisher(null, new DeliveryOptions()));
+ assertNullPointerException(() -> eb.registerCodec(null));
+ assertNullPointerException(() -> eb.unregisterCodec(null));
+ assertNullPointerException(() -> eb.registerDefaultCodec(null, new MyPOJOEncoder1()));
+ assertNullPointerException(() -> eb.registerDefaultCodec(Object.class, null));
+ assertNullPointerException(() -> eb.unregisterDefaultCodec(null));
+ }
+
@Test
public void testRegisterUnregister() {
String str = TestUtils.randomUnicodeString(100);
@@ -791,22 +829,12 @@ public class LocalEventBusTest extends EventBusTestBase {
@Test
public void testNoRegisteredDefaultDecoder() throws Exception {
- try {
- vertx.eventBus().send(ADDRESS1, new MyPOJO("foo"));
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> vertx.eventBus().send(ADDRESS1, new MyPOJO("foo")));
}
@Test
public void testRegisterDefaultSystemDecoder() throws Exception {
- try {
- vertx.eventBus().registerDefaultCodec(MyPOJO.class, new MySystemDecoder());
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> vertx.eventBus().registerDefaultCodec(MyPOJO.class, new MySystemDecoder()));
}
@Test
@@ -814,34 +842,19 @@ public class LocalEventBusTest extends EventBusTestBase {
MessageCodec codec = new MyPOJOEncoder1();
vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec);
vertx.eventBus().unregisterDefaultCodec(MyPOJO.class);
- try {
- vertx.eventBus().send(ADDRESS1, new MyPOJO("foo"));
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // OK
- }
+ assertIllegalArgumentException(() -> vertx.eventBus().send(ADDRESS1, new MyPOJO("foo")));
}
@Test
public void testRegisterDefaultTwice() throws Exception {
MessageCodec codec = new MyPOJOEncoder1();
vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec);
- try {
- vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec);
- fail("Should throw exception");
- } catch (IllegalStateException e) {
- // OK
- }
+ assertIllegalStateException(() -> vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec));
}
@Test
public void testDefaultCodecNullName() throws Exception {
- try {
- vertx.eventBus().registerDefaultCodec(String.class, new NullNameCodec());
- fail("Should throw exception");
- } catch (NullPointerException e) {
- // OK
- }
+ assertNullPointerException(() -> vertx.eventBus().registerDefaultCodec(String.class, new NullNameCodec()));
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/LocalSharedDataTest.java b/vertx-core/src/test/java/io/vertx/test/core/LocalSharedDataTest.java
index 76b074b02..22aa44f9a 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/LocalSharedDataTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/LocalSharedDataTest.java
@@ -23,6 +23,9 @@ import org.junit.Test;
import java.util.Random;
+import static io.vertx.test.core.TestUtils.assertIllegalArgumentException;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Tim Fox
*/
@@ -37,7 +40,7 @@ public class LocalSharedDataTest extends VertxTestBase {
@Test
public void testMap() throws Exception {
-
+ assertNullPointerException(() -> sharedData.getLocalMap(null));
LocalMap map = sharedData.getLocalMap("foo");
LocalMap map2 = sharedData.getLocalMap("foo");
assertTrue(map == map2);
@@ -110,12 +113,7 @@ public class LocalSharedDataTest extends VertxTestBase {
assertTrue(bgot1 != bgot2);
assertTrue(TestUtils.byteArraysEqual(bytes, bgot2));
- try {
- map.put(key, new SomeOtherClass());
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- //OK
- }
+ assertIllegalArgumentException(() -> map.put(key, new SomeOtherClass()));
}
diff --git a/vertx-core/src/test/java/io/vertx/test/core/RecordParserTest.java b/vertx-core/src/test/java/io/vertx/test/core/RecordParserTest.java
index e2b27c016..29eabdcd7 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/RecordParserTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/RecordParserTest.java
@@ -25,13 +25,25 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import static org.junit.Assert.*;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+import static org.junit.Assert.assertEquals;
/**
* @author Tim Fox
*/
public class RecordParserTest {
+ @Test
+ public void testIllegalArguments() throws Exception {
+ assertNullPointerException(() -> RecordParser.newDelimited((byte[]) null, handler -> {}));
+ assertNullPointerException(() -> RecordParser.newDelimited((String) null, handler -> {}));
+
+ RecordParser parser = RecordParser.newDelimited("", handler -> {});
+ assertNullPointerException(() -> parser.setOutput(null));
+ assertNullPointerException(() -> parser.delimitedMode((byte[]) null));
+ assertNullPointerException(() -> parser.delimitedMode((String) null));
+ }
+
@Test
/*
Test parsing with delimiters
diff --git a/vertx-core/src/test/java/io/vertx/test/core/SharedCounterTest.java b/vertx-core/src/test/java/io/vertx/test/core/SharedCounterTest.java
index 9d1ef2aa2..c5f50c3e7 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/SharedCounterTest.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/SharedCounterTest.java
@@ -20,6 +20,8 @@ import io.vertx.core.Vertx;
import io.vertx.core.shareddata.Counter;
import org.junit.Test;
+import static io.vertx.test.core.TestUtils.assertNullPointerException;
+
/**
* @author Tim Fox
*/
@@ -29,6 +31,24 @@ public class SharedCounterTest extends VertxTestBase {
return vertx;
}
+ @Test
+ public void testIllegalArguments() throws Exception {
+ assertNullPointerException(() -> getVertx().sharedData().getCounter(null, ar -> {}));
+ assertNullPointerException(() -> getVertx().sharedData().getCounter("foo", null));
+ getVertx().sharedData().getCounter("foo", ar -> {
+ Counter counter = ar.result();
+ assertNullPointerException(() -> counter.get(null));
+ assertNullPointerException(() -> counter.incrementAndGet(null));
+ assertNullPointerException(() -> counter.getAndIncrement(null));
+ assertNullPointerException(() -> counter.decrementAndGet(null));
+ assertNullPointerException(() -> counter.addAndGet(1, null));
+ assertNullPointerException(() -> counter.getAndAdd(1, null));
+ assertNullPointerException(() -> counter.compareAndSet(1, 1, null));
+ testComplete();
+ });
+ await();
+ }
+
@Test
public void testGet() {
getVertx().sharedData().getCounter("foo", ar -> {
diff --git a/vertx-core/src/test/java/io/vertx/test/core/TestUtils.java b/vertx-core/src/test/java/io/vertx/test/core/TestUtils.java
index 8c57726db..701caf069 100644
--- a/vertx-core/src/test/java/io/vertx/test/core/TestUtils.java
+++ b/vertx-core/src/test/java/io/vertx/test/core/TestUtils.java
@@ -252,4 +252,17 @@ public class TestUtils {
// OK
}
}
+
+ /**
+ * Asserts that an IndexOutOfBoundsException is thrown by the code block.
+ * @param runnable code block to execute
+ */
+ public static void assertIndexOutOfBoundsException(Runnable runnable) {
+ try {
+ runnable.run();
+ fail("Should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // OK
+ }
+ }
}