Author: sebb
Date: Thu Sep 30 01:08:56 2010
New Revision: 1002918
URL: http://svn.apache.org/viewvc?rev=1002918&view=rev
Log:
Add test for and fix NPE if thread stopped with no file found
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/input/Tailer.java
commons/proper/io/trunk/src/test/org/apache/commons/io/input/TailerTest.java
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/input/Tailer.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/Tailer.java?rev=1002918&r1=1002917&r2=1002918&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/input/Tailer.java
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/Tailer.java
Thu Sep 30 01:08:56 2010
@@ -148,13 +148,14 @@ public class Tailer implements Runnable
if (reader == null) {
Thread.sleep(delay);
+ } else {
+ // The current position in the file
+ position = end ? file.length() : 0;
+ last = System.currentTimeMillis();
+ reader.seek(position);
}
}
- // The current position in the file
- position = end ? file.length() : 0;
- last = System.currentTimeMillis();
- reader.seek(position);
while (run) {
Modified:
commons/proper/io/trunk/src/test/org/apache/commons/io/input/TailerTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/TailerTest.java?rev=1002918&r1=1002917&r2=1002918&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/org/apache/commons/io/input/TailerTest.java
(original)
+++
commons/proper/io/trunk/src/test/org/apache/commons/io/input/TailerTest.java
Thu Sep 30 01:08:56 2010
@@ -110,6 +110,19 @@ public class TailerTest extends FileBase
}
}
+ public void testStopWithNoFile() throws Exception {
+ File file = new File(getTestDirectory(),"nosuchfile");
+ assertFalse("nosuchfile should not exist", file.exists());
+ TestTailerListener listener = new TestTailerListener();
+ int delay = 100;
+ int idle = 50; // allow time for thread to work
+ Tailer tailer = start(file, listener, delay, false);
+ Thread.sleep(idle);
+ tailer.stop();
+ Thread.sleep(delay+idle);
+ assertNull(listener.exception);
+ }
+
/**
* Test {...@link TailerListener} implementation.
*/
@@ -117,6 +130,9 @@ public class TailerTest extends FileBase
private final List<String> lines = new ArrayList<String>();
+ volatile Exception exception = null;
+
+ @Override
public void handle(String line) {
lines.add(line);
}
@@ -126,5 +142,9 @@ public class TailerTest extends FileBase
public void clear() {
lines.clear();
}
+ @Override
+ public void handle(Exception e) {
+ exception = e;
+ }
}
}