From cbb1073433cf76cfe4eb5055a97c60f7e16631b2 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Fri, 17 Aug 2007 08:14:31 +0000 Subject: [PATCH] add: - more tests for 'FileWatcher' SVN: 1404 --- .../cisd/common/utilities/FileWatcher.java | 1 + .../common/utilities/FileWatcherTest.java | 84 ++++++++++++------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileWatcher.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileWatcher.java index 3caf200ebf9..65b709d6fe3 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileWatcher.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileWatcher.java @@ -158,6 +158,7 @@ public abstract class FileWatcher extends TimerTask public final void saveInitialState(File fileToWatch) { + // This returns <code>0L</code> if the file does not exist! lastModified = fileToWatch.lastModified(); } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileWatcherTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileWatcherTest.java index ce2fe55f1c5..eae352bacc8 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileWatcherTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileWatcherTest.java @@ -23,19 +23,18 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.Properties; +import java.util.Timer; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.PatternLayout; import org.apache.log4j.PropertyConfigurator; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; -import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; -import ch.systemsx.cisd.common.logging.LogInitializer; - /** * Test cases for the {@link FileWatcher}. * <p> @@ -50,7 +49,9 @@ public class FileWatcherTest private static final File workingDirectory = new File(unitTestRootDirectory, "FileWatcherTest"); - private static final File touchedFile1 = new File(workingDirectory, "touchedFile1"); + private static final File tmpFile1 = new File(workingDirectory, "tmpFile1"); + + private static final File tmpFile2 = new File(workingDirectory, "tmpFile2"); private PrintStream systemOut; @@ -58,37 +59,45 @@ public class FileWatcherTest private ByteArrayOutputStream logRecorder; + private volatile boolean onChangeCalled; + private final static void createNewFile(File file) throws IOException { FileUtils.touch(file); + file.setLastModified(0); assert file.exists(); file.deleteOnExit(); } - @BeforeSuite - public final void beforeSuite() throws IOException + private final static Properties createLogProperties() { - workingDirectory.mkdirs(); - assert workingDirectory.isDirectory(); - createNewFile(touchedFile1); - workingDirectory.deleteOnExit(); + Properties properties = new Properties(); + properties.setProperty("log4j.rootLogger", "TRACE, TestAppender"); + properties.setProperty("log4j.appender.TestAppender", ConsoleAppender.class.getName()); + properties.setProperty("log4j.appender.TestAppender.layout", PatternLayout.class.getName()); + properties.setProperty("log4j.appender.TestAppender.layout.ConversionPattern", "%m%n"); + return properties; + } + + private final String getLogContent() + { + return new String(logRecorder.toByteArray()).trim(); } @BeforeClass public final void setUp() throws IOException { - LogInitializer.init(); logRecorder = new ByteArrayOutputStream(); systemOut = System.out; systemErr = System.err; System.setErr(new PrintStream(logRecorder)); System.setOut(new PrintStream(logRecorder)); - Properties properties = new Properties(); - properties.setProperty("log4j.rootLogger", "TRACE, TestAppender"); - properties.setProperty("log4j.appender.TestAppender", ConsoleAppender.class.getName()); - properties.setProperty("log4j.appender.TestAppender.layout", PatternLayout.class.getName()); - properties.setProperty("log4j.appender.TestAppender.layout.ConversionPattern", "%m%n"); - PropertyConfigurator.configure(properties); + PropertyConfigurator.configure(createLogProperties()); + workingDirectory.mkdirs(); + assert workingDirectory.isDirectory(); + createNewFile(tmpFile1); + createNewFile(tmpFile2); + workingDirectory.deleteOnExit(); } @AfterClass @@ -111,11 +120,6 @@ public class FileWatcherTest logRecorder.reset(); } - private String getLogContent() - { - return new String(logRecorder.toByteArray()).trim(); - } - @Test public final void testWithNonExistingFile() { @@ -128,17 +132,39 @@ public class FileWatcherTest @Test public final void testNonChangingFile() { - new TestFileWatcher(touchedFile1).run(); - assertEquals(String.format(FileWatcher.HAS_NOT_CHANGED_FORMAT, touchedFile1), getLogContent()); + new TestFileWatcher(tmpFile1).run(); + assertEquals(String.format(FileWatcher.HAS_NOT_CHANGED_FORMAT, tmpFile1), getLogContent()); } - // @Test + @Test public final void testFileHasChanged() throws IOException { - FileWatcher fileWatcher = new TestFileWatcher(touchedFile1); - FileUtils.touch(touchedFile1); + onChangeCalled = false; + FileWatcher fileWatcher = new TestFileWatcher(tmpFile1); + FileUtils.touch(tmpFile1); fileWatcher.run(); - assertEquals(String.format(FileWatcher.HAS_CHANGED_FORMAT, touchedFile1), getLogContent()); + assertEquals(String.format(FileWatcher.HAS_CHANGED_FORMAT, tmpFile1), getLogContent()); + assertEquals(true, onChangeCalled); + } + + @Test + public final void testWithTimer() throws IOException + { + onChangeCalled = false; + FileWatcher fileWatcher = new TestFileWatcher(tmpFile2); + FileUtils.touch(tmpFile2); + Timer timer = new Timer(true); + timer.schedule(fileWatcher, 0); + } + + @Test(dependsOnMethods = "testWithTimer", timeOut = 5000, groups = "slow") + public final void testOnChangeCalled() throws InterruptedException + { + while (onChangeCalled == false) + { + Thread.sleep(200); + } + assertEquals(StringUtils.EMPTY, getLogContent()); } // @@ -156,7 +182,7 @@ public class FileWatcherTest @Override protected void onChange() { - + onChangeCalled = true; } } } -- GitLab