[CAMEL-6694] Added OSGi integration tests for logging with MDC SIFT logger
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/45305434 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/45305434 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/45305434 Branch: refs/heads/master Commit: 45305434b80e3b9168b599f38312969d4d0c115e Parents: 7d9f281 Author: Grzegorz Grzybek <gr.grzy...@gmail.com> Authored: Thu Apr 3 13:38:07 2014 +0200 Committer: Grzegorz Grzybek <gr.grzy...@gmail.com> Committed: Thu Apr 3 13:38:07 2014 +0200 ---------------------------------------------------------------------- .../camel/itest/osgi/core/log/LogRouteTest.java | 46 --------- ...ogRouteWithLoggersPresentInRegistryTest.java | 98 ++++++++++++++++++++ .../log/LogRouteWithNonDefaultLoggerTest.java | 96 +++++++++++++++++++ 3 files changed, 194 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/45305434/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java index e7faed4..139daeb 100644 --- a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java +++ b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java @@ -16,21 +16,10 @@ */ package org.apache.camel.itest.osgi.core.log; -import java.io.File; - -import org.apache.camel.CamelContext; -import org.apache.camel.impl.JndiRegistry; import org.apache.camel.itest.osgi.OSGiIntegrationTestSupport; -import org.apache.karaf.tooling.exam.options.DoNotModifyLogOption; -import org.apache.karaf.tooling.exam.options.KarafDistributionConfigurationFileReplacementOption; import org.junit.Test; import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.Configuration; import org.ops4j.pax.exam.junit.JUnit4TestRunner; -import org.slf4j.LoggerFactory; - -import static org.ops4j.pax.exam.OptionUtils.*; @RunWith(JUnit4TestRunner.class) public class LogRouteTest extends OSGiIntegrationTestSupport { @@ -51,14 +40,6 @@ public class LogRouteTest extends OSGiIntegrationTestSupport { } @Test - public void testSendMessageToInfoLogWithSiftLogging() throws Exception { - template.sendBody("log:org.apache.camel.SIFT?level=info&logger=#mylogger", "<level>INFO</level>"); - template.sendBody("log:org.apache.camel.SIFT?level=debug&logger=#mylogger", "<level>DEBUG</level>"); - template.sendBody("log:org.apache.camel.SIFT?level=info&logger=#mylogger", "<level>INFO</level>"); - template.sendBody("log:org.apache.camel.SIFT?level=error&logger=#mylogger", "<level>ERROR</level>"); - } - - @Test public void testSendMessageToBadLevel() throws Exception { try { template.sendBody("log:org.apache.camel.TEST?level=noSuchLevel", "<level>noSuchLevel</level>"); @@ -73,32 +54,5 @@ public class LogRouteTest extends OSGiIntegrationTestSupport { return false; } - @Override - protected CamelContext createCamelContext() throws Exception { - CamelContext camelContext = super.createCamelContext(); - camelContext.setApplicationContextClassLoader(getClass().getClassLoader()); - camelContext.setUseMDCLogging(true); - return camelContext; - } - - @Override - protected JndiRegistry createRegistry() throws Exception { - JndiRegistry registry = super.createRegistry(); - registry.bind("mylogger", LoggerFactory.getLogger("org.apache.camel.SIFT")); - return registry; - } - - @Configuration - public static Option[] configure() throws Exception { - Option[] options = combine( - getDefaultCamelKarafOptions(), - new Option[] { - new DoNotModifyLogOption(), - new KarafDistributionConfigurationFileReplacementOption("etc/org.ops4j.pax.logging.cfg", new File("src/test/resources/log4j.properties")), -// KarafDistributionOption.debugConfiguration("9999", true) - }); - return options; - } - } http://git-wip-us.apache.org/repos/asf/camel/blob/45305434/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithLoggersPresentInRegistryTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithLoggersPresentInRegistryTest.java b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithLoggersPresentInRegistryTest.java new file mode 100644 index 0000000..2a15d73 --- /dev/null +++ b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithLoggersPresentInRegistryTest.java @@ -0,0 +1,98 @@ +/** + * 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.camel.itest.osgi.core.log; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.camel.itest.osgi.OSGiIntegrationTestSupport; +import org.apache.camel.osgi.CamelContextFactory; +import org.apache.karaf.tooling.exam.options.DoNotModifyLogOption; +import org.apache.karaf.tooling.exam.options.KarafDistributionConfigurationFileReplacementOption; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.Configuration; +import org.ops4j.pax.exam.junit.JUnit4TestRunner; +import org.slf4j.LoggerFactory; + +import java.io.File; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.ops4j.pax.exam.OptionUtils.combine; + +@RunWith(JUnit4TestRunner.class) +public class LogRouteWithLoggersPresentInRegistryTest extends OSGiIntegrationTestSupport { + + @Test + public void testSendMessageToProvidedLogggerWithSiftLogging() throws Exception { + template.sendBody("log:irrelevant.logger.name?level=info&logger=#mylogger1", "<level>INFO</level>"); + template.sendBody("log:irrelevant.logger.name?level=debug&logger=#mylogger1", "<level>DEBUG</level>"); + template.sendBody("log:irrelevant.logger.name?level=error&logger=#mylogger1", "<level>ERROR</level>"); + + File logDir = new File(System.getProperty("karaf.base"), "data/log"); + File[] files = logDir.listFiles(); + assertThat(files.length, equalTo(1)); + assertThat(files[0].getName(), equalTo(bundleContext.getBundle().getSymbolicName() + ".log")); + } + + @Test + public void testSendMessageToCamelCreatedLogggerWithSiftLogging() throws Exception { + template.sendBody("log:org.apache.camel.SIFT.relevant.logger.name?level=info", "<level>INFO</level>"); + template.sendBody("log:org.apache.camel.SIFT.relevant.logger.name?level=debug", "<level>DEBUG</level>"); + template.sendBody("log:org.apache.camel.SIFT.relevant.logger.name?level=error", "<level>ERROR</level>"); + + File logDir = new File(System.getProperty("karaf.base"), "data/log"); + File[] files = logDir.listFiles(); + assertThat(files.length, equalTo(1)); + assertThat(files[0].getName(), not(equalTo(bundleContext.getBundle().getSymbolicName() + ".log"))); + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + LOG.info("Get the bundleContext is " + bundleContext); + LOG.info("Application installed as bundle id: " + bundleContext.getBundle().getBundleId()); + + setThreadContextClassLoader(); + + CamelContextFactory factory = new CamelContextFactory(); + factory.setBundleContext(bundleContext); + SimpleRegistry registry = new SimpleRegistry(); + registry.put("mylogger1", LoggerFactory.getLogger("org.apache.camel.SIFT.l1")); + registry.put("mylogger2", LoggerFactory.getLogger("org.apache.camel.SIFT.l2")); + factory.setRegistry(registry); + CamelContext camelContext = factory.createContext(); + camelContext.setApplicationContextClassLoader(getClass().getClassLoader()); + camelContext.setUseMDCLogging(true); + return camelContext; + } + + @Configuration + public static Option[] configure() throws Exception { + return combine( + getDefaultCamelKarafOptions(), + new DoNotModifyLogOption(), + new KarafDistributionConfigurationFileReplacementOption("etc/org.ops4j.pax.logging.cfg", new File("src/test/resources/log4j.properties")) + ); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/45305434/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithNonDefaultLoggerTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithNonDefaultLoggerTest.java b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithNonDefaultLoggerTest.java new file mode 100644 index 0000000..408b7d8 --- /dev/null +++ b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteWithNonDefaultLoggerTest.java @@ -0,0 +1,96 @@ +/** + * 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.camel.itest.osgi.core.log; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.camel.itest.osgi.OSGiIntegrationTestSupport; +import org.apache.camel.osgi.CamelContextFactory; +import org.apache.karaf.tooling.exam.options.DoNotModifyLogOption; +import org.apache.karaf.tooling.exam.options.KarafDistributionConfigurationFileReplacementOption; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.Configuration; +import org.ops4j.pax.exam.junit.JUnit4TestRunner; +import org.slf4j.LoggerFactory; + +import java.io.File; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.ops4j.pax.exam.OptionUtils.combine; + +@RunWith(JUnit4TestRunner.class) +public class LogRouteWithNonDefaultLoggerTest extends OSGiIntegrationTestSupport { + + @Test + public void testSendMessageToProvidedLogggerWithSiftLogging() throws Exception { + template.sendBody("log:irrelevant.logger.name?level=info&logger=#mylogger", "<level>INFO</level>"); + template.sendBody("log:irrelevant.logger.name?level=debug&logger=#mylogger", "<level>DEBUG</level>"); + template.sendBody("log:irrelevant.logger.name?level=error&logger=#mylogger", "<level>ERROR</level>"); + + File logDir = new File(System.getProperty("karaf.base"), "data/log"); + File[] files = logDir.listFiles(); + assertThat(files.length, equalTo(1)); + assertThat(files[0].getName(), equalTo(bundleContext.getBundle().getSymbolicName() + ".log")); + } + + @Test + public void testSendMessageToRegistryDefaultLogggerWithSiftLogging() throws Exception { + template.sendBody("log:irrelevant.logger.name?level=info", "<level>INFO</level>"); + template.sendBody("log:irrelevant.logger.name?level=debug", "<level>DEBUG</level>"); + template.sendBody("log:irrelevant.logger.name?level=error", "<level>ERROR</level>"); + + File logDir = new File(System.getProperty("karaf.base"), "data/log"); + File[] files = logDir.listFiles(); + assertThat(files.length, equalTo(1)); + assertThat(files[0].getName(), equalTo(bundleContext.getBundle().getSymbolicName() + ".log")); + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + LOG.info("Get the bundleContext is " + bundleContext); + LOG.info("Application installed as bundle id: " + bundleContext.getBundle().getBundleId()); + + setThreadContextClassLoader(); + + CamelContextFactory factory = new CamelContextFactory(); + factory.setBundleContext(bundleContext); + SimpleRegistry registry = new SimpleRegistry(); + registry.put("mylogger", LoggerFactory.getLogger("org.apache.camel.SIFT")); + factory.setRegistry(registry); + CamelContext camelContext = factory.createContext(); + camelContext.setApplicationContextClassLoader(getClass().getClassLoader()); + camelContext.setUseMDCLogging(true); + return camelContext; + } + + @Configuration + public static Option[] configure() throws Exception { + return combine( + getDefaultCamelKarafOptions(), + new DoNotModifyLogOption(), + new KarafDistributionConfigurationFileReplacementOption("etc/org.ops4j.pax.logging.cfg", new File("src/test/resources/log4j.properties")) + ); + } + +}