svn commit: r1915 - logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling

Author: ceki Date: Wed Oct 29 23:04:19 2008 New Revision: 1915 Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java Log: - refactoring TimeBasedRollingTest for improved clarity Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java Wed Oct 29 23:04:19 2008 @@ -11,7 +11,7 @@ TestSuite suite = new TestSuite(); suite.addTestSuite(RenamingTest.class); suite.addTest(new JUnit4TestAdapter(SizeBasedRollingTest.class)); - suite.addTestSuite(TimeBasedRollingTest.class); + suite.addTest(new JUnit4TestAdapter(TimeBasedRollingTest.class)); suite.addTest(ch.qos.logback.core.rolling.helper.PackageTest.suite()); return suite; } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java Wed Oct 29 23:04:19 2008 @@ -10,14 +10,17 @@ package ch.qos.logback.core.rolling; +import static org.junit.Assert.*; + import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.concurrent.TimeUnit; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import ch.qos.logback.core.Context; import ch.qos.logback.core.ContextBase; import ch.qos.logback.core.layout.EchoLayout; @@ -45,19 +48,29 @@ * * @author Ceki Gülcü */ -public class TimeBasedRollingTest extends TestCase { +public class TimeBasedRollingTest { static final String DATE_PATTERN = "yyyy-MM-dd_HH_mm_ss"; + SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); EchoLayout<Object> layout = new EchoLayout<Object>(); Context context = new ContextBase(); - public TimeBasedRollingTest(String name) { - super(name); - } + RollingFileAppender<Object> rfa1 = new RollingFileAppender<Object>(); + TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy(); + + RollingFileAppender<Object> rfa2 = new RollingFileAppender<Object>(); + TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy(); + Calendar cal = Calendar.getInstance(); + long currentTime; + + @Before public void setUp() { context.setName("test"); + cal.set(Calendar.MILLISECOND, 0); + currentTime = cal.getTimeInMillis(); + // Delete .log files { File target = new File(Constants.OUTPUT_DIR_PREFIX + "test4.log"); @@ -76,47 +89,68 @@ } } + @After public void tearDown() { } - /** - * Test rolling without compression, file option left blank, no stop/start - */ - public void test1() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); - - RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); + void initRFA(RollingFileAppender<Object> rfa, String filename) { rfa.setContext(context); rfa.setLayout(layout); + if (filename != null) { + rfa.setFile(filename); + } + } - TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); + void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy tbrp, + String filenamePattern, long currentTime) { tbrp.setContext(context); - tbrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test1-%d{" - + DATE_PATTERN + "}"); + tbrp.setFileNamePattern(filenamePattern); tbrp.setParent(rfa); tbrp.setCurrentTime(currentTime); tbrp.start(); rfa.setRollingPolicy(tbrp); rfa.start(); + } - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); + String[] computeFilenames(String testStr, + boolean compression, String lastFile) { String[] filenames = new String[3]; - - for (int i = 0; i < 3; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test1-" + int oneBeforeLast = filenames.length - 1; + for (int i = 0; i < oneBeforeLast; i++) { + filenames[i] = Constants.OUTPUT_DIR_PREFIX + testStr + sdf.format(cal.getTime()); + if (compression) { + filenames[i] += ".gz"; + } cal.add(Calendar.SECOND, 1); } + if (lastFile != null) { + filenames[oneBeforeLast] = Constants.OUTPUT_DIR_PREFIX + lastFile; + } else { + filenames[oneBeforeLast] = Constants.OUTPUT_DIR_PREFIX + testStr + + sdf.format(cal.getTime()); + } + return filenames; + } + + /** + * Test rolling without compression, file option left blank, no stop/start + */ + @Test + public void test1() throws Exception { + initRFA(rfa1, null); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test1-%d{" + + DATE_PATTERN + "}", currentTime); + + String[] filenames = computeFilenames("test1-", false, null); // System.out.println("Waiting until next second and 100 millis."); - tbrp.setCurrentTime(addTime(currentTime, 1100)); + tbrp1.setCurrentTime(addTime(currentTime, 1100)); // System.out.println("Done waiting."); for (int i = 0; i < 3; i++) { - rfa.doAppend("Hello---" + i); - tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), 500)); + rfa1.doAppend("Hello---" + i); + tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500)); // Thread.sleep(500); } @@ -133,33 +167,14 @@ /** * No compression, with stop/restart, file option left blank */ + @Test public void test2() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); - RollingFileAppender<Object> rfa1 = new RollingFileAppender<Object>(); - rfa1.setContext(context); - rfa1.setLayout(layout); - - TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy(); - tbrp1.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test2-%d{" - + DATE_PATTERN + "}"); - tbrp1.setContext(context); - tbrp1.setParent(rfa1); - tbrp1.setCurrentTime(currentTime); - tbrp1.start(); - rfa1.setRollingPolicy(tbrp1); - rfa1.start(); - - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); - String[] filenames = new String[3]; + initRFA(rfa1, null); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test2-%d{" + + DATE_PATTERN + "}", currentTime); - for (int i = 0; i < 3; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test2-" - + sdf.format(cal.getTime()); - cal.add(Calendar.SECOND, 1); - } + String[] filenames = computeFilenames("test2-", false, null); // System.out.println("Waiting until next second and 100 millis."); tbrp1.setCurrentTime(addTime(currentTime, 1100)); @@ -173,19 +188,9 @@ rfa1.stop(); - RollingFileAppender<Object> rfa2 = new RollingFileAppender<Object>(); - rfa2.setContext(context); - rfa2.setLayout(layout); - - TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy(); - tbrp2.setContext(context); - tbrp2.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test2-%d{" - + DATE_PATTERN + "}"); - tbrp2.setParent(rfa2); - tbrp2.setCurrentTime(tbrp1.getCurrentTime()); - tbrp2.start(); - rfa2.setRollingPolicy(tbrp2); - rfa2.start(); + initRFA(rfa2, null); + initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test2-%d{" + + DATE_PATTERN + "}", tbrp1.getCurrentTime()); for (int i = 0; i <= 2; i++) { rfa2.doAppend("Hello---" + i); @@ -202,90 +207,42 @@ /** * With compression, file option left blank, no stop/restart */ + @Test public void test3() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); + initRFA(rfa1, null); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test3-%d{" + + DATE_PATTERN + "}.gz", currentTime); - RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); - rfa.setContext(context); - rfa.setLayout(layout); + String[] filenames = computeFilenames("test3-", true, null); - TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); - tbrp.setContext(context); - tbrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test3-%d{" - + DATE_PATTERN + "}.gz"); - tbrp.setParent(rfa); - tbrp.setCurrentTime(currentTime); - tbrp.start(); - rfa.setRollingPolicy(tbrp); - rfa.start(); - - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); - String[] filenames = new String[4]; - - for (int i = 0; i < 2; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test3-" - + sdf.format(cal.getTime()) + ".gz"; - cal.add(Calendar.SECOND, 1); - } - - filenames[3] = Constants.OUTPUT_DIR_PREFIX + "test3-" - + sdf.format(cal.getTime()); - - // System.out.println("Waiting until next second and 100 millis."); - tbrp.setCurrentTime(addTime(currentTime, 1100)); - // System.out.println("Done waiting."); + tbrp1.setCurrentTime(addTime(currentTime, 1100)); for (int i = 0; i < 3; i++) { - rfa.doAppend("Hello---" + i); - tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), 500)); + rfa1.doAppend("Hello---" + i); + tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500)); } - tbrp.future.get(2000, TimeUnit.MILLISECONDS); + tbrp1.future.get(2000, TimeUnit.MILLISECONDS); for (int i = 0; i < 2; i++) { assertTrue(Compare.gzCompare(filenames[i], Constants.TEST_DIR_PREFIX + "witness/rolling/tbr-test3." + i + ".gz")); } - assertTrue(Compare.compare(filenames[3], Constants.TEST_DIR_PREFIX + assertTrue(Compare.compare(filenames[2], Constants.TEST_DIR_PREFIX + "witness/rolling/tbr-test3.2")); } /** * Without compression, file option set, with stop/restart */ + @Test public void test4() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); + initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test4.log"); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test4-%d{" + + DATE_PATTERN + "}", currentTime); - RollingFileAppender<Object> rfa1 = new RollingFileAppender<Object>(); - rfa1.setContext(context); - rfa1.setLayout(layout); - rfa1.setFile(Constants.OUTPUT_DIR_PREFIX + "test4.log"); - - TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy(); - tbrp1.setContext(context); - // tbrp1.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/test4.log"); - tbrp1.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test4-%d{" - + DATE_PATTERN + "}"); - tbrp1.setParent(rfa1); - tbrp1.setCurrentTime(currentTime); - tbrp1.start(); - rfa1.setRollingPolicy(tbrp1); - rfa1.start(); - - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); - String[] filenames = new String[3]; - - for (int i = 0; i < 2; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test4-" - + sdf.format(cal.getTime()); - cal.add(Calendar.SECOND, 1); - } - filenames[2] = Constants.OUTPUT_DIR_PREFIX + "test4.log"; + String[] filenames = computeFilenames("test4-", false, "test4.log"); // System.out.println("Waiting until next second and 100 millis."); tbrp1.setCurrentTime(addTime(currentTime, 1100)); @@ -298,21 +255,9 @@ rfa1.stop(); - RollingFileAppender<Object> rfa2 = new RollingFileAppender<Object>(); - rfa2.setContext(context); - rfa2.setLayout(layout); - rfa2.setFile(Constants.OUTPUT_DIR_PREFIX + "test4.log"); - - TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy(); - tbrp2.setContext(context); - tbrp2.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test4-%d{" - + DATE_PATTERN + "}"); - // tbrp2.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/test4.log"); - tbrp2.setParent(rfa2); - tbrp2.setCurrentTime(tbrp1.getCurrentTime()); - tbrp2.start(); - rfa2.setRollingPolicy(tbrp2); - rfa2.start(); + initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4.log"); + initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test4-%d{" + + DATE_PATTERN + "}", tbrp1.getCurrentTime()); for (int i = 0; i <= 2; i++) { rfa2.doAppend("Hello---" + i); @@ -328,45 +273,19 @@ /** * No compression, file option set, without stop/restart */ + @Test public void test5() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); + initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test5.log"); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test5-%d{" + + DATE_PATTERN + "}", currentTime); - RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); - rfa.setContext(context); - rfa.setLayout(layout); - rfa.setFile(Constants.OUTPUT_DIR_PREFIX + "test5.log"); + String[] filenames = computeFilenames("test5-", false, "test5.log"); - TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); - tbrp.setContext(context); - tbrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test5-%d{" - + DATE_PATTERN + "}"); - // tbrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/test5.log"); - tbrp.setParent(rfa); - tbrp.setCurrentTime(currentTime); - tbrp.start(); - rfa.setRollingPolicy(tbrp); - rfa.start(); - - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); - String[] filenames = new String[4]; - - for (int i = 0; i < 2; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test5-" - + sdf.format(cal.getTime()); - cal.add(Calendar.SECOND, 1); - } - - filenames[2] = Constants.OUTPUT_DIR_PREFIX + "test5.log"; - - // System.out.println("Waiting until next second and 100 millis."); - tbrp.setCurrentTime(addTime(currentTime, 1100)); - // System.out.println("Done waiting."); + tbrp1.setCurrentTime(addTime(currentTime, 1100)); for (int i = 0; i < 3; i++) { - rfa.doAppend("Hello---" + i); - tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), 500)); + rfa1.doAppend("Hello---" + i); + tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500)); } for (int i = 0; i < 3; i++) { @@ -378,54 +297,24 @@ /** * With compression, file option set, no stop/restart, */ + @Test public void test6() throws Exception { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.MILLISECOND, 0); - long currentTime = cal.getTimeInMillis(); + initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test6.log"); + initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test6-%d{" + + DATE_PATTERN + "}.gz", currentTime); - RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); - rfa.setContext(context); - rfa.setLayout(layout); - rfa.setFile(Constants.OUTPUT_DIR_PREFIX+ "test6.log"); + String[] filenames = computeFilenames("test6-", true, "test6.log"); - TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); - tbrp.setContext(context); - tbrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX + "test6-%d{" - + DATE_PATTERN + "}.gz"); - // tbrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/test6.log"); - tbrp.setParent(rfa); - tbrp.setCurrentTime(currentTime); - tbrp.start(); - rfa.setRollingPolicy(tbrp); - rfa.start(); - - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); - String[] filenames = new String[4]; - - for (int i = 0; i < 2; i++) { - filenames[i] = Constants.OUTPUT_DIR_PREFIX + "test6-" - + sdf.format(cal.getTime()) + ".gz"; - cal.add(Calendar.SECOND, 1); - } - - filenames[2] = Constants.OUTPUT_DIR_PREFIX + "test6.log"; - - // System.out.println("Waiting until next second and 100 millis."); - tbrp.setCurrentTime(addTime(currentTime, 1100)); - // System.out.println("Done waiting."); + tbrp1.setCurrentTime(addTime(currentTime, 1100)); for (int i = 0; i < 3; i++) { - rfa.doAppend("Hello---" + i); - tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), 500)); + rfa1.doAppend("Hello---" + i); + tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500)); } - // for (int i = 0; i < 4; i++) { - // System.out.println(i + " expected filename [" + filenames[i] + "]."); - // } - // wait for the compression task to finish - tbrp.future.get(1000, TimeUnit.MILLISECONDS); - + tbrp1.future.get(1000, TimeUnit.MILLISECONDS); + for (int i = 0; i < 2; i++) { assertTrue(Compare.gzCompare(filenames[i], Constants.TEST_DIR_PREFIX + "witness/rolling/tbr-test6." + i + ".gz")); @@ -435,12 +324,6 @@ + "witness/rolling/tbr-test6.2")); } - public static Test suite() { - TestSuite suite = new TestSuite(); - suite.addTestSuite(TimeBasedRollingTest.class); - return suite; - } - static long addTime(long currentTime, long timeToWait) { return currentTime + timeToWait; }
participants (1)
-
noreply.ceki@qos.ch