You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2014/05/13 19:36:14 UTC
svn commit: r1594294 [4/4] - in /logging/log4j/log4j2/trunk: ./
log4j-api/src/main/java/org/apache/logging/log4j/ log4j-core/
log4j-core/src/main/java/org/apache/logging/log4j/core/
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ log4j-cor...
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JSONCompleteFileAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JSONCompleteFileAppenderTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JSONCompleteFileAppenderTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JSONCompleteFileAppenderTest.java Tue May 13 17:36:12 2014
@@ -54,6 +54,7 @@ public class JSONCompleteFileAppenderTes
String line3;
String line4;
String line5;
+ String line6;
final BufferedReader reader = new BufferedReader(new FileReader(this.logFile));
try {
line1 = reader.readLine();
@@ -61,6 +62,7 @@ public class JSONCompleteFileAppenderTes
line3 = reader.readLine();
line4 = reader.readLine();
line5 = reader.readLine();
+ line6 = reader.readLine();
} finally {
reader.close();
}
@@ -69,21 +71,25 @@ public class JSONCompleteFileAppenderTes
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg1 + ']', line1.equals(msg1));
assertNotNull("line2", line2);
- final String msg2 = " {";
+ final String msg2 = "{";
assertTrue("line2 incorrect: [" + line2 + "], does not contain: [" + msg2 + ']', line2.equals(msg2));
assertNotNull("line3", line3);
- final String msg3 = " \"logger\":\"com.foo.Bar\",";
+ final String msg3 = " \"timeMillis\" : ";
assertTrue("line3 incorrect: [" + line3 + "], does not contain: [" + msg3 + ']', line3.contains(msg3));
assertNotNull("line4", line4);
- final String msg4 = "\"timestamp\":";
+ final String msg4 = " \"thread\" : \"main\",";
assertTrue("line4 incorrect: [" + line4 + "], does not contain: [" + msg4 + ']', line4.contains(msg4));
assertNotNull("line5", line5);
- final String msg5 = " \"level\":\"INFO\",";
+ final String msg5 = " \"level\" : \"INFO\",";
assertTrue("line5 incorrect: [" + line5 + "], does not contain: [" + msg5 + ']', line5.contains(msg5));
+ assertNotNull("line6", line6);
+ final String msg6 = " \"loggerName\" : \"com.foo.Bar\",";
+ assertTrue("line5 incorrect: [" + line6 + "], does not contain: [" + msg6 + ']', line6.contains(msg6));
+
final String location = "testFlushAtEndOfBatch";
assertTrue("no location", !line1.contains(location));
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java Tue May 13 17:36:12 2014
@@ -66,7 +66,7 @@ public class XmlFileAppenderTest {
assertNotNull("line1", line1);
assertNotNull("line1", line1);
- final String msg1 = "<log4j:Event ";
+ final String msg1 = "<Event ";
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg1 + ']', line1.contains(msg1));
assertNotNull("line2", line2);
@@ -74,7 +74,7 @@ public class XmlFileAppenderTest {
assertTrue("line2 incorrect: [" + line2 + "], does not contain: [" + msg2 + ']', line2.contains(msg2));
assertNotNull("line3", line3);
- final String msg3 = "</log4j:Event>";
+ final String msg3 = "</Event>";
assertTrue("line3 incorrect: [" + line3 + "], does not contain: [" + msg3 + ']', line3.contains(msg3));
final String location = "testFlushAtEndOfBatch";
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rewrite/MapRewritePolicyTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rewrite/MapRewritePolicyTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rewrite/MapRewritePolicyTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rewrite/MapRewritePolicyTest.java Tue May 13 17:36:12 2014
@@ -140,7 +140,7 @@ public class MapRewritePolicyTest {
Assert.assertEquals("Marker changed", orig.getMarker(), changed.getMarker());
Assert.assertEquals("FQCN changed", orig.getLoggerFQCN(), changed.getLoggerFQCN());
Assert.assertEquals("Level changed", orig.getLevel(), changed.getLevel());
- Assert.assertEquals("Throwable changed", orig.getThrown() == null //
+ Assert.assertArrayEquals("Throwable changed", orig.getThrown() == null //
? null //
: ((Log4jLogEvent) orig).getThrownProxy().getExtendedStackTrace(), //
changed.getThrown() == null //
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java Tue May 13 17:36:12 2014
@@ -16,26 +16,50 @@
*/
package org.apache.logging.log4j.core.impl;
-import org.junit.Assert;
-import org.junit.Test;
-
+import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
/**
*
*/
public class ThrowableProxyTest {
+ static class Fixture {
+ @JsonProperty
+ ThrowableProxy proxy = new ThrowableProxy(new IOException("test"));
+ }
+
+ @Test
+ public void testJsonIoContainer() throws IOException {
+ ObjectMapper objectMapper = new Log4jJsonObjectMapper();
+ Fixture expected = new Fixture();
+ final String s = objectMapper.writeValueAsString(expected);
+ Fixture actual = objectMapper.readValue(s, Fixture.class);
+ Assert.assertEquals(expected.proxy.getName(), actual.proxy.getName());
+ Assert.assertEquals(expected.proxy.getMessage(), actual.proxy.getMessage());
+ Assert.assertEquals(expected.proxy.getLocalizedMessage(), actual.proxy.getLocalizedMessage());
+ Assert.assertEquals(expected.proxy.getCommonElementCount(), actual.proxy.getCommonElementCount());
+ Assert.assertArrayEquals(expected.proxy.getExtendedStackTrace(), actual.proxy.getExtendedStackTrace());
+ Assert.assertEquals(expected.proxy, actual.proxy);
+ }
+
@Test
public void testStack() {
final Map<String, ThrowableProxy.CacheEntry> map = new HashMap<String, ThrowableProxy.CacheEntry>();
final Stack<Class<?>> stack = new Stack<Class<?>>();
final Throwable throwable = new IllegalStateException("This is a test");
final ThrowableProxy proxy = new ThrowableProxy(throwable);
- final StackTracePackageElement[] callerPackageData = proxy.resolvePackageData(stack, map, null,
- throwable.getStackTrace());
+ final ExtendedStackTraceElement[] callerPackageData = proxy.toExtendedStackTrace(stack, map, null,
+ throwable.getStackTrace());
Assert.assertNotNull("No package data returned", callerPackageData);
}
}
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java?rev=1594294&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java Tue May 13 17:36:12 2014
@@ -0,0 +1,127 @@
+/*
+ * 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.logging.log4j.core.jackson;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+public class JacksonIssue429MyNamesTest {
+
+ @SuppressWarnings("serial")
+ static class MyStackTraceElementDeserializer extends StdScalarDeserializer<StackTraceElement> {
+ private static final long serialVersionUID = 1L;
+
+ public final static MyStackTraceElementDeserializer instance = new MyStackTraceElementDeserializer();
+
+ public MyStackTraceElementDeserializer() {
+ super(StackTraceElement.class);
+ }
+
+ @Override
+ public StackTraceElement deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
+ JsonProcessingException {
+ JsonToken t = jp.getCurrentToken();
+ // Must get an Object
+ if (t == JsonToken.START_OBJECT) {
+ String className = "", methodName = "", fileName = "";
+ int lineNumber = -1;
+
+ while ((t = jp.nextValue()) != JsonToken.END_OBJECT) {
+ String propName = jp.getCurrentName();
+ if ("class".equals(propName)) {
+ className = jp.getText();
+ } else if ("file".equals(propName)) {
+ fileName = jp.getText();
+ } else if ("line".equals(propName)) {
+ if (t.isNumeric()) {
+ lineNumber = jp.getIntValue();
+ } else {
+ throw JsonMappingException.from(jp, "Non-numeric token (" + t
+ + ") for property 'lineNumber'");
+ }
+ } else if ("method".equals(propName)) {
+ methodName = jp.getText();
+ } else if ("nativeMethod".equals(propName)) {
+ // no setter, not passed via constructor: ignore
+ } else {
+ handleUnknownProperty(jp, ctxt, _valueClass, propName);
+ }
+ }
+ return new StackTraceElement(className, methodName, fileName, lineNumber);
+ }
+ throw ctxt.mappingException(_valueClass, t);
+ }
+ }
+
+ static class StackTraceBean {
+ public final static int NUM = 13;
+
+ @JsonProperty("Location")
+ @JsonDeserialize(using = MyStackTraceElementDeserializer.class)
+ private StackTraceElement location;
+ }
+
+ private final static ObjectMapper SHARED_MAPPER = new ObjectMapper();
+
+ private final ObjectMapper MAPPER = objectMapper();
+
+ protected String aposToQuotes(String json) {
+ return json.replace("'", "\"");
+ }
+
+ protected ObjectMapper objectMapper() {
+ return SHARED_MAPPER;
+ }
+
+ @Test
+ public void testStackTraceElementWithCustom() throws Exception {
+ // first, via bean that contains StackTraceElement
+ StackTraceBean bean = MAPPER
+ .readValue(
+ aposToQuotes("{'Location':{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':13}}"),
+ StackTraceBean.class);
+ Assert.assertNotNull(bean);
+ Assert.assertNotNull(bean.location);
+ Assert.assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
+
+ // and then directly, iff registered
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule();
+ module.addDeserializer(StackTraceElement.class, new MyStackTraceElementDeserializer());
+ mapper.registerModule(module);
+
+ StackTraceElement elem = mapper.readValue(
+ aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':13}"),
+ StackTraceElement.class);
+ Assert.assertNotNull(elem);
+ Assert.assertEquals(StackTraceBean.NUM, elem.getLineNumber());
+ }
+}
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429MyNamesTest.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java?rev=1594294&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java Tue May 13 17:36:12 2014
@@ -0,0 +1,90 @@
+/*
+ * 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.logging.log4j.core.jackson;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+public class JacksonIssue429Test {
+
+ @SuppressWarnings("serial")
+ static class Jackson429StackTraceElementDeserializer extends StdDeserializer<StackTraceElement> {
+ public Jackson429StackTraceElementDeserializer() {
+ super(StackTraceElement.class);
+ }
+
+ @Override
+ public StackTraceElement deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
+ JsonProcessingException {
+ jp.skipChildren();
+ return new StackTraceElement("a", "b", "b", StackTraceBean.NUM);
+ }
+
+ }
+
+ static class StackTraceBean {
+ public final static int NUM = 13;
+
+ @JsonProperty("Location")
+ @JsonDeserialize(using = Jackson429StackTraceElementDeserializer.class)
+ private StackTraceElement location;
+ }
+
+ private final static ObjectMapper SHARED_MAPPER = new ObjectMapper();
+
+ private final ObjectMapper MAPPER = objectMapper();
+
+ protected String aposToQuotes(String json) {
+ return json.replace("'", "\"");
+ }
+
+ protected ObjectMapper objectMapper() {
+ return SHARED_MAPPER;
+ }
+
+ @Test
+ public void testStackTraceElementWithCustom() throws Exception {
+ // first, via bean that contains StackTraceElement
+ StackTraceBean bean = MAPPER.readValue(aposToQuotes("{'Location':'foobar'}"), StackTraceBean.class);
+ Assert.assertNotNull(bean);
+ Assert.assertNotNull(bean.location);
+ Assert.assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
+
+ // and then directly, iff registered
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule();
+ module.addDeserializer(StackTraceElement.class, new Jackson429StackTraceElementDeserializer());
+ mapper.registerModule(module);
+
+ StackTraceElement elem = mapper.readValue(
+ aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}"),
+ StackTraceElement.class);
+ Assert.assertNotNull(elem);
+ Assert.assertEquals(StackTraceBean.NUM, elem.getLineNumber());
+ }
+}
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/JacksonIssue429Test.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java?rev=1594294&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java Tue May 13 17:36:12 2014
@@ -0,0 +1,84 @@
+package org.apache.logging.log4j.core.jackson;
+
+import java.io.IOException;
+
+import org.apache.logging.log4j.Level;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+/**
+ * Tests {@link LevelMixIn}.
+ */
+public class LevelMixInTest {
+
+ static class Fixture {
+ @JsonProperty
+ private final Level level = Level.DEBUG;
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final Fixture other = (Fixture) obj;
+ if (this.level == null) {
+ if (other.level != null) {
+ return false;
+ }
+ } else if (!this.level.equals(other.level)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((this.level == null) ? 0 : this.level.hashCode());
+ return result;
+ }
+ }
+ private Log4jJsonObjectMapper log4jObjectMapper;
+
+ private ObjectReader reader;
+
+ private ObjectWriter writer;
+
+ @Before
+ public void setUp() {
+ log4jObjectMapper = new Log4jJsonObjectMapper();
+ writer = log4jObjectMapper.writer();
+ reader = log4jObjectMapper.reader(Level.class);
+ }
+
+ @Test
+ public void testContainer() throws IOException {
+ final Fixture expected = new Fixture();
+ final String str = writer.writeValueAsString(expected);
+ Assert.assertTrue(str.contains("DEBUG"));
+ final ObjectReader fixtureReader = log4jObjectMapper.reader(Fixture.class);
+ final Fixture actual = fixtureReader.readValue(str);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testNameOnly() throws IOException {
+ final Level expected = Level.getLevel("DEBUG");
+ final String str = writer.writeValueAsString(expected);
+ Assert.assertTrue(str.contains("DEBUG"));
+ final Level actual = reader.readValue(str);
+ Assert.assertEquals(expected, actual);
+ }
+}
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/LevelMixInTest.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java?rev=1594294&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java Tue May 13 17:36:12 2014
@@ -0,0 +1,83 @@
+/*
+ * 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.logging.log4j.core.jackson;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+public class StackTraceElementMixInTest {
+
+ @Test
+ public void testLog4jJsonObjectMapper() throws Exception {
+ this.roundtrip(new Log4jJsonObjectMapper());
+ }
+
+ /**
+ * @param mapper
+ * @throws JsonProcessingException
+ * @throws IOException
+ * @throws JsonParseException
+ * @throws JsonMappingException
+ */
+ private void roundtrip(final ObjectMapper mapper) throws JsonProcessingException, IOException, JsonParseException, JsonMappingException {
+ final StackTraceElement expected = new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
+ final String s = mapper.writeValueAsString(expected);
+ final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testLog4jXmlObjectMapper() throws Exception {
+ this.roundtrip(new Log4jXmlObjectMapper());
+ }
+
+ protected String aposToQuotes(final String json) {
+ return json.replace("'", "\"");
+ }
+
+ @Test
+ public void testFromJsonWithSimpleModule() throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+ final SimpleModule module = new SimpleModule();
+ module.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer());
+ mapper.registerModule(module);
+ final StackTraceElement expected = new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
+ final String s = this.aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}");
+ final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testFromJsonWithLog4jModule() throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+ final SimpleModule module = new Log4jJsonModule();
+ module.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer());
+ mapper.registerModule(module);
+ final StackTraceElement expected = new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
+ final String s = this.aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}");
+ final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
+ Assert.assertEquals(expected, actual);
+ }
+}
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/jackson/StackTraceElementMixInTest.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/JSONLayoutTest.java Tue May 13 17:36:12 2014
@@ -17,8 +17,8 @@
package org.apache.logging.log4j.core.layout;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.List;
import java.util.Map;
@@ -32,8 +32,12 @@ import org.apache.logging.log4j.core.Log
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.helpers.Charsets;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
+import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
+import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -43,13 +47,17 @@ import org.junit.Test;
public class JSONLayoutTest {
static ConfigurationFactory cf = new BasicConfigurationFactory();
+ private static final String DQUOTE = "\"";
+
@AfterClass
public static void cleanupClass() {
ConfigurationFactory.removeConfigurationFactory(cf);
+ ThreadContext.clearAll();
}
@BeforeClass
public static void setupClass() {
+ ThreadContext.clearAll();
ConfigurationFactory.setConfigurationFactory(cf);
final LoggerContext ctx = (LoggerContext) LogManager.getContext();
ctx.reconfigure();
@@ -57,103 +65,172 @@ public class JSONLayoutTest {
LoggerContext ctx = (LoggerContext) LogManager.getContext();
- Logger root = this.ctx.getLogger("");
+ Logger rootLogger = this.ctx.getLogger("");
+
+ 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 + ": \"" + trimedLine + '"', trimedLine.equals(expected));
+ }
+
+ private void checkContains(final String expected, final List<String> list) {
+ for (final String string : list) {
+ final String trimedLine = string.trim();
+ if (trimedLine.equals(expected)) {
+ return;
+ }
+ }
+ Assert.fail("Cannot find " + expected + " in " + list);
+ }
+
+ private void checkMapEntry(final String key, final String value, final boolean compact, final String str) {
+ final String propSep = this.toPropertySeparator(compact);
+ // "name":"value"
+ final String expected = String.format("{\"key\":\"%s\",\"value\":\"%s\"}", key, value);
+ assertTrue("Cannot find " + expected + " in " + str, str.contains(expected));
+ }
+
+ private void checkProperty(final String key, final String value, final boolean compact, final String str) {
+ final String propSep = this.toPropertySeparator(compact);
+ // {"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));
+ }
+
+ private void checkPropertyName(final String name, final boolean compact, final String str) {
+ final String propSep = this.toPropertySeparator(compact);
+ assertTrue(str, str.contains(DQUOTE + name + DQUOTE + propSep));
+ }
+
+ private void testAllFeatures(final boolean includeSource, final boolean compact, final boolean includeContext) throws Exception {
+ final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
+ final AbstractJacksonLayout layout = JSONLayout.createLayout(Boolean.toString(includeSource), Boolean.toString(includeContext),
+ "false", Boolean.toString(compact), "UTF-8");
+ final String str = layout.toSerializable(expected);
+ final String propSep = this.toPropertySeparator(compact);
+ // Just check for \n since \r might or might not be there.
+ assertEquals(str, !compact, str.contains("\n"));
+ assertEquals(str, includeSource, str.contains("source"));
+ assertEquals(str, includeContext, str.contains("contextMap"));
+ final Log4jLogEvent actual = new Log4jJsonObjectMapper().readValue(str, Log4jLogEvent.class);
+ LogEventFixtures.assertEqualLogEvents(expected, actual, includeSource, includeContext);
+ if (includeContext) {
+ this.checkMapEntry("MDC.A", "A_Value", compact, str);
+ this.checkMapEntry("MDC.B", "B_Value", compact, str);
+ }
+ //
+ assertNull(actual.getThrown());
+ // make sure the names we want are used
+ this.checkPropertyName("timeMillis", compact, str);
+ this.checkPropertyName("thread", compact, str); // and not threadName
+ this.checkPropertyName("level", compact, str);
+ this.checkPropertyName("loggerName", compact, str);
+ this.checkPropertyName("marker", compact, str);
+ this.checkPropertyName("name", compact, str);
+ this.checkPropertyName("parents", compact, str);
+ this.checkPropertyName("message", compact, str);
+ this.checkPropertyName("thrown", compact, str);
+ this.checkPropertyName("cause", compact, str);
+ this.checkPropertyName("class", compact, str);
+ this.checkPropertyName("method", compact, str);
+ this.checkPropertyName("file", compact, str);
+ this.checkPropertyName("line", compact, str);
+ this.checkPropertyName("exact", compact, str);
+ this.checkPropertyName("location", compact, str);
+ this.checkPropertyName("version", compact, str);
+ this.checkPropertyName("commonElementCount", compact, str);
+ this.checkPropertyName("localizedMessage", compact, str);
+ this.checkPropertyName("extendedStackTrace", compact, str);
+ this.checkPropertyName("suppressed", compact, str);
+ this.checkPropertyName("loggerFQCN", compact, str);
+ this.checkPropertyName("endOfBatch", compact, str);
+ if (includeContext) {
+ this.checkPropertyName("contextMap", compact, str);
+ }
+ this.checkPropertyName("contextStack", compact, str);
+ if (includeSource) {
+ this.checkPropertyName("source", compact, str);
+ }
+ // check some attrs
+ this.checkProperty("loggerFQCN", "f.q.c.n", compact, str);
+ this.checkProperty("loggerName", "a.B", compact, str);
+ }
@Test
public void testContentType() {
- final JSONLayout layout = JSONLayout.createLayout(null, null, null, null, null);
+ final AbstractJacksonLayout layout = JSONLayout.createLayout(null, null, null, null, null);
assertEquals("application/json; charset=UTF-8", layout.getContentType());
}
@Test
public void testDefaultCharset() {
- final JSONLayout layout = JSONLayout.createLayout(null, null, null, null, null);
+ final AbstractJacksonLayout layout = JSONLayout.createLayout(null, null, null, null, null);
assertEquals(Charsets.UTF_8, layout.getCharset());
}
- /**
- * Test case for MDC conversion pattern.
- */
@Test
- public void testLayout() throws Exception {
- final Map<String, Appender> appenders = root.getAppenders();
- for (Appender appender : appenders.values()) {
- root.removeAppender(appender);
+ public void testEscapeLayout() throws Exception {
+ final Map<String, Appender> appenders = this.rootLogger.getAppenders();
+ for (final Appender appender : appenders.values()) {
+ this.rootLogger.removeAppender(appender);
}
// set up appender
- final JSONLayout layout = JSONLayout.createLayout("true", "true", "true", "false", null);
+ final AbstractJacksonLayout layout = JSONLayout.createLayout("true", "true", "true", "false", null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
- this.root.addAppender(appender);
- this.root.setLevel(Level.DEBUG);
+ this.rootLogger.addAppender(appender);
+ this.rootLogger.setLevel(Level.DEBUG);
// output starting message
- this.root.debug("starting mdc pattern test");
-
- this.root.debug("empty mdc");
-
- ThreadContext.put("key1", "value1");
- ThreadContext.put("key2", "value2");
-
- this.root.debug("filled mdc");
-
- ThreadContext.remove("key1");
- ThreadContext.remove("key2");
-
- this.root.error("finished mdc pattern test", new NullPointerException("test"));
+ this.rootLogger.debug("Here is a quote ' and then a double quote \"");
appender.stop();
final List<String> list = appender.getMessages();
- // System.out.println(list);
- // [[, {, "logger":"root",, "timestamp":"1376676700199",, "level":"DEBUG",, "thread":"main",,
- // "message":"starting mdc pattern test",, "LocationInfo":{,
- // "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
- // "file":"JSONLayoutTest.java",, "line":"87", }, },, {, "logger":"root",, "timestamp":"1376676700203",,
- // "level":"DEBUG",, "thread":"main",, "message":"empty mdc",, "LocationInfo":{,
- // "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
- // "file":"JSONLayoutTest.java",, "line":"89", }, },, {, "logger":"root",, "timestamp":"1376676700204",,
- // "level":"DEBUG",, "thread":"main",, "message":"filled mdc",, "LocationInfo":{,
- // "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
- // "file":"JSONLayoutTest.java",, "line":"94", },, "Properties":[, {, "name":"key2",, "value":"value2", },, {,
- // "name":"key1",, "value":"value1", }, ], },, {, "logger":"root",, "timestamp":"1376676700204",,
- // "level":"ERROR",, "thread":"main",, "message":"finished mdc pattern test",,
- // "throwable":"java.lang.NullPointerException: test\\n\\tat org.apache.logging.log4j.core.layout.JSONLayoutTest.testLayout(JSONLayoutTest.java:99)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\\n\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n\\tat java.lang.reflect.Method.invoke(Method.java:606)\\n\\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)\\n\\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)\\n\\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)\\n\\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)\\n\\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)\\n\\tat or
g.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\\n\\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)\\n\\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)\\n\\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)\\n\\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)\\n\\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)\\n\\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\\n\\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)\\n\\tat org.junit.runners.ParentRunner.run(ParentRunner.java:309)\\n\\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\\n\\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\\n\\tat org.eclipse.jdt.internal.jun
it.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\\n",,
- // "LocationInfo":{, "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
- // "file":"JSONLayoutTest.java",, "line":"99", }, },, ]]
-
this.checkAt("[", 0, list);
this.checkAt("{", 1, list);
- this.checkAt("\"logger\":\"root\",", 2, list);
- this.checkAt("\"level\":\"DEBUG\",", 4, list);
- this.checkAt("\"message\":\"starting mdc pattern test\",", 6, list);
- this.checkAt("\"Properties\": {", 41, list);
- this.checkAt("\"key2\": \"value2\"", list, 42, 43);
- for (Appender app : appenders.values()) {
- root.addAppender(app);
+ this.checkContains("\"level\" : \"DEBUG\",", list);
+ this.checkContains("\"message\" : \"Here is a quote ' and then a double quote \\\"\",", list);
+ this.checkContains("\"loggerFQCN\" : \"org.apache.logging.log4j.spi.AbstractLoggerProvider\",", list);
+ for (final Appender app : appenders.values()) {
+ this.rootLogger.addAppender(app);
}
}
+ /**
+ * Test case for MDC conversion pattern.
+ */
@Test
- public void testEscapeLayout() throws Exception {
- final Map<String, Appender> appenders = root.getAppenders();
- for (Appender appender : appenders.values()) {
- root.removeAppender(appender);
+ public void testLayout() throws Exception {
+ final Map<String, Appender> appenders = this.rootLogger.getAppenders();
+ for (final Appender appender : appenders.values()) {
+ this.rootLogger.removeAppender(appender);
}
// set up appender
- final JSONLayout layout = JSONLayout.createLayout("true", "true", "true", "false", null);
+ final AbstractJacksonLayout layout = JSONLayout.createLayout("true", "true", "true", "false", null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
- this.root.addAppender(appender);
- this.root.setLevel(Level.DEBUG);
+ this.rootLogger.addAppender(appender);
+ this.rootLogger.setLevel(Level.DEBUG);
// output starting message
- this.root.debug("Here is a quote ' and then a double quote \"");
+ this.rootLogger.debug("starting mdc pattern test");
+
+ this.rootLogger.debug("empty mdc");
+
+ ThreadContext.put("key1", "value1");
+ ThreadContext.put("key2", "value2");
+
+ this.rootLogger.debug("filled mdc");
+
+ ThreadContext.remove("key1");
+ ThreadContext.remove("key2");
+
+ this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
@@ -161,26 +238,37 @@ public class JSONLayoutTest {
this.checkAt("[", 0, list);
this.checkAt("{", 1, list);
- this.checkAt("\"logger\":\"root\",", 2, list);
- this.checkAt("\"level\":\"DEBUG\",", 4, list);
- this.checkAt("\"message\":\"Here is a quote ' and then a double quote \\\"\",", 6, list);
- for (Appender app : appenders.values()) {
- root.addAppender(app);
+ this.checkContains("\"loggerFQCN\" : \"org.apache.logging.log4j.spi.AbstractLoggerProvider\",", list);
+ this.checkContains("\"level\" : \"DEBUG\",", list);
+ this.checkContains("\"message\" : \"starting mdc pattern test\",", list);
+ for (final Appender app : appenders.values()) {
+ this.rootLogger.addAppender(app);
}
}
- private void checkAt(String expected, int lineIndex, List<String> list) {
- final String trimmedLine = list.get(lineIndex).trim();
- assertTrue("Incorrect line index " + lineIndex + ": \"" + trimmedLine + '"', trimmedLine.equals(expected));
+ @Test
+ public void testLayoutLoggerName() throws Exception {
+ final AbstractJacksonLayout layout = JSONLayout.createLayout("false", null, "false", "true", "UTF-8");
+ final Log4jLogEvent expected = Log4jLogEvent.createEvent("a.B", null, "f.q.c.n", Level.DEBUG, new SimpleMessage("M"), null, null,
+ null, "threadName", null, 1);
+ final String str = layout.toSerializable(expected);
+ assertTrue(str, str.contains("\"loggerName\":\"a.B\""));
+ final Log4jLogEvent actual = new Log4jJsonObjectMapper().readValue(str, Log4jLogEvent.class);
+ assertEquals(expected.getLoggerName(), actual.getLoggerName());
+ assertEquals(expected, actual);
}
- private void checkAt(String expected, List<String> list, int... lineIndexes) {
- for (int lineIndex : lineIndexes) {
- final String trimmedLine = list.get(lineIndex).trim();
- if (trimmedLine.startsWith(expected)) {
- return;
- }
- }
- fail("Incorrect lines. Expected: " + expected);
+ @Test
+ public void testLocationOffCompactOffMdcOff() throws Exception {
+ this.testAllFeatures(false, false, false);
+ }
+
+ @Test
+ public void testLocationOnCompactOnMdcOn() throws Exception {
+ this.testAllFeatures(true, true, true);
+ }
+
+ private String toPropertySeparator(final boolean compact) {
+ return compact ? ":" : " : ";
}
}
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java?rev=1594294&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java Tue May 13 17:36:12 2014
@@ -0,0 +1,94 @@
+/*
+ * 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.logging.log4j.core.layout;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.helpers.Throwables;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.impl.ThrowableProxy;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.spi.DefaultThreadContextStack;
+
+class LogEventFixtures {
+
+ /**
+ * @return a log event that uses all the bells and whistles, features, nooks and crannies
+ */
+ static Log4jLogEvent createLogEvent() {
+ final Marker cMarker = MarkerManager.getMarker("Marker1");
+ final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1");
+ final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2");
+ final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker");
+ final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker");
+ cMarker.addParents(pMarker1);
+ cMarker.addParents(gfMarker);
+ pMarker1.addParents(gmMarker);
+ pMarker1.addParents(gfMarker);
+ final Exception sourceHelper = new Exception();
+ sourceHelper.fillInStackTrace();
+ final Exception cause = new NullPointerException("testNPEx");
+ sourceHelper.fillInStackTrace();
+ final StackTraceElement source = sourceHelper.getStackTrace()[0];
+ final IOException ioException = new IOException("testIOEx", cause);
+ Throwables.addSuppressed(ioException, new IndexOutOfBoundsException("I am suppressed exception 1"));
+ Throwables.addSuppressed(ioException, new IndexOutOfBoundsException("I am suppressed exception 2"));
+ final ThrowableProxy throwable = new ThrowableProxy(ioException);
+ final Map<String, String> contextMap = new HashMap<String, String>();
+ contextMap.put("MDC.A", "A_Value");
+ contextMap.put("MDC.B", "B_Value");
+ final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
+ contextStack.clear();
+ contextStack.push("stack_msg1");
+ contextStack.add("stack_msg2");
+ final Log4jLogEvent expected = Log4jLogEvent.createEvent("a.B", cMarker, "f.q.c.n", Level.DEBUG, new SimpleMessage("Msg"),
+ throwable, contextMap, contextStack, "MyThreadName", source, 1);
+ // validate event?
+ return expected;
+ }
+
+ static void assertEqualLogEvents(final LogEvent expected, final LogEvent actual, final boolean includeSource, final boolean includeContext) {
+ assertEquals(expected.getClass(), actual.getClass());
+ assertEquals(includeContext ? expected.getContextMap() : Collections.EMPTY_MAP, actual.getContextMap());
+ assertEquals(expected.getContextStack(), actual.getContextStack());
+ assertEquals(expected.getLevel(), actual.getLevel());
+ assertEquals(expected.getLoggerName(), actual.getLoggerName());
+ assertEquals(expected.getLoggerFQCN(), actual.getLoggerFQCN());
+ assertEquals(expected.getMarker(), actual.getMarker());
+ assertEquals(expected.getMessage(), actual.getMessage());
+ assertEquals(expected.getTimeMillis(), actual.getTimeMillis());
+ assertEquals(includeSource ? expected.getSource() : null, actual.getSource());
+ assertEquals(expected.getThreadName(), actual.getThreadName());
+ assertEquals(expected.getThrownProxy(), actual.getThrownProxy());
+ assertEquals(expected.isEndOfBatch(), actual.isEndOfBatch());
+ assertEquals(expected.isIncludeLocation(), actual.isIncludeLocation());
+ if (includeSource) {
+ assertEquals(expected.hashCode(), actual.hashCode());
+ assertEquals(expected, actual);
+ }
+ }
+
+}
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/LogEventFixtures.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/XMLLayoutTest.java Tue May 13 17:36:12 2014
@@ -17,8 +17,10 @@
package org.apache.logging.log4j.core.layout;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
import java.util.List;
import java.util.Map;
@@ -33,26 +35,35 @@ import org.apache.logging.log4j.core.Log
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.helpers.Charsets;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
+import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
+import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+
/**
- *
+ * Tests {@link XMLLayout}.
*/
public class XMLLayoutTest {
- private static final String body = "<Message><![CDATA[empty mdc]]></Message>";
- private static final String markerTag = "<Marker>EVENT</Marker>";
+ private static final String body = "<Message>empty mdc</Message>";
static ConfigurationFactory cf = new BasicConfigurationFactory();
+ private static final String markerTag = "<Marker name=\"EVENT\"/>";
@AfterClass
public static void cleanupClass() {
ConfigurationFactory.removeConfigurationFactory(cf);
+ ThreadContext.clearAll();
}
@BeforeClass
public static void setupClass() {
+ ThreadContext.clearAll();
ConfigurationFactory.setConfigurationFactory(cf);
final LoggerContext ctx = (LoggerContext) LogManager.getContext();
ctx.reconfigure();
@@ -60,17 +71,126 @@ public class XMLLayoutTest {
LoggerContext ctx = (LoggerContext) LogManager.getContext();
- Logger root = ctx.getLogger("");
+ Logger rootLogger = this.ctx.getLogger("");
+
+ private void checkAttribute(final String name, final String value, final boolean compact, final String str) {
+ Assert.assertTrue(str, str.contains(name + "=\"" + value + "\""));
+ }
+
+ private void checkAttributeName(final String name, final boolean compact, final String str) {
+ Assert.assertTrue(str, str.contains(name + "=\""));
+ }
+
+ private void checkContains(final String expected, final List<String> list) {
+ for (final String string : list) {
+ final String trimedLine = string.trim();
+ if (trimedLine.contains(expected)) {
+ return;
+ }
+ }
+ Assert.fail("Cannot find " + expected + " in " + list);
+ }
+
+ private void checkElement(final String key, final String value, final boolean compact, final String str) {
+ // <item key="MDC.A" value="A_Value"/>
+ assertTrue(str, str.contains(String.format("<item key=\"%s\" value=\"%s\"/>", key, value)));
+ }
+
+ private void checkElementName(final String name, final boolean compact, final String str, final boolean withAttributes,
+ final boolean withChildren) {
+ // simple checks, don't try to be too smart here, we're just looking for the names and basic shape.
+ // start
+ final String startStr = withAttributes ? "<" + name + " " : "<" + name + ">";
+ final int startPos = str.indexOf(startStr);
+ Assert.assertTrue(str, startPos >= 0);
+ // end
+ final String endStr = withChildren ? "</" + name + ">" : "/>";
+ final int endPos = str.indexOf(endStr, startPos + startStr.length());
+ Assert.assertTrue(str, endPos >= 0);
+ }
+
+ /**
+ * @param includeSource TODO
+ * @param compact
+ * @param includeContext TODO
+ * @throws IOException
+ * @throws JsonParseException
+ * @throws JsonMappingException
+ */
+ private void testAllFeatures(final boolean includeSource, final boolean compact, final boolean includeContext) throws IOException,
+ JsonParseException, JsonMappingException {
+ final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
+ final XMLLayout layout = XMLLayout.createLayout(Boolean.toString(includeSource), Boolean.toString(includeContext), "false",
+ Boolean.toString(compact), "UTF-8");
+ final String str = layout.toSerializable(expected);
+ assertEquals(str, !compact, str.contains("\n"));
+ assertEquals(str, includeSource, str.contains("Source"));
+ assertEquals(str, includeContext, str.contains("ContextMap"));
+ final Log4jLogEvent actual = new Log4jXmlObjectMapper().readValue(str, Log4jLogEvent.class);
+ LogEventFixtures.assertEqualLogEvents(expected, actual, includeSource, includeContext);
+ if (includeContext) {
+ this.checkElement("MDC.A", "A_Value", compact, str);
+ this.checkElement("MDC.B", "B_Value", compact, str);
+ }
+
+ //
+ assertNull(actual.getThrown());
+ // check some attrs
+ assertTrue(str, str.contains("loggerFQCN=\"f.q.c.n\""));
+ assertTrue(str, str.contains("loggerName=\"a.B\""));
+ // make sure short names are used
+ assertTrue(str, str.contains("<Event "));
+ assertTrue(str, str.contains("class="));
+ assertTrue(str, str.contains("method="));
+ assertTrue(str, str.contains("file="));
+ assertTrue(str, str.contains("line="));
+ //
+ // make sure the names we want are used
+ this.checkAttributeName("timeMillis", compact, str);
+ this.checkAttributeName("thread", compact, str); // and not threadName
+ this.checkAttributeName("level", compact, str);
+ this.checkAttributeName("loggerName", compact, str);
+ this.checkElementName("Marker", compact, str, true, true);
+ this.checkAttributeName("name", compact, str);
+ this.checkElementName("Parents", compact, str, false, true);
+ this.checkElementName("Message", compact, str, false, true);
+ this.checkElementName("Thrown", compact, str, true, true);
+ this.checkElementName("Cause", compact, str, true, true);
+ this.checkAttributeName("class", compact, str);
+ this.checkAttributeName("method", compact, str);
+ this.checkAttributeName("file", compact, str);
+ this.checkAttributeName("line", compact, str);
+ this.checkAttributeName("exact", compact, str);
+ this.checkAttributeName("location", compact, str);
+ this.checkAttributeName("version", compact, str);
+ this.checkAttributeName("commonElementCount", compact, str);
+ this.checkAttributeName("message", compact, str);
+ this.checkAttributeName("localizedMessage", compact, str);
+ this.checkElementName("ExtendedStackTrace", compact, str, false, true);
+ this.checkElementName("Suppressed", compact, str, false, true);
+ this.checkAttributeName("loggerFQCN", compact, str);
+ this.checkAttributeName("endOfBatch", compact, str);
+ if (includeContext) {
+ this.checkElementName("ContextMap", compact, str, false, true);
+ }
+ this.checkElementName("ContextStack", compact, str, false, true);
+ if (includeSource) {
+ this.checkElementName("Source", compact, str, true, false);
+ }
+ // check some attrs
+ this.checkAttribute("loggerFQCN", "f.q.c.n", compact, str);
+ this.checkAttribute("loggerName", "a.B", compact, str);
+ }
@Test
public void testContentType() {
- final XMLLayout layout = XMLLayout.createLayout(null, null, null, null, null, null);
+ final XMLLayout layout = XMLLayout.createLayout(null, null, null, null, null);
assertEquals("text/xml; charset=UTF-8", layout.getContentType());
}
@Test
public void testDefaultCharset() {
- final XMLLayout layout = XMLLayout.createLayout(null, null, null, null, null, null);
+ final XMLLayout layout = XMLLayout.createLayout(null, null, null, null, null);
assertEquals(Charsets.UTF_8, layout.getCharset());
}
@@ -79,51 +199,74 @@ public class XMLLayoutTest {
*/
@Test
public void testLayout() throws Exception {
- final Map<String, Appender> appenders = root.getAppenders();
- for (Appender appender : appenders.values()) {
- root.removeAppender(appender);
+ final Map<String, Appender> appenders = this.rootLogger.getAppenders();
+ for (final Appender appender : appenders.values()) {
+ this.rootLogger.removeAppender(appender);
}
// set up appender
- final XMLLayout layout = XMLLayout.createLayout("true", "true", "true", null, null, null);
+ final XMLLayout layout = XMLLayout.createLayout("true", "true", "true", null, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
- root.addAppender(appender);
- root.setLevel(Level.DEBUG);
+ this.rootLogger.addAppender(appender);
+ this.rootLogger.setLevel(Level.DEBUG);
// output starting message
- root.debug("starting mdc pattern test");
+ this.rootLogger.debug("starting mdc pattern test");
- root.debug("empty mdc");
+ this.rootLogger.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
- root.debug("filled mdc");
+ this.rootLogger.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
- root.error("finished mdc pattern test", new NullPointerException("test"));
+ this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
final Marker marker = MarkerManager.getMarker("EVENT");
- root.error(marker, "marker test");
+ this.rootLogger.error(marker, "marker test");
appender.stop();
final List<String> list = appender.getMessages();
- assertTrue("Incorrect number of lines. Require at least 50 " + list.size(), list.size() > 50);
final String string = list.get(0);
assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</Events>"));
- assertTrue("Incorrect body. Expected " + body + " Actual: " + list.get(7), list.get(7).trim().equals(body));
+ this.checkContains("loggerFQCN=\"org.apache.logging.log4j.spi.AbstractLoggerProvider\"", list);
+ this.checkContains("level=\"DEBUG\"", list);
+ this.checkContains(">starting mdc pattern test</Message>", list);
+ // this.checkContains("<Message>starting mdc pattern test</Message>", list);
+
+ // <Marker xmlns="" _class="org.apache.logging.log4j.MarkerManager..Log4jMarker" name="EVENT"/>
+ this.checkContains("<Marker", list);
+ this.checkContains("name=\"EVENT\"/>", list);
- assertTrue("Missing Marker-Tag, Expected " + markerTag + ", Actual" + list.get(list.size() - 4),
- list.get(list.size() - 4).contains(markerTag));
- for (Appender app : appenders.values()) {
- root.addAppender(app);
+ for (final Appender app : appenders.values()) {
+ this.rootLogger.addAppender(app);
}
}
+
+ @Test
+ public void testLayoutLoggerName() {
+ final XMLLayout layout = XMLLayout.createLayout("false", "true", "true", null, null);
+ final Log4jLogEvent event = Log4jLogEvent.createEvent("a.B", null, "f.q.c.n", Level.DEBUG, new SimpleMessage("M"), null, null,
+ null, "threadName", null, 1);
+ final String str = layout.toSerializable(event);
+ assertTrue(str, str.contains("loggerName=\"a.B\""));
+ }
+
+ @Test
+ public void testLocationOffCompactOffMdcOff() throws Exception {
+ this.testAllFeatures(false, false, false);
+ }
+
+ @Test
+ public void testLocationOnCompactOnMdcOn() throws Exception {
+ this.testAllFeatures(true, true, true);
+ }
}
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java (from r1591291, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java&r1=1591291&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/AbstractSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,8 +14,9 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -42,6 +43,7 @@ import org.apache.logging.log4j.core.lay
import org.apache.logging.log4j.core.layout.XMLLayout;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.After;
+import org.junit.Ignore;
import org.junit.Test;
/**
@@ -61,20 +63,24 @@ public abstract class AbstractSocketServ
}
}
- private static final String MESSAGE_1 = "This is a test message";
+ private static final String MESSAGE = "This is test message";
private static final String MESSAGE_2 = "This is test message 2";
+ static final String PORT = "8199";
+
+ static final int PORT_NUM = Integer.parseInt(PORT);
+
private final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
-
+
private final boolean expectLengthException;
private final String port;
private final String protocol;
-
+
private final Logger root = ctx.getLogger(AbstractSocketServerTest.class.getSimpleName());
-
+
protected AbstractSocketServerTest(final String protocol, final String port, final boolean expectLengthException) {
this.protocol = protocol;
this.port = port;
@@ -82,7 +88,7 @@ public abstract class AbstractSocketServ
}
protected Layout<String> createJsonLayout() {
- return JSONLayout.createLayout("true", "true", "true", "false", null);
+ return JSONLayout.createLayout("true", "true", "false", "false", null);
}
protected abstract Layout<? extends Serializable> createLayout();
@@ -92,20 +98,47 @@ public abstract class AbstractSocketServ
}
protected Layout<String> createXmlLayout() {
- return XMLLayout.createLayout("true", "true", "true", null, null, null);
+ return XMLLayout.createLayout("true", "true", "false", null, null);
}
@After
public void tearDown() {
final Map<String, Appender> map = root.getAppenders();
for (final Map.Entry<String, Appender> entry : map.entrySet()) {
- final Appender app = entry.getValue();
- root.removeAppender(app);
- app.stop();
+ final Appender appender = entry.getValue();
+ root.removeAppender(appender);
+ appender.stop();
}
}
@Test
+ @Ignore("Broken test?")
+ public void test1000ShortMessages() throws Exception {
+ testServer(1000);
+ }
+
+ @Test
+ @Ignore("Broken test?")
+ public void test100ShortMessages() throws Exception {
+ testServer(100);
+ }
+
+ @Test
+ public void test10ShortMessages() throws Exception {
+ testServer(10);
+ }
+
+ @Test
+ public void test1ShortMessages() throws Exception {
+ testServer(1);
+ }
+
+ @Test
+ public void test2ShortMessages() throws Exception {
+ testServer(2);
+ }
+
+ @Test
public void test64KMessages() throws Exception {
final char[] a64K = new char[1024 * 64];
Arrays.fill(a64K, 'a');
@@ -114,7 +147,6 @@ public abstract class AbstractSocketServ
if (expectLengthException) {
try {
testServer(m1, m2);
-
} catch (final AppenderLoggingException are) {
assertTrue("", are.getCause() != null && are.getCause() instanceof IOException);
// Failure expected.
@@ -124,17 +156,26 @@ public abstract class AbstractSocketServ
}
}
- protected void testServer(final String message1, final String message2) throws Exception {
+ private void testServer(final int size) throws Exception {
+ final String[] messages = new String[size];
+ for (int i = 0; i < messages.length; i++) {
+ messages[i] = MESSAGE + " " + i;
+ }
+ testServer(messages);
+ }
+
+ protected void testServer(final String... messages) throws Exception {
final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final Layout<? extends Serializable> socketLayout = createLayout();
- final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1", null, "Test", null,
- "false", socketLayout, socketFilter, null, null);
+ final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1",
+ null, "Test", null, "false", socketLayout, socketFilter, null, null);
appender.start();
final ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null, null, null, null);
- final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
+ final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false",
+ "true");
final Logger serverLogger = ctx.getLogger(this.getClass().getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
@@ -144,20 +185,16 @@ public abstract class AbstractSocketServ
root.addAppender(listApp);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
- root.debug(message1);
- root.debug(message2);
- Thread.sleep(100);
+ for (final String message : messages) {
+ root.debug(message);
+ }
+ Thread.sleep(2000);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
- assertTrue("No events retrieved", events.size() > 0);
- assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1));
- assertTrue("Incorrect number of events received: " + events.size(), events.size() == 2);
- assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2));
- }
-
- @Test
- public void testShortMessages() throws Exception {
- testServer(MESSAGE_1, MESSAGE_2);
+ assertEquals("Incorrect number of events received", messages.length, events.size());
+ for (int i = 0; i < messages.length; i++) {
+ assertTrue("Incorrect event", events.get(i).getMessage().getFormattedMessage().equals(messages[i]));
+ }
}
}
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpJsonSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpJsonSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpJsonSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpJsonSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpJsonSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpJsonSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpJsonSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,8 +14,9 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
+import java.io.IOException;
import java.io.InputStream;
import org.apache.logging.log4j.LogManager;
@@ -25,10 +26,8 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
-@Ignore("Not implemented yet")
+//@Ignore("Not implemented yet")
public class TcpJsonSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8198";
- private static final int PORT_NUM = Integer.parseInt(PORT);
private static TCPSocketServer<InputStream> tcpSocketServer;
private static Thread thread;
@@ -43,7 +42,11 @@ public class TcpJsonSocketServerTest ext
@AfterClass
public static void tearDownClass() {
- tcpSocketServer.shutdown();
+ try {
+ tcpSocketServer.shutdown();
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
try {
thread.join();
} catch (final InterruptedException e) {
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpSerializedSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpSerializedSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpSerializedSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpSerializedSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpSerializedSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpSerializedSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpSerializedSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,8 +14,9 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
+import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
@@ -26,8 +27,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
public class TcpSerializedSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8198";
- private static final int PORT_NUM = Integer.parseInt(PORT);
private static TCPSocketServer<ObjectInputStream> tcpSocketServer;
private static Thread thread;
@@ -42,7 +41,11 @@ public class TcpSerializedSocketServerTe
@AfterClass
public static void tearDownClass() {
- tcpSocketServer.shutdown();
+ try {
+ tcpSocketServer.shutdown();
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
try {
thread.join();
} catch (final InterruptedException e) {
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpXmlSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpXmlSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpXmlSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpXmlSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpXmlSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/TcpXmlSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/TcpXmlSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,36 +14,39 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
+import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.Charset;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.AfterClass;
import org.junit.BeforeClass;
-import org.junit.Ignore;
-@Ignore("Not implemented yet")
public class TcpXmlSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8198";
- private static final int PORT_NUM = Integer.parseInt(PORT);
- private static TCPSocketServer<InputStream> tcpSocketServer;
-
+ private static TCPSocketServer<InputStream> socketServer;
private static Thread thread;
@BeforeClass
public static void setupClass() throws Exception {
((LoggerContext) LogManager.getContext(false)).reconfigure();
- tcpSocketServer = TCPSocketServer.createXmlSocketServer(PORT_NUM);
- thread = new Thread(tcpSocketServer);
+ // Use a large buffer just to test the code, the UDP test uses a tiny buffer
+ socketServer = new TCPSocketServer<InputStream>(PORT_NUM, new XmlInputStreamLogEventBridge(1024 * 100,
+ Charset.defaultCharset()));
+ thread = new Thread(socketServer);
thread.start();
}
@AfterClass
public static void tearDownClass() {
- tcpSocketServer.shutdown();
+ try {
+ socketServer.shutdown();
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
try {
thread.join();
} catch (final InterruptedException e) {
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpJsonSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpJsonSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpJsonSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpJsonSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpJsonSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpJsonSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpJsonSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,7 +14,7 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
import java.io.InputStream;
import java.io.Serializable;
@@ -26,10 +26,8 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
-@Ignore("Not implemented yet")
+//@Ignore("Not implemented yet")
public class UdpJsonSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8199";
- private static final int PORT_NUM = Integer.parseInt(PORT);
private static Thread thread;
private static UDPSocketServer<InputStream> udpSocketServer;
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpSerializedSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpSerializedSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpSerializedSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpSerializedSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpSerializedSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpSerializedSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpSerializedSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,7 +14,7 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
import java.io.ObjectInputStream;
import java.io.Serializable;
@@ -26,8 +26,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
public class UdpSerializedSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8199";
- private static final int PORT_NUM = Integer.parseInt(PORT);
private static Thread thread;
private static UDPSocketServer<ObjectInputStream> udpSocketServer;
Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpXmlSocketServerTest.java (from r1583378, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpXmlSocketServerTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpXmlSocketServerTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpXmlSocketServerTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpXmlSocketServerTest.java&r1=1583378&r2=1594294&rev=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/UdpXmlSocketServerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/UdpXmlSocketServerTest.java Tue May 13 17:36:12 2014
@@ -14,36 +14,35 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.core.net;
+package org.apache.logging.log4j.core.net.server;
import java.io.InputStream;
import java.io.Serializable;
+import java.nio.charset.Charset;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.AfterClass;
import org.junit.BeforeClass;
-import org.junit.Ignore;
-@Ignore("Not implemented yet")
public class UdpXmlSocketServerTest extends AbstractSocketServerTest {
- private static final String PORT = "8199";
- private static final int PORT_NUM = Integer.parseInt(PORT);
+ private static UDPSocketServer<InputStream> socketServer;
private static Thread thread;
- private static UDPSocketServer<InputStream> udpSocketServer;
@BeforeClass
public static void setupClass() throws Exception {
((LoggerContext) LogManager.getContext(false)).reconfigure();
- udpSocketServer = UDPSocketServer.createXmlSocketServer(PORT_NUM);
- thread = new Thread(udpSocketServer);
+ // Use a tiny buffer just to test the code, the TCP test uses a large buffer
+ socketServer = new UDPSocketServer<InputStream>(PORT_NUM, new XmlInputStreamLogEventBridge(100,
+ Charset.defaultCharset()));
+ thread = new Thread(socketServer);
thread.start();
}
@AfterClass
public static void tearDownClass() {
- udpSocketServer.shutdown();
+ socketServer.shutdown();
try {
thread.join();
} catch (final InterruptedException e) {
Modified: logging/log4j/log4j2/trunk/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/pom.xml?rev=1594294&r1=1594293&r2=1594294&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/pom.xml (original)
+++ logging/log4j/log4j2/trunk/pom.xml Tue May 13 17:36:12 2014
@@ -455,6 +455,18 @@
<version>${jackson2.version}</version>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-xml</artifactId>
+ <version>${jackson2.version}</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.module</groupId>
+ <artifactId>jackson-module-jaxb-annotations</artifactId>
+ <version>${jackson2.version}</version>
+ <optional>true</optional>
+ </dependency>
<!-- Jackson 2 end -->
<dependency>
<groupId>com.sun.mail</groupId>