|
| 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.slf4j.message; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.message.Message; |
| 22 | +import org.apache.logging.log4j.message.MessageFactory2; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +public class ThrowableConsumingMessageFactoryTest { |
| 26 | + |
| 27 | + private static final String MESSAGE = "MESSAGE"; |
| 28 | + private static final Object P0 = new Object(); |
| 29 | + private static final Object P1 = new Object(); |
| 30 | + private static final Object P2 = new Object(); |
| 31 | + private static final Object P3 = new Object(); |
| 32 | + private static final Object P4 = new Object(); |
| 33 | + private static final Object P5 = new Object(); |
| 34 | + private static final Object P6 = new Object(); |
| 35 | + private static final Object P7 = new Object(); |
| 36 | + private static final Object P8 = new Object(); |
| 37 | + private static final Object P9 = new Object(); |
| 38 | + private static final Object P10 = new Object(); |
| 39 | + private static final Object THROWABLE = new Throwable(); |
| 40 | + |
| 41 | + @Test |
| 42 | + void should_not_consume_last_object_parameter() { |
| 43 | + final MessageFactory2 factory = new ThrowableConsumingMessageFactory(); |
| 44 | + assertThat(factory.newMessage(MESSAGE, P0)) |
| 45 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 46 | + .as("checking parameter count and throwable") |
| 47 | + .containsExactly(1, null); |
| 48 | + assertThat(factory.newMessage(MESSAGE, P0, P1)) |
| 49 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 50 | + .as("checking parameter count and throwable") |
| 51 | + .containsExactly(2, null); |
| 52 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2)) |
| 53 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 54 | + .as("checking parameter count and throwable") |
| 55 | + .containsExactly(3, null); |
| 56 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3)) |
| 57 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 58 | + .as("checking parameter count and throwable") |
| 59 | + .containsExactly(4, null); |
| 60 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4)) |
| 61 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 62 | + .as("checking parameter count and throwable") |
| 63 | + .containsExactly(5, null); |
| 64 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5)) |
| 65 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 66 | + .as("checking parameter count and throwable") |
| 67 | + .containsExactly(6, null); |
| 68 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6)) |
| 69 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 70 | + .as("checking parameter count and throwable") |
| 71 | + .containsExactly(7, null); |
| 72 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7)) |
| 73 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 74 | + .as("checking parameter count and throwable") |
| 75 | + .containsExactly(8, null); |
| 76 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, P8)) |
| 77 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 78 | + .as("checking parameter count and throwable") |
| 79 | + .containsExactly(9, null); |
| 80 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9)) |
| 81 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 82 | + .as("checking parameter count and throwable") |
| 83 | + .containsExactly(10, null); |
| 84 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10)) |
| 85 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 86 | + .as("checking parameter count and throwable") |
| 87 | + .containsExactly(11, null); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void should_consume_last_throwable_parameter() { |
| 92 | + final MessageFactory2 factory = new ThrowableConsumingMessageFactory(); |
| 93 | + assertThat(factory.newMessage(MESSAGE, THROWABLE)) |
| 94 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 95 | + .as("checking parameter count and throwable") |
| 96 | + .containsExactly(0, THROWABLE); |
| 97 | + assertThat(factory.newMessage(MESSAGE, P0, THROWABLE)) |
| 98 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 99 | + .as("checking parameter count and throwable") |
| 100 | + .containsExactly(1, THROWABLE); |
| 101 | + assertThat(factory.newMessage(MESSAGE, P0, P1, THROWABLE)) |
| 102 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 103 | + .as("checking parameter count and throwable") |
| 104 | + .containsExactly(2, THROWABLE); |
| 105 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, THROWABLE)) |
| 106 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 107 | + .as("checking parameter count and throwable") |
| 108 | + .containsExactly(3, THROWABLE); |
| 109 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, THROWABLE)) |
| 110 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 111 | + .as("checking parameter count and throwable") |
| 112 | + .containsExactly(4, THROWABLE); |
| 113 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, THROWABLE)) |
| 114 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 115 | + .as("checking parameter count and throwable") |
| 116 | + .containsExactly(5, THROWABLE); |
| 117 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, THROWABLE)) |
| 118 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 119 | + .as("checking parameter count and throwable") |
| 120 | + .containsExactly(6, THROWABLE); |
| 121 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, THROWABLE)) |
| 122 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 123 | + .as("checking parameter count and throwable") |
| 124 | + .containsExactly(7, THROWABLE); |
| 125 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, THROWABLE)) |
| 126 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 127 | + .as("checking parameter count and throwable") |
| 128 | + .containsExactly(8, THROWABLE); |
| 129 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, P8, THROWABLE)) |
| 130 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 131 | + .as("checking parameter count and throwable") |
| 132 | + .containsExactly(9, THROWABLE); |
| 133 | + assertThat(factory.newMessage(MESSAGE, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, THROWABLE)) |
| 134 | + .extracting(m -> m.getParameters().length, Message::getThrowable) |
| 135 | + .as("checking parameter count and throwable") |
| 136 | + .containsExactly(10, THROWABLE); |
| 137 | + } |
| 138 | +} |
0 commit comments