diff --git a/pom.xml b/pom.xml index 7b81758..ada0ac8 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,15 @@ 8 8 + UTF-8 + true + true + true + + -Xlint:all + -Xlint:-options + -Werror + @@ -53,24 +62,6 @@ org.testcontainers testcontainers 1.19.8 - test - - - com.jam2in.arcus - arcus-java-client - 1.13.4 - test - - - org.testcontainers - testcontainers - 1.19.8 - compile - - - org.apache.zookeeper - zookeeper - 3.5.9 org.junit.jupiter @@ -90,5 +81,11 @@ 1.19.8 test + + com.jam2in.arcus + arcus-java-client + 1.13.4 + test + diff --git a/src/main/java/com/jam2in/arcus/testcontainers/ArcusClusterContainer.java b/src/main/java/com/jam2in/arcus/testcontainers/ArcusClusterContainer.java index c52a557..81fd5a5 100644 --- a/src/main/java/com/jam2in/arcus/testcontainers/ArcusClusterContainer.java +++ b/src/main/java/com/jam2in/arcus/testcontainers/ArcusClusterContainer.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.ArrayList; +import org.testcontainers.containers.ContainerState; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.Network; import org.testcontainers.utility.DockerImageName; @@ -58,7 +59,6 @@ public class ArcusClusterContainer extends GenericContainer containers = new ArrayList<>(); private ArcusClusterContainer(DockerImageName imageName, ArcusContainerProps props) { - this.network = Network.newNetwork(); + super(imageName); + + final Network network = Network.newNetwork(); this.zkContainer = new ZookeeperContainer(network); this.serviceCode = props.getServiceCode(); @@ -131,43 +133,36 @@ public Integer getFirstMappedPort() { return zkContainer.getFirstMappedPort(); } - @Override - public String getDockerImageName() { - return containers.get(0).getDockerImageName(); - } - @Override public void start() { zkContainer.start(); try { createZnode(); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (InterruptedException e) { + } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } - containers.stream().forEach(ac -> ac.start()); + containers.forEach(ArcusContainer::start); } @Override public boolean isRunning() { - return containers.stream().allMatch(c -> c.isRunning()) && zkContainer.isRunning(); + return containers.stream().allMatch(ContainerState::isRunning) && zkContainer.isRunning(); } @Override public void stop() { - containers.stream().forEach(ac -> ac.stop()); + containers.forEach(ArcusContainer::stop); zkContainer.stop(); } @Override public boolean isCreated() { - return containers.stream().allMatch(c -> c.isCreated()) && zkContainer.isCreated(); + return containers.stream().allMatch(ContainerState::isCreated) && zkContainer.isCreated(); } @Override public boolean isHealthy() { - return containers.stream().allMatch(c -> c.isHealthy()) && zkContainer.isHealthy(); + return containers.stream().allMatch(ContainerState::isHealthy) && zkContainer.isHealthy(); } private void createZnode() throws IOException, InterruptedException { @@ -192,12 +187,13 @@ private static class ZookeeperContainer extends GenericContainer { + + this.withNetwork(network); + this.withEnv("ZOO_MY_ID", "1"); + this.withCreateContainerCmdModifier(cmd -> { cmd.withHostName("zoo1"); }); - withExposedPorts(DEFAULT_ZK_CONTAINER_PORT); + this.withExposedPorts(DEFAULT_ZK_CONTAINER_PORT); } } } diff --git a/src/main/java/com/jam2in/arcus/testcontainers/ArcusContainer.java b/src/main/java/com/jam2in/arcus/testcontainers/ArcusContainer.java index 3ed470f..e33b7b3 100644 --- a/src/main/java/com/jam2in/arcus/testcontainers/ArcusContainer.java +++ b/src/main/java/com/jam2in/arcus/testcontainers/ArcusContainer.java @@ -5,6 +5,8 @@ import com.github.dockerjava.api.model.Ports; import com.github.dockerjava.api.model.RestartPolicy; +import java.util.Objects; + import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.Network; import org.testcontainers.utility.DockerImageName; @@ -100,25 +102,26 @@ private void setupContainer(int port, int memSize) { } private void setupContainer(int port, String host, Network network, int memorySize) { - withCreateContainerCmdModifier(cmd -> { + this.withCreateContainerCmdModifier(cmd -> { if (host != null) { cmd.withHostName(host); } - cmd.getHostConfig().withRestartPolicy(RestartPolicy.alwaysRestart()); - cmd.getHostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(port), new ExposedPort(port))); + Objects.requireNonNull(cmd.getHostConfig()) + .withRestartPolicy(RestartPolicy.alwaysRestart()) + .withPortBindings(new PortBinding(Ports.Binding.bindPort(port), new ExposedPort(port))); }); - withExposedPorts(port); - withCommand(buildContainerCommand(port, network, memorySize)); + this.withExposedPorts(port); + this.withCommand(buildContainerCommand(port, network, memorySize)); } private String buildContainerCommand(int port, Network network, int memorySize) { StringBuilder sb = new StringBuilder(); - sb.append("-m " + memorySize + " "); - sb.append("-p " + port); + sb.append("-m ").append(memorySize).append(" "); + sb.append("-p ").append(port); if (network != null) { sb.append(" -z zoo1:2181"); - withEnv("ARCUS_CACHE_PUBLIC_IP", "127.0.0.1"); + this.withEnv("ARCUS_CACHE_PUBLIC_IP", "127.0.0.1"); } return sb.toString(); } diff --git a/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTest.java b/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTest.java index 6f9932a..28c1b7f 100644 --- a/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTest.java +++ b/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTest.java @@ -36,6 +36,21 @@ void createArcusContainerSingle() throws ExecutionException, InterruptedExceptio assertThat(set.get()).isTrue(); } + @Test + void createArcusContainerSingle2() throws ExecutionException, InterruptedException { + //given + ArcusClientPool arcusClient = ArcusClient.createArcusClientPool(ARCUS_CLUSTER_CONTAINER.getHostPorts(), + "test", new ConnectionFactoryBuilder(), 2); + + //when + OperationFuture set = arcusClient.set("test2", 10, "testValue2"); + + //then + assertThat(ARCUS_CLUSTER_CONTAINER.isCreated()).isTrue(); + assertThat(ARCUS_CLUSTER_CONTAINER.isRunning()).isTrue(); + assertThat(set.get()).isTrue(); + } + @Test void createNotSharedClusterTest() throws ExecutionException, InterruptedException { //given diff --git a/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTestOther.java b/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTestOther.java deleted file mode 100644 index d5cffd4..0000000 --- a/src/test/java/com/jam2in/arcus/testcontainers/ArcusClusterContainerTestOther.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jam2in.arcus.testcontainers; - -import java.util.concurrent.ExecutionException; - -import net.spy.memcached.ArcusClient; -import net.spy.memcached.ArcusClientPool; -import net.spy.memcached.ConnectionFactoryBuilder; -import net.spy.memcached.internal.OperationFuture; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * This class is a test class for the ArcusClusterContainer class, extending the ArcusClusterTestBase class. - * Test with singleton ArcusContainer object. - */ -class ArcusClusterContainerTestOther extends ArcusClusterTestBase { - - @Test - void createArcusContainerSingle() throws ExecutionException, InterruptedException { - //given - ArcusClientPool arcusClient = ArcusClient.createArcusClientPool(ARCUS_CLUSTER_CONTAINER.getHostPorts(), - "test", new ConnectionFactoryBuilder(), 2); - - //when - OperationFuture set = arcusClient.set("test2", 10, "testValue2"); - - //then - assertThat(ARCUS_CLUSTER_CONTAINER.isRunning()).isTrue(); - assertThat(set.get()).isTrue(); - } -} diff --git a/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerPropsTest.java b/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerPropsTest.java index c53c60f..07775db 100644 --- a/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerPropsTest.java +++ b/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerPropsTest.java @@ -160,10 +160,10 @@ void createArcusContainerWithPropsTest() { .build(); //when - ArcusContainer arcusContainer = ArcusContainer.create(props); - - //then - assertThat(arcusContainer.getDockerImageName()) - .isEqualTo(ArcusContainer.DEFAULT_ARCUS_IMAGE_NAME.toString()); + try (ArcusContainer arcusContainer = ArcusContainer.create(props)) { + //then + assertThat(arcusContainer.getDockerImageName()) + .isEqualTo(ArcusContainer.DEFAULT_ARCUS_IMAGE_NAME.toString()); + } } } diff --git a/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerTest.java b/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerTest.java index e6a5fa8..e9015c5 100644 --- a/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerTest.java +++ b/src/test/java/com/jam2in/arcus/testcontainers/ArcusContainerTest.java @@ -2,8 +2,7 @@ import java.io.IOException; import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.concurrent.ExecutionException; import net.spy.memcached.ArcusClient; @@ -29,10 +28,8 @@ void createArcusContainerSingle() throws IOException, ExecutionException, Interr //given ArcusClient arcusClient = new ArcusClient( new DefaultConnectionFactory(), - new ArrayList<>(Arrays.asList( - new InetSocketAddress( - "127.0.0.1", - arcusContainer.getFirstMappedPort())))); + Collections.singletonList( + new InetSocketAddress("127.0.0.1", arcusContainer.getFirstMappedPort()))); //when Boolean b = arcusClient.set("test", 10, "singleTestValue").get();