|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * |
| 4 | + * Copyright 2025 Red Hat, Inc., and individual contributors |
| 5 | + * as indicated by the @author tags. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.jboss.logmanager.handlers; |
| 21 | + |
| 22 | +import java.util.concurrent.BlockingDeque; |
| 23 | +import java.util.concurrent.LinkedBlockingDeque; |
| 24 | +import java.util.logging.Handler; |
| 25 | +import java.util.logging.LogRecord; |
| 26 | + |
| 27 | +import org.jboss.logmanager.ExtFormatter; |
| 28 | +import org.jboss.logmanager.ExtLogRecord; |
| 29 | +import org.jboss.logmanager.Level; |
| 30 | +import org.jboss.logmanager.LogContext; |
| 31 | +import org.jboss.logmanager.Logger; |
| 32 | +import org.jboss.logmanager.formatters.PatternFormatter; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author <a href="mailto:[email protected]">James R. Perkins</a> |
| 38 | + */ |
| 39 | +public class NestedHandlerTest { |
| 40 | + |
| 41 | + public static class TestHandler extends Handler { |
| 42 | + final BlockingDeque<ExtLogRecord> messages = new LinkedBlockingDeque<>(); |
| 43 | + final ExtFormatter formatter; |
| 44 | + |
| 45 | + TestHandler() { |
| 46 | + this.formatter = new PatternFormatter("%s"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void publish(final LogRecord record) { |
| 51 | + final ExtLogRecord extLogRecord; |
| 52 | + if (record instanceof ExtLogRecord) { |
| 53 | + extLogRecord = (ExtLogRecord) record; |
| 54 | + } else { |
| 55 | + extLogRecord = new ExtLogRecord(Level.ERROR, String.format("An ExtLogRecord was expected but was %s", record), |
| 56 | + ExtLogRecord.FormatStyle.PRINTF, NestedHandlerTest.class.getPackage().getName()); |
| 57 | + } |
| 58 | + messages.add(extLogRecord); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void flush() { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void close() throws SecurityException { |
| 68 | + messages.clear(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void julHandler() { |
| 74 | + final TestHandler handler = new TestHandler(); |
| 75 | + handler.setErrorManager(AssertingErrorManager.of()); |
| 76 | + try (DelayedHandler delayedHandler = new DelayedHandler()) { |
| 77 | + delayedHandler.addHandler(handler); |
| 78 | + final ExtLogRecord record = new ExtLogRecord(Level.INFO, "FormatStyle %s", ExtLogRecord.FormatStyle.PRINTF, |
| 79 | + NestedHandlerTest.class.getPackage().getName()); |
| 80 | + record.setParameters(new Object[] { "PRINTF" }); |
| 81 | + delayedHandler.publish(record); |
| 82 | + final var found = handler.messages.poll(); |
| 83 | + Assertions.assertNotNull(found); |
| 84 | + Assertions.assertEquals("FormatStyle PRINTF", found.getMessage()); |
| 85 | + Assertions.assertEquals(ExtLogRecord.FormatStyle.NO_FORMAT, found.getFormatStyle()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void loggerNode() throws Exception { |
| 91 | + final TestHandler handler = new TestHandler(); |
| 92 | + handler.setErrorManager(AssertingErrorManager.of()); |
| 93 | + try (LogContext context = LogContext.getLogContext()) { |
| 94 | + final Logger logger = context.getLogger(NestedHandlerTest.class.getName()); |
| 95 | + logger.addHandler(handler); |
| 96 | + final ExtLogRecord record = new ExtLogRecord(Level.INFO, "FormatStyle %s", ExtLogRecord.FormatStyle.PRINTF, |
| 97 | + NestedHandlerTest.class.getPackage().getName()); |
| 98 | + record.setParameters(new Object[] { "PRINTF" }); |
| 99 | + logger.log(record); |
| 100 | + final var found = handler.messages.poll(); |
| 101 | + Assertions.assertNotNull(found); |
| 102 | + Assertions.assertEquals("FormatStyle PRINTF", found.getMessage()); |
| 103 | + Assertions.assertEquals(ExtLogRecord.FormatStyle.NO_FORMAT, found.getFormatStyle()); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments