MPT-44 Migrate DiscardProtocolTest to JUNIT 5 JUNIT 5 helps testing build instability thanks to @RepeatedTest feature
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/16e5de17 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/16e5de17 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/16e5de17 Branch: refs/heads/master Commit: 16e5de179160ac258eea3fa0810b1402bd0b077f Parents: 334ee22 Author: Benoit Tellier <[email protected]> Authored: Mon Nov 26 13:21:20 2018 +0700 Committer: Benoit Tellier <[email protected]> Committed: Tue Nov 27 09:02:26 2018 +0700 ---------------------------------------------------------------------- mpt/core/pom.xml | 17 +++- .../apache/james/mpt/DiscardProtocolTest.java | 102 +++++++++++++++++++ .../apache/james/mpt/TestDiscardProtocol.java | 102 ------------------- 3 files changed, 114 insertions(+), 107 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/16e5de17/mpt/core/pom.xml ---------------------------------------------------------------------- diff --git a/mpt/core/pom.xml b/mpt/core/pom.xml index 9f6872a..a9d0ff0 100644 --- a/mpt/core/pom.xml +++ b/mpt/core/pom.xml @@ -44,11 +44,6 @@ <artifactId>commons-io</artifactId> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>compile</scope> - </dependency> - <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> @@ -62,6 +57,18 @@ <artifactId>awaitility</artifactId> </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/16e5de17/mpt/core/src/test/java/org/apache/james/mpt/DiscardProtocolTest.java ---------------------------------------------------------------------- diff --git a/mpt/core/src/test/java/org/apache/james/mpt/DiscardProtocolTest.java b/mpt/core/src/test/java/org/apache/james/mpt/DiscardProtocolTest.java new file mode 100644 index 0000000..02463cd --- /dev/null +++ b/mpt/core/src/test/java/org/apache/james/mpt/DiscardProtocolTest.java @@ -0,0 +1,102 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mpt; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.Socket; + +import javax.net.SocketFactory; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class DiscardProtocolTest { + + private final class InputLater implements Runnable { + private Exception e; + + @Override + public void run() { + try { + Thread.sleep(1000); + input(); + } catch (Exception e) { + this.e = e; + } + } + + void assertExecutedSuccessfully() throws Exception { + if (e != null) { + e.printStackTrace(); + throw e; + } + } + } + + private static final String INPUT = "One, two, three - Testing"; + + private DiscardProtocol protocol; + private Socket socket; + + private DiscardProtocol.Record record; + + @BeforeEach + void setUp() throws Exception { + protocol = new DiscardProtocol(); + protocol.start(); + socket = SocketFactory.getDefault().createSocket("127.0.0.1", protocol.getPort().getValue()); + record = protocol.recordNext(); + } + + @AfterEach + void tearDown() { + protocol.stop(); + } + + @Test + void testRecord() throws Exception { + assertThat(socket.isConnected()).isTrue(); + input(); + String output = record.complete(); + assertThat(output).isEqualTo(INPUT); + } + + private void input() throws IOException { + Writer out = new OutputStreamWriter(socket.getOutputStream()); + out.append(INPUT); + out.close(); + socket.close(); + } + + @Test + void testComplete() throws Exception { + InputLater inputLater = new InputLater(); + Thread thread = new Thread(inputLater); + thread.start(); + String output = record.complete(); + assertThat(output).isEqualTo(INPUT); + inputLater.assertExecutedSuccessfully(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/16e5de17/mpt/core/src/test/java/org/apache/james/mpt/TestDiscardProtocol.java ---------------------------------------------------------------------- diff --git a/mpt/core/src/test/java/org/apache/james/mpt/TestDiscardProtocol.java b/mpt/core/src/test/java/org/apache/james/mpt/TestDiscardProtocol.java deleted file mode 100644 index 7ff9dd4..0000000 --- a/mpt/core/src/test/java/org/apache/james/mpt/TestDiscardProtocol.java +++ /dev/null @@ -1,102 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mpt; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.net.Socket; - -import javax.net.SocketFactory; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class TestDiscardProtocol { - - private final class InputLater implements Runnable { - private Exception e; - - @Override - public void run() { - try { - Thread.sleep(1000); - input(); - } catch (Exception e) { - this.e = e; - } - } - - public void assertExecutedSuccessfully() throws Exception { - if (e != null) { - e.printStackTrace(); - throw e; - } - } - } - - private static final String INPUT = "One, two, three - Testing"; - - private DiscardProtocol protocol; - private Socket socket; - - private DiscardProtocol.Record record; - - @Before - public void setUp() throws Exception { - protocol = new DiscardProtocol(); - protocol.start(); - socket = SocketFactory.getDefault().createSocket("127.0.0.1", protocol.getPort().getValue()); - record = protocol.recordNext(); - } - - @After - public void tearDown() throws Exception { - protocol.stop(); - } - - @Test - public void testRecord() throws Exception { - assertThat(socket.isConnected()).isTrue(); - input(); - String output = record.complete(); - assertThat(output).isEqualTo(INPUT); - } - - private void input() throws IOException { - Writer out = new OutputStreamWriter(socket.getOutputStream()); - out.append(INPUT); - out.close(); - socket.close(); - } - - @Test - public void testComplete() throws Exception { - InputLater inputLater = new InputLater(); - Thread thread = new Thread(inputLater); - thread.start(); - String output = record.complete(); - assertThat(output).isEqualTo(INPUT); - inputLater.assertExecutedSuccessfully(); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
