1 17 package org.apache.log.output.test; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import junit.framework.TestCase; 22 import org.apache.log.output.io.rotate.RevolvingFileStrategy; 23 24 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 m_baseFile; 36 private final long m_now = System.currentTimeMillis(); 37 38 public RevolvingFileStrategyTestCase( final String name ) 39 throws IOException 40 { 41 super( name ); 42 43 m_baseFile = ( new File ( "build/testdata/log" ) ).getCanonicalFile(); 44 m_baseFile.getParentFile().mkdirs(); 45 } 46 47 private void deleteFiles( final int maxRotation ) 48 throws IOException 49 { 50 for( int i = 0; i <= maxRotation; i++ ) 51 { 52 final File file = new File ( getFilename( i ) ); 53 if( file.exists() && !file.delete() ) 54 { 55 throw new IOException ( "Failed to delete file " + file ); 56 } 57 } 58 } 59 60 private String 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 67 { 68 final File file = new File ( getFilename( rotation ) ); 69 if( !file.createNewFile() ) 70 { 71 throw new IOException ( "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 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 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 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 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 131 { 132 deleteFiles( 9 ); 133 createFile( 0, OLD_AGE ); 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 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 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 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 |