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  package org.apache.log4j.rolling.helper;
18  
19  import junit.framework.TestCase;
20  import org.apache.log4j.rolling.RollingPolicyBase;
21  import org.apache.log4j.rolling.RolloverDescription;
22  
23  import java.util.Calendar;
24  
25  
26  /***
27   * Tests for FileNamePattern.
28   *
29   * @author Ceki
30   * @author Curt Arnold
31   *
32   */
33  public final class FileNamePatternTestCase extends TestCase {
34      /***
35       * Construct new test.
36       * @param name test name
37       */
38      public FileNamePatternTestCase(final String name) {
39          super(name);
40      }
41  
42      private static class FileNameTestRollingPolicy extends RollingPolicyBase {
43          public FileNameTestRollingPolicy(final String pattern) {
44              setFileNamePattern(pattern);
45              parseFileNamePattern();
46          }
47  
48          public void activateOptions() {
49          }
50          public RolloverDescription initialize(final String activeName, final boolean append) {
51              return null;
52          }
53          public RolloverDescription rollover(final String activeName) {
54              return null;
55          }
56          public String format(Object obj) {
57              StringBuffer buf = new StringBuffer();
58              formatFileName(obj, buf);
59              return buf.toString();
60          }
61      }
62  
63      private void assertDatePattern(final String pattern, final int year,
64          final int month, final int day, final int hour, final int min,
65          final String expected) {
66          Calendar cal = Calendar.getInstance();
67          cal.set(year, month, day, hour, min);
68          FileNameTestRollingPolicy policy = new FileNameTestRollingPolicy(pattern);
69          assertEquals(expected,
70              policy.format(cal.getTime()));
71      }
72  
73      private void assertIntegerPattern(final String pattern, final int value,
74          final String expected) {
75          FileNameTestRollingPolicy policy = new FileNameTestRollingPolicy(pattern);
76          assertEquals(expected, policy.format(new Integer(value)));
77      }
78  
79      public void testFormatInteger1() {
80          assertIntegerPattern("t", 3, "t");
81      }
82  
83      public void testFormatInteger2() {
84          assertIntegerPattern("foo", 3, "foo");
85      }
86  
87      public void testFormatInteger3() {
88          assertIntegerPattern("foo%", 3, "foo%");
89      }
90  
91      public void testFormatInteger4() {
92          assertIntegerPattern("%ifoo", 3, "3foo");
93      }
94  
95      public void testFormatInteger5() {
96          assertIntegerPattern("foo%ixixo", 3, "foo3xixo");
97      }
98  
99      public void testFormatInteger6() {
100         assertIntegerPattern("foo%i.log", 3, "foo3.log");
101     }
102 
103     public void testFormatInteger7() {
104         assertIntegerPattern("foo.%i.log", 3, "foo.3.log");
105     }
106 
107     public void testFormatInteger8() {
108         assertIntegerPattern("%ifoo%", 3, "3foo%");
109     }
110 
111     public void testFormatInteger9() {
112         assertIntegerPattern("%ifoo%%", 3, "3foo%");
113     }
114 
115     public void testFormatInteger10() {
116         assertIntegerPattern("%%foo", 3, "%foo");
117     }
118 
119     public void testFormatInteger11() {
120         assertIntegerPattern("foo%ibar%i", 3, "foo3bar3");
121     }
122 
123     public void testFormatDate1() {
124         assertDatePattern("foo%d{yyyy.MM.dd}", 2003, 4, 20, 17, 55,
125             "foo2003.05.20");
126     }
127 
128     public void testFormatDate2() {
129         assertDatePattern("foo%d{yyyy.MM.dd HH:mm}", 2003, 4, 20, 17, 55,
130             "foo2003.05.20 17:55");
131     }
132 
133     public void testFormatDate3() {
134         assertDatePattern("%d{yyyy.MM.dd HH:mm} foo", 2003, 4, 20, 17, 55,
135             "2003.05.20 17:55 foo");
136     }
137 
138     /***
139      * A %d is treated as %d{yyyy-MM-dd} if followed by a malformed format specifier.
140      *
141      */
142     public void testFormatDate4() {
143         assertDatePattern("foo%dyyyy.MM.dd}", 2003, 4, 20, 17, 55,
144             "foo2003-05-20yyyy.MM.dd}");
145     }
146 
147     /***
148      * A %d is treated as %d{yyyy-MM-dd} if followed by a malformed format specifier.
149      *
150      */
151     public void testFormatDate5() {
152         assertDatePattern("foo%d{yyyy.MM.dd", 2003, 4, 20, 17, 55,
153             "foo2003-05-20{yyyy.MM.dd");
154     }
155 
156 }