View Javadoc

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  
18  package org.apache.log4j.rolling;
19  
20  import junit.framework.Assert;
21  import junit.framework.TestCase;
22  
23  import org.apache.log4j.Appender;
24  import org.apache.log4j.ConsoleAppender;
25  import org.apache.log4j.Level;
26  import org.apache.log4j.Logger;
27  import org.apache.log4j.PatternLayout;
28  import org.apache.log4j.spi.LoggingEvent;
29  
30  
31  /***
32   * Test CompositeTriggeringPolicy
33   * 
34   */
35  public class CompositeTriggeringPolicyTest extends TestCase {
36      private CompositeTriggeringPolicy composite;
37      private ConsoleAppender appender;
38      private LoggingEvent event;
39  
40      protected void setUp() throws Exception {
41          event = new LoggingEvent("Classname", Logger.getLogger("Logger"), System.currentTimeMillis(), Level.INFO, "msg", null);
42          appender = new ConsoleAppender(new PatternLayout("%d %level %c -%m%n"));
43          composite = new CompositeTriggeringPolicy();
44      }
45  
46      protected void tearDown() throws Exception {
47      }
48      
49      public void testNoPolicies() {
50          composite.activateOptions();
51          Assert.assertFalse(composite.isTriggeringEvent(appender, event, "file", 100));
52      }
53      
54      public void testOneTruePolicy() {
55          composite.addTriggeringPolicy(new TestTriggeringPolicy(true));
56          composite.activateOptions();
57          Assert.assertTrue(composite.isTriggeringEvent(appender, event, "file", 100));
58      }
59      
60      public void testOneFalsePolicy() {
61          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
62          composite.activateOptions();
63          Assert.assertFalse(composite.isTriggeringEvent(appender, event, "file", 100));
64      }
65  
66      public void testAllFalsePolicies() {
67          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
68          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
69          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
70          composite.activateOptions();
71          Assert.assertFalse(composite.isTriggeringEvent(appender, event, "file", 100));
72      }
73      
74      public void testAllTruePolicies() {
75          composite.addTriggeringPolicy(new TestTriggeringPolicy(true));
76          composite.addTriggeringPolicy(new TestTriggeringPolicy(true));
77          composite.addTriggeringPolicy(new TestTriggeringPolicy(true));
78          composite.activateOptions();
79          Assert.assertTrue(composite.isTriggeringEvent(appender, event, "file", 100));
80      }
81      
82      public void testTrueAndFalsePolicies() {
83          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
84          composite.addTriggeringPolicy(new TestTriggeringPolicy(false));
85          composite.addTriggeringPolicy(new TestTriggeringPolicy(true));
86          composite.activateOptions();
87          Assert.assertTrue(composite.isTriggeringEvent(appender, event, "file", 100));
88      }
89      
90      public void testActivateOptionsCalledByCompositeActivateOptions() {
91          TestTriggeringPolicy policy1 = new TestTriggeringPolicy(true);
92          TestTriggeringPolicy policy2 = new TestTriggeringPolicy(true);
93          
94          composite.addTriggeringPolicy(policy1);
95          composite.addTriggeringPolicy(policy2);
96          composite.activateOptions();
97          
98          Assert.assertTrue(policy1.activateOptionsCalled());
99          Assert.assertTrue(policy2.activateOptionsCalled());
100     }
101 
102     public void testActivateOptionsNotCalledByAddTriggeringPolicy() {
103         TestTriggeringPolicy policy1 = new TestTriggeringPolicy(true);
104         TestTriggeringPolicy policy2 = new TestTriggeringPolicy(true);
105         
106         composite.addTriggeringPolicy(policy1);
107         composite.addTriggeringPolicy(policy2);
108         
109         Assert.assertFalse(policy1.activateOptionsCalled());
110         Assert.assertFalse(policy2.activateOptionsCalled());
111     }
112 
113     class TestTriggeringPolicy implements TriggeringPolicy {
114         private final boolean result;
115         private boolean activateOptionsCalled = false;
116 
117         public TestTriggeringPolicy(boolean result) {
118             this.result = result;
119         }
120         
121         public boolean activateOptionsCalled() {
122             return activateOptionsCalled;
123         }
124 
125         public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
126                 String filename, long fileLength) {
127             return result;
128         }
129 
130         public void activateOptions() {
131             activateOptionsCalled = true;
132         }
133     }
134 }