Make unit test more resilient to environment differences.
*/
package org.apache.logging.log4j.core.layout;
+import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.categories.Layouts;
@@ -314,8 +319,19 @@ 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() + "\""));
+ final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+ JsonNode node = mapper.readTree(str);
+ assertThat(getJSONChildNodeValueAsText(node, "KEY1"), equalTo("VALUE1"));
+ assertThat(getJSONChildNodeValueAsText(node, "KEY2"), equalTo(new JavaLookup().getRuntime()));
+ }
+
+ private String getJSONChildNodeValueAsText(final JsonNode parentNode, final String key) {
+ JsonNode childNode = parentNode.get(key);
+ if (childNode != null) {
+ return childNode.asText();
+ } else {
+ return Strings.EMPTY;
+ }
}
@Test
