From 615bda54ff93a5ebf4b51c2289d36ed54bed368d Mon Sep 17 00:00:00 2001 From: James Perkins Date: Mon, 23 Oct 2017 10:51:37 -0700 Subject: [PATCH] [LOGMGR-174] Added a test for the filter expressions. It was previously thought there was some issues parsing regular expression filters. This test proves that they are being parsed correctly. --- .../config/LogContextConfigurationTests.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/org/jboss/logmanager/config/LogContextConfigurationTests.java diff --git a/src/test/java/org/jboss/logmanager/config/LogContextConfigurationTests.java b/src/test/java/org/jboss/logmanager/config/LogContextConfigurationTests.java new file mode 100644 index 00000000..1dc08683 --- /dev/null +++ b/src/test/java/org/jboss/logmanager/config/LogContextConfigurationTests.java @@ -0,0 +1,108 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2017 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * Licensed 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.jboss.logmanager.config; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.logging.Filter; + +import org.jboss.logmanager.ExtLogRecord; +import org.jboss.logmanager.Level; +import org.jboss.logmanager.LogContext; +import org.jboss.logmanager.Logger; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author James R. Perkins + */ +public class LogContextConfigurationTests { + private static final String LOGGER_NAME = LogContextConfigurationTests.class.getName(); + private static final Collection FILTER_DESCRIPTIONS = new ArrayList<>(); + + @BeforeClass + public static void setup() { + FILTER_DESCRIPTIONS.add(new FilterDescription("substituteAll(\"\\\\s\", \"_\")", " test ", "_test_", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("substituteAll(\"\\\"\", \"'\")", "Test \"quoted\" replacement", "Test 'quoted' replacement", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("substitute(\"\\\\s\", \"_\")", " test ", "_test ", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("substitute(\"\\\\n\", \"\\r\\n\")", "Test Unix to Windows\n", "Test Unix to Windows\r\n", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("accept", "test accept", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("deny", "test deny", false)); + FILTER_DESCRIPTIONS.add(new FilterDescription("match(\"\\\\f\")", "test match \u000C", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("match(\"[a-zA-Z \\\"]+\")", "Test \"quoted\" text", true)); + FILTER_DESCRIPTIONS.add(new FilterDescription("match(\"\\\\s+\")", "test_match_no_spaces", false)); + FILTER_DESCRIPTIONS.add(new FilterDescription("not(match(\"\\\\b\"))", "Test word boundaries", false)); + } + + @Test + public void testFilterParsing() throws Exception { + final LogContext context = LogContext.create(); + final LogContextConfiguration configuration = LogContextConfiguration.Factory.create(context); + final LoggerConfiguration loggerConfiguration = configuration.addLoggerConfiguration(LOGGER_NAME); + final Logger logger = context.getLogger(LOGGER_NAME); + + for (FilterDescription description : FILTER_DESCRIPTIONS) { + // Set the filter and commit + loggerConfiguration.setFilter(description.filterExpression); + configuration.commit(); + + final ExtLogRecord record = create(description.logMessage); + + final Filter filter = logger.getFilter(); + Assert.assertNotNull("Expected a filter on the logger, but one was not found: " + description, filter); + Assert.assertEquals("Filter.isLoggable() test failed: " + description, description.isLoggable, filter.isLoggable(record)); + final String msg = record.getFormattedMessage(); + Assert.assertEquals(String.format("Expected %s found %s: %n%s", description.expectedMessage, msg, description), description.expectedMessage, msg); + } + } + + private static ExtLogRecord create(final String message) { + final ExtLogRecord record = new ExtLogRecord(Level.INFO, message, Logger.class.getName()); + record.setLoggerName(LOGGER_NAME); + return record; + } + + private static class FilterDescription { + final String filterExpression; + final String logMessage; + final String expectedMessage; + final boolean isLoggable; + + private FilterDescription(final String filterExpression, final String message, final boolean isLoggable) { + this(filterExpression, message, message, isLoggable); + } + + private FilterDescription(final String filterExpression, final String logMessage, final String expectedMessage, final boolean isLoggable) { + this.filterExpression = filterExpression; + this.logMessage = logMessage; + this.expectedMessage = expectedMessage; + this.isLoggable = isLoggable; + } + + @Override + public String toString() { + return "FilterDescription(filterExpression=" + filterExpression + + ", logMessage=" + logMessage + + ", expectedMessage=" + expectedMessage + + ", isLoggable=" + isLoggable + ")"; + } + } +}