View Javadoc

1   /*
2    * Copyright 2013 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.common.log4j;
17  
18  import org.apache.log4j.LogManager;
19  import org.junit.After;
20  import org.junit.AfterClass;
21  import org.junit.Assert;
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.junit.runners.BlockJUnit4ClassRunner;
26  import org.kuali.student.common.test.spring.log4j.KSLog4JConfigurer;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Test that the KSLog4JConfigurer works to let the log4j.properties to be overridden.
32   * 
33   * @author Kuali Student Team 
34   *
35   */
36  @RunWith (BlockJUnit4ClassRunner.class)
37  public class TestConfigureLog4jOverride { 
38      
39      private static Logger log;
40  
41      /**
42       * 
43       */
44      public TestConfigureLog4jOverride() {
45          
46      }
47      
48      @AfterClass
49      public static void afterClass() {
50          
51          /*
52           * Used to go back to the default log4j.properties file so that subsequent tests will not be effected and continue to run at the
53           * WARN log level.
54           * 
55           */
56          System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES);
57          System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES_REFRESH);
58          
59          LogManager.resetConfiguration();
60          
61          System.setProperty(KSLog4JConfigurer.LOG4J_PROPERTIES, "classpath:log4j.properties");
62          
63          log = KSLog4JConfigurer.getLogger(TestConfigureLog4jOverride.class);
64      }
65  
66      @Test
67      public void testStandardOverride() {
68          
69          LogManager.resetConfiguration();
70          
71          System.setProperty("log4j.configuration,", "src/test/resources/override-log4j-test.properties");
72          
73          log = LoggerFactory.getLogger(TestConfigureLog4jOverride.class);
74          
75          Assert.assertTrue(log.isDebugEnabled());
76      }
77      
78      @Test
79      public void testBasic() {
80          
81          System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES);
82          System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES_REFRESH);
83          
84          LogManager.resetConfiguration();
85          
86          // setup the log4j override to the log4j properties file we added into the test resources
87          // When a user calls the getLogger method they would be setting this via a -D option to the test launcher.
88          System.setProperty(KSLog4JConfigurer.LOG4J_PROPERTIES, "classpath:override-log4j-test.properties");
89          
90          log = KSLog4JConfigurer.getLogger(TestConfigureLog4jOverride.class);
91          
92          Assert.assertTrue("Failed to use override-log4j-test.properties to set the log level to debugging", log.isDebugEnabled());
93          
94          log.debug ("at debug level");
95      }
96      
97      @Test
98      public void testRefresh() {
99          
100         System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES);
101         System.getProperties().remove(KSLog4JConfigurer.LOG4J_PROPERTIES_REFRESH);
102         
103         LogManager.resetConfiguration();
104         
105         // setup the log4j override to the log4j properties file we added into the test resources
106         // When a user calls the getLogger method they would be setting this via a -D option to the test launcher.
107         System.setProperty(KSLog4JConfigurer.LOG4J_PROPERTIES, "classpath:override-log4j-test.properties");
108         
109         System.setProperty(KSLog4JConfigurer.LOG4J_PROPERTIES_REFRESH, "15");
110         
111         log = KSLog4JConfigurer.getLogger(TestConfigureLog4jOverride.class);
112         
113         Assert.assertTrue("Failed to use override-log4j-test.properties to set the log level to debugging", log.isDebugEnabled());
114         
115         log.debug ("at debug level");
116     }
117 }