diff --git a/pom.xml b/pom.xml index 8090f488..f138de6a 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. com.jcabi jcabi - 1.24 + 1.26.0 jcabi-log 1.0-SNAPSHOT @@ -79,13 +79,24 @@ OF THE POSSIBILITY OF SUCH DAMAGE. --> org.slf4j slf4j-api - 1.7.21 + 1.8.0-beta1 + + + org.junit.jupiter + junit-jupiter-params + 5.6.2 + test org.apache.commons commons-lang3 test + + org.apache.commons + commons-text + test + @@ -104,21 +115,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE. true - - - - maven-compiler-plugin - - 1.8 - 1.8 - - - - maven-invoker-plugin - 2.0.0 - - - diff --git a/src/it/it-decoloring/pom.xml b/src/it/it-decoloring/pom.xml index d26a2bea..9fb60287 100644 --- a/src/it/it-decoloring/pom.xml +++ b/src/it/it-decoloring/pom.xml @@ -33,7 +33,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. com.jcabi jcabi - 1.20.1 + 1.26.0 com.jcabi jcabi-log-test @@ -46,7 +46,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. org.apache.commons - commons-lang3 + commons-text test diff --git a/src/it/it-decoloring/src/test/java/com/jcabi/log/MulticolorLayoutIntegrationTest.java b/src/it/it-decoloring/src/test/java/com/jcabi/log/MulticolorLayoutIntegrationTest.java index ca412890..06172b00 100644 --- a/src/it/it-decoloring/src/test/java/com/jcabi/log/MulticolorLayoutIntegrationTest.java +++ b/src/it/it-decoloring/src/test/java/com/jcabi/log/MulticolorLayoutIntegrationTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2012-2017, jcabi.com * All rights reserved. * @@ -29,12 +29,12 @@ */ package com.jcabi.log; -import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.text.StringEscapeUtils; import org.apache.log4j.Level; import org.apache.log4j.spi.LoggingEvent; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; /** diff --git a/src/main/java/com/jcabi/log/MsDecor.java b/src/main/java/com/jcabi/log/MsDecor.java index 78f3921f..97199562 100644 --- a/src/main/java/com/jcabi/log/MsDecor.java +++ b/src/main/java/com/jcabi/log/MsDecor.java @@ -112,23 +112,23 @@ public void formatTo(final Formatter formatter, final int flags, private String toText(final int precision) { final double number; final String title; - if (this.millis < 1000L) { + if (this.millis < 1000.0) { number = this.millis; title = "ms"; - } else if (this.millis < 1000L * 60) { - number = this.millis / 1000L; + } else if (this.millis < (double) (1000L * 60L)) { + number = this.millis / 1000.0; title = "s"; - } else if (this.millis < 1000L * 60 * 60) { - number = this.millis / (1000L * 60); + } else if (this.millis < (double) (1000L * 60L * 60L)) { + number = this.millis / (double) (1000L * 60L); title = "min"; - } else if (this.millis < 1000L * 60 * 60 * 24) { - number = this.millis / (1000L * 60 * 60); + } else if (this.millis < (double) (1000L * 60L * 60L * 24L)) { + number = this.millis / (double) (1000L * 60L * 60L); title = "hr"; - } else if (this.millis < 1000L * 60 * 60 * 24 * 30) { - number = this.millis / (1000L * 60 * 60 * 24); + } else if (this.millis < (double) (1000L * 60L * 60L * 24L * 30L)) { + number = this.millis / (double) (1000L * 60L * 60L * 24L); title = "days"; } else { - number = this.millis / (1000L * 60 * 60 * 24 * 30); + number = this.millis / (double) (1000L * 60L * 60L * 24L * 30L); title = "mon"; } final String format; diff --git a/src/test/java/com/jcabi/log/ConversionPatternTest.java b/src/test/java/com/jcabi/log/ConversionPatternTest.java index 9fb5ea73..f7ddd491 100644 --- a/src/test/java/com/jcabi/log/ConversionPatternTest.java +++ b/src/test/java/com/jcabi/log/ConversionPatternTest.java @@ -31,7 +31,7 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test case for {@link ConversionPattern}. @@ -51,9 +51,6 @@ public final class ConversionPatternTest { */ private static final Colors COLORS = new Colors(); - /** - * Test some edge cases: these shouldn't result in replacement. - */ @Test public void testGenerateNoReplacement() { MatcherAssert.assertThat( @@ -78,10 +75,6 @@ public void testGenerateNoReplacement() { ); } - /** - * Test the empty edge case: should be replaced with the wrapped empty - * string. - */ @Test public void testGenerateEmpty() { MatcherAssert.assertThat( @@ -90,9 +83,6 @@ public void testGenerateEmpty() { ); } - /** - * Normal test cases. - */ @Test public void testGenerateSimple() { MatcherAssert.assertThat( @@ -111,27 +101,32 @@ public void testGenerateSimple() { ); } - /** - * Test cases where the body includes curly braces. The correct (balanced) - * curly brace pair should be replaced. - */ @Test public void testGenerateCurlyBraces() { MatcherAssert.assertThat( - convert("%color{%c{1}}"), - Matchers.equalTo(colorWrap("%c{1}")) + ConversionPatternTest.convert("%color{%c{1}}"), + Matchers.equalTo(ConversionPatternTest.colorWrap("%c{1}")) ); MatcherAssert.assertThat( - convert("%color{%c{1}}foo"), - Matchers.equalTo(String.format("%sfoo", colorWrap("%c{1}"))) + ConversionPatternTest.convert("%color{%c{1}}foo"), + Matchers.equalTo( + String.format("%sfoo", ConversionPatternTest.colorWrap("%c{1}")) + ) ); MatcherAssert.assertThat( - convert("%color{%c1}}foo"), - Matchers.equalTo(String.format("%s}foo", colorWrap("%c1"))) + ConversionPatternTest.convert("%color{%c1}}foo"), + Matchers.equalTo( + String.format("%s}foo", ConversionPatternTest.colorWrap("%c1")) + ) ); MatcherAssert.assertThat( - convert("%color{%c{{{1}{2}}}}foo"), - Matchers.equalTo(String.format("%sfoo", colorWrap("%c{{{1}{2}}}"))) + ConversionPatternTest.convert("%color{%c{{{1}{2}}}}foo"), + Matchers.equalTo( + String.format( + "%sfoo", + ConversionPatternTest.colorWrap("%c{{{1}{2}}}") + ) + ) ); } diff --git a/src/test/java/com/jcabi/log/DecorsManagerTest.java b/src/test/java/com/jcabi/log/DecorsManagerTest.java index 5d9327b3..192da827 100644 --- a/src/test/java/com/jcabi/log/DecorsManagerTest.java +++ b/src/test/java/com/jcabi/log/DecorsManagerTest.java @@ -31,7 +31,8 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test case for {@link DecorsManager}. @@ -42,10 +43,6 @@ */ public final class DecorsManagerTest { - /** - * DecorsManager can return one of built-in decors. - * @throws Exception If some problem - */ @Test public void hasBuiltInDecors() throws Exception { MatcherAssert.assertThat( @@ -54,13 +51,15 @@ public void hasBuiltInDecors() throws Exception { ); } - /** - * DecorsManager can throw exception if a decor is missed. - * @throws Exception If some problem - */ - @Test(expected = DecorException.class) - public void throwsExceptionForAbsentDecor() throws Exception { - DecorsManager.decor("non-existing-formatter", null); + @Test + public void throwsExceptionForAbsentDecor() { + Assertions.assertThrows( + DecorException.class, + () -> { + DecorsManager.decor("non-existing-formatter", null); + } + ); + } } diff --git a/src/test/java/com/jcabi/log/DomDecorTest.java b/src/test/java/com/jcabi/log/DomDecorTest.java index e92deccd..ad98c47c 100644 --- a/src/test/java/com/jcabi/log/DomDecorTest.java +++ b/src/test/java/com/jcabi/log/DomDecorTest.java @@ -33,7 +33,7 @@ import java.util.Formatter; import javax.xml.parsers.DocumentBuilderFactory; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.mockito.hamcrest.MockitoHamcrest; import org.w3c.dom.Document; @@ -46,10 +46,6 @@ */ public final class DomDecorTest { - /** - * DocumentDecor can transform Document to text. - * @throws Exception If some problem - */ @Test public void convertsDocumentToText() throws Exception { final Document doc = DocumentBuilderFactory.newInstance() @@ -64,10 +60,6 @@ public void convertsDocumentToText() throws Exception { ); } - /** - * DocumentDecor can handle NULL properly. - * @throws Exception If some problem - */ @Test public void convertsNullToText() throws Exception { final Formattable decor = new DomDecor(null); diff --git a/src/test/java/com/jcabi/log/ExceptionDecorTest.java b/src/test/java/com/jcabi/log/ExceptionDecorTest.java index f3b40e12..22593820 100644 --- a/src/test/java/com/jcabi/log/ExceptionDecorTest.java +++ b/src/test/java/com/jcabi/log/ExceptionDecorTest.java @@ -33,7 +33,7 @@ import java.util.Formattable; import java.util.Formatter; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.mockito.hamcrest.MockitoHamcrest; @@ -45,10 +45,6 @@ */ public final class ExceptionDecorTest { - /** - * ExceptionDecor can transform exception to text. - * @throws Exception If some problem - */ @Test public void convertsExceptionToText() throws Exception { final Formattable decor = new ExceptionDecor(new IOException("ouch!")); @@ -69,10 +65,6 @@ public void convertsExceptionToText() throws Exception { ); } - /** - * ExceptionDecor can handle NULL properly. - * @throws Exception If some problem - */ @Test public void convertsNullToText() throws Exception { final Formattable decor = new ExceptionDecor(null); diff --git a/src/test/java/com/jcabi/log/LineNumberTest.java b/src/test/java/com/jcabi/log/LineNumberTest.java index 2b8bcb96..575e8a01 100644 --- a/src/test/java/com/jcabi/log/LineNumberTest.java +++ b/src/test/java/com/jcabi/log/LineNumberTest.java @@ -38,7 +38,7 @@ import org.apache.log4j.WriterAppender; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test case for %L pattern. @@ -55,10 +55,6 @@ public final class LineNumberTest { */ private static final String CONV_PATTERN = "%c:%L"; - /** - * PaternLayout can use %L properly. - * @throws Exception If something goes wrong - */ @Test public void testLineNumber() throws Exception { final PatternLayout layout = new PatternLayout(); @@ -75,7 +71,7 @@ public void testLineNumber() throws Exception { MatcherAssert.assertThat( writer.toString(), Matchers.containsString( - "com.jcabi.log.LineNumberTest:73" + "com.jcabi.log.LineNumberTest:247" ) ); } finally { diff --git a/src/test/java/com/jcabi/log/ListDecorTest.java b/src/test/java/com/jcabi/log/ListDecorTest.java index 22b76d0f..1e5f64e5 100644 --- a/src/test/java/com/jcabi/log/ListDecorTest.java +++ b/src/test/java/com/jcabi/log/ListDecorTest.java @@ -32,10 +32,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link ListDecor}. @@ -43,31 +44,35 @@ * @version $Id$ * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class ListDecorTest extends AbstractDecorTest { +public final class ListDecorTest { - /** - * Public ctor. - * @param list The list to test - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public ListDecorTest(final Object list, final String text, - final int flags, final int width, final int precision) { - super(list, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new ListDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new ListDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle MultipleStringLiterals (8 lines) @@ -83,10 +88,4 @@ public static Collection params() { } ); } - - @Override - public Formattable decor() throws Exception { - return new ListDecor(this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/AbstractDecorTest.java b/src/test/java/com/jcabi/log/Logged.java similarity index 55% rename from src/test/java/com/jcabi/log/AbstractDecorTest.java rename to src/test/java/com/jcabi/log/Logged.java index 8956c3b0..d9060c27 100644 --- a/src/test/java/com/jcabi/log/AbstractDecorTest.java +++ b/src/test/java/com/jcabi/log/Logged.java @@ -31,31 +31,18 @@ import java.util.Formattable; import java.util.FormattableFlags; -import java.util.Formatter; -import java.util.Locale; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.mockito.Mockito; /** - * Abstract test case for all decors in the package. - * @author Marina Kosenko (marina.kosenko@gmail.com) - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * Logs decor. + * * @since 0.1 */ -public abstract class AbstractDecorTest { +public final class Logged { /** - * Object/system under test. + * The decor. */ - private final transient Object sut; - - /** - * The text to expect as an output. - */ - private final transient String text; + private final transient Formattable decor; /** * Formatting flas. @@ -74,54 +61,33 @@ public abstract class AbstractDecorTest { /** * Public ctor. - * @param obj The object - * @param txt Expected text + * @param dcr Decor * @param flgs Flags * @param wdt Width * @param prcs Precission * @checkstyle ParameterNumber (3 lines) */ - @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") - public AbstractDecorTest(final Object obj, final String txt, + public Logged(final Formattable dcr, final int flgs, final int wdt, final int prcs) { - this.sut = obj; - this.text = txt; + this.decor = dcr; this.flags = flgs; this.width = wdt; this.precision = prcs; - Locale.setDefault(Locale.US); } - /** - * AbstractDecor can convert object to text. - * @throws Exception If some problem inside - */ - @Test - public final void convertsDifferentFormats() throws Exception { - final Formattable decor = this.decor(); - final Appendable dest = Mockito.mock(Appendable.class); - final Formatter fmt = new Formatter(dest); - decor.formatTo(fmt, this.flags, this.width, this.precision); - Mockito.verify(dest).append(this.text); - } - - /** - * AbstractDecor can convert object to text, via Logger. - * @throws Exception If some problem inside - */ - @Test - public final void convertsDifferentFormatsViaLogger() throws Exception { - final StringBuilder format = new StringBuilder(); + @Override + public String toString() { + final StringBuilder format = new StringBuilder(0); format.append('%'); if ((this.flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags .LEFT_JUSTIFY) { format.append('-'); } if (this.width > 0) { - format.append(Integer.toString(this.width)); + format.append(this.width); } if (this.precision > 0) { - format.append('.').append(Integer.toString(this.precision)); + format.append('.').append(this.precision); } if ((this.flags & FormattableFlags.UPPERCASE) == FormattableFlags .UPPERCASE) { @@ -129,25 +95,7 @@ public final void convertsDifferentFormatsViaLogger() throws Exception { } else { format.append('s'); } - MatcherAssert.assertThat( - Logger.format(format.toString(), this.decor()), - Matchers.equalTo(this.text) - ); - } - - /** - * Get decor with the object. - * @return The decor to test - * @throws Exception If some problem - */ - protected abstract Formattable decor() throws Exception; - - /** - * Get object under test. - * @return The object - */ - protected final Object object() { - return this.sut; + return Logger.format(format.toString(), this.decor); } } diff --git a/src/test/java/com/jcabi/log/LoggerTest.java b/src/test/java/com/jcabi/log/LoggerTest.java index c5afe305..e4bfeb29 100644 --- a/src/test/java/com/jcabi/log/LoggerTest.java +++ b/src/test/java/com/jcabi/log/LoggerTest.java @@ -37,7 +37,8 @@ import org.apache.log4j.LogManager; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test case for {@link Logger}. @@ -47,46 +48,29 @@ */ public final class LoggerTest { - /** - * Logger can detect logging class name correctly. - * @throws Exception If something goes wrong - */ @Test public void detectsLoggerNameCorrectly() throws Exception { // not implemented yet } - /** - * Logger can detect logging class with a static param. - * @throws Exception If something goes wrong - */ @Test public void detectsNameOfStaticSource() throws Exception { // not implemented yet } - /** - * Logger can set logging level correctly. - * @throws Exception If something goes wrong - */ @Test public void setsLoggingLevel() throws Exception { // not implemented yet } - /** - * Logger can not format arrays since they are interpreted as varags. - * @throws Exception If something goes wrong - */ - @Test(expected = IllegalArgumentException.class) - public void doesntFormatArraysSinceTheyAreVarArgs() throws Exception { - Logger.format("array: %[list]s", new Object[] {"hi", 1}); + @Test + public void doesntFormatArraysSinceTheyAreVarArgs() { + Assertions.assertThrows( + IllegalArgumentException.class, + () -> Logger.format("array: %[list]s", new Object[] {"hi", 1}) + ); } - /** - * Logger formats arrays as if they were separate arguments as they are - * interpreted as varargs. - */ @Test public void interpretsArraysAsVarArgs() { MatcherAssert.assertThat( @@ -95,10 +79,6 @@ public void interpretsArraysAsVarArgs() { ); } - /** - * Logger can provide an output stream. - * @throws Exception If something goes wrong - */ @Test public void providesOutputStream() throws Exception { final OutputStream stream = Logger.stream(Level.INFO, this); @@ -111,28 +91,22 @@ public void providesOutputStream() throws Exception { writer.close(); } - /** - * Logger throws an exception when there are less parameters than there - * are format args. - */ - @Test(expected = ArrayIndexOutOfBoundsException.class) + @Test public void throwsWhenParamsLessThanFormatArgs() { - Logger.format("String %s Char %c Number %d", "howdy", 'x'); + Assertions.assertThrows( + ArrayIndexOutOfBoundsException.class, + () -> Logger.format("String %s Char %c Number %d", "howdy", 'x') + ); } - /** - * Logger throws an exception when there are more parameters than there - * are format args. - */ - @Test(expected = IllegalArgumentException.class) + @Test public void throwsWhenParamsMoreThanFormatArgs() { - Logger.format("String %s Number %d Char %c", "hey", 1, 'x', 2); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> Logger.format("String %s Number %d Char %c", "hey", 1, 'x', 2) + ); } - /** - * Logger can correctly check the current logging level. - * @throws Exception If fails - */ @Test public void checksLogLevel() throws Exception { LogManager.getRootLogger().setLevel(org.apache.log4j.Level.INFO); @@ -147,17 +121,11 @@ public void checksLogLevel() throws Exception { ); } - /** - * Logger can use String as a logger name. - */ @Test public void usesStringAsLoggerName() { Logger.info("com.jcabi.log...why.not", "hello, %s!", "world!"); } - /** - * Finds args by position numbers. - */ @Test public void findsArgsByPositions() { final String first = "xyz"; diff --git a/src/test/java/com/jcabi/log/MsDecorTest.java b/src/test/java/com/jcabi/log/MsDecorTest.java index 60550da9..28e1f1d9 100644 --- a/src/test/java/com/jcabi/log/MsDecorTest.java +++ b/src/test/java/com/jcabi/log/MsDecorTest.java @@ -31,48 +31,60 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; import java.util.FormattableFlags; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link MsDecor}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class MsDecorTest extends AbstractDecorTest { +public final class MsDecorTest { - /** - * Public ctor. - * @param nano The amount of nanoseconds - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public MsDecorTest(final Long nano, final String text, + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final long value, final String text, + final int flags, final int width, final int precision) { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new MsDecor(value), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final long value, final String text, final int flags, final int width, final int precision) { - super(nano, text, flags, width, precision); + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new MsDecor(value), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @Test + public void testPrintsNullRight() { + MatcherAssert.assertThat( + new Logged(new MsDecor(null), 0, 0, 0), + Matchers.hasToString("NULL") + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle LineLength (20 lines) // @checkstyle MagicNumber (20 lines) - {null, "NULL", 0, 0, 0}, {13L, "13ms", 0, 0, -1}, {13L, "13.0ms", 0, 0, 1}, {1024L, "1s", 0, 0, 0}, @@ -84,10 +96,4 @@ public static Collection params() { } ); } - - @Override - public Formattable decor() { - return new MsDecor((Long) this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/MulticolorLayoutTest.java b/src/test/java/com/jcabi/log/MulticolorLayoutTest.java index 6fc3fd72..ad595a13 100644 --- a/src/test/java/com/jcabi/log/MulticolorLayoutTest.java +++ b/src/test/java/com/jcabi/log/MulticolorLayoutTest.java @@ -29,18 +29,18 @@ */ package com.jcabi.log; -import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.text.StringEscapeUtils; import org.apache.log4j.Level; import org.apache.log4j.spi.LoggingEvent; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; /** * Test case for {@link MulticolorLayout}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ public final class MulticolorLayoutTest { @@ -50,12 +50,8 @@ public final class MulticolorLayoutTest { */ private static final String CONV_PATTERN = "[%color{%p}] %color{%m}"; - /** - * MulticolorLayout can transform event to text. - * @throws Exception If something goes wrong - */ @Test - public void transformsLoggingEventToText() throws Exception { + public void transformsLoggingEventToText() { final MulticolorLayout layout = new MulticolorLayout(); layout.setConversionPattern(MulticolorLayoutTest.CONV_PATTERN); final LoggingEvent event = Mockito.mock(LoggingEvent.class); @@ -69,12 +65,8 @@ public void transformsLoggingEventToText() throws Exception { ); } - /** - * MulticolorLayout can transform event to text. - * @throws Exception If something goes wrong - */ @Test - public void overwriteDefaultColor() throws Exception { + public void overwriteDefaultColor() { final MulticolorLayout layout = new MulticolorLayout(); layout.setConversionPattern(MulticolorLayoutTest.CONV_PATTERN); layout.setLevels("INFO:2;10"); @@ -89,12 +81,8 @@ public void overwriteDefaultColor() throws Exception { ); } - /** - * MulticolorLayout can render custom color. - * @throws Exception If something goes wrong - */ @Test - public void rendersCustomConstantColor() throws Exception { + public void rendersCustomConstantColor() { final MulticolorLayout layout = new MulticolorLayout(); layout.setConversionPattern("%color-red{%p} %m"); final LoggingEvent event = Mockito.mock(LoggingEvent.class); @@ -106,12 +94,8 @@ public void rendersCustomConstantColor() throws Exception { ); } - /** - * MulticolorLayout can render custom color. - * @throws Exception If something goes wrong - */ @Test - public void overwriteCustomConstantColor() throws Exception { + public void overwriteCustomConstantColor() { final MulticolorLayout layout = new MulticolorLayout(); layout.setConversionPattern("%color-white{%p} %m"); layout.setColors("white:10"); @@ -124,12 +108,8 @@ public void overwriteCustomConstantColor() throws Exception { ); } - /** - * MulticolorLayout can render any ANSI color. - * @throws Exception If something goes wrong - */ @Test - public void rendersAnsiConstantColor() throws Exception { + public void rendersAnsiConstantColor() { final MulticolorLayout layout = new MulticolorLayout(); layout.setConversionPattern("%color-0;0;31{%p} %m"); final LoggingEvent event = Mockito.mock(LoggingEvent.class); @@ -141,18 +121,19 @@ public void rendersAnsiConstantColor() throws Exception { ); } - /** - * MulticolorLayout can throw if color name is not valid. - * @throws Exception If something goes wrong - */ - @Test(expected = IllegalArgumentException.class) - public void throwsOnIllegalColorName() throws Exception { - final MulticolorLayout layout = new MulticolorLayout(); - layout.setConversionPattern("%color-oops{%p} %m"); - final LoggingEvent event = Mockito.mock(LoggingEvent.class); - Mockito.doReturn(Level.DEBUG).when(event).getLevel(); - Mockito.doReturn("text").when(event).getRenderedMessage(); - layout.format(event); + @Test + public void throwsOnIllegalColorName() { + Assertions.assertThrows( + IllegalArgumentException.class, + () -> { + final MulticolorLayout layout = new MulticolorLayout(); + layout.setConversionPattern("%color-oops{%p} %m"); + final LoggingEvent event = Mockito.mock(LoggingEvent.class); + Mockito.doReturn(Level.DEBUG).when(event).getLevel(); + Mockito.doReturn("text").when(event).getRenderedMessage(); + layout.format(event); + } + ); } } diff --git a/src/test/java/com/jcabi/log/NanoDecorTest.java b/src/test/java/com/jcabi/log/NanoDecorTest.java index a80687fc..1fa10339 100644 --- a/src/test/java/com/jcabi/log/NanoDecorTest.java +++ b/src/test/java/com/jcabi/log/NanoDecorTest.java @@ -31,11 +31,13 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; import java.util.FormattableFlags; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link NanoDecor}. @@ -44,36 +46,47 @@ * @version $Id$ * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class NanoDecorTest extends AbstractDecorTest { +public final class NanoDecorTest { - /** - * Public ctor. - * @param nano The amount of nanoseconds - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public NanoDecorTest(final Long nano, final String text, - final int flags, final int width, final int precision) { - super(nano, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final long nano, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new NanoDecor(nano), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final long nano, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new NanoDecor(nano), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @Test + public void testPrintsNullRight() { + MatcherAssert.assertThat( + new Logged(new NanoDecor(null), 0, 0, 0), + Matchers.hasToString("NULL") + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle LineLength (20 lines) // @checkstyle MagicNumber (20 lines) - {null, "NULL", 0, 0, 0}, {13L, "13ns", 0, 0, -1}, {13L, "13.0ns", 0, 0, 1}, {25L, "25.00ns", 0, 0, 2}, @@ -96,9 +109,4 @@ public static Collection params() { ); } - @Override - public Formattable decor() { - return new NanoDecor((Long) this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/ObjectDecorTest.java b/src/test/java/com/jcabi/log/ObjectDecorProbe.java similarity index 74% rename from src/test/java/com/jcabi/log/ObjectDecorTest.java rename to src/test/java/com/jcabi/log/ObjectDecorProbe.java index 9a4fbae1..a61f9041 100644 --- a/src/test/java/com/jcabi/log/ObjectDecorTest.java +++ b/src/test/java/com/jcabi/log/ObjectDecorProbe.java @@ -31,43 +31,46 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link ObjectDecor}. - * @author Marina Kosenko (marina.kosenko@gmail.com) - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class ObjectDecorTest extends AbstractDecorTest { +public final class ObjectDecorProbe { - /** - * Public ctor. - * @param object The object - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public ObjectDecorTest(final Object object, final String text, - final int flags, final int width, final int precision) { - super(object, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final Object obj, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new ObjectDecor(obj), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final Object obj, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new ObjectDecor(obj), flags, width, precision), + Matchers.hasToString(text) + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { {null, "NULL", 0, 0, 0}, @@ -87,11 +90,6 @@ public static Collection params() { ); } - @Override - public Formattable decor() { - return new ObjectDecor(this.object()); - } - /** * Test class for displaying object contents. */ diff --git a/src/test/java/com/jcabi/log/ParseableInformationTest.java b/src/test/java/com/jcabi/log/ParseableInformationTest.java index 9e1538e2..0ba4aad6 100644 --- a/src/test/java/com/jcabi/log/ParseableInformationTest.java +++ b/src/test/java/com/jcabi/log/ParseableInformationTest.java @@ -33,8 +33,8 @@ import org.apache.commons.lang3.StringUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * ParseableInformation test case. @@ -66,7 +66,7 @@ public final void throwsAnExceptionWhenParsingSomethingWrong() { final String white = "white"; try { new ParseableInformation(white).information(); - Assert.fail("Should never enter this assert!"); + Assertions.fail("Should never enter this assert!"); } catch (final IllegalStateException ex) { MatcherAssert.assertThat( ex.getMessage(), Matchers.equalTo( diff --git a/src/test/java/com/jcabi/log/ParseableLevelInformationTest.java b/src/test/java/com/jcabi/log/ParseableLevelInformationTest.java index 0af1da01..1b05fc8b 100644 --- a/src/test/java/com/jcabi/log/ParseableLevelInformationTest.java +++ b/src/test/java/com/jcabi/log/ParseableLevelInformationTest.java @@ -33,8 +33,8 @@ import org.apache.commons.lang3.StringUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * ParseableLevelInformation test case. @@ -67,7 +67,7 @@ public final void throwsAnExceptionWhenParsingIncorrectInformation() { final String wrong = "INFO;10,WARN;32"; try { new ParseableLevelInformation(wrong).information(); - Assert.fail(); + Assertions.fail("Something was wrong"); } catch (final IllegalStateException ex) { MatcherAssert.assertThat( ex.getMessage(), Matchers.equalTo( @@ -92,7 +92,7 @@ public final void throwsAnExceptionWhenParsingWrongLevelType() { new ParseableLevelInformation( "INFO:2;10,EXTREME:2;32" ).information(); - Assert.fail(); + Assertions.fail(""); } catch (final IllegalStateException ex) { MatcherAssert.assertThat( ex.getMessage(), diff --git a/src/test/java/com/jcabi/log/PreFormatterTest.java b/src/test/java/com/jcabi/log/PreFormatterTest.java index 46d9de09..f855f638 100644 --- a/src/test/java/com/jcabi/log/PreFormatterTest.java +++ b/src/test/java/com/jcabi/log/PreFormatterTest.java @@ -31,7 +31,7 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test case for {@link PreFormatter}. diff --git a/src/test/java/com/jcabi/log/Printed.java b/src/test/java/com/jcabi/log/Printed.java new file mode 100644 index 00000000..ce0768cc --- /dev/null +++ b/src/test/java/com/jcabi/log/Printed.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2012-2017, jcabi.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the jcabi.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jcabi.log; + +import java.io.ByteArrayOutputStream; +import java.util.Formattable; +import java.util.Formatter; + +/** + * Prints decor. + * + * @since 0.1 + */ +public final class Printed { + + /** + * The decor. + */ + private final transient Formattable decor; + + /** + * Formatting flas. + */ + private final transient int flags; + + /** + * Formatting width. + */ + private final transient int width; + + /** + * Formatting precision. + */ + private final transient int precision; + + /** + * Public ctor. + * @param dcr Decor + * @param flgs Flags + * @param wdt Width + * @param prcs Precission + * @checkstyle ParameterNumber (3 lines) + */ + public Printed(final Formattable dcr, + final int flgs, final int wdt, final int prcs) { + this.decor = dcr; + this.flags = flgs; + this.width = wdt; + this.precision = prcs; + } + + @Override + public String toString() { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final Formatter fmt = new Formatter(baos); + this.decor.formatTo(fmt, this.flags, this.width, this.precision); + fmt.flush(); + return baos.toString(); + } + +} diff --git a/src/test/java/com/jcabi/log/SecretDecorTest.java b/src/test/java/com/jcabi/log/SecretDecorTest.java index 04a84c9f..594861b9 100644 --- a/src/test/java/com/jcabi/log/SecretDecorTest.java +++ b/src/test/java/com/jcabi/log/SecretDecorTest.java @@ -31,44 +31,47 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; import java.util.FormattableFlags; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link SecretDecor}. - * @author Marina Kosenko (marina.kosenko@gmail.com) - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class SecretDecorTest extends AbstractDecorTest { +public final class SecretDecorTest { - /** - * Public ctor. - * @param secret The secret - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public SecretDecorTest(final String secret, final String text, - final int flags, final int width, final int precision) { - super(secret, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new SecretDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new SecretDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle MagicNumber (4 lines) @@ -79,10 +82,4 @@ public static Collection params() { } ); } - - @Override - public Formattable decor() { - return new SecretDecor(this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/SizeDecorTest.java b/src/test/java/com/jcabi/log/SizeDecorTest.java index cec2d4dd..7633ae19 100644 --- a/src/test/java/com/jcabi/log/SizeDecorTest.java +++ b/src/test/java/com/jcabi/log/SizeDecorTest.java @@ -31,48 +31,59 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; import java.util.FormattableFlags; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link SizeDecor}. - * @author Marina Kosenko (marina.kosenko@gmail.com) - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class SizeDecorTest extends AbstractDecorTest { +public final class SizeDecorTest { - /** - * Public ctor. - * @param size The size - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public SizeDecorTest(final Long size, final String text, - final int flags, final int width, final int precision) { - super(size, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final long size, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new SizeDecor(size), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final long size, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new SizeDecor(size), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @Test + public void testPrintsNullRight() { + MatcherAssert.assertThat( + new Logged(new SizeDecor(null), 0, 0, 0), + Matchers.hasToString("NULL") + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle MagicNumber (14 lines) - {null, "NULL", 0, 0, 0}, {1L, "1b", 0, 0, 0}, {123L, " 123b", 0, 6, 0}, {1024L, "1.000Kb", 0, 0, 3}, @@ -89,9 +100,4 @@ public static Collection params() { ); } - @Override - public Formattable decor() { - return new SizeDecor((Long) this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/SupplierLoggerTest.java b/src/test/java/com/jcabi/log/SupplierLoggerTest.java index 18f679eb..bbe5e984 100644 --- a/src/test/java/com/jcabi/log/SupplierLoggerTest.java +++ b/src/test/java/com/jcabi/log/SupplierLoggerTest.java @@ -32,8 +32,8 @@ import org.apache.log4j.Level; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; /** * Test case for {@link SupplierLogger}. @@ -47,13 +47,8 @@ @SuppressWarnings("PMD.MoreThanOneLogger") public final class SupplierLoggerTest { - /** - * SupplierLogger can tell if debug is disabled and the message is not - * logged enabled. - * @throws Exception If something goes wrong - */ @Test - public void debugIsDisabled() throws Exception { + public void debugIsDisabled() { final String name = "nodebug"; final String appender = "nodebugapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -65,17 +60,13 @@ public void debugIsDisabled() throws Exception { ); MatcherAssert.assertThat( ((UnitTestAppender) logger.getAppender(appender)).output(), - Matchers.isEmptyString() + Matchers.emptyString() ); } - /** - * SupplierLogger can log a message with debug level. - * @throws Exception If something goes wrong - */ @Test - @Ignore - public void debugIsEnabled() throws Exception { + @Disabled + public void debugIsEnabled() { final String name = "debugen"; final String appender = "debugapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -92,11 +83,6 @@ public void debugIsEnabled() throws Exception { ); } - /** - * SupplierLogger can tell if trace is disabled and the message is not - * logged enabled. - * @throws Exception If something goes wrong - */ @Test public void traceIsDisabled() throws Exception { final String name = "notrace"; @@ -110,17 +96,12 @@ public void traceIsDisabled() throws Exception { ); MatcherAssert.assertThat( ((UnitTestAppender) logger.getAppender(appender)).output(), - Matchers.isEmptyString() + Matchers.emptyString() ); } - /** - * SupplierLogger can log a message with trace level. - * @throws Exception If something goes wrong - */ @Test - @Ignore - public void traceIsEnabled() throws Exception { + public void traceIsEnabled() { final String name = "enabledtrace"; final String appender = "traceapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -137,13 +118,8 @@ public void traceIsEnabled() throws Exception { ); } - /** - * SupplierLogger can tell if warn is disabled and the message is not - * logged enabled. - * @throws Exception If something goes wrong - */ @Test - public void warnIsDisabled() throws Exception { + public void warnIsDisabled() { final String name = "nowarn"; final String appender = "nowarnapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -155,17 +131,13 @@ public void warnIsDisabled() throws Exception { ); MatcherAssert.assertThat( ((UnitTestAppender) logger.getAppender(appender)).output(), - Matchers.isEmptyString() + Matchers.emptyString() ); } - /** - * SupplierLogger can log a message with warn level. - * @throws Exception If something goes wrong - */ @Test - @Ignore - public void warnIsEnabled() throws Exception { + @Disabled + public void warnIsEnabled() { final String name = "enwarn"; final String appender = "warnapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -182,13 +154,8 @@ public void warnIsEnabled() throws Exception { ); } - /** - * SupplierLogger can tell if info is disabled and the message is not - * logged. - * @throws Exception If something goes wrong - */ @Test - public void infoIsDisabled() throws Exception { + public void infoIsDisabled() { final String name = "noinfo"; final String appender = "noinfoapp"; final org.apache.log4j.Logger logger = this.loggerForTest( @@ -200,17 +167,13 @@ public void infoIsDisabled() throws Exception { ); MatcherAssert.assertThat( ((UnitTestAppender) logger.getAppender(appender)).output(), - Matchers.isEmptyString() + Matchers.emptyString() ); } - /** - * SupplierLogger can log a message with info level. - * @throws Exception If something goes wrong - */ @Test - @Ignore - public void infoIsEnabled() throws Exception { + @Disabled + public void infoIsEnabled() { final String name = "withinfo"; final String appender = "infoapp"; final org.apache.log4j.Logger logger = this.loggerForTest( diff --git a/src/test/java/com/jcabi/log/TextDecorTest.java b/src/test/java/com/jcabi/log/TextDecorTest.java index 389451a9..bb7c8abf 100644 --- a/src/test/java/com/jcabi/log/TextDecorTest.java +++ b/src/test/java/com/jcabi/log/TextDecorTest.java @@ -33,50 +33,49 @@ import java.util.Collection; import java.util.Formattable; import java.util.Formatter; +import java.util.Locale; import org.apache.commons.lang3.StringUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link TextDecor}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class TextDecorTest extends AbstractDecorTest { +public final class TextDecorTest { - /** - * Public ctor. - * @param obj The object - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public TextDecorTest(final Object obj, final String text, - final int flags, final int width, final int precision) { - super(obj, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final String obj, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new TextDecor(obj), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final String obj, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new TextDecor(obj), flags, width, precision), + Matchers.hasToString(text) + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings( - { - "PMD.ProhibitPublicStaticMethods", - "PMD.AvoidDuplicateLiterals" - } - ) - public static Collection params() { + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + private static Collection params() { return Arrays.asList( new Object[][] { // @checkstyle MultipleStringLiterals (1 line) @@ -106,10 +105,4 @@ public void compressesLongText() { ) ); } - - @Override - public Formattable decor() { - return new TextDecor(this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/TypeDecorTest.java b/src/test/java/com/jcabi/log/TypeDecorTest.java index fa98fa8d..059b5354 100644 --- a/src/test/java/com/jcabi/log/TypeDecorTest.java +++ b/src/test/java/com/jcabi/log/TypeDecorTest.java @@ -31,42 +31,46 @@ import java.util.Arrays; import java.util.Collection; -import java.util.Formattable; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; +import java.util.Locale; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Test case for {@link TypeDecor}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ + * * @since 0.1 */ -@RunWith(Parameterized.class) -@SuppressWarnings("PMD.TestClassWithoutTestCases") -public final class TypeDecorTest extends AbstractDecorTest { +public final class TypeDecorTest { - /** - * Public ctor. - * @param obj The object - * @param text Expected text - * @param flags Flags - * @param width Width - * @param precision Precission - * @checkstyle ParameterNumber (3 lines) - */ - public TypeDecorTest(final Object obj, final String text, - final int flags, final int width, final int precision) { - super(obj, text, flags, width, precision); + @ParameterizedTest + @MethodSource("params") + public void testPrintsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Printed(new TypeDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); + } + + @ParameterizedTest + @MethodSource("params") + public void testLogsRight(final Object list, final String text, + final int flags, final int width, final int precision) throws DecorException { + Locale.setDefault(Locale.US); + MatcherAssert.assertThat( + new Logged(new TypeDecor(list), flags, width, precision), + Matchers.hasToString(text) + ); } /** * Params for this parametrized test. * @return Array of arrays of params for ctor */ - @Parameters - @SuppressWarnings("PMD.ProhibitPublicStaticMethods") - public static Collection params() { + private static Collection params() { return Arrays.asList( new Object[][] { {"testing", "java.lang.String", 0, 0, 0}, @@ -75,10 +79,4 @@ public static Collection params() { } ); } - - @Override - public Formattable decor() { - return new TypeDecor(this.object()); - } - } diff --git a/src/test/java/com/jcabi/log/VerboseCallableTest.java b/src/test/java/com/jcabi/log/VerboseCallableTest.java index ac57aec9..1dd5ae23 100644 --- a/src/test/java/com/jcabi/log/VerboseCallableTest.java +++ b/src/test/java/com/jcabi/log/VerboseCallableTest.java @@ -29,8 +29,8 @@ */ package com.jcabi.log; -import java.util.concurrent.Callable; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test case for {@link VerboseCallable}. @@ -43,18 +43,17 @@ public final class VerboseCallableTest { /** * VerboseCallable can log exceptions inside Callable. - * @throws Exception If something goes wrong */ - @Test(expected = IllegalArgumentException.class) - public void logsExceptionsInCallable() throws Exception { - new VerboseCallable( - new Callable() { - @Override - public Integer call() { + @Test + public void logsExceptionsInCallable() { + Assertions.assertThrows( + IllegalArgumentException.class, + () -> new VerboseCallable( + () -> { throw new IllegalArgumentException("oops"); } - } - ).call(); + ).call() + ); } } diff --git a/src/test/java/com/jcabi/log/VerboseProcessTest.java b/src/test/java/com/jcabi/log/VerboseProcessTest.java index 08be44b7..89a1f134 100644 --- a/src/test/java/com/jcabi/log/VerboseProcessTest.java +++ b/src/test/java/com/jcabi/log/VerboseProcessTest.java @@ -48,10 +48,10 @@ import org.apache.log4j.spi.LoggingEvent; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; /** @@ -69,15 +69,10 @@ @SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals" }) public final class VerboseProcessTest { - /** - * VerboseProcess can run a command line script. - * @throws Exception If something goes wrong - * @link http://stackoverflow.com/questions/24802042 - */ @Test - @Ignore - public void runsACommandLineScript() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + @Disabled + public void runsACommandLineScript() { + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final VerboseProcess process = new VerboseProcess( new ProcessBuilder("echo", "hey \u20ac!").redirectErrorStream(true) ); @@ -87,15 +82,10 @@ public void runsACommandLineScript() throws Exception { ); } - /** - * VerboseProcess can run a command line script. - * @throws Exception If something goes wrong - * @link http://stackoverflow.com/questions/24802042 - */ @Test - @Ignore - public void echosUnicodeCorrectly() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + @Disabled + public void echosUnicodeCorrectly() { + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); MatcherAssert.assertThat( new VerboseProcess( new ProcessBuilder( @@ -107,20 +97,16 @@ public void echosUnicodeCorrectly() throws Exception { ); } - /** - * VerboseProcess can run a command line script with exception. - * @throws Exception If something goes wrong - */ @Test - public void runsACommandLineScriptWithException() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + public void runsACommandLineScriptWithException() { + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final VerboseProcess process = new VerboseProcess( new ProcessBuilder("cat", "/non-existing-file.txt") .redirectErrorStream(true) ); try { process.stdout(); - Assert.fail("exception expected"); + Assertions.fail("exception expected"); } catch (final IllegalArgumentException ex) { MatcherAssert.assertThat( ex.getMessage(), @@ -129,14 +115,9 @@ public void runsACommandLineScriptWithException() throws Exception { } } - /** - * VerboseProcess can run a command line script with exception, without - * errorStream redirection. - * @throws Exception If something goes wrong - */ @Test public void runsACommandLineScriptWithExceptionNoRedir() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final VerboseProcess process = new VerboseProcess( new ProcessBuilder("cat", "/non-existing-file.txt") ); @@ -151,13 +132,9 @@ public void runsACommandLineScriptWithExceptionNoRedir() throws Exception { ); } - /** - * VerboseProcess can handle a long running command. - * @throws Exception If something goes wrong - */ @Test - public void handlesLongRunningCommand() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + public void handlesLongRunningCommand() { + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final VerboseProcess process = new VerboseProcess( new ProcessBuilder("/bin/bash", "-c", "sleep 2; echo 'done'") ); @@ -167,27 +144,24 @@ public void handlesLongRunningCommand() throws Exception { ); } - /** - * VerboseProcess can reject NULL. - * @throws Exception If something goes wrong - */ - @Test(expected = RuntimeException.class) - public void rejectsNullProcesses() throws Exception { - final ProcessBuilder builder = null; - new VerboseProcess(builder); + @Test + public void rejectsNullProcesses() { + Assertions.assertThrows( + RuntimeException.class, + () -> { + final ProcessBuilder builder = null; + new VerboseProcess(builder); + } + ); } - /** - * VerboseProcess can reject ALL stdout level. - * @throws Exception If something goes wrong - */ @Test - public void rejectsStdoutWithLevelAll() throws Exception { + public void rejectsStdoutWithLevelAll() { try { new VerboseProcess( Mockito.mock(Process.class), Level.ALL, Level.INFO ); - Assert.fail("IllegalArgumentException expected"); + Assertions.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException ex) { MatcherAssert.assertThat( ex.getMessage(), @@ -201,17 +175,13 @@ public void rejectsStdoutWithLevelAll() throws Exception { } } - /** - * VerboseProcess can reject ALL stderr level. - * @throws Exception If something goes wrong - */ @Test - public void rejectsStderrWithLevelAll() throws Exception { + public void rejectsStderrWithLevelAll() { try { new VerboseProcess( Mockito.mock(Process.class), Level.INFO, Level.ALL ); - Assert.fail("IllegalArgumentException expected here"); + Assertions.fail("IllegalArgumentException expected here"); } catch (final IllegalArgumentException ex) { MatcherAssert.assertThat( ex.getMessage(), @@ -225,14 +195,10 @@ public void rejectsStderrWithLevelAll() throws Exception { } } - /** - * VerboseProcess can quietly terminate a long-running process. - * @throws Exception If something goes wrong - */ @Test @SuppressWarnings("PMD.DoNotUseThreads") public void quietlyTerminatesLongRunningProcess() throws Exception { - Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); + Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final Process proc = new ProcessBuilder("sleep", "10000").start(); final VerboseProcess process = new VerboseProcess(proc); final CountDownLatch start = new CountDownLatch(1); @@ -255,12 +221,8 @@ public void quietlyTerminatesLongRunningProcess() throws Exception { ); } - /** - * VerboseProcess.stdoutQuietly() should log stderr messages. - * @throws Exception If something goes wrong - */ @Test - public void stdoutQuietlyLogsErrors() throws Exception { + public void stdoutQuietlyLogsErrors() { final StringWriter writer = new StringWriter(); org.apache.log4j.Logger.getRootLogger().addAppender( new WriterAppender(new SimpleLayout(), writer) @@ -284,11 +246,6 @@ public void stdoutQuietlyLogsErrors() throws Exception { ); } - /** - * VerboseProcess exits "gracefully" when it can't read from the process - * stream, and logs the error that is thrown. - * @throws Exception If something goes wrong - */ @Test public void logsErrorWhenUnderlyingStreamIsClosed() throws Exception { final StringWriter writer = new StringWriter(); @@ -320,22 +277,12 @@ public void logsErrorWhenUnderlyingStreamIsClosed() throws Exception { ); } - /** - * VerboseProcess can terminate its monitors and underlying Process if - * closed before real usage. - * @throws Exception If something goes wrong - */ @Test public void terminatesMonitorsAndProcessIfClosedInstantly() throws Exception { this.terminatesMonitorsAndProcessIfClosed(0); } - /** - * VerboseProcess can terminate its monitors and underlying Process if - * closed shortly after real usage. - * @throws Exception If something goes wrong - */ @Test public void terminatesMonitorsAndProcessIfClosedShortly() throws Exception { @@ -343,11 +290,6 @@ public void terminatesMonitorsAndProcessIfClosedShortly() this.terminatesMonitorsAndProcessIfClosed(50); } - /** - * VerboseProcess can terminate its monitors and underlying Process if - * closed after longer time since real usage. - * @throws Exception If something goes wrong - */ @Test public void terminatesMonitorsAndProcessIfClosedNormal() throws Exception { final long delay = 400; diff --git a/src/test/java/com/jcabi/log/VerboseRunnableTest.java b/src/test/java/com/jcabi/log/VerboseRunnableTest.java index fd086d11..4378fb00 100644 --- a/src/test/java/com/jcabi/log/VerboseRunnableTest.java +++ b/src/test/java/com/jcabi/log/VerboseRunnableTest.java @@ -37,7 +37,8 @@ import java.util.concurrent.atomic.AtomicReference; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test case for {@link VerboseRunnable}. @@ -48,25 +49,21 @@ @SuppressWarnings({ "PMD.DoNotUseThreads", "PMD.TooManyMethods" }) public final class VerboseRunnableTest { - /** - * VerboseRunnable can log exceptions inside Runnable. - * @throws Exception If something goes wrong - */ - @Test(expected = IllegalArgumentException.class) - public void logsExceptionsInRunnable() throws Exception { - new VerboseRunnable( - (Runnable) () -> { - throw new IllegalArgumentException("oops"); - } - ).run(); + @Test + public void logsExceptionsInRunnable() { + Assertions.assertThrows( + IllegalArgumentException.class, + () -> new VerboseRunnable( + (Runnable) () -> { + throw new IllegalArgumentException("oops"); + } + ).run() + ); + } - /** - * VerboseRunnable can swallow exceptions. - * @throws Exception If something goes wrong - */ @Test - public void swallowsExceptionsInRunnable() throws Exception { + public void swallowsExceptionsInRunnable() { new VerboseRunnable( (Runnable) () -> { throw new IllegalArgumentException("boom"); @@ -75,12 +72,8 @@ public void swallowsExceptionsInRunnable() throws Exception { ).run(); } - /** - * VerboseRunnable can swallow exceptions in Callable. - * @throws Exception If something goes wrong - */ @Test - public void swallowsExceptionsInCallable() throws Exception { + public void swallowsExceptionsInCallable() { new VerboseRunnable( () -> { throw new IllegalArgumentException("boom-2"); @@ -89,11 +82,6 @@ public void swallowsExceptionsInCallable() throws Exception { ).run(); } - /** - * VerboseRunnable can translate {@code toString()} - * from an underlying {@link Runnable}. - * @throws Exception If something goes wrong - */ @Test public void translatesToStringFromUnderlyingRunnable() throws Exception { final String text = "some text abc"; @@ -115,11 +103,6 @@ public String toString() { ); } - /** - * VerboseRunnable can translate {@code toString()} - * from an underlying {@link Callable}. - * @throws Exception If something goes wrong - */ @Test public void translatesToStringFromUnderlyingCallable() throws Exception { final String text = "some text abc-2"; @@ -142,11 +125,6 @@ public String toString() { ); } - /** - * VerboseRunnable can preserve interrupted status. - * @throws Exception If something goes wrong - * @since 0.14 - */ @Test public void preservesInterruptedStatus() throws Exception { final ScheduledExecutorService svc = diff --git a/src/test/java/com/jcabi/log/VerboseThreadsTest.java b/src/test/java/com/jcabi/log/VerboseThreadsTest.java index e05fc698..2731e095 100644 --- a/src/test/java/com/jcabi/log/VerboseThreadsTest.java +++ b/src/test/java/com/jcabi/log/VerboseThreadsTest.java @@ -33,7 +33,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test case for {@link VerboseThreads}. @@ -44,10 +44,6 @@ @SuppressWarnings("PMD.DoNotUseThreads") public final class VerboseThreadsTest { - /** - * VerboseThreads can create new threads on demand. - * @throws Exception If something goes wrong - */ @Test public void instantiatesThreadsOnDemand() throws Exception { final ExecutorService service = Executors @@ -61,10 +57,6 @@ public void instantiatesThreadsOnDemand() throws Exception { service.shutdown(); } - /** - * VerboseThreads can log exceptions with {@link ExecutorService#submit()}. - * @throws Exception If something goes wrong - */ @Test public void logsWhenThreadsAreNotDying() throws Exception { final ExecutorService service = Executors