|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.core.appender; |
| 18 | + |
| 19 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | +import static org.hamcrest.Matchers.containsString; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | + |
| 23 | +import org.apache.logging.log4j.core.test.junit.LoggerContextSource; |
| 24 | +import org.apache.logging.log4j.spi.ExtendedLogger; |
| 25 | +import org.apache.logging.log4j.core.script.ScriptRef; // Correct import for ScriptRef |
| 26 | +import org.apache.logging.log4j.core.config.Configuration; // Import for Configuration |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +@LoggerContextSource("log4j-script-ref-test.xml") |
| 30 | +class ScriptConfigurationTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void testScriptRefConfiguration(final Configuration configuration) { |
| 34 | + // Verify that the main Scripts element is initialized |
| 35 | + assertNotNull(configuration.getScriptManager(), "ScriptManager should not be null"); |
| 36 | + |
| 37 | + // Verify a ScriptRef element is correctly resolved |
| 38 | + ScriptRef scriptRef = (ScriptRef) configuration.getScriptManager().getScript("ExampleScriptRef"); |
| 39 | + assertNotNull(scriptRef, "ScriptRef should not be null"); |
| 40 | + assertThat(scriptRef.getLanguage(), containsString("groovy")); |
| 41 | + assertThat(scriptRef.getScriptText(), containsString("return \"Hello, Log4j!\";")); |
| 42 | + |
| 43 | + // Ensure that the script executes correctly |
| 44 | + Object result = configuration.getScriptManager().execute("ExampleScriptRef", null); |
| 45 | + assertNotNull(result, "Script execution result should not be null"); |
| 46 | + assertThat(result.toString(), containsString("Hello, Log4j!")); |
| 47 | + |
| 48 | + // Verify that the console appender is initialized |
| 49 | + final ConsoleAppender consoleAppender = (ConsoleAppender) configuration.getAppender("Console"); |
| 50 | + assertNotNull(consoleAppender, "Console appender should be initialized"); |
| 51 | + |
| 52 | + // Verify that the log messages are printed to the console |
| 53 | + ExtendedLogger logger = configuration.getLoggerContext().getLogger(ScriptConfigurationTest.class); |
| 54 | + logger.info("Test message"); |
| 55 | + |
| 56 | + // Capture console output (depending on test framework, this might need a mock or special handling) |
| 57 | + // Check if the expected log message is printed in the console output |
| 58 | + assertThat(consoleAppender.getLayout().toString(), containsString("Test message")); |
| 59 | + } |
| 60 | +} |
0 commit comments