001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.tomcat;
015
016import static org.junit.Assert.*;
017
018import org.apache.catalina.LifecycleException;
019import org.apache.catalina.core.ContainerBase;
020import org.junit.After;
021import org.junit.Test;
022
023import ch.qos.logback.access.AccessTestConstants;
024import ch.qos.logback.core.status.Status;
025import ch.qos.logback.core.testUtil.StatusChecker;
026
027public class LogbackValveTest {
028
029    LogbackValve valve = new LogbackValve();
030    StatusChecker checker = new StatusChecker(valve);
031
032    @After
033    public void tearDown() {
034        System.clearProperty(LogbackValve.CATALINA_BASE_KEY);
035        System.clearProperty(LogbackValve.CATALINA_HOME_KEY);
036    }
037
038    @Test
039    public void nonExistingConfigFileShouldResultInWarning() throws LifecycleException {
040        final String resourceName = "logback-test2-config.xml";
041        setupValve(resourceName);
042        valve.start();
043        checker.assertContainsMatch(Status.WARN, "Failed to find valid");
044    }
045
046    @Test
047    public void fileUnderCatalinaBaseShouldBeFound() throws LifecycleException {
048        System.setProperty(LogbackValve.CATALINA_BASE_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/");
049        final String fileName = "logback-access.xml";
050        setupValve(fileName);
051        valve.start();
052        checker.assertContainsMatch("Found configuration file");
053        checker.assertContainsMatch("Done configuring");
054        checker.assertIsErrorFree();
055    }
056
057    @Test
058    public void fileUnderCatalinaHomeShouldBeFound() throws LifecycleException {
059        System.setProperty(LogbackValve.CATALINA_HOME_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/");
060        final String fileName = "logback-access.xml";
061        setupValve(fileName);
062        valve.start();
063        checker.assertContainsMatch("Found configuration file");
064        checker.assertContainsMatch("Done configuring");
065        checker.assertIsErrorFree();
066    }
067
068    @Test
069    public void resourceShouldBeFound() throws LifecycleException {
070        final String fileName = "logback-asResource.xml";
071        setupValve(fileName);
072        valve.start();
073        checker.assertContainsMatch("Found ." + fileName + ". as a resource.");
074        checker.assertContainsMatch("Done configuring");
075        checker.assertIsErrorFree();
076    }
077
078    @Test
079    public void executorServiceShouldBeNotNull() throws LifecycleException {
080        final String fileName = "logback-asResource.xml";
081        setupValve(fileName);
082        valve.start();
083        assertNotNull(valve.getScheduledExecutorService());
084        
085    }
086    
087    private void setupValve(final String resourceName) {
088        valve.setFilename(resourceName);
089        valve.setName("test");
090        valve.setContainer(new ContainerBase() {
091            @Override
092            protected String getObjectNameKeyProperties() {
093                return "getObjectNameKeyProperties-test";
094            }
095        });
096    }
097}