View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.slf4j.impl;
20  
21  import org.apache.maven.logwrapper.LogLevelRecorder;
22  import org.junit.jupiter.api.Test;
23  import org.slf4j.Logger;
24  
25  import static org.hamcrest.CoreMatchers.instanceOf;
26  import static org.hamcrest.MatcherAssert.assertThat;
27  import static org.junit.jupiter.api.Assertions.assertFalse;
28  import static org.junit.jupiter.api.Assertions.assertNotNull;
29  import static org.junit.jupiter.api.Assertions.assertNotSame;
30  import static org.junit.jupiter.api.Assertions.assertSame;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  class MavenLoggerFactoryTest {
35      @Test
36      void createsSimpleLogger() {
37          MavenLoggerFactory mavenLoggerFactory = new MavenLoggerFactory();
38  
39          Logger logger = mavenLoggerFactory.getLogger("Test");
40  
41          assertThat(logger, instanceOf(MavenSimpleLogger.class));
42      }
43  
44      @Test
45      void loggerCachingWorks() {
46          MavenLoggerFactory mavenLoggerFactory = new MavenLoggerFactory();
47  
48          Logger logger = mavenLoggerFactory.getLogger("Test");
49          Logger logger2 = mavenLoggerFactory.getLogger("Test");
50          Logger differentLogger = mavenLoggerFactory.getLogger("TestWithDifferentName");
51  
52          assertNotNull(logger);
53          assertNotNull(differentLogger);
54          assertSame(logger, logger2);
55          assertNotSame(logger, differentLogger);
56      }
57  
58      @Test
59      void reportsWhenFailOnSeverityThresholdHasBeenHit() {
60          MavenLoggerFactory mavenLoggerFactory = new MavenLoggerFactory();
61          mavenLoggerFactory.setLogLevelRecorder(new LogLevelRecorder("ERROR"));
62  
63          assertTrue(mavenLoggerFactory.getLogLevelRecorder().isPresent());
64          LogLevelRecorder logLevelRecorder =
65                  mavenLoggerFactory.getLogLevelRecorder().get();
66  
67          MavenFailOnSeverityLogger logger = (MavenFailOnSeverityLogger) mavenLoggerFactory.getLogger("Test");
68          assertFalse(logLevelRecorder.metThreshold());
69  
70          logger.warn("This should not hit the fail threshold");
71          assertFalse(logLevelRecorder.metThreshold());
72  
73          logger.error("This should hit the fail threshold");
74          assertTrue(logLevelRecorder.metThreshold());
75  
76          logger.warn("This should not reset the fail threshold");
77          assertTrue(logLevelRecorder.metThreshold());
78      }
79  
80      @Test
81      void failOnSeverityThresholdCanOnlyBeSetOnce() {
82          MavenLoggerFactory mavenLoggerFactory = new MavenLoggerFactory();
83          mavenLoggerFactory.setLogLevelRecorder(new LogLevelRecorder("WARN"));
84          assertThrows(
85                  IllegalStateException.class,
86                  () -> mavenLoggerFactory.setLogLevelRecorder(new LogLevelRecorder("ERROR")));
87      }
88  }