Repository: logging-log4j2 Updated Branches: refs/heads/master ed600f7e0 -> 26fc9e079
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java index c4df884..f7794e6 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java @@ -419,7 +419,7 @@ public class JmsManager extends AbstractManager { private MapMessage map(final org.apache.logging.log4j.message.MapMessage<?, ?> log4jMapMessage, final MapMessage jmsMapMessage) { - // Map without calling rg.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. + // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. log4jMapMessage.forEach(new BiConsumer<String, Object>() { @Override public void accept(final String key, final Object value) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppender.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppender.java index c26ad8f..aae28ff 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppender.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppender.java @@ -16,14 +16,17 @@ */ package org.apache.logging.log4j.core.appender.nosql; +import java.io.Serializable; + import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender; import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.util.Booleans; /** @@ -41,39 +44,97 @@ import org.apache.logging.log4j.core.util.Booleans; */ @Plugin(name = "NoSql", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true) public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> { - private final String description; - private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions, - final NoSqlDatabaseManager<?> manager) { - super(name, filter, ignoreExceptions, manager); - this.description = this.getName() + "{ manager=" + this.getManager() + " }"; - } + /** + * Builds ConsoleAppender instances. + * + * @param <B> + * The type to build + */ + public static class Builder<B extends Builder<B>> extends AbstractAppender.Builder<B> + implements org.apache.logging.log4j.core.util.Builder<NoSqlAppender> { - @Override - public String toString() { - return this.description; + @PluginBuilderAttribute("bufferSize") + private int bufferSize; + + @PluginElement("NoSqlProvider") + private NoSqlProvider<?> provider; + + @SuppressWarnings("resource") + @Override + public NoSqlAppender build() { + final String name = getName(); + if (provider == null) { + LOGGER.error("NoSQL provider not specified for appender [{}].", name); + return null; + } + + final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSize + + ", provider=" + provider + " }"; + + final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(managerName, + bufferSize, provider); + if (manager == null) { + return null; + } + + return new NoSqlAppender(name, getFilter(), getLayout(), isIgnoreExceptions(), manager); + } + + /** + * Sets the buffer size. + * + * @param bufferSize + * If an integer greater than 0, this causes the appender to buffer log events and flush whenever the + * buffer reaches this size. + * @return this + */ + public B setBufferSize(int bufferSize) { + this.bufferSize = bufferSize; + return asBuilder(); + } + + /** + * Sets the provider. + * + * @param provider + * The NoSQL provider that provides connections to the chosen NoSQL database. + * @return this + */ + public B setProvider(NoSqlProvider<?> provider) { + this.provider = provider; + return asBuilder(); + } } /** * Factory method for creating a NoSQL appender within the plugin manager. * - * @param name The name of the appender. - * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise - * they are propagated to the caller. - * @param filter The filter, if any, to use. - * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever - * the buffer reaches this size. - * @param provider The NoSQL provider that provides connections to the chosen NoSQL database. + * @param name + * The name of the appender. + * @param ignore + * If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they + * are propagated to the caller. + * @param filter + * The filter, if any, to use. + * @param bufferSize + * If an integer greater than 0, this causes the appender to buffer log events and flush whenever the + * buffer reaches this size. + * @param provider + * The NoSQL provider that provides connections to the chosen NoSQL database. * @return a new NoSQL appender. + * @deprecated since 2.10.1; use {@link Builder}. */ @SuppressWarnings("resource") - @PluginFactory + @Deprecated public static NoSqlAppender createAppender( - @PluginAttribute("name") final String name, - @PluginAttribute("ignoreExceptions") final String ignore, - @PluginElement("Filter") final Filter filter, - @PluginAttribute("bufferSize") final String bufferSize, - @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) { + // @formatter:off + final String name, + final String ignore, + final Filter filter, + final String bufferSize, + final NoSqlProvider<?> provider) { + // @formatter:on if (provider == null) { LOGGER.error("NoSQL provider not specified for appender [{}].", name); return null; @@ -82,16 +143,33 @@ public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseM final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); - final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt - + ", provider=" + provider + " }"; + final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt + ", provider=" + + provider + " }"; - final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager( - managerName, bufferSizeInt, provider - ); + final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(managerName, bufferSizeInt, + provider); if (manager == null) { return null; } - return new NoSqlAppender(name, filter, ignoreExceptions, manager); + return new NoSqlAppender(name, filter, null, ignoreExceptions, manager); + } + + @PluginBuilderFactory + public static <B extends Builder<B>> B newBuilder() { + return new Builder<B>().asBuilder(); + } + + private final String description; + + private NoSqlAppender(final String name, final Filter filter, Layout<? extends Serializable> layout, + final boolean ignoreExceptions, final NoSqlDatabaseManager<?> manager) { + super(name, filter, layout, ignoreExceptions, manager); + this.description = this.getName() + "{ manager=" + this.getManager() + " }"; + } + + @Override + public String toString() { + return this.description; } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java index a0a29ce..361ae2b 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManager.java @@ -16,6 +16,10 @@ */ package org.apache.logging.log4j.core.appender.nosql; +import java.io.Serializable; + +import javax.jms.JMSException; + import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.LogEvent; @@ -23,6 +27,7 @@ import org.apache.logging.log4j.core.appender.AppenderLoggingException; import org.apache.logging.log4j.core.appender.ManagerFactory; import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager; import org.apache.logging.log4j.core.util.Closer; +import org.apache.logging.log4j.message.MapMessage; import org.apache.logging.log4j.util.BiConsumer; import org.apache.logging.log4j.util.ReadOnlyStringMap; @@ -64,14 +69,40 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { } } + @Deprecated @Override protected void writeInternal(final LogEvent event) { + writeInternal(event, null); + } + + @Override + protected void writeInternal(final LogEvent event, final Serializable serializable) { if (!this.isRunning() || this.connection == null || this.connection.isClosed()) { throw new AppenderLoggingException( "Cannot write logging event; NoSQL manager not connected to the database."); } final NoSqlObject<W> entity = this.connection.createObject(); + if (serializable instanceof MapMessage) { + setFields((MapMessage<?, ?>) serializable, entity); + } else { + setFields(event, entity); + } + + this.connection.insertObject(entity); + } + + private void setFields(final MapMessage<?, ?> mapMessage, final NoSqlObject<W> noSqlObject) { + // Map without calling org.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. + mapMessage.forEach(new BiConsumer<String, Object>() { + @Override + public void accept(final String key, final Object value) { + noSqlObject.set(key, value); + } + }); + } + + private void setFields(final LogEvent event, final NoSqlObject<W> entity) { entity.set("level", event.getLevel()); entity.set("loggerName", event.getLoggerName()); entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage()); @@ -139,8 +170,6 @@ public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager { } else { entity.set("contextStack", contextStack.asList().toArray()); } - - this.connection.insertObject(entity); } private NoSqlObject<W> buildMarkerEntity(final Marker marker) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java index cf33cea..c21311a 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java @@ -95,14 +95,14 @@ public class AbstractDatabaseAppenderTest { appender.append(event1); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event1)); + then(manager).should().writeInternal(same(event1), null); then(manager).should().commitAndClose(); reset(manager); appender.append(event2); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event2)); + then(manager).should().writeInternal(same(event2), null); then(manager).should().commitAndClose(); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java index 41e7dd8..26462c3 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java @@ -24,6 +24,8 @@ import static org.mockito.ArgumentMatchers.same; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.*; +import java.io.Serializable; + public class AbstractDatabaseManagerTest { private AbstractDatabaseManager manager; @@ -89,21 +91,21 @@ public class AbstractDatabaseManagerTest { then(manager).should().startupInternal(); reset(manager); - manager.write(event1); + manager.write(event1, null); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event1)); + then(manager).should().writeInternal(same(event1), null); then(manager).should().commitAndClose(); reset(manager); - manager.write(event2); + manager.write(event2, null); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event2)); + then(manager).should().writeInternal(same(event2), null); then(manager).should().commitAndClose(); reset(manager); - manager.write(event3); + manager.write(event3, null); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event3)); + then(manager).should().writeInternal(same(event3), null); then(manager).should().commitAndClose(); then(manager).shouldHaveNoMoreInteractions(); } @@ -130,16 +132,16 @@ public class AbstractDatabaseManagerTest { manager.startup(); then(manager).should().startupInternal(); - manager.write(event1); - manager.write(event2); - manager.write(event3); - manager.write(event4); + manager.write(event1, null); + manager.write(event2, null); + manager.write(event3, null); + manager.write(event4, null); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event1copy)); - then(manager).should().writeInternal(same(event2copy)); - then(manager).should().writeInternal(same(event3copy)); - then(manager).should().writeInternal(same(event4copy)); + then(manager).should().writeInternal(same(event1copy), null); + then(manager).should().writeInternal(same(event2copy), null); + then(manager).should().writeInternal(same(event3copy), null); + then(manager).should().writeInternal(same(event4copy), null); then(manager).should().commitAndClose(); then(manager).shouldHaveNoMoreInteractions(); } @@ -163,15 +165,15 @@ public class AbstractDatabaseManagerTest { manager.startup(); then(manager).should().startupInternal(); - manager.write(event1); - manager.write(event2); - manager.write(event3); + manager.write(event1, null); + manager.write(event2, null); + manager.write(event3, null); manager.flush(); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event1copy)); - then(manager).should().writeInternal(same(event2copy)); - then(manager).should().writeInternal(same(event3copy)); + then(manager).should().writeInternal(same(event1copy), null); + then(manager).should().writeInternal(same(event2copy), null); + then(manager).should().writeInternal(same(event3copy), null); then(manager).should().commitAndClose(); then(manager).shouldHaveNoMoreInteractions(); } @@ -195,15 +197,15 @@ public class AbstractDatabaseManagerTest { manager.startup(); then(manager).should().startupInternal(); - manager.write(event1); - manager.write(event2); - manager.write(event3); + manager.write(event1, null); + manager.write(event2, null); + manager.write(event3, null); manager.shutdown(); then(manager).should().connectAndStart(); - then(manager).should().writeInternal(same(event1copy)); - then(manager).should().writeInternal(same(event2copy)); - then(manager).should().writeInternal(same(event3copy)); + then(manager).should().writeInternal(same(event1copy), null); + then(manager).should().writeInternal(same(event2copy), null); + then(manager).should().writeInternal(same(event3copy), null); then(manager).should().commitAndClose(); then(manager).should().shutdownInternal(); then(manager).shouldHaveNoMoreInteractions(); @@ -218,6 +220,7 @@ public class AbstractDatabaseManagerTest { @Override protected void startupInternal() throws Exception { + // noop } @Override @@ -227,15 +230,23 @@ public class AbstractDatabaseManagerTest { @Override protected void connectAndStart() { + // noop } + @Deprecated @Override protected void writeInternal(final LogEvent event) { + // noop } @Override + protected void writeInternal(LogEvent event, Serializable serializable) { + // noop + } + @Override protected boolean commitAndClose() { return true; } + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java index 7597cb5..772a947 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java @@ -102,7 +102,7 @@ public class NoSqlDatabaseManagerTest { try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { expectedException.expect(AppenderLoggingException.class); - manager.writeInternal(mock(LogEvent.class)); + manager.writeInternal(mock(LogEvent.class), null); } } @@ -118,7 +118,7 @@ public class NoSqlDatabaseManagerTest { then(provider).should().getConnection(); expectedException.expect(AppenderLoggingException.class); - manager.writeInternal(mock(LogEvent.class)); + manager.writeInternal(mock(LogEvent.class), null); } } @@ -145,7 +145,7 @@ public class NoSqlDatabaseManagerTest { .setTimeMillis(1234567890123L) .build(); - manager.writeInternal(event); + manager.writeInternal(event, null); then(connection).should().insertObject(captor.capture()); final NoSqlObject<Map<String, Object>> inserted = captor.getValue(); @@ -217,7 +217,7 @@ public class NoSqlDatabaseManagerTest { .setContextStack(stack) .build(); - manager.writeInternal(event); + manager.writeInternal(event, null); then(connection).should().insertObject(captor.capture()); final NoSqlObject<Map<String, Object>> inserted = captor.getValue(); @@ -318,7 +318,7 @@ public class NoSqlDatabaseManagerTest { .setContextStack(stack) .build(); - manager.writeInternal(event); + manager.writeInternal(event, null); then(connection).should().insertObject(captor.capture()); final NoSqlObject<Map<String, Object>> inserted = captor.getValue(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyRule.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyRule.java deleted file mode 100644 index 0098057..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyRule.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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.test; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -/** - * A JUnit TestRule to discover an available port and save it in a system property. Useful for setting up tests using - * Apache Active MQ. - */ -public class AvailablePortSystemPropertyRule implements TestRule { - - public static AvailablePortSystemPropertyRule create(final String name) { - return new AvailablePortSystemPropertyRule(name); - } - - protected final String name; - protected int port; - - protected AvailablePortSystemPropertyRule(final String name) { - this.name = name; - } - - @Override - public Statement apply(final Statement base, final Description description) { - return new Statement() { - - @Override - public void evaluate() throws Throwable { - final String oldValue = System.getProperty(name); - try { - port = AvailablePortFinder.getNextAvailable(); - System.setProperty(name, Integer.toString(port)); - base.evaluate(); - } finally { - // Restore if previously set - if (oldValue != null) { - System.setProperty(name, oldValue); - } - } - } - }; - } - - public String getName() { - return name; - } - - public int getPort() { - return port; - } - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("AvailablePortSystemPropertyRule [name="); - builder.append(name); - builder.append(", port="); - builder.append(port); - builder.append("]"); - return builder.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java new file mode 100644 index 0000000..1bece7b --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/AvailablePortSystemPropertyTestRule.java @@ -0,0 +1,81 @@ +/* + * 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.test; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A JUnit TestRule to discover an available port and save it in a system property. Useful for setting up tests using + * Apache Active MQ. + */ +public class AvailablePortSystemPropertyTestRule implements TestRule { + + public static AvailablePortSystemPropertyTestRule create(final String name) { + return new AvailablePortSystemPropertyTestRule(name); + } + + protected final String name; + protected int port; + + protected AvailablePortSystemPropertyTestRule(final String name) { + this.name = name; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + final String oldValue = System.getProperty(name); + try { + port = AvailablePortFinder.getNextAvailable(); + System.setProperty(name, Integer.toString(port)); + base.evaluate(); + } finally { + // Restore if previously set + if (oldValue != null) { + System.setProperty(name, oldValue); + } + } + } + }; + } + + public String getName() { + return name; + } + + public int getPort() { + return port; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("AvailablePortSystemPropertyRule [name="); + builder.append(name); + builder.append(", port="); + builder.append(port); + builder.append("]"); + return builder.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-mongodb/pom.xml b/log4j-mongodb/pom.xml index 8e484ee..2440cfc 100644 --- a/log4j-mongodb/pom.xml +++ b/log4j-mongodb/pom.xml @@ -70,6 +70,12 @@ <artifactId>log4j-slf4j-impl</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>de.flapdoodle.embed</groupId> + <artifactId>de.flapdoodle.embed.mongo</artifactId> + <version>2.0.0</version> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/Java8Test.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/Java8Test.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/Java8Test.java new file mode 100644 index 0000000..817141d --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/Java8Test.java @@ -0,0 +1,48 @@ +/* + * 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.mongodb; + +import org.apache.commons.lang3.JavaVersion; +import org.apache.commons.lang3.SystemUtils; +import org.junit.Assume; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Runs all MongoDB only on Java 8. + * <p> + * The test framework {@code de.flapdoodle.embed.mongo} requires Java 8. + * </p> + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ MongoDbTestTestRuleTestJava8.class, MongoDbAuthTestJava8.class, MongoDbCappedTestJava8.class, + MongoDbMapMessageTestJava8.class, MongoDbTestJava8.class }) +public class Java8Test { + + @BeforeClass + public static void beforeClass() { + Assume.assumeTrue(SystemUtils.JAVA_VERSION, SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)); + } + + @Test + public void test() { + // noop + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTest.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTest.java deleted file mode 100644 index 142a470..0000000 --- a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.mongodb; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.categories.Appenders; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Ignore("Requires a running MongoDB server") -@Category(Appenders.MongoDb.class) -public class MongoDbAuthTest { - - @ClassRule - public static LoggerContextRule context = new LoggerContextRule("log4j2-mongodb-auth.xml"); - - @Test - public void test() { - final Logger logger = LogManager.getLogger(); - logger.info("Hello log"); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTestJava8.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTestJava8.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTestJava8.java new file mode 100644 index 0000000..64f51a2 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthTestJava8.java @@ -0,0 +1,52 @@ +/* + * 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.mongodb; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.Appenders; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyTestRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.RuleChain; + +/** + * This class name does NOT end in "Test" in order to only be picked up by {@link Java8Test}. + */ +@Category(Appenders.MongoDb.class) +public class MongoDbAuthTestJava8 { + + static LoggerContextRule loggerContextTestRule = new LoggerContextRule("log4j2-mongodb-auth.xml"); + + static final AvailablePortSystemPropertyTestRule mongoDbPortTestRule = AvailablePortSystemPropertyTestRule + .create(MongoDbTestJava8.class.getName()); + + static final MongoDbTestRule mongoDbTestRule = new MongoDbTestRule(mongoDbPortTestRule.getName()); + + @ClassRule + public static RuleChain ruleChain = RuleChainFactory.create(mongoDbPortTestRule, mongoDbTestRule, + loggerContextTestRule); + + @Test + public void test() { + final Logger logger = LogManager.getLogger(); + logger.info("Hello log"); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTest.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTest.java deleted file mode 100644 index d5aa5a9..0000000 --- a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.mongodb; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.categories.Appenders; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Ignore("Requires a running MongoDB server") -@Category(Appenders.MongoDb.class) -public class MongoDbCappedTest { - - @ClassRule - public static LoggerContextRule context = new LoggerContextRule("log4j2-mongodb-capped.xml"); - - @Test - public void test() { - final Logger logger = LogManager.getLogger(); - logger.info("Hello log"); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTestJava8.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTestJava8.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTestJava8.java new file mode 100644 index 0000000..78668f3 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedTestJava8.java @@ -0,0 +1,52 @@ +/* + * 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.mongodb; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.Appenders; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyTestRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.RuleChain; + +/** + * This class name does NOT end in "Test" in order to only be picked up by {@link Java8Test}. + */ +@Category(Appenders.MongoDb.class) +public class MongoDbCappedTestJava8 { + + static LoggerContextRule loggerContextTestRule = new LoggerContextRule("log4j2-mongodb-capped.xml"); + + static final AvailablePortSystemPropertyTestRule mongoDbPortTestRule = AvailablePortSystemPropertyTestRule + .create(MongoDbTestJava8.class.getName()); + + static final MongoDbTestRule mongoDbTestRule = new MongoDbTestRule(mongoDbPortTestRule.getName()); + + @ClassRule + public static RuleChain ruleChain = RuleChainFactory.create(mongoDbPortTestRule, mongoDbTestRule, + loggerContextTestRule); + + @Test + public void test() { + final Logger logger = LogManager.getLogger(); + logger.info("Hello log"); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTestJava8.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTestJava8.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTestJava8.java new file mode 100644 index 0000000..76432ee --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTestJava8.java @@ -0,0 +1,74 @@ +/* + * 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.mongodb; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.Appenders; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.message.MapMessage; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyTestRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.bson.Document; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.RuleChain; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +/** + * This class name does NOT end in "Test" in order to only be picked up by {@link Java8Test}. + */ +@Category(Appenders.MongoDb.class) +public class MongoDbMapMessageTestJava8 { + + static LoggerContextRule loggerContextTestRule = new LoggerContextRule("log4j2-mongodb-map-message.xml"); + + static final AvailablePortSystemPropertyTestRule mongoDbPortTestRule = AvailablePortSystemPropertyTestRule + .create(MongoDbMapMessageTestJava8.class.getName()); + + static final MongoDbTestRule mongoDbTestRule = new MongoDbTestRule(mongoDbPortTestRule.getName()); + + @ClassRule + public static RuleChain ruleChain = RuleChainFactory.create(mongoDbPortTestRule, mongoDbTestRule, + loggerContextTestRule); + + @Test + public void test() { + final Logger logger = LogManager.getLogger(); + final MapMessage<?, Object> map = new MapMessage<>(); + map.with("SomeName", "SomeValue"); + map.with("SomeInt", 1); + logger.info(map); + // + try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) { + final MongoDatabase database = mongoClient.getDatabase("test"); + Assert.assertNotNull(database); + final MongoCollection<Document> collection = database.getCollection("applog"); + Assert.assertNotNull(collection); + Document first = collection.find().first(); + Assert.assertNotNull(first); + Assert.assertEquals(first.toJson(), "SomeValue", first.getString("SomeName")); + Assert.assertEquals(first.toJson(), Integer.valueOf(1), first.getInteger("SomeInt")); + } + + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java deleted file mode 100644 index eab3c1c..0000000 --- a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.mongodb; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.categories.Appenders; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Ignore("Requires a running MongoDB server") -@Category(Appenders.MongoDb.class) -public class MongoDbTest { - - @ClassRule - public static LoggerContextRule context = new LoggerContextRule("log4j2-mongodb.xml"); - - @Test - public void test() { - final Logger logger = LogManager.getLogger(); - logger.info("Hello log"); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestJava8.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestJava8.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestJava8.java new file mode 100644 index 0000000..032eb73 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestJava8.java @@ -0,0 +1,52 @@ +/* + * 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.mongodb; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.categories.Appenders; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyTestRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.RuleChain; + +/** + * This class name does NOT end in "Test" in order to only be picked up by {@link Java8Test}. + */ +@Category(Appenders.MongoDb.class) +public class MongoDbTestJava8 { + + static LoggerContextRule loggerContextTestRule = new LoggerContextRule("log4j2-mongodb.xml"); + + static final AvailablePortSystemPropertyTestRule mongoDbPortTestRule = AvailablePortSystemPropertyTestRule + .create(MongoDbTestJava8.class.getName()); + + static final MongoDbTestRule mongoDbTestRule = new MongoDbTestRule(mongoDbPortTestRule.getName()); + + @ClassRule + public static RuleChain ruleChain = RuleChainFactory.create(mongoDbPortTestRule, mongoDbTestRule, + loggerContextTestRule); + + @Test + public void test() { + final Logger logger = LogManager.getLogger(); + logger.info("Hello log"); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestRule.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestRule.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestRule.java new file mode 100644 index 0000000..73fef40 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestRule.java @@ -0,0 +1,139 @@ +/* + * 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.mongodb; + +import java.util.Objects; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import com.mongodb.MongoClient; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodProcess; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.config.Timeout; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; + +/** + * A JUnit test rule to manage a MongoDB embedded instance. + */ +public class MongoDbTestRule implements TestRule { + + private static final int BUILDER_TIMEOUT_MILLIS = 30000; + + /** + * Store {@link MongodStarter} (or RuntimeConfig) in a static final field if you want to use artifact store caching + * (or else disable caching). + * <p> + * The test framework {@code de.flapdoodle.embed.mongo} requires Java 8. + * </p> + */ + protected static final MongodStarter starter = MongodStarter.getDefaultInstance(); + + public static int getBuilderTimeoutMillis() { + return BUILDER_TIMEOUT_MILLIS; + } + + public static MongodStarter getStarter() { + return starter; + } + + protected final String portSystemPropertyName; + protected MongoClient mongoClient; + protected MongodExecutable mongodExecutable; + protected MongodProcess mongodProcess; + + /** + * Constructs a new test rule. + * + * @param portSystemPropertyName + * The system property name for the MongoDB port. + */ + public MongoDbTestRule(final String portSystemPropertyName) { + this.portSystemPropertyName = Objects.requireNonNull(portSystemPropertyName, "portSystemPropertyName"); + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + final String value = Objects.requireNonNull(System.getProperty(portSystemPropertyName), + "System property '" + portSystemPropertyName + "' is null"); + final int port = Integer.parseInt(value); + mongodExecutable = starter.prepare( + // @formatter:off + new MongodConfigBuilder() + .version(Version.Main.PRODUCTION) + .timeout(new Timeout(BUILDER_TIMEOUT_MILLIS)) + .net( + new Net("localhost", port, Network.localhostIsIPv6())) + .build()); + // @formatter:on + mongodProcess = mongodExecutable.start(); + mongoClient = new MongoClient("localhost", port); + try { + base.evaluate(); + } finally { + if (mongodProcess != null) { + mongodProcess.stop(); + mongodProcess = null; + } + if (mongodExecutable != null) { + mongodExecutable.stop(); + mongodExecutable = null; + } + } + } + }; + } + + public MongoClient getMongoClient() { + return mongoClient; + } + + public MongodExecutable getMongodExecutable() { + return mongodExecutable; + } + + public MongodProcess getMongodProcess() { + return mongodProcess; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("MongoDbTestRule [portSystemPropertyName="); + builder.append(portSystemPropertyName); + builder.append(", mongoClient="); + builder.append(mongoClient); + builder.append(", mongodExecutable="); + builder.append(mongodExecutable); + builder.append(", mongodProcess="); + builder.append(mongodProcess); + builder.append("]"); + return builder.toString(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestTestRuleTestJava8.java ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestTestRuleTestJava8.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestTestRuleTestJava8.java new file mode 100644 index 0000000..545c959 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestTestRuleTestJava8.java @@ -0,0 +1,69 @@ +/* + * 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.mongodb; + +import org.apache.commons.lang3.JavaVersion; +import org.apache.commons.lang3.SystemUtils; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyTestRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.RuleChain; + +import com.mongodb.client.MongoIterable; + +/** + * Tests MongoDbRule. This class name does NOT end in "Test" in order to only be picked up by {@link Java8Test}. + * <p> + * The test framework {@code de.flapdoodle.embed.mongo} requires Java 8. + * </p> + */ +public class MongoDbTestTestRuleTestJava8 { + + @BeforeClass + public static void beforeClass() { + Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)); + } + + public static final AvailablePortSystemPropertyTestRule mongoDbPortTestRule = AvailablePortSystemPropertyTestRule + .create(MongoDbTestTestRuleTestJava8.class.getName()); + + public static final MongoDbTestRule mongoDbTestRule = new MongoDbTestRule(mongoDbPortTestRule.getName()); + + @ClassRule + public static RuleChain mongoDbChain = RuleChainFactory.create(mongoDbPortTestRule, mongoDbTestRule); + + @Test + public void testAccess() { + final MongoIterable<String> databaseNames = mongoDbTestRule.getMongoClient().listDatabaseNames(); + Assert.assertNotNull(databaseNames); + Assert.assertNotNull(databaseNames.first()); + } + + @Test + public void testMongoDbTestRule() { + Assert.assertNotNull(MongoDbTestRule.getStarter()); + Assert.assertNotNull(mongoDbTestRule); + Assert.assertNotNull(mongoDbTestRule.getMongoClient()); + Assert.assertNotNull(mongoDbTestRule.getMongodExecutable()); + Assert.assertNotNull(mongoDbTestRule.getMongodProcess()); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml ---------------------------------------------------------------------- diff --git a/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml new file mode 100644 index 0000000..84e49e5 --- /dev/null +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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. --> +<Configuration status="debug"> + <Appenders> + <NoSql name="MongoDbAppender"> + <MongoDb databaseName="test" collectionName="applog" server="localhost" + port="${sys:org.apache.logging.log4j.mongodb.MongoDbMapMessageTestJava8:-27017}"> + </MongoDb> + <MessageLayout /> + </NoSql> + </Appenders> + <Loggers> + <Root level="ALL"> + <AppenderRef ref="MongoDbAppender" /> + </Root> + </Loggers> +</Configuration> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/26fc9e07/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 52ce994..55e7051 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -74,6 +74,12 @@ <action issue="LOG4J2-2165" dev="ggregory" type="update"> Update Jackson from 2.9.2 to 2.9.3. </action> + <action issue="LOG4J2-2179" dev="ggregory" type="add"> + The MongoDB Appender should use a keys and values for a Log4j MapMessage. + </action> + <action issue="LOG4J2-2180" dev="ggregory" type="add"> + Add a MongoDbProvider builder for and deprecate org.apache.logging.log4j.mongodb.MongoDbProvider.createNoSqlProvider(). + </action> </release> <release version="2.10.0" date="2017-11-18" description="GA Release 2.10.0"> <action issue="LOG4J2-2120" dev="mikes" type="add" due-to="Carter Douglas Kozak ">