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