Skip to content

Commit

Permalink
Migrate remaining usage of hamcrest to assertj (#1032)
Browse files Browse the repository at this point in the history
* is(Optional) -> contains
* is(true) -> isTrue
* instanceOf -> isInstanceOf
* is(nullValue) -> isNull
* greaterThan -> isGreaterThan, startsWith -> startsWith, misc
* is(not) -> isNotEqualTo
* is -> isEqualTo
* Optimize imports
* Remove remaining hamcrest references.
* Replace import with static import
* Remove hamcrest dependency
  • Loading branch information
anuraaga authored and trustin committed Mar 2, 2018
1 parent 5e4a2f4 commit 22da86e
Show file tree
Hide file tree
Showing 30 changed files with 433 additions and 495 deletions.
5 changes: 0 additions & 5 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ This product depends on Guava, distributed by Google:
* License: licenses/LICENSE.guava.al20.txt (Apache License v2.0)
* Homepage: https://github.com/google/guava

This product depends on Hamcrest, distributed by hamcrest.org:

* License: licenses/LICENSE.hamcrest.bsd.txt (BSD License)
* Homepage: http://hamcrest.org/

This product depends on Hibernate Validator, distributed by Red Hat, Inc:

* License: licenses/LICENSE.hibernate-validator.al20.txt (Apache License v2.0)
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ configure(projectsWithFlags('java')) {
testCompile 'net.javacrumbs.json-unit:json-unit-fluent'
testCompile 'org.awaitility:awaitility'
testRuntime 'org.checkerframework:checker-compat-qual' // Required by guava-testlib
testCompile 'org.hamcrest:hamcrest-library'
testCompile 'org.assertj:assertj-core'
testCompile 'org.mockito:mockito-core'
testCompile 'org.apache.httpcomponents:httpclient'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package com.linecorp.armeria.client;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

Expand All @@ -34,10 +31,10 @@ public void testSetHttpHeader() {
HttpHeaders httpHeader = HttpHeaders.of(AsciiString.of("x-user-defined"), "HEADER_VALUE");

ClientOptions options = ClientOptions.of(ClientOption.HTTP_HEADERS.newValue(httpHeader));
assertThat(options.get(ClientOption.HTTP_HEADERS), is(Optional.of(httpHeader)));
assertThat(options.get(ClientOption.HTTP_HEADERS)).contains(httpHeader);

ClientOptions options2 = ClientOptions.DEFAULT;
assertThat(options2.get(ClientOption.HTTP_HEADERS), is(Optional.of(HttpHeaders.EMPTY_HEADERS)));
assertThat(options2.get(ClientOption.HTTP_HEADERS)).contains(HttpHeaders.EMPTY_HEADERS);
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package com.linecorp.armeria.client;

import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.After;
Expand Down Expand Up @@ -94,14 +93,14 @@ private void readResponse() {
session.unfinishedResponses--;
final Object res = new Object();
ch.writeInbound(res);
assertThat(ch.readInbound(), is(res));
assertThat((Object) ch.readInbound()).isEqualTo(res);
}

private void writeRequest() {
session.unfinishedResponses++;
final Object req = new Object();
ch.writeOutbound(req);
assertThat(ch.readOutbound(), is(req));
assertThat((Object) ch.readOutbound()).isEqualTo(req);
}

private static final class MockHttpSessionHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.linecorp.armeria.client.circuitbreaker;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.time.Duration;
Expand Down Expand Up @@ -50,8 +48,8 @@ private static CircuitBreakerBuilder builder() {

@Test
public void testConstructor() {
assertThat(new CircuitBreakerBuilder(remoteServiceName).build().name(), is(remoteServiceName));
assertThat(new CircuitBreakerBuilder().build().name(), is(startsWith("circuit-breaker-")));
assertThat(new CircuitBreakerBuilder(remoteServiceName).build().name()).isEqualTo(remoteServiceName);
assertThat(new CircuitBreakerBuilder().build().name()).startsWith("circuit-breaker-");
}

@Test
Expand All @@ -66,8 +64,10 @@ CircuitBreakerConfig confOf(CircuitBreaker circuitBreaker) {

@Test
public void testFailureRateThreshold() {
assertThat(confOf(builder().failureRateThreshold(0.123).build()).failureRateThreshold(), is(0.123));
assertThat(confOf(builder().failureRateThreshold(1).build()).failureRateThreshold(), is(1.0));
assertThat(confOf(builder().failureRateThreshold(0.123).build()).failureRateThreshold())
.isEqualTo(0.123);
assertThat(confOf(builder().failureRateThreshold(1).build()).failureRateThreshold())
.isEqualTo(1.0);
}

@Test
Expand All @@ -80,10 +80,10 @@ public void testFailureRateThresholdWithInvalidArgument() {
@Test
public void testMinimumRequestThreshold() {
CircuitBreakerConfig config1 = confOf(builder().minimumRequestThreshold(Long.MAX_VALUE).build());
assertThat(config1.minimumRequestThreshold(), is(Long.MAX_VALUE));
assertThat(config1.minimumRequestThreshold()).isEqualTo(Long.MAX_VALUE);

CircuitBreakerConfig config2 = confOf(builder().minimumRequestThreshold(0).build());
assertThat(config2.minimumRequestThreshold(), is(0L));
assertThat(config2.minimumRequestThreshold()).isEqualTo(0L);
}

@Test
Expand All @@ -94,14 +94,14 @@ public void testMinimumRequestThresholdWithInvalidArgument() {
@Test
public void testTrialRequestInterval() {
CircuitBreakerConfig config = confOf(builder().trialRequestInterval(oneSecond).build());
assertThat(config.trialRequestInterval(), is(oneSecond));
assertThat(config.trialRequestInterval()).isEqualTo(oneSecond);
}

@Test
public void testTrialRequestIntervalInMillis() {
CircuitBreakerConfig config = confOf(
builder().trialRequestIntervalMillis(oneSecond.toMillis()).build());
assertThat(config.trialRequestInterval(), is(oneSecond));
assertThat(config.trialRequestInterval()).isEqualTo(oneSecond);
}

@Test
Expand All @@ -116,13 +116,13 @@ public void testTrialRequestIntervalWithInvalidArgument() {
@Test
public void testCircuitOpenWindow() {
CircuitBreakerConfig config = confOf(builder().circuitOpenWindow(oneSecond).build());
assertThat(config.circuitOpenWindow(), is(oneSecond));
assertThat(config.circuitOpenWindow()).isEqualTo(oneSecond);
}

@Test
public void testCircuitOpenWindowInMillis() {
CircuitBreakerConfig config = confOf(builder().circuitOpenWindowMillis(oneSecond.toMillis()).build());
assertThat(config.circuitOpenWindow(), is(oneSecond));
assertThat(config.circuitOpenWindow()).isEqualTo(oneSecond);
}

@Test
Expand All @@ -137,14 +137,14 @@ public void testCircuitOpenWindowWithInvalidArgument() {
@Test
public void testCounterSlidingWindow() {
CircuitBreakerConfig config = confOf(builder().counterSlidingWindow(twoSeconds).build());
assertThat(config.counterSlidingWindow(), is(twoSeconds));
assertThat(config.counterSlidingWindow()).isEqualTo(twoSeconds);
}

@Test
public void testCounterSlidingWindowInMillis() {
CircuitBreakerConfig config = confOf(
builder().counterSlidingWindowMillis(twoSeconds.toMillis()).build());
assertThat(config.counterSlidingWindow(), is(twoSeconds));
assertThat(config.counterSlidingWindow()).isEqualTo(twoSeconds);
}

@Test
Expand All @@ -162,14 +162,14 @@ public void testCounterSlidingWindowWithInvalidArgument() {
@Test
public void testCounterUpdateInterval() {
CircuitBreakerConfig config = confOf(builder().counterUpdateInterval(oneSecond).build());
assertThat(config.counterUpdateInterval(), is(oneSecond));
assertThat(config.counterUpdateInterval()).isEqualTo(oneSecond);
}

@Test
public void testCounterUpdateIntervalInMillis() {
CircuitBreakerConfig config = confOf(
builder().counterUpdateIntervalMillis(oneSecond.toMillis()).build());
assertThat(config.counterUpdateInterval(), is(oneSecond));
assertThat(config.counterUpdateInterval()).isEqualTo(oneSecond);
}

@Test
Expand All @@ -184,7 +184,7 @@ public void testCounterUpdateIntervalWithInvalidArgument() {
@Test
public void testExceptionFilter() {
ExceptionFilter instance = e -> true;
assertThat(confOf(builder().exceptionFilter(instance).build()).exceptionFilter(), is(instance));
assertThat(confOf(builder().exceptionFilter(instance).build()).exceptionFilter()).isEqualTo(instance);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@
package com.linecorp.armeria.client.circuitbreaker;

import static com.linecorp.armeria.common.SessionProtocol.H2C;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -192,9 +186,9 @@ public void testStateTransition() throws Exception {
for (int i = 0; i < minimumRequestThreshold + 1; i++) {
RpcResponse future = stub.execute(ctx, req);
// The future is `failureRes` itself
assertThat(future.isCompletedExceptionally(), is(true));
assertThat(future.isCompletedExceptionally()).isTrue();
// This is not a CircuitBreakerException
assertThat(future.cause(), is(not(instanceOf(FailFastException.class))));
assertThat(future.cause()).isNotInstanceOf(FailFastException.class);
ticker.advance(Duration.ofMillis(1).toNanos());
}

Expand All @@ -204,7 +198,7 @@ public void testStateTransition() throws Exception {
fail();
} catch (FailFastException e) {
// The circuit is OPEN
assertThat(e.getCircuitBreaker(), is(circuitBreaker));
assertThat(e.getCircuitBreaker()).isEqualTo(circuitBreaker);
}

ticker.advance(circuitOpenWindow.toNanos());
Expand All @@ -214,11 +208,11 @@ public void testStateTransition() throws Exception {

// HALF OPEN
RpcResponse future2 = stub.execute(ctx, req);
assertThat(future2.get(), is(nullValue()));
assertThat(future2.get()).isNull();

// CLOSED
RpcResponse future3 = stub.execute(ctx, req);
assertThat(future3.get(), is(nullValue()));
assertThat(future3.get()).isNull();
}

@Test
Expand Down Expand Up @@ -301,9 +295,9 @@ public void testPerMethodScope() throws Exception {
for (int i = 0; i < minimumRequestThreshold + 1; i++) {
try {
stub.execute(ctx, req);
assertThat(i, is(lessThanOrEqualTo(minimumRequestThreshold)));
assertThat(i).isLessThanOrEqualTo(minimumRequestThreshold);
} catch (FailFastException e) {
assertThat(i, is(greaterThan(minimumRequestThreshold)));
assertThat(i).isGreaterThan(minimumRequestThreshold);
}
ticker.advance(Duration.ofMillis(1).toNanos());
}
Expand All @@ -318,7 +312,7 @@ public void testPerMethodScope() throws Exception {

// CLOSED (methodB)
RpcResponse future2 = stub.execute(ctxB, reqB);
assertThat(future2.get(), is(nullValue()));
assertThat(future2.get()).isNull();
}

@Test
Expand Down Expand Up @@ -353,17 +347,17 @@ public void testExceptionFilter() throws Exception {
for (int i = 0; i < minimumRequestThreshold + 1; i++) {
RpcResponse future = stub.execute(ctx, req);
// The future is `failedFuture` itself
assertThat(future.isCompletedExceptionally(), is(true));
assertThat(future.isCompletedExceptionally()).isTrue();
// This is not a CircuitBreakerException
assertThat(future.cause(), is(not(instanceOf(FailFastException.class))));
assertThat(future.cause()).isNotInstanceOf(FailFastException.class);
ticker.advance(Duration.ofMillis(1).toNanos());
}

// OPEN
RpcResponse future1 = stub.execute(ctx, req);
// The circuit is still CLOSED
assertThat(future1.isCompletedExceptionally(), is(true));
assertThat(future1.cause(), is(not(instanceOf(FailFastException.class))));
assertThat(future1.isCompletedExceptionally()).isTrue();
assertThat(future1.cause()).isNotInstanceOf(FailFastException.class);
}

private static void invoke(Function<Client<RpcRequest, RpcResponse>,
Expand All @@ -386,7 +380,7 @@ private static void failFastInvocation(
invoke(decorator);
fail();
} catch (FailFastException e) {
assertThat(e.getCircuitBreaker(), is(circuitBreaker));
assertThat(e.getCircuitBreaker()).isEqualTo(circuitBreaker);
}
}
}
Expand Down
Loading

0 comments on commit 22da86e

Please sign in to comment.