Skip to content

Commit

Permalink
Have Assertions return tested object
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Feb 8, 2016
1 parent 57f8540 commit 3480c91
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 36 deletions.
10 changes: 3 additions & 7 deletions client/src/main/java/org/asynchttpclient/Realm.java
Expand Up @@ -83,13 +83,9 @@ private Realm(AuthScheme scheme,//
boolean useAbsoluteURI,// boolean useAbsoluteURI,//
boolean omitQuery) { boolean omitQuery) {


assertNotNull(scheme, "scheme"); this.scheme = assertNotNull(scheme, "scheme");
assertNotNull(principal, "principal"); this.principal = assertNotNull(principal, "principal");
assertNotNull(password, "password"); this.password = assertNotNull(password, "password");

this.scheme = scheme;
this.principal = principal;
this.password = password;
this.realmName = realmName; this.realmName = realmName;
this.nonce = nonce; this.nonce = nonce;
this.algorithm = algorithm; this.algorithm = algorithm;
Expand Down
9 changes: 2 additions & 7 deletions client/src/main/java/org/asynchttpclient/cookie/Cookie.java
Expand Up @@ -18,8 +18,7 @@ public class Cookie {


public static Cookie newValidCookie(String name, String value, boolean wrap, String domain, String path, long maxAge, boolean secure, boolean httpOnly) { public static Cookie newValidCookie(String name, String value, boolean wrap, String domain, String path, long maxAge, boolean secure, boolean httpOnly) {


assertNotNull(name, "name"); name = assertNotNull(name, "name").trim();
name = name.trim();
assertNotEmpty(name, "name"); assertNotEmpty(name, "name");


for (int i = 0; i < name.length(); i++) { for (int i = 0; i < name.length(); i++) {
Expand Down Expand Up @@ -47,11 +46,7 @@ public static Cookie newValidCookie(String name, String value, boolean wrap, Str
throw new IllegalArgumentException("name starting with '$' not allowed: " + name); throw new IllegalArgumentException("name starting with '$' not allowed: " + name);
} }


assertNotNull(value, "value"); return new Cookie(name, assertNotNull(value, "value"), wrap, validateValue("domain", domain), validateValue("path", path), maxAge, secure, httpOnly);
domain = validateValue("domain", domain);
path = validateValue("path", path);

return new Cookie(name, value, wrap, domain, path, maxAge, secure, httpOnly);
} }


private static String validateValue(String name, String value) { private static String validateValue(String name, String value) {
Expand Down
Expand Up @@ -99,8 +99,7 @@ private static final class IdleChannel {
final long start; final long start;


IdleChannel(Channel channel, long start) { IdleChannel(Channel channel, long start) {
assertNotNull(channel, "channel"); this.channel = assertNotNull(channel, "channel");
this.channel = channel;
this.start = start; this.start = start;
} }


Expand Down
Expand Up @@ -32,8 +32,7 @@ public class BodyChunkedInput implements ChunkedInput<ByteBuf> {
private boolean endOfInput; private boolean endOfInput;


public BodyChunkedInput(Body body) { public BodyChunkedInput(Body body) {
assertNotNull(body, "body"); this.body = assertNotNull(body, "body");
this.body = body;
long contentLength = body.getContentLength(); long contentLength = body.getContentLength();
if (contentLength <= 0) if (contentLength <= 0)
chunkSize = DEFAULT_CHUNK_SIZE; chunkSize = DEFAULT_CHUNK_SIZE;
Expand Down
Expand Up @@ -33,8 +33,7 @@ public class BodyFileRegion extends AbstractReferenceCounted implements FileRegi
private long transfered; private long transfered;


public BodyFileRegion(RandomAccessBody body) { public BodyFileRegion(RandomAccessBody body) {
assertNotNull(body, "body"); this.body = assertNotNull(body, "body");
this.body = body;
} }


@Override @Override
Expand Down
Expand Up @@ -32,8 +32,7 @@ public FileBodyGenerator(File file) {
} }


public FileBodyGenerator(File file, long regionSeek, long regionLength) { public FileBodyGenerator(File file, long regionSeek, long regionLength) {
assertNotNull(file, "file"); this.file = assertNotNull(file, "file");
this.file = file;
this.regionLength = regionLength; this.regionLength = regionLength;
this.regionSeek = regionSeek; this.regionSeek = regionSeek;
} }
Expand Down
Expand Up @@ -42,8 +42,7 @@ public ByteArrayPart(String name, byte[] bytes, String contentType, Charset char


public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) { public ByteArrayPart(String name, byte[] bytes, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
super(name, contentType, charset, contentId, transferEncoding); super(name, contentType, charset, contentId, transferEncoding);
assertNotNull(bytes, "bytes"); this.bytes = assertNotNull(bytes, "bytes");
this.bytes = bytes;
setFileName(fileName); setFileName(fileName);
} }


Expand Down
Expand Up @@ -43,8 +43,7 @@ public FilePart(String name, File file, String contentType, Charset charset, Str


public FilePart(String name, File file, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) { public FilePart(String name, File file, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
super(name, contentType, charset, contentId, transferEncoding); super(name, contentType, charset, contentId, transferEncoding);
assertNotNull(file, "file"); if (!assertNotNull(file, "file").isFile())
if (!file.isFile())
throw new IllegalArgumentException("File is not a normal file " + file.getAbsolutePath()); throw new IllegalArgumentException("File is not a normal file " + file.getAbsolutePath());
if (!file.canRead()) if (!file.canRead())
throw new IllegalArgumentException("File is not readable " + file.getAbsolutePath()); throw new IllegalArgumentException("File is not readable " + file.getAbsolutePath());
Expand Down
Expand Up @@ -42,10 +42,9 @@ public class MultipartBody implements RandomAccessBody {
private AtomicBoolean closed = new AtomicBoolean(); private AtomicBoolean closed = new AtomicBoolean();


public MultipartBody(List<MultipartPart<? extends Part>> parts, String contentType, byte[] boundary) { public MultipartBody(List<MultipartPart<? extends Part>> parts, String contentType, byte[] boundary) {
assertNotNull(parts, "parts");
this.boundary = boundary; this.boundary = boundary;
this.contentType = contentType; this.contentType = contentType;
this.parts = parts; this.parts = assertNotNull(parts, "parts");
this.contentLength = computeContentLength(); this.contentLength = computeContentLength();
} }


Expand Down
6 changes: 2 additions & 4 deletions client/src/main/java/org/asynchttpclient/uri/Uri.java
Expand Up @@ -60,11 +60,9 @@ public Uri(String scheme,//
String path,// String path,//
String query) { String query) {


assertNotNull(scheme, "scheme"); this.scheme = assertNotNull(scheme, "scheme");
assertNotNull(host, "host");
this.scheme = scheme;
this.userInfo = userInfo; this.userInfo = userInfo;
this.host = host; this.host = assertNotNull(host, "host");
this.port = port; this.port = port;
this.path = path; this.path = path;
this.query = query; this.query = query;
Expand Down
7 changes: 5 additions & 2 deletions client/src/main/java/org/asynchttpclient/util/Assertions.java
Expand Up @@ -18,13 +18,16 @@ public final class Assertions {
private Assertions() { private Assertions() {
} }


public static void assertNotNull(Object value, String name) { public static <T> T assertNotNull(T value, String name) {
if (value == null) if (value == null)
throw new NullPointerException(name); throw new NullPointerException(name);
return value;

} }


public static void assertNotEmpty(String value, String name) { public static String assertNotEmpty(String value, String name) {
if (value.length() == 0) if (value.length() == 0)
throw new IllegalArgumentException("empty " + name); throw new IllegalArgumentException("empty " + name);
return value;
} }
} }
Expand Up @@ -95,8 +95,7 @@ public final WebSocket onCompleted() throws Exception {
throw e; throw e;
} }


assertNotNull(webSocket, "webSocket"); return assertNotNull(webSocket, "webSocket");
return webSocket;
} }


/** /**
Expand Down

0 comments on commit 3480c91

Please sign in to comment.