KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > rollover > TestOfRolloverManager


1 package org.grlea.log.rollover;
2
3 // $Id: TestOfRolloverManager.java,v 1.3 2006/07/21 11:57:29 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18
19 import junit.framework.TestCase;
20
21 import java.io.IOException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.TimeZone JavaDoc;
28
29 /**
30  * <p>Tests the public interface of {@link RolloverManager}.</p>
31  *
32  * @author Graham Lea
33  * @version $Revision: 1.3 $
34  */

35 public class
36 TestOfRolloverManager
37 extends TestCase
38 {
39    private static final String JavaDoc KEY_ROLLOVER = "simplelog.rollover";
40
41    public
42    TestOfRolloverManager(String JavaDoc name)
43    {
44       // Standard TestCase constructor. You shouldn't edit this.
45
super(name);
46    }
47
48    protected void
49    setUp()
50    {
51    }
52
53    protected void
54    tearDown()
55    {
56    }
57
58    public void
59    testFileSizeStrategy()
60    throws Exception JavaDoc
61    {
62       Properties JavaDoc properties = new Properties JavaDoc();
63       properties.setProperty("simplelog.logFile", "foo.log");
64
65       properties.setProperty(KEY_ROLLOVER, "fileSize");
66       RolloverManager rollover = new RolloverManager(properties, null);
67       RolloverStrategy strategy = rollover.getStrategy();
68       assertEquals(FileSizeRolloverStrategy.class, strategy.getClass());
69    }
70
71    public void
72    testTimeOfDayStrategy()
73    throws Exception JavaDoc
74    {
75       Properties JavaDoc properties = new Properties JavaDoc();
76       properties.setProperty("simplelog.logFile", "foo.log");
77
78       properties.setProperty(KEY_ROLLOVER, "timeOfDay");
79       RolloverManager rollover = new RolloverManager(properties, null);
80       RolloverStrategy strategy = rollover.getStrategy();
81       assertEquals(TimeOfDayRolloverStrategy.class, strategy.getClass());
82    }
83
84    public void
85    testFileSizeStrategyConfigured()
86    throws Exception JavaDoc
87    {
88       Properties JavaDoc properties = new Properties JavaDoc();
89       properties.setProperty("simplelog.logFile", "foo.log");
90
91       properties.setProperty(KEY_ROLLOVER, "fileSize");
92       properties.setProperty(TestOfFileSizeRolloverStrategy.FILE_SIZE_KEY, "120K");
93       RolloverManager rollover = new RolloverManager(properties, null);
94       FileSizeRolloverStrategy strategy = (FileSizeRolloverStrategy) rollover.getStrategy();
95       assertEquals(120 * 1024, strategy.getRolloverSize());
96    }
97
98    public void
99    testTimeOfDayStrategyConfigured()
100    throws Exception JavaDoc
101    {
102       Field JavaDoc timezoneField = TimeOfDayRolloverStrategy.class.getDeclaredField("timeZone");
103       Field JavaDoc hourField = TimeOfDayRolloverStrategy.class.getDeclaredField("hour");
104       Field JavaDoc minuteField = TimeOfDayRolloverStrategy.class.getDeclaredField("minute");
105
106       timezoneField.setAccessible(true);
107       hourField.setAccessible(true);
108       minuteField.setAccessible(true);
109
110       Properties JavaDoc properties = new Properties JavaDoc();
111       properties.setProperty("simplelog.logFile", "foo.log");
112
113       properties.setProperty(KEY_ROLLOVER, "timeOfDay");
114       properties.setProperty(TestOfTimeOfDayRolloverStrategy.KEY_ROLLOVER_TIME, "8:30");
115       properties.setProperty(TestOfTimeOfDayRolloverStrategy.KEY_TIMEZONE, "Australia/Perth");
116       RolloverManager rollover = new RolloverManager(properties, null);
117       TimeOfDayRolloverStrategy strategy = (TimeOfDayRolloverStrategy) rollover.getStrategy();
118       assertEquals(8, hourField.getInt(strategy));
119       assertEquals(30, minuteField.getInt(strategy));
120       assertEquals("Australia/Perth", ((TimeZone JavaDoc) timezoneField.get(strategy)).getID());
121    }
122
123
124    public void
125    testNamedStrategy()
126    throws Exception JavaDoc
127    {
128       Properties JavaDoc properties = new Properties JavaDoc();
129       properties.setProperty("simplelog.logFile", "foo.log");
130
131       properties.setProperty(KEY_ROLLOVER, TestRolloverStrategy.class.getName());
132       RolloverManager rollover = new RolloverManager(properties, null);
133       RolloverStrategy strategy = rollover.getStrategy();
134       assertEquals(TestRolloverStrategy.class, strategy.getClass());
135    }
136
137    public void
138    testConfigureExceptionCommunicated()
139    throws Exception JavaDoc
140    {
141       Properties JavaDoc properties = new Properties JavaDoc();
142       properties.setProperty("simplelog.logFile", "foo.log");
143
144       properties.setProperty(KEY_ROLLOVER, ExceptionThrowingTestRolloverStrategy.class.getName());
145       try
146       {
147          new RolloverManager(properties, null);
148          fail("IOException expected");
149       }
150       catch (IOException JavaDoc e)
151       {}
152    }
153
154    public void
155    testNullStrategyNotAllowed()
156    {
157       Properties JavaDoc properties = new Properties JavaDoc();
158       properties.setProperty("simplelog.logFile", "foo.log");
159
160       try
161       {
162          new RolloverManager(properties, null);
163          fail("IOException expected");
164       }
165       catch (IOException JavaDoc e)
166       {}
167    }
168
169    public void
170    testEmptyStrategyNotAllowed()
171    {
172       Properties JavaDoc properties = new Properties JavaDoc();
173       properties.setProperty("simplelog.logFile", "foo.log");
174
175       properties.setProperty(KEY_ROLLOVER, " ");
176       try
177       {
178          new RolloverManager(properties, null);
179          fail("IOException expected");
180       }
181       catch (IOException JavaDoc e)
182       {}
183    }
184
185    public void
186    testMissingActiveFileNotAllowed()
187    {
188       Properties JavaDoc properties = new Properties JavaDoc();
189       properties.setProperty(KEY_ROLLOVER, "fileSize");
190       try
191       {
192          new RolloverManager(properties, null);
193          fail("IOException expected");
194       }
195       catch (IOException JavaDoc e)
196       {}
197    }
198
199    public void
200    testFileWithPseudoUniqueNumberFormat() throws IOException JavaDoc
201    {
202       String JavaDoc rolloverDirectoryName = "rolloverTest";
203       File JavaDoc rolloverDirectory = new File JavaDoc(rolloverDirectoryName);
204       rolloverDirectory.mkdirs();
205
206       File JavaDoc fileWithPseudoUniqueNumberFormat = new File JavaDoc(rolloverDirectory, "TEST-foo.log");
207       fileWithPseudoUniqueNumberFormat.createNewFile();
208
209       Properties JavaDoc properties = new Properties JavaDoc();
210       properties.setProperty("simplelog.logFile", "foo.log");
211       properties.setProperty("simplelog.rollover.directory", rolloverDirectoryName);
212       properties.setProperty("simplelog.rollover.filename", "{1}-foo.log");
213       properties.setProperty(KEY_ROLLOVER, "fileSize");
214
215       try
216       {
217          new RolloverManager(properties, null);
218       }
219       catch (NumberFormatException JavaDoc e)
220       {
221          fail("File caused NumberFormatException");
222       }
223
224       fileWithPseudoUniqueNumberFormat.delete();
225       rolloverDirectory.delete();
226    }
227
228    public static class
229    TestRolloverStrategy
230    implements RolloverStrategy
231    {
232       private boolean throwExceptionFromConfigure;
233
234       public
235       TestRolloverStrategy()
236       {
237          this(false);
238       }
239
240       public
241       TestRolloverStrategy(boolean throwExceptionFromConfigure)
242       {
243          this.throwExceptionFromConfigure = throwExceptionFromConfigure;
244       }
245
246       public void
247       configure(Map JavaDoc properties)
248       throws IOException JavaDoc
249       {
250          if (throwExceptionFromConfigure)
251          {
252             throw new IOException JavaDoc("Test Exception");
253          }
254       }
255
256       public boolean
257       rolloverNow(Date JavaDoc fileCreated, long fileLength)
258       {
259          return false;
260       }
261    }
262
263    public static class
264    ExceptionThrowingTestRolloverStrategy
265    extends TestRolloverStrategy
266    {
267       public
268       ExceptionThrowingTestRolloverStrategy()
269       {
270          super(true);
271       }
272    }
273 }
Popular Tags