KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > test > RevolvingFileStrategyTestCase


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

17 package org.apache.log.output.test;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import junit.framework.TestCase;
22 import org.apache.log.output.io.rotate.RevolvingFileStrategy;
23
24 /**
25  * Test suite for the RevolvingFileStrategy.
26  *
27  * @author Peter Donald
28  */

29 public final class RevolvingFileStrategyTestCase
30     extends TestCase
31 {
32     private static final int OLD_AGE = 100000000;
33     private static final int YOUNG_AGE = -100000000;
34
35     private final File JavaDoc m_baseFile;
36     private final long m_now = System.currentTimeMillis();
37
38     public RevolvingFileStrategyTestCase( final String JavaDoc name )
39         throws IOException JavaDoc
40     {
41         super( name );
42
43         m_baseFile = ( new File JavaDoc( "build/testdata/log" ) ).getCanonicalFile();
44         m_baseFile.getParentFile().mkdirs();
45     }
46
47     private void deleteFiles( final int maxRotation )
48         throws IOException JavaDoc
49     {
50         for( int i = 0; i <= maxRotation; i++ )
51         {
52             final File JavaDoc file = new File JavaDoc( getFilename( i ) );
53             if( file.exists() && !file.delete() )
54             {
55                 throw new IOException JavaDoc( "Failed to delete file " + file );
56             }
57         }
58     }
59
60     private String JavaDoc getFilename( int i )
61     {
62         return m_baseFile.toString() + ".00000" + i;
63     }
64
65     private void createFile( final int rotation, final long age )
66         throws IOException JavaDoc
67     {
68         final File JavaDoc file = new File JavaDoc( getFilename( rotation ) );
69         if( !file.createNewFile() )
70         {
71             throw new IOException JavaDoc( "Failed to create file " + file );
72         }
73         final long time = m_now - age;
74         file.setLastModified( time );
75     }
76
77     public void testNew()
78         throws Exception JavaDoc
79     {
80         deleteFiles( 9 );
81
82         final RevolvingFileStrategy strategy =
83             new RevolvingFileStrategy( m_baseFile, 9 );
84
85         assertEquals( "rotation", 0, strategy.getCurrentRotation() );
86     }
87
88     public void testRotationExisting()
89         throws Exception JavaDoc
90     {
91         deleteFiles( 9 );
92         createFile( 0, 0 );
93
94         final RevolvingFileStrategy strategy =
95             new RevolvingFileStrategy( m_baseFile, 9 );
96
97         assertEquals( "rotation", 1, strategy.getCurrentRotation() );
98     }
99
100     public void testRotationExisting2()
101         throws Exception JavaDoc
102     {
103         deleteFiles( 9 );
104         createFile( 0, 0 );
105         createFile( 1, 0 );
106         createFile( 2, 0 );
107         createFile( 3, 0 );
108         createFile( 4, 0 );
109
110         final RevolvingFileStrategy strategy =
111             new RevolvingFileStrategy( m_baseFile, 9 );
112
113         assertEquals( "rotation", 5, strategy.getCurrentRotation() );
114     }
115
116     public void testRotationExistingWithMissing()
117         throws Exception JavaDoc
118     {
119         deleteFiles( 9 );
120         createFile( 0, 0 );
121         createFile( 4, 0 );
122
123         final RevolvingFileStrategy strategy =
124             new RevolvingFileStrategy( m_baseFile, 9 );
125
126         assertEquals( "rotation", 5, strategy.getCurrentRotation() );
127     }
128
129     public void testRotationExistingWithOlderLower()
130         throws Exception JavaDoc
131     {
132         deleteFiles( 9 );
133         createFile( 0, OLD_AGE ); //Note this is oldest
134
createFile( 4, 0 );
135
136         final RevolvingFileStrategy strategy =
137             new RevolvingFileStrategy( m_baseFile, 9 );
138
139         assertEquals( "rotation", 5, strategy.getCurrentRotation() );
140     }
141
142     public void testRotationExistingWithOlderHigher()
143         throws Exception JavaDoc
144     {
145         deleteFiles( 9 );
146         createFile( 0, 0 );
147         createFile( 4, OLD_AGE );
148
149         final RevolvingFileStrategy strategy =
150             new RevolvingFileStrategy( m_baseFile, 9 );
151
152         assertEquals( "rotation", 5, strategy.getCurrentRotation() );
153     }
154
155     public void testFullRotation()
156         throws Exception JavaDoc
157     {
158         deleteFiles( 9 );
159         createFile( 0, 0 );
160         createFile( 1, 0 );
161         createFile( 2, 0 );
162         createFile( 3, 0 );
163         createFile( 4, 0 );
164         createFile( 5, 0 );
165         createFile( 6, 0 );
166         createFile( 7, 0 );
167         createFile( 8, 0 );
168         createFile( 9, 0 );
169
170         final RevolvingFileStrategy strategy =
171             new RevolvingFileStrategy( m_baseFile, 9 );
172
173         assertEquals( "rotation", 0, strategy.getCurrentRotation() );
174     }
175
176     public void testFullRotationWithOlder()
177         throws Exception JavaDoc
178     {
179         deleteFiles( 9 );
180         createFile( 0, 0 );
181         createFile( 1, 0 );
182         createFile( 2, 0 );
183         createFile( 3, 0 );
184         createFile( 4, 0 );
185         createFile( 5, 0 );
186         createFile( 6, 0 );
187         createFile( 7, OLD_AGE );
188         createFile( 8, 0 );
189         createFile( 9, 0 );
190
191         final RevolvingFileStrategy strategy =
192             new RevolvingFileStrategy( m_baseFile, 9 );
193
194         assertEquals( "rotation", 7, strategy.getCurrentRotation() );
195     }
196 }
197
Popular Tags