Skip to content

Commit a32a395

Browse files
committed
Add test for and fix NPE if thread stopped with no file found
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1002918 13f79535-47bb-0310-9956-ffa450edef68
1 parent a360f97 commit a32a395

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/java/org/apache/commons/io/input/Tailer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ public void run() {
148148

149149
if (reader == null) {
150150
Thread.sleep(delay);
151+
} else {
152+
// The current position in the file
153+
position = end ? file.length() : 0;
154+
last = System.currentTimeMillis();
155+
reader.seek(position);
151156
}
152157
}
153158

154-
// The current position in the file
155-
position = end ? file.length() : 0;
156-
last = System.currentTimeMillis();
157-
reader.seek(position);
158159

159160
while (run) {
160161

src/test/org/apache/commons/io/input/TailerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,29 @@ private void write(File file, String... lines) throws Exception {
110110
}
111111
}
112112

113+
public void testStopWithNoFile() throws Exception {
114+
File file = new File(getTestDirectory(),"nosuchfile");
115+
assertFalse("nosuchfile should not exist", file.exists());
116+
TestTailerListener listener = new TestTailerListener();
117+
int delay = 100;
118+
int idle = 50; // allow time for thread to work
119+
Tailer tailer = start(file, listener, delay, false);
120+
Thread.sleep(idle);
121+
tailer.stop();
122+
Thread.sleep(delay+idle);
123+
assertNull(listener.exception);
124+
}
125+
113126
/**
114127
* Test {@link TailerListener} implementation.
115128
*/
116129
private static class TestTailerListener extends TailerListenerAdapter {
117130

118131
private final List<String> lines = new ArrayList<String>();
119132

133+
volatile Exception exception = null;
134+
135+
@Override
120136
public void handle(String line) {
121137
lines.add(line);
122138
}
@@ -126,5 +142,9 @@ public List<String> getLines() {
126142
public void clear() {
127143
lines.clear();
128144
}
145+
@Override
146+
public void handle(Exception e) {
147+
exception = e;
148+
}
129149
}
130150
}

0 commit comments

Comments
 (0)