From 3754c666045c1420d9bd118bc0f4ea8fee85a680 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Mon, 24 Aug 2020 23:12:45 +0200 Subject: [PATCH] Replace System Rules with System Lambda System Lambda is more specific. It only wraps the part of the code that produces the output. --- pom.xml | 6 +-- subclass-sandbox/pom.xml | 2 +- .../subclasssandbox/GroundDiveTest.java | 38 +++++++++--------- .../subclasssandbox/SkyLaunchTest.java | 39 +++++++++---------- 4 files changed, 40 insertions(+), 45 deletions(-) diff --git a/pom.xml b/pom.xml index 1275ce115..3b7ad3568 100644 --- a/pom.xml +++ b/pom.xml @@ -52,7 +52,7 @@ 2.3.1 2.3.2 1.3.2 - 1.19.0 + 1.1.0 2.0.0 3.5.0 @@ -338,8 +338,8 @@ com.github.stefanbirkner - system-rules - ${system-rules.version} + system-lambda + ${system-lambda.version} test diff --git a/subclass-sandbox/pom.xml b/subclass-sandbox/pom.xml index ac718291e..a912ada3f 100644 --- a/subclass-sandbox/pom.xml +++ b/subclass-sandbox/pom.xml @@ -42,7 +42,7 @@ com.github.stefanbirkner - system-rules + system-lambda org.junit.jupiter diff --git a/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/GroundDiveTest.java b/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/GroundDiveTest.java index 97e2ac67d..3b379946c 100644 --- a/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/GroundDiveTest.java +++ b/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/GroundDiveTest.java @@ -23,55 +23,48 @@ package com.iluwatar.subclasssandbox; +import com.github.stefanbirkner.systemlambda.Statement; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.SystemOutRule; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized; /** * GroundDive unit tests. */ public class GroundDiveTest { - @Rule - public SystemOutRule log = new SystemOutRule().enableLog(); - @Test - public void testMove() { - log.clearLog(); + public void testMove() throws Exception { var groundDive = new GroundDive(); groundDive.move(1.0, 1.0, 1.0); - var outputLog = getLogContent(log.getLog()); + var outputLog = getLogContent(() -> groundDive.move(1.0, 1.0, 1.0)); var expectedLog = "Move to ( 1.0, 1.0, 1.0 )"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testPlaySound() { - log.clearLog(); + public void testPlaySound() throws Exception { var groundDive = new GroundDive(); - groundDive.playSound("SOUND_NAME", 1); - var outputLog = getLogContent(log.getLog()); + var outputLog = getLogContent(() -> groundDive.playSound("SOUND_NAME", 1)); var expectedLog = "Play SOUND_NAME with volumn 1"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testSpawnParticles() { - log.clearLog(); + public void testSpawnParticles() throws Exception { var groundDive = new GroundDive(); - groundDive.spawnParticles("PARTICLE_TYPE", 100); - final var outputLog = getLogContent(log.getLog()); + final var outputLog = getLogContent( + () -> groundDive.spawnParticles("PARTICLE_TYPE", 100)); final var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testActivate() { - log.clearLog(); + public void testActivate() throws Exception { var groundDive = new GroundDive(); - groundDive.activate(); - var logs = log.getLog().split("\n"); + var logs = tapSystemOutNormalized(groundDive::activate) + .split("\n"); final var expectedSize = 3; final var log1 = logs[0].split("-")[1].trim() + " -" + logs[0].split("-")[2].trim(); final var expectedLog1 = "Move to ( 0.0, 0.0, -20.0 )"; @@ -85,6 +78,11 @@ public class GroundDiveTest { Assert.assertEquals(log3, expectedLog3); } + private String getLogContent(Statement statement) throws Exception { + var log = tapSystemOutNormalized(statement); + return getLogContent(log); + } + private String getLogContent(String log) { return log.split("-")[1].trim(); } diff --git a/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/SkyLaunchTest.java b/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/SkyLaunchTest.java index e192737f6..d285e6c7d 100644 --- a/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/SkyLaunchTest.java +++ b/subclass-sandbox/src/test/java/com/iluwatar/subclasssandbox/SkyLaunchTest.java @@ -23,55 +23,47 @@ package com.iluwatar.subclasssandbox; +import com.github.stefanbirkner.systemlambda.Statement; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.SystemOutRule; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized; /** * SkyLaunch unit tests. */ public class SkyLaunchTest { - @Rule - public SystemOutRule log = new SystemOutRule().enableLog(); - @Test - public void testMove() { - log.clearLog(); + public void testMove() throws Exception { var skyLaunch = new SkyLaunch(); - skyLaunch.move(1.0, 1.0, 1.0); - var outputLog = getLogContent(log.getLog()); + var outputLog = getLogContent(() -> skyLaunch.move(1.0, 1.0, 1.0)); var expectedLog = "Move to ( 1.0, 1.0, 1.0 )"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testPlaySound() { - log.clearLog(); + public void testPlaySound() throws Exception { var skyLaunch = new SkyLaunch(); - skyLaunch.playSound("SOUND_NAME", 1); - var outputLog = getLogContent(log.getLog()); + var outputLog = getLogContent(() -> skyLaunch.playSound("SOUND_NAME", 1)); var expectedLog = "Play SOUND_NAME with volumn 1"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testSpawnParticles() { - log.clearLog(); + public void testSpawnParticles() throws Exception { var skyLaunch = new SkyLaunch(); - skyLaunch.spawnParticles("PARTICLE_TYPE", 100); - var outputLog = getLogContent(log.getLog()); + var outputLog = getLogContent( + () -> skyLaunch.spawnParticles("PARTICLE_TYPE", 100)); var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE"; Assert.assertEquals(outputLog, expectedLog); } @Test - public void testActivate() { - log.clearLog(); + public void testActivate() throws Exception { var skyLaunch = new SkyLaunch(); - skyLaunch.activate(); - var logs = log.getLog().split("\n"); + var logs = tapSystemOutNormalized(skyLaunch::activate) + .split("\n"); final var expectedSize = 3; final var log1 = getLogContent(logs[0]); final var expectedLog1 = "Move to ( 0.0, 0.0, 20.0 )"; @@ -85,6 +77,11 @@ public class SkyLaunchTest { Assert.assertEquals(log3, expectedLog3); } + private String getLogContent(Statement statement) throws Exception { + var log = tapSystemOutNormalized(statement); + return getLogContent(log); + } + private String getLogContent(String log) { return log.split("-")[1].trim(); }