diff --git a/vertx-core/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/vertx-core/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index ae5c866f4..493321c1f 100644 --- a/vertx-core/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -20,9 +20,11 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; import io.vertx.core.buffer.Buffer; +import io.vertx.core.impl.Arguments; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.util.Objects; /** * @author Tim Fox @@ -44,7 +46,7 @@ public class BufferImpl implements Buffer { } BufferImpl(String str, String enc) { - this(str.getBytes(Charset.forName(enc))); + this(str.getBytes(Charset.forName(Objects.requireNonNull(enc)))); } BufferImpl(String str) { @@ -94,6 +96,7 @@ public class BufferImpl implements Buffer { } public byte[] getBytes(int start, int end) { + Arguments.require(end >= start, "end must be greater or equal than start"); byte[] arr = new byte[end - start]; buffer.getBytes(start, arr, 0, end - start); return arr; @@ -168,7 +171,7 @@ public class BufferImpl implements Buffer { } public Buffer appendString(String str, String enc) { - return append(str, Charset.forName(enc)); + return append(str, Charset.forName(Objects.requireNonNull(enc))); } public Buffer appendString(String str) { diff --git a/vertx-core/src/main/java/io/vertx/core/datagram/DatagramSocketOptions.java b/vertx-core/src/main/java/io/vertx/core/datagram/DatagramSocketOptions.java index d4ea05dd9..6bcca7e0b 100644 --- a/vertx-core/src/main/java/io/vertx/core/datagram/DatagramSocketOptions.java +++ b/vertx-core/src/main/java/io/vertx/core/datagram/DatagramSocketOptions.java @@ -17,6 +17,7 @@ package io.vertx.core.datagram; import io.vertx.codegen.annotations.Options; +import io.vertx.core.impl.Arguments; import io.vertx.core.json.JsonObject; import io.vertx.core.net.NetworkOptions; @@ -130,9 +131,7 @@ public class DatagramSocketOptions extends NetworkOptions { } public DatagramSocketOptions setMulticastTimeToLive(int multicastTimeToLive) { - if (multicastTimeToLive < 0) { - throw new IllegalArgumentException("multicastTimeToLive must be >= 0"); - } + Arguments.require(multicastTimeToLive >= 0, "multicastTimeToLive must be >= 0"); this.multicastTimeToLive = multicastTimeToLive; return this; } diff --git a/vertx-core/src/main/java/io/vertx/core/datagram/impl/DatagramSocketImpl.java b/vertx-core/src/main/java/io/vertx/core/datagram/impl/DatagramSocketImpl.java index 107601b95..972c3c737 100644 --- a/vertx-core/src/main/java/io/vertx/core/datagram/impl/DatagramSocketImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/datagram/impl/DatagramSocketImpl.java @@ -28,13 +28,13 @@ import io.vertx.core.buffer.Buffer; import io.vertx.core.datagram.DatagramSocket; import io.vertx.core.datagram.DatagramSocketOptions; import io.vertx.core.datagram.PacketWritestream; +import io.vertx.core.impl.Arguments; import io.vertx.core.impl.ContextImpl; import io.vertx.core.impl.VertxInternal; import io.vertx.core.json.JsonObject; import io.vertx.core.net.SocketAddress; import io.vertx.core.net.impl.ConnectionBase; import io.vertx.core.net.impl.SocketAddressImpl; -import io.vertx.core.streams.WriteStream; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -42,6 +42,7 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -169,6 +170,7 @@ public class DatagramSocketImpl extends ConnectionBase implements DatagramSocket } private DatagramSocket listen(SocketAddress local, Handler> handler) { + Objects.requireNonNull(handler, "no null handler accepted"); InetSocketAddress is = new InetSocketAddress(local.hostAddress(), local.hostPort()); ChannelFuture future = channel().bind(is); addListener(future, ar -> { @@ -202,6 +204,7 @@ public class DatagramSocketImpl extends ConnectionBase implements DatagramSocket @Override @SuppressWarnings("unchecked") public DatagramSocket send(Buffer packet, int port, String host, Handler> handler) { + Objects.requireNonNull(host, "no null host accepted"); ChannelFuture future = channel().writeAndFlush(new DatagramPacket(packet.getByteBuf(), new InetSocketAddress(host, port))); addListener(future, handler); if (metrics.isEnabled()) metrics.bytesWritten(new SocketAddressImpl(port, host), packet.length()); @@ -211,6 +214,8 @@ public class DatagramSocketImpl extends ConnectionBase implements DatagramSocket @Override public PacketWritestream sender(int port, String host) { + Arguments.requireInRange(port, 0, 65535, "port p must be in range 0 <= p <= 65535"); + Objects.requireNonNull(host, "no null host accepted"); return new PacketWriteStreamImpl(this, port, host); } diff --git a/vertx-core/src/main/java/io/vertx/core/dns/impl/DnsClientImpl.java b/vertx-core/src/main/java/io/vertx/core/dns/impl/DnsClientImpl.java index 232530676..ed784d122 100644 --- a/vertx-core/src/main/java/io/vertx/core/dns/impl/DnsClientImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/dns/impl/DnsClientImpl.java @@ -45,7 +45,6 @@ import io.vertx.core.dns.impl.netty.decoder.RecordDecoderFactory; import io.vertx.core.dns.impl.netty.decoder.record.MailExchangerRecord; import io.vertx.core.dns.impl.netty.decoder.record.ServiceRecord; import io.vertx.core.impl.ContextImpl; - import io.vertx.core.impl.VertxInternal; import io.vertx.core.net.impl.PartialPooledByteBufAllocator; @@ -57,6 +56,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.concurrent.ThreadLocalRandom; /** @@ -238,6 +238,7 @@ public final class DnsClientImpl implements DnsClient { @SuppressWarnings("unchecked") private void lookup(String name, Future result, int... types) { + Objects.requireNonNull(name, "no null name accepted"); bootstrap.connect(dnsServer).addListener(new RetryChannelFutureListener(result) { @Override public void onSuccess(ChannelFuture future) throws Exception { diff --git a/vertx-core/src/main/java/io/vertx/core/eventbus/DeliveryOptions.java b/vertx-core/src/main/java/io/vertx/core/eventbus/DeliveryOptions.java index e78c8a6be..3f274ae92 100644 --- a/vertx-core/src/main/java/io/vertx/core/eventbus/DeliveryOptions.java +++ b/vertx-core/src/main/java/io/vertx/core/eventbus/DeliveryOptions.java @@ -17,11 +17,13 @@ package io.vertx.core.eventbus; import io.vertx.codegen.annotations.Options; -import io.vertx.core.http.CaseInsensitiveHeaders; import io.vertx.core.MultiMap; +import io.vertx.core.http.CaseInsensitiveHeaders; +import io.vertx.core.impl.Arguments; import io.vertx.core.json.JsonObject; import java.util.Map; +import java.util.Objects; /** * @author Tim Fox @@ -64,9 +66,7 @@ public class DeliveryOptions { } public DeliveryOptions setSendTimeout(long timeout) { - if (timeout < 1) { - throw new IllegalArgumentException("sendTimeout must be >= 1"); - } + Arguments.require(timeout >= 1, "sendTimeout must be >= 1"); this.timeout = timeout; return this; } @@ -82,6 +82,8 @@ public class DeliveryOptions { public DeliveryOptions addHeader(String key, String value) { checkHeaders(); + Objects.requireNonNull(key, "no null key accepted"); + Objects.requireNonNull(value, "no null value accepted"); headers.add(key, value); return this; } diff --git a/vertx-core/src/main/java/io/vertx/core/eventbus/EventBus.java b/vertx-core/src/main/java/io/vertx/core/eventbus/EventBus.java index 42fb70f23..d19c976af 100644 --- a/vertx-core/src/main/java/io/vertx/core/eventbus/EventBus.java +++ b/vertx-core/src/main/java/io/vertx/core/eventbus/EventBus.java @@ -58,15 +58,15 @@ public interface EventBus extends Measured { /** * Close the EventBus and release all resources. - * - * @param completionHandler - */ + * + * @param completionHandler may be {@code null} + */ void close(Handler> completionHandler); /** * Send a message * @param address The address to send it to - * @param message The message + * @param message The message, may be {@code null} */ @Fluent EventBus send(String address, Object message); @@ -74,8 +74,8 @@ public interface EventBus extends Measured { /** * Send a message * @param address The address to send it to - * @param message The message - * @param replyHandler Reply handler will be called when any reply from the recipient is received + * @param message The message, may be {@code null} + * @param replyHandler Reply handler will be called when any reply from the recipient is received, may be {@code null} */ @Fluent EventBus send(String address, Object message, Handler>> replyHandler); @@ -89,7 +89,7 @@ public interface EventBus extends Measured { /** * Publish a message * @param address The address to publish it to - * @param message The message + * @param message The message, may be {@code null} */ @Fluent EventBus publish(String address, Object message); diff --git a/vertx-core/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java b/vertx-core/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java index e76f5b16b..87cb6b999 100644 --- a/vertx-core/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java @@ -44,6 +44,7 @@ import io.vertx.core.eventbus.impl.codecs.PingMessageCodec; import io.vertx.core.eventbus.impl.codecs.ReplyExceptionMessageCodec; import io.vertx.core.eventbus.impl.codecs.ShortMessageCodec; import io.vertx.core.eventbus.impl.codecs.StringMessageCodec; +import io.vertx.core.impl.Arguments; import io.vertx.core.impl.Closeable; import io.vertx.core.impl.ContextImpl; import io.vertx.core.impl.VertxInternal; @@ -184,21 +185,27 @@ public class EventBusImpl implements EventBus { @Override public WriteStream sender(String address) { + Objects.requireNonNull(address, "address"); return (ProducerBase) data -> send(address, data); } @Override public WriteStream sender(String address, DeliveryOptions options) { + Objects.requireNonNull(address, "address"); + Objects.requireNonNull(options, "options"); return (ProducerBase) data -> send(address, data, options); } @Override public WriteStream publisher(String address) { + Objects.requireNonNull(address, "address"); return (ProducerBase) data -> publish(address, data); } @Override public WriteStream publisher(String address, DeliveryOptions options) { + Objects.requireNonNull(address, "address"); + Objects.requireNonNull(options, "options"); return (ProducerBase) data -> publish(address, data, options); } @@ -215,11 +222,13 @@ public class EventBusImpl implements EventBus { @Override public MessageConsumer consumer(String address) { + Objects.requireNonNull(address, "address"); return new HandlerRegistration<>(address, false, false, -1); } @Override public MessageConsumer localConsumer(String address) { + Objects.requireNonNull(address, "address"); return new HandlerRegistration<>(address, false, true, -1); } @@ -310,6 +319,7 @@ public class EventBusImpl implements EventBus { } MessageImpl createMessage(boolean send, String address, MultiMap headers, Object body, String codecName) { + Objects.requireNonNull(address, "no null address accepted"); MessageCodec codec; if (codecName != null) { codec = userCodecMap.get(codecName); @@ -976,9 +986,7 @@ public class EventBusImpl implements EventBus { @Override public MessageConsumer setMaxBufferedMessages(int maxBufferedMessages) { - if (maxBufferedMessages < 0) { - throw new IllegalArgumentException("Max buffered messages cannot be negative"); - } + Arguments.require(maxBufferedMessages >= 0, "Max buffered messages cannot be negative"); while (pending.size() > maxBufferedMessages) { pending.poll(); } diff --git a/vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java b/vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java index 116562e47..f4659ad8d 100644 --- a/vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java @@ -24,6 +24,7 @@ import io.vertx.core.buffer.Buffer; import io.vertx.core.file.AsyncFile; import io.vertx.core.file.FileSystemException; import io.vertx.core.file.OpenOptions; +import io.vertx.core.impl.Arguments; import io.vertx.core.impl.ContextImpl; import io.vertx.core.impl.VertxInternal; import io.vertx.core.logging.Logger; @@ -41,6 +42,7 @@ import java.nio.file.attribute.PosixFilePermissions; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; +import java.util.Objects; /** * @author Tim Fox @@ -111,6 +113,9 @@ public class AsyncFileImpl implements AsyncFile { @Override public AsyncFile write(Buffer buffer, long position, Handler> handler) { + Objects.requireNonNull(buffer, "buffer"); + Objects.requireNonNull(handler, "handler"); + Arguments.require(position >= 0, "position must be >= 0"); check(); ByteBuf buf = buffer.getByteBuf(); if (buf.nioBufferCount() > 1) { @@ -140,6 +145,11 @@ public class AsyncFileImpl implements AsyncFile { } @Override public AsyncFile read(Buffer buffer, int offset, long position, int length, Handler> handler) { + Objects.requireNonNull(buffer, "buffer"); + Objects.requireNonNull(handler, "handler"); + Arguments.require(offset >= 0, "offset must be >= 0"); + Arguments.require(position >= 0, "position must be >= 0"); + Arguments.require(length >= 0, "length must be >= 0"); check(); ByteBuffer bb = ByteBuffer.allocate(length); doRead(buffer, offset, bb, position, handler); @@ -184,6 +194,7 @@ public class AsyncFileImpl implements AsyncFile { @Override public AsyncFile setWriteQueueMaxSize(int maxSize) { + Arguments.require(maxSize >= 2, "maxSize must be >= 2"); check(); this.maxWrites = maxSize; this.lwm = maxWrites / 2; diff --git a/vertx-core/src/main/java/io/vertx/core/net/NetworkOptions.java b/vertx-core/src/main/java/io/vertx/core/net/NetworkOptions.java index b51685a3f..79711d9d1 100644 --- a/vertx-core/src/main/java/io/vertx/core/net/NetworkOptions.java +++ b/vertx-core/src/main/java/io/vertx/core/net/NetworkOptions.java @@ -16,6 +16,7 @@ package io.vertx.core.net; +import io.vertx.core.impl.Arguments; import io.vertx.core.json.JsonObject; import io.vertx.core.net.impl.SocketDefaults; @@ -60,9 +61,7 @@ public abstract class NetworkOptions { } public NetworkOptions setSendBufferSize(int sendBufferSize) { - if (sendBufferSize < 1) { - throw new IllegalArgumentException("sendBufferSize must be > 0"); - } + Arguments.require(sendBufferSize > 0, "sendBufferSize must be > 0"); this.sendBufferSize = sendBufferSize; return this; } @@ -72,9 +71,7 @@ public abstract class NetworkOptions { } public NetworkOptions setReceiveBufferSize(int receiveBufferSize) { - if (receiveBufferSize < 1) { - throw new IllegalArgumentException("receiveBufferSize must be > 0"); - } + Arguments.require(receiveBufferSize > 0, "receiveBufferSize must be > 0"); this.receiveBufferSize = receiveBufferSize; return this; } @@ -93,9 +90,7 @@ public abstract class NetworkOptions { } public NetworkOptions setTrafficClass(int trafficClass) { - if (trafficClass < 0 || trafficClass > 255) { - throw new IllegalArgumentException("trafficClass tc must be 0 <= tc <= 255"); - } + Arguments.requireInRange(trafficClass, 0, 255, "trafficClass tc must be 0 <= tc <= 255"); this.trafficClass = trafficClass; return this; } diff --git a/vertx-core/src/main/java/io/vertx/core/parsetools/RecordParser.java b/vertx-core/src/main/java/io/vertx/core/parsetools/RecordParser.java index ba5d48a30..f74c1c009 100644 --- a/vertx-core/src/main/java/io/vertx/core/parsetools/RecordParser.java +++ b/vertx-core/src/main/java/io/vertx/core/parsetools/RecordParser.java @@ -18,6 +18,9 @@ package io.vertx.core.parsetools; import io.vertx.core.Handler; import io.vertx.core.buffer.Buffer; +import io.vertx.core.impl.Arguments; + +import java.util.Objects; /** * A helper class which allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed @@ -65,6 +68,7 @@ public class RecordParser implements Handler { } public void setOutput(Handler output) { + Objects.requireNonNull(output, "output"); this.output = output; } @@ -110,7 +114,7 @@ public class RecordParser implements Handler { * {@code output} Will receive whole records which have been parsed. */ public static RecordParser newFixed(int size, Handler output) { - if (size <= 0) throw new IllegalArgumentException("Size must be > 0"); + Arguments.require(size > 0, "Size must be > 0"); RecordParser ls = new RecordParser(output); ls.fixedSizeMode(size); return ls; @@ -131,6 +135,7 @@ public class RecordParser implements Handler { * This method can be called multiple times with different values of delim while data is being parsed. */ public void delimitedMode(byte[] delim) { + Objects.requireNonNull(delim, "delim"); delimited = true; this.delim = delim; delimPos = 0; @@ -142,7 +147,7 @@ public class RecordParser implements Handler { * This method can be called multiple times with different values of size while data is being parsed. */ public void fixedSizeMode(int size) { - if (size <= 0) throw new IllegalArgumentException("Size must be > 0"); + Arguments.require(size > 0, "Size must be > 0"); delimited = false; recordSize = size; reset = true; diff --git a/vertx-core/src/main/java/io/vertx/core/shareddata/impl/AsynchronousCounter.java b/vertx-core/src/main/java/io/vertx/core/shareddata/impl/AsynchronousCounter.java index 91e70c68a..644f6755c 100644 --- a/vertx-core/src/main/java/io/vertx/core/shareddata/impl/AsynchronousCounter.java +++ b/vertx-core/src/main/java/io/vertx/core/shareddata/impl/AsynchronousCounter.java @@ -23,6 +23,7 @@ import io.vertx.core.Handler; import io.vertx.core.impl.VertxInternal; import io.vertx.core.shareddata.Counter; +import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; /** @@ -39,42 +40,49 @@ public class AsynchronousCounter implements Counter { @Override public void get(Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.get()))); } @Override public void incrementAndGet(Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.incrementAndGet()))); } @Override public void getAndIncrement(Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.getAndIncrement()))); } @Override public void decrementAndGet(Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.decrementAndGet()))); } @Override public void addAndGet(long value, Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.addAndGet(value)))); } @Override public void getAndAdd(long value, Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.getAndAdd(value)))); } @Override public void compareAndSet(long expected, long value, Handler> resultHandler) { + Objects.requireNonNull(resultHandler, "resultHandler"); Context context = vertx.getOrCreateContext(); context.runOnContext(v -> resultHandler.handle(Future.completedFuture(counter.compareAndSet(expected, value)))); } diff --git a/vertx-core/src/main/java/io/vertx/core/shareddata/impl/SharedDataImpl.java b/vertx-core/src/main/java/io/vertx/core/shareddata/impl/SharedDataImpl.java index f793ad504..2888cdf6c 100644 --- a/vertx-core/src/main/java/io/vertx/core/shareddata/impl/SharedDataImpl.java +++ b/vertx-core/src/main/java/io/vertx/core/shareddata/impl/SharedDataImpl.java @@ -20,6 +20,7 @@ import io.vertx.core.AsyncResult; import io.vertx.core.Context; import io.vertx.core.Future; import io.vertx.core.Handler; +import io.vertx.core.impl.Arguments; import io.vertx.core.impl.VertxInternal; import io.vertx.core.shareddata.AsyncMap; import io.vertx.core.shareddata.Counter; @@ -29,6 +30,7 @@ import io.vertx.core.shareddata.SharedData; import io.vertx.core.spi.cluster.ClusterManager; import java.io.Serializable; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -52,6 +54,8 @@ public class SharedDataImpl implements SharedData { @Override public void getClusterWideMap(String name, Handler>> resultHandler) { + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(resultHandler, "resultHandler"); if (clusterManager == null) { throw new IllegalStateException("Can't get cluster wide map if not clustered"); } @@ -67,11 +71,16 @@ public class SharedDataImpl implements SharedData { @Override public void getLock(String name, Handler> resultHandler) { + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(resultHandler, "resultHandler"); getLockWithTimeout(name, DEFAULT_LOCK_TIMEOUT, resultHandler); } @Override public void getLockWithTimeout(String name, long timeout, Handler> resultHandler) { + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(resultHandler, "resultHandler"); + Arguments.require(timeout >= 0, "timeout must be >= 0"); if (clusterManager == null) { getLocalLock(name, timeout, resultHandler); } else { @@ -81,6 +90,8 @@ public class SharedDataImpl implements SharedData { @Override public void getCounter(String name, Handler> 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 + } + } }