001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.test;
056:
057: import java.io.File;
058: import java.io.IOException;
059: import junit.framework.TestCase;
060: import org.apache.log.output.io.rotate.RevolvingFileStrategy;
061:
062: /**
063: * Test suite for the RevolvingFileStrategy.
064: *
065: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
066: */
067: public final class RevolvingFileStrategyTestCase extends TestCase {
068: private static final int OLD_AGE = 100000000;
069: private static final int YOUNG_AGE = -100000000;
070:
071: private final File m_baseFile;
072: private final long m_now = System.currentTimeMillis();
073:
074: public RevolvingFileStrategyTestCase(final String name)
075: throws IOException {
076: super (name);
077:
078: m_baseFile = (new File("build/testdata/log"))
079: .getCanonicalFile();
080: m_baseFile.getParentFile().mkdirs();
081: }
082:
083: private void deleteFiles(final int maxRotation) throws IOException {
084: for (int i = 0; i <= maxRotation; i++) {
085: final File file = new File(getFilename(i));
086: if (file.exists() && !file.delete()) {
087: throw new IOException("Failed to delete file " + file);
088: }
089: }
090: }
091:
092: private String getFilename(int i) {
093: return m_baseFile.toString() + ".00000" + i;
094: }
095:
096: private void createFile(final int rotation, final long age)
097: throws IOException {
098: final File file = new File(getFilename(rotation));
099: if (!file.createNewFile()) {
100: throw new IOException("Failed to create file " + file);
101: }
102: final long time = m_now - age;
103: file.setLastModified(time);
104: }
105:
106: public void testNew() throws Exception {
107: deleteFiles(9);
108:
109: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
110: m_baseFile, 9);
111:
112: assertEquals("rotation", 0, strategy.getCurrentRotation());
113: }
114:
115: public void testRotationExisting() throws Exception {
116: deleteFiles(9);
117: createFile(0, 0);
118:
119: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
120: m_baseFile, 9);
121:
122: assertEquals("rotation", 1, strategy.getCurrentRotation());
123: }
124:
125: public void testRotationExisting2() throws Exception {
126: deleteFiles(9);
127: createFile(0, 0);
128: createFile(1, 0);
129: createFile(2, 0);
130: createFile(3, 0);
131: createFile(4, 0);
132:
133: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
134: m_baseFile, 9);
135:
136: assertEquals("rotation", 5, strategy.getCurrentRotation());
137: }
138:
139: public void testRotationExistingWithMissing() throws Exception {
140: deleteFiles(9);
141: createFile(0, 0);
142: createFile(4, 0);
143:
144: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
145: m_baseFile, 9);
146:
147: assertEquals("rotation", 5, strategy.getCurrentRotation());
148: }
149:
150: public void testRotationExistingWithOlderLower() throws Exception {
151: deleteFiles(9);
152: createFile(0, OLD_AGE); //Note this is oldest
153: createFile(4, 0);
154:
155: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
156: m_baseFile, 9);
157:
158: assertEquals("rotation", 5, strategy.getCurrentRotation());
159: }
160:
161: public void testRotationExistingWithOlderHigher() throws Exception {
162: deleteFiles(9);
163: createFile(0, 0);
164: createFile(4, OLD_AGE);
165:
166: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
167: m_baseFile, 9);
168:
169: assertEquals("rotation", 5, strategy.getCurrentRotation());
170: }
171:
172: public void testFullRotation() throws Exception {
173: deleteFiles(9);
174: createFile(0, 0);
175: createFile(1, 0);
176: createFile(2, 0);
177: createFile(3, 0);
178: createFile(4, 0);
179: createFile(5, 0);
180: createFile(6, 0);
181: createFile(7, 0);
182: createFile(8, 0);
183: createFile(9, 0);
184:
185: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
186: m_baseFile, 9);
187:
188: assertEquals("rotation", 0, strategy.getCurrentRotation());
189: }
190:
191: public void testFullRotationWithOlder() throws Exception {
192: deleteFiles(9);
193: createFile(0, 0);
194: createFile(1, 0);
195: createFile(2, 0);
196: createFile(3, 0);
197: createFile(4, 0);
198: createFile(5, 0);
199: createFile(6, 0);
200: createFile(7, OLD_AGE);
201: createFile(8, 0);
202: createFile(9, 0);
203:
204: final RevolvingFileStrategy strategy = new RevolvingFileStrategy(
205: m_baseFile, 9);
206:
207: assertEquals("rotation", 7, strategy.getCurrentRotation());
208: }
209: }
|