You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2019/09/02 22:52:05 UTC

[logging-log4j2] branch master updated: Improve test assertions

This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new d4f74cf  Improve test assertions
d4f74cf is described below

commit d4f74cf29e8a7c67805cfb67063f0de1d679db43
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Mon Sep 2 17:51:28 2019 -0500

    Improve test assertions
    
    This should help debug string-based assertions when they fail.
    
    Signed-off-by: Matt Sicker <bo...@gmail.com>
---
 .../log4j/jackson/yaml/layout/YamlLayoutTest.java  | 33 ++++++++++------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/log4j-layout-jackson-yaml/src/test/java/org/apache/logging/log4j/jackson/yaml/layout/YamlLayoutTest.java b/log4j-layout-jackson-yaml/src/test/java/org/apache/logging/log4j/jackson/yaml/layout/YamlLayoutTest.java
index 8248b9f..d9ee191 100644
--- a/log4j-layout-jackson-yaml/src/test/java/org/apache/logging/log4j/jackson/yaml/layout/YamlLayoutTest.java
+++ b/log4j-layout-jackson-yaml/src/test/java/org/apache/logging/log4j/jackson/yaml/layout/YamlLayoutTest.java
@@ -16,11 +16,6 @@
  */
 package org.apache.logging.log4j.jackson.yaml.layout;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
@@ -50,6 +45,9 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
 /**
  * Tests the YamlLayout class.
  */
@@ -77,8 +75,7 @@ public class YamlLayoutTest {
 
     private void checkAt(final String expected, final int lineIndex, final List<String> list) {
         final String trimedLine = list.get(lineIndex).trim();
-        assertTrue("Incorrect line index " + lineIndex + ": " + Strings.dquote(trimedLine),
-                trimedLine.equals(expected));
+        assertEquals("Incorrect line index " + lineIndex + ": " + Strings.dquote(trimedLine), trimedLine, expected);
     }
 
     private void checkContains(final String expected, final List<String> list) {
@@ -95,7 +92,7 @@ public class YamlLayoutTest {
         // "name":"value"
         //final String expected = String.format("- key: \"%s\"\n  value: \"%s\"", key, value);
         final String expected = String.format("%s: \"%s\"", key, value);
-        assertTrue("Cannot find " + expected + " in " + str, str.contains(expected));
+        assertThat(str, containsString(expected));
     }
 
     private void checkProperty(final String key, final String value, final boolean compact, final String str,
@@ -103,17 +100,17 @@ public class YamlLayoutTest {
         final String propSep = this.toPropertySeparator(compact, isValue);
         // {"key":"MDC.B","value":"B_Value"}
         final String expected = String.format("%s%s\"%s\"", key, propSep, value);
-        assertTrue("Cannot find " + expected + " in " + str, str.contains(expected));
+        assertThat(str, containsString(expected));
     }
 
     private void checkPropertyName(final String name, final boolean compact, final String str, final boolean isValue) {
         final String propSep = this.toPropertySeparator(compact, isValue);
-        assertTrue(str, str.contains(name + propSep));
+        assertThat(str, containsString(name + propSep));
     }
 
     private void checkPropertyNameAbsent(final String name, final boolean compact, final String str, final boolean isValue) {
         final String propSep = this.toPropertySeparator(compact, isValue);
-        assertFalse(str, str.contains(name + propSep));
+        assertThat(str, not(containsString(name + propSep)));
     }
 
     private String prepareYAMLForStacktraceTests(final boolean stacktraceAsString) {
@@ -140,8 +137,8 @@ public class YamlLayoutTest {
                 .setConfiguration(ctx.getConfiguration())
                 .build();
         final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
-        assertTrue(str, str.contains("KEY1: \"VALUE1\""));
-        assertTrue(str, str.contains("KEY2: \"" + new JavaLookup().getRuntime() + "\""));
+        assertThat(str, containsString("KEY1: \"VALUE1\""));
+        assertThat(str, containsString("KEY2: \"" + new JavaLookup().getRuntime() + "\""));
     }
 
     private void testAllFeatures(final boolean includeSource, final boolean compact, final boolean eventEol,
@@ -272,7 +269,7 @@ public class YamlLayoutTest {
                 .setIncludeNullDelimiter(false)
                 .build();
         final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
-        assertFalse(str.endsWith("\0"));
+        assertThat(str, not(endsWith("\0")));
     }
 
     @Test
@@ -281,7 +278,7 @@ public class YamlLayoutTest {
                 .setIncludeNullDelimiter(true)
                 .build();
         final String str = layout.toSerializable(LogEventFixtures.createLogEvent());
-        assertTrue(str.endsWith("\0"));
+        assertThat(str, endsWith("\0"));
     }
 
     /**
@@ -350,7 +347,7 @@ public class YamlLayoutTest {
                 .setThreadName("threadName") //
                 .setTimeMillis(1).build();
         final String str = layout.toSerializable(expected);
-        assertTrue(str, str.contains("loggerName: \"a.B\""));
+        assertThat(str, containsString("loggerName: \"a.B\""));
         final Log4jLogEvent actual = new Log4jYamlObjectMapper(false, true, false).readValue(str, Log4jLogEvent.class);
         assertEquals(expected.getLoggerName(), actual.getLoggerName());
         assertEquals(expected, actual);
@@ -369,13 +366,13 @@ public class YamlLayoutTest {
     @Test
     public void testStacktraceAsNonString() throws Exception {
         final String str = prepareYAMLForStacktraceTests(false);
-        assertTrue(str, str.contains("extendedStackTrace:\n    - "));
+        assertThat(str, containsString("extendedStackTrace:\n    - "));
     }
 
     @Test
     public void testStacktraceAsString() throws Exception {
         final String str = prepareYAMLForStacktraceTests(true);
-        assertTrue(str, str.contains("extendedStackTrace: \"java.lang.NullPointerException"));
+        assertThat(str, containsString("extendedStackTrace: \"java.lang.NullPointerException"));
     }
 
     private String toPropertySeparator(final boolean compact, final boolean value) {