1 package org.grlea.log.rollover; 2 3 6 18 19 import junit.framework.TestCase; 20 21 import java.io.IOException ; 22 import java.io.File ; 23 import java.lang.reflect.Field ; 24 import java.util.Date ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.TimeZone ; 28 29 35 public class 36 TestOfRolloverManager 37 extends TestCase 38 { 39 private static final String KEY_ROLLOVER = "simplelog.rollover"; 40 41 public 42 TestOfRolloverManager(String name) 43 { 44 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 61 { 62 Properties properties = new Properties (); 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 74 { 75 Properties properties = new Properties (); 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 87 { 88 Properties properties = new Properties (); 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 101 { 102 Field timezoneField = TimeOfDayRolloverStrategy.class.getDeclaredField("timeZone"); 103 Field hourField = TimeOfDayRolloverStrategy.class.getDeclaredField("hour"); 104 Field minuteField = TimeOfDayRolloverStrategy.class.getDeclaredField("minute"); 105 106 timezoneField.setAccessible(true); 107 hourField.setAccessible(true); 108 minuteField.setAccessible(true); 109 110 Properties properties = new Properties (); 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 ) timezoneField.get(strategy)).getID()); 121 } 122 123 124 public void 125 testNamedStrategy() 126 throws Exception 127 { 128 Properties properties = new Properties (); 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 140 { 141 Properties properties = new Properties (); 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 e) 151 {} 152 } 153 154 public void 155 testNullStrategyNotAllowed() 156 { 157 Properties properties = new Properties (); 158 properties.setProperty("simplelog.logFile", "foo.log"); 159 160 try 161 { 162 new RolloverManager(properties, null); 163 fail("IOException expected"); 164 } 165 catch (IOException e) 166 {} 167 } 168 169 public void 170 testEmptyStrategyNotAllowed() 171 { 172 Properties properties = new Properties (); 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 e) 182 {} 183 } 184 185 public void 186 testMissingActiveFileNotAllowed() 187 { 188 Properties properties = new Properties (); 189 properties.setProperty(KEY_ROLLOVER, "fileSize"); 190 try 191 { 192 new RolloverManager(properties, null); 193 fail("IOException expected"); 194 } 195 catch (IOException e) 196 {} 197 } 198 199 public void 200 testFileWithPseudoUniqueNumberFormat() throws IOException 201 { 202 String rolloverDirectoryName = "rolloverTest"; 203 File rolloverDirectory = new File (rolloverDirectoryName); 204 rolloverDirectory.mkdirs(); 205 206 File fileWithPseudoUniqueNumberFormat = new File (rolloverDirectory, "TEST-foo.log"); 207 fileWithPseudoUniqueNumberFormat.createNewFile(); 208 209 Properties properties = new Properties (); 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 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 properties) 248 throws IOException 249 { 250 if (throwExceptionFromConfigure) 251 { 252 throw new IOException ("Test Exception"); 253 } 254 } 255 256 public boolean 257 rolloverNow(Date 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 |