Skip to content

Commit

Permalink
Remove use of hamcrest in production (non-test) code. (#480)
Browse files Browse the repository at this point in the history
* Remove TransformingMatcher
  • Loading branch information
dvlato authored and mikkokar committed Oct 11, 2019
1 parent c32933a commit a19e30b
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 317 deletions.
6 changes: 0 additions & 6 deletions components/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
6 changes: 0 additions & 6 deletions components/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,8 @@
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.SocketChannel;

import static com.hotels.styx.common.MorePreconditions.checkArgument;
import static com.hotels.styx.common.MorePreconditions.checkNotEmpty;
import static com.hotels.styx.common.Preconditions.checkArgument;
import static com.hotels.styx.common.Preconditions.checkNotEmpty;

/**
* A factory for creating Epoll-based client event loops.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.hotels.styx.client.origincommands.DisableOrigin;
import com.hotels.styx.client.origincommands.EnableOrigin;
import com.hotels.styx.support.matchers.LoggingTestSupport;
import org.hamcrest.Matcher;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -44,7 +43,6 @@
import static com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings;
import static com.hotels.styx.client.OriginsInventory.OriginState.ACTIVE;
import static com.hotels.styx.client.OriginsInventory.OriginState.DISABLED;
import static com.hotels.styx.common.testing.matcher.TransformingMatcher.hasDerivedValue;
import static com.hotels.styx.support.matchers.ContainsExactlyOneMatcher.containsExactlyOne;
import static com.hotels.styx.support.matchers.IsOptional.isAbsent;
import static com.hotels.styx.support.matchers.IsOptional.isValue;
Expand Down Expand Up @@ -93,7 +91,7 @@ public void stop() {
public void startsMonitoringNewOrigins() {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));

Expand All @@ -110,14 +108,14 @@ public void updatesOriginPortNumber() throws Exception {

inventory.setOrigins(originV1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).monitor(singleton(originV1));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
verify(eventBus).post(any(OriginsSnapshot.class));

inventory.setOrigins(originV2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(originV1));
verify(monitor).monitor(singleton(originV2));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
Expand All @@ -131,14 +129,14 @@ public void updatesOriginHostName() throws Exception {

inventory.setOrigins(originV1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).monitor(singleton(originV1));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
verify(eventBus).post(any(OriginsSnapshot.class));

inventory.setOrigins(originV2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(originV1));
verify(monitor).monitor(singleton(originV2));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
Expand Down Expand Up @@ -187,7 +185,7 @@ public void shutsConnectionPoolForModifiedOrigin() {
public void ignoresUnchangedOrigins() throws Exception {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
Expand All @@ -196,7 +194,7 @@ public void ignoresUnchangedOrigins() throws Exception {

inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor, times(1)).monitor(singleton(ORIGIN_1));
verify(monitor, times(1)).monitor(singleton(ORIGIN_2));
verify(eventBus).post(any(OriginsSnapshot.class));
Expand All @@ -206,7 +204,7 @@ public void ignoresUnchangedOrigins() throws Exception {
public void removesOrigin() throws Exception {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
Expand All @@ -215,7 +213,7 @@ public void removesOrigin() throws Exception {

inventory.setOrigins(ORIGIN_2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isAbsent());
assertThat(gaugeValue("origins.generic-app.app-02.status"), isValue(1));
Expand Down Expand Up @@ -268,7 +266,7 @@ public void willNotDisableOriginsNotBelongingToTheApp() {

inventory.onCommand(new DisableOrigin(id("some-other-app"), ORIGIN_1.id()));

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(eventBus).post(any(OriginsSnapshot.class));
}

Expand All @@ -279,7 +277,7 @@ public void willNotEnableOriginsNotBelongingToTheApp() {
inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));
inventory.onCommand(new EnableOrigin(id("some-other-app"), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory.originCount(ACTIVE), is(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

Expand All @@ -289,8 +287,8 @@ public void disablingAnOriginRemovesItFromActiveSetAndStopsHealthCheckMonitoring

inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory, hasDisabledOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(inventory.originCount(DISABLED), is(1));

verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(-1));
Expand All @@ -304,8 +302,8 @@ public void disablingAnOriginRemovesItFromInactiveSetAndStopsHealthCheckMonitori
inventory.originUnhealthy(ORIGIN_1);
inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory, hasDisabledOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(inventory.originCount(DISABLED), is(1));

verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(-1));
Expand All @@ -327,51 +325,51 @@ public void enablingAnOriginWillReInitiateHealthCheckMonitoring() {
@Test
public void removesUnhealthyOriginsFromActiveSet() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

@Test
public void putsHealthyOriginsBackIntoActiveSet() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
verify(eventBus, times(3)).post(any(OriginsSnapshot.class));
}

@Test
public void reportingUpRepeatedlyDoesNotAffectCurrentActiveOrigins() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originHealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(eventBus, times(1)).post(any(OriginsSnapshot.class));
}

@Test
public void reportingDownRepeatedlyDoesNotAffectCurrentActiveOrigins() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);
inventory.originUnhealthy(ORIGIN_1);
inventory.originUnhealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(0));
assertThat(inventory.originCount(ACTIVE), is(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

Expand Down Expand Up @@ -475,22 +473,6 @@ public void stopsMonitoringAndUnregistersWhenClosed() {
verify(eventBus).unregister(eq(inventory));
}

private static Matcher<OriginsInventory> hasNoActiveOrigins() {
return hasActiveOrigins(0);
}

private static Matcher<OriginsInventory> hasActiveOrigins(int numberOfActiveOrigins) {
return hasOrigins(numberOfActiveOrigins, ACTIVE);
}

private static Matcher<OriginsInventory> hasDisabledOrigins(int numberOfDisabledOrigins) {
return hasOrigins(numberOfDisabledOrigins, DISABLED);
}

private static Matcher<OriginsInventory> hasOrigins(int numberOfOrigins, OriginsInventory.OriginState state) {
return hasDerivedValue(inventory -> inventory.originCount(state), is(numberOfOrigins));
}

private Optional<Integer> gaugeValue(String name) {
return gauge(name)
.map(Gauge::getValue)
Expand Down
5 changes: 5 additions & 0 deletions components/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>testng</artifactId>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down

This file was deleted.

Loading

0 comments on commit a19e30b

Please sign in to comment.