Skip to content
Snippets Groups Projects
Commit cbb10734 authored by ribeaudc's avatar ribeaudc
Browse files

add:

- more tests for 'FileWatcher'

SVN: 1404
parent a4b4fd9e
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment