View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.tomcat;
15  
16  import static org.junit.Assert.*;
17  
18  import org.apache.catalina.LifecycleException;
19  import org.apache.catalina.core.ContainerBase;
20  import org.junit.After;
21  import org.junit.Test;
22  
23  import ch.qos.logback.access.AccessTestConstants;
24  import ch.qos.logback.core.status.Status;
25  import ch.qos.logback.core.testUtil.StatusChecker;
26  
27  public class LogbackValveTest {
28  
29      LogbackValve valve = new LogbackValve();
30      StatusChecker checker = new StatusChecker(valve);
31  
32      @After
33      public void tearDown() {
34          System.clearProperty(LogbackValve.CATALINA_BASE_KEY);
35          System.clearProperty(LogbackValve.CATALINA_HOME_KEY);
36      }
37  
38      @Test
39      public void nonExistingConfigFileShouldResultInWarning() throws LifecycleException {
40          final String resourceName = "logback-test2-config.xml";
41          setupValve(resourceName);
42          valve.start();
43          checker.assertContainsMatch(Status.WARN, "Failed to find valid");
44      }
45  
46      @Test
47      public void fileUnderCatalinaBaseShouldBeFound() throws LifecycleException {
48          System.setProperty(LogbackValve.CATALINA_BASE_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/");
49          final String fileName = "logback-access.xml";
50          setupValve(fileName);
51          valve.start();
52          checker.assertContainsMatch("Found configuration file");
53          checker.assertContainsMatch("Done configuring");
54          checker.assertIsErrorFree();
55      }
56  
57      @Test
58      public void fileUnderCatalinaHomeShouldBeFound() throws LifecycleException {
59          System.setProperty(LogbackValve.CATALINA_HOME_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/");
60          final String fileName = "logback-access.xml";
61          setupValve(fileName);
62          valve.start();
63          checker.assertContainsMatch("Found configuration file");
64          checker.assertContainsMatch("Done configuring");
65          checker.assertIsErrorFree();
66      }
67  
68      @Test
69      public void resourceShouldBeFound() throws LifecycleException {
70          final String fileName = "logback-asResource.xml";
71          setupValve(fileName);
72          valve.start();
73          checker.assertContainsMatch("Found ." + fileName + ". as a resource.");
74          checker.assertContainsMatch("Done configuring");
75          checker.assertIsErrorFree();
76      }
77  
78      @Test
79      public void executorServiceShouldBeNotNull() throws LifecycleException {
80          final String fileName = "logback-asResource.xml";
81          setupValve(fileName);
82          valve.start();
83          assertNotNull(valve.getScheduledExecutorService());
84          
85      }
86      
87      private void setupValve(final String resourceName) {
88          valve.setFilename(resourceName);
89          valve.setName("test");
90          valve.setContainer(new ContainerBase() {
91              @Override
92              protected String getObjectNameKeyProperties() {
93                  return "getObjectNameKeyProperties-test";
94              }
95          });
96      }
97  }