Skip to content

Commit 46bd1c2

Browse files
committed
Javadoc
- Use JUnit fail() - Use final - Remove unnecessary parentheses - Reduce vertical whitespace
1 parent f93d421 commit 46bd1c2

File tree

2 files changed

+22
-64
lines changed

2 files changed

+22
-64
lines changed

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

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ protected void createFile(final File file, final long size) throws IOException {
198198
try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
199199
TestUtils.generateTestData(output, size);
200200
}
201-
202201
// try to make sure file is found
203202
// (to stop continuum occasionally failing)
204203
RandomAccessFile reader = null;
@@ -219,36 +218,33 @@ protected void createFile(final File file, final long size) throws IOException {
219218
assertEquals(size, file.length());
220219
}
221220

222-
private List<String> expectLinesWithLongTimeout(final TestTailerListener listener, final long minDelay, int count) throws Exception {
223-
for (int i = 0; i < count ; i++) {
221+
private List<String> expectLinesWithLongTimeout(final TestTailerListener listener, final long minDelay, final int count) throws Exception {
222+
for (int i = 0; i < count; i++) {
224223
TestUtils.sleep(minDelay);
225224
final List<String> lines = listener.getLines();
226225
if (lines.size() > 0) {
227226
return lines;
228227
}
229228
}
230-
throw new RuntimeException("waiting for TestTailerListener.getLines() timed out after " + (count * minDelay) + " ms");
229+
fail("Waiting for TestTailerListener.getLines() timed out after " + count * minDelay + " ms");
230+
return null;
231231
}
232232

233233
@Test
234234
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
235235
void testBufferBreak() throws Exception {
236236
final long delay = 50;
237-
238237
final File file = new File(temporaryFolder, "testBufferBreak.txt");
239238
createFile(file, 0);
240239
writeStrings(file, "SBTOURIST\n");
241-
242240
final TestTailerListener listener = new TestTailerListener();
243241
try (Tailer tailer = new Tailer(file, listener, delay, false, 1)) {
244242
final Thread thread = new Thread(tailer);
245243
thread.start();
246-
247244
List<String> lines = listener.getLines();
248245
while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) {
249246
lines = listener.getLines();
250247
}
251-
252248
listener.clear();
253249
}
254250
}
@@ -393,7 +389,6 @@ void testIO335() throws Exception { // test CR behavior
393389
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
394390
void testLongFile() throws Exception {
395391
final long delay = 50;
396-
397392
final File file = new File(temporaryFolder, "testLongFile.txt");
398393
createFile(file, 0);
399394
try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) {
@@ -402,21 +397,16 @@ void testLongFile() throws Exception {
402397
}
403398
writer.write("SBTOURIST\n");
404399
}
405-
406400
final TestTailerListener listener = new TestTailerListener();
407401
try (Tailer tailer = new Tailer(file, listener, delay, false)) {
408-
409402
// final long start = System.currentTimeMillis();
410-
411403
final Thread thread = new Thread(tailer);
412404
thread.start();
413-
414405
List<String> lines = listener.getLines();
415406
while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) {
416407
lines = listener.getLines();
417408
}
418409
// System.out.println("Elapsed: " + (System.currentTimeMillis() - start));
419-
420410
listener.clear();
421411
}
422412
}
@@ -436,9 +426,8 @@ void testMultiByteBreak() throws Exception {
436426
try (Tailer tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, IOUtils.DEFAULT_BUFFER_SIZE)) {
437427
final Thread thread = new Thread(tailer);
438428
thread.start();
439-
440429
try (Writer out = new OutputStreamWriter(Files.newOutputStream(file.toPath()), charsetUTF8);
441-
BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(origin.toPath()), charsetUTF8))) {
430+
BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(origin.toPath()), charsetUTF8))) {
442431
final List<String> lines = new ArrayList<>();
443432
String line;
444433
while ((line = reader.readLine()) != null) {
@@ -447,7 +436,6 @@ void testMultiByteBreak() throws Exception {
447436
lines.add(line);
448437
}
449438
out.close(); // ensure data is written
450-
451439
final long testDelayMillis = delay * 10;
452440
TestUtils.sleep(testDelayMillis);
453441
final List<String> tailerlines = listener.getLines();
@@ -590,7 +578,6 @@ void testStopWithNoFileUsingExecutor() throws Exception {
590578

591579
@Test
592580
void testTailer() throws Exception {
593-
594581
// Create & start the Tailer
595582
final long delayMillis = 50;
596583
final File file = new File(temporaryFolder, "tailer1-test.txt");
@@ -601,7 +588,6 @@ void testTailer() throws Exception {
601588
try (Tailer tailer = new Tailer(file, listener, delayMillis, false, isWindows)) {
602589
final Thread thread = new Thread(tailer);
603590
thread.start();
604-
605591
// Write some lines to the file
606592
writeLines(file, "Line one", "Line two");
607593
final long testDelayMillis = delayMillis * 10;
@@ -611,37 +597,32 @@ void testTailer() throws Exception {
611597
assertEquals("Line one", lines.get(0), "1 line 1");
612598
assertEquals("Line two", lines.get(1), "1 line 2");
613599
listener.clear();
614-
615600
// Write another line to the file
616601
writeLines(file, "Line three");
617602
TestUtils.sleep(testDelayMillis);
618603
lines = listener.getLines();
619604
assertEquals(1, lines.size(), "2 line count");
620605
assertEquals("Line three", lines.get(0), "2 line 3");
621606
listener.clear();
622-
623607
// Check file does actually have all the lines
624608
lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
625609
assertEquals(3, lines.size(), "3 line count");
626610
assertEquals("Line one", lines.get(0), "3 line 1");
627611
assertEquals("Line two", lines.get(1), "3 line 2");
628612
assertEquals("Line three", lines.get(2), "3 line 3");
629-
630613
// Delete & re-create
631614
file.delete();
632615
assertFalse(file.exists(), "File should not exist");
633616
createFile(file, 0);
634617
assertTrue(file.exists(), "File should now exist");
635618
TestUtils.sleep(testDelayMillis);
636-
637619
// Write another line
638620
writeLines(file, "Line four");
639621
TestUtils.sleep(testDelayMillis);
640622
lines = listener.getLines();
641623
assertEquals(1, lines.size(), "4 line count");
642624
assertEquals("Line four", lines.get(0), "4 line 3");
643625
listener.clear();
644-
645626
// Stop
646627
thread.interrupt();
647628
TestUtils.sleep(testDelayMillis * 4);
@@ -669,19 +650,15 @@ void testTailerEndOfFileReached() throws Exception {
669650
try (Tailer tailer = new Tailer(file, listener, delayMillis, false, isWindows)) {
670651
final Thread thread = new Thread(tailer);
671652
thread.start();
672-
673653
// write a few lines
674654
writeLines(file, "line1", "line2", "line3");
675655
TestUtils.sleep(testDelayMillis);
676-
677656
// write a few lines
678657
writeLines(file, "line4", "line5", "line6");
679658
TestUtils.sleep(testDelayMillis);
680-
681659
// write a few lines
682660
writeLines(file, "line7", "line8", "line9");
683661
TestUtils.sleep(testDelayMillis);
684-
685662
// May be > 3 times due to underlying OS behavior wrt streams
686663
assertTrue(listener.reachedEndOfFile >= 3, "end of file reached at least 3 times");
687664
}
@@ -697,21 +674,16 @@ void testTailerEof() throws Exception {
697674
try (Tailer tailer = new Tailer(file, listener, delayMillis, false)) {
698675
final Thread thread = new Thread(tailer);
699676
thread.start();
700-
701677
// Write some lines to the file
702678
writeStrings(file, "Line");
703-
704679
TestUtils.sleep(delayMillis * 2);
705680
List<String> lines = listener.getLines();
706681
assertEquals(0, lines.size(), "1 line count");
707-
708682
writeStrings(file, " one\n");
709683
TestUtils.sleep(delayMillis * 4);
710684
lines = listener.getLines();
711-
712685
assertEquals(1, lines.size(), "1 line count");
713686
assertEquals("Line one", lines.get(0), "1 line 1");
714-
715687
listener.clear();
716688
}
717689
}
@@ -723,25 +695,18 @@ void testTailerIgnoreTouch() throws Exception {
723695
final File file = new File(temporaryFolder, "tailer1-testIgnoreTouch.txt");
724696
createFile(file, 0);
725697
final TestTailerListener listener = new TestTailerListener();
726-
try (Tailer tailer = Tailer.builder()
727-
.setFile(file)
728-
.setTailerListener(listener)
729-
.setDelayDuration(Duration.ofMillis(delayMillis))
730-
.setStartThread(false)
731-
.setIgnoreTouch(true)
732-
.get()) {
698+
try (Tailer tailer = Tailer.builder().setFile(file).setTailerListener(listener).setDelayDuration(Duration.ofMillis(delayMillis)).setStartThread(false)
699+
.setIgnoreTouch(true).get()) {
733700
final Thread thread = new Thread(tailer);
734701
thread.start();
735-
736702
// Write some lines to the file
737703
writeLines(file, "Line one");
738704
List<String> lines = expectLinesWithLongTimeout(listener, delayMillis, 20);
739705
assertEquals(1, lines.size(), "1 line count");
740706
assertEquals("Line one", lines.get(0), "1 line 1");
741707
listener.clear();
742-
743708
// touch the file
744-
TestUtils.sleepTillNextFullSecond(); // ensure to be within the next second because of posix fs limitation
709+
TestUtils.sleepToNextSecond(); // ensure to be within the next second because of posix fs limitation
745710
file.setLastModified(System.currentTimeMillis());
746711
TestUtils.sleep(delayMillis * 10);
747712
lines = listener.getLines();
@@ -756,25 +721,18 @@ void testTailerReissueOnTouch() throws Exception {
756721
final File file = new File(temporaryFolder, "tailer1-testReissueOnTouch.txt");
757722
createFile(file, 0);
758723
final TestTailerListener listener = new TestTailerListener();
759-
try (Tailer tailer = Tailer.builder()
760-
.setFile(file)
761-
.setTailerListener(listener)
762-
.setDelayDuration(Duration.ofMillis(delayMillis))
763-
.setStartThread(false)
764-
.setIgnoreTouch(false)
765-
.get()) {
724+
try (Tailer tailer = Tailer.builder().setFile(file).setTailerListener(listener).setDelayDuration(Duration.ofMillis(delayMillis)).setStartThread(false)
725+
.setIgnoreTouch(false).get()) {
766726
final Thread thread = new Thread(tailer);
767727
thread.start();
768-
769728
// Write some lines to the file
770729
writeLines(file, "Line one");
771730
List<String> lines = expectLinesWithLongTimeout(listener, delayMillis, 50);
772731
assertEquals(1, lines.size(), "1 line count");
773732
assertEquals("Line one", lines.get(0), "1 line 1");
774733
listener.clear();
775-
776734
// touch the file
777-
TestUtils.sleepTillNextFullSecond(); // ensure to be within the next second because of posix fs limitation
735+
TestUtils.sleepToNextSecond(); // ensure to be within the next second because of posix fs limitation
778736
file.setLastModified(System.currentTimeMillis());
779737
lines = expectLinesWithLongTimeout(listener, delayMillis, 20);
780738
assertEquals(1, lines.size(), "1 line count");

src/test/java/org/apache/commons/io/test/TestUtils.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ public static File newFile(final File testDirectory, final String fileName) thro
241241
/**
242242
* Sleeps for a guaranteed number of milliseconds unless interrupted.
243243
*
244-
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
245-
* it deems appropriate. Read the docs on Thread.sleep for further details.
244+
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else it deems appropriate. Read the docs on Thread.sleep for
245+
* further details.
246246
*
247247
* @param millis the number of milliseconds to sleep.
248-
* @throws InterruptedException if interrupted.
248+
* @throws InterruptedException if {@code interrupt()} was called for this Thread while it was sleeping.
249249
*/
250250
public static void sleep(final long millis) throws InterruptedException {
251251
ThreadUtils.sleep(Duration.ofMillis(millis));
@@ -265,16 +265,16 @@ public static void sleepQuietly(final long millis) {
265265
}
266266

267267
/**
268-
* Sleeps till the next full second.
268+
* Sleeps to the next full second boundary.
269+
* <p>
270+
* Use this method when to set a guaranteed newer file system timestamp. POSIX file systems only guarantee one second resolution, for example some macOS
271+
* file systems.
272+
* </p>
269273
*
270-
* This method is useful when you want to set a guaranteed newer file system timestamp.
271-
* Posix file systems only guarantee one second resolution, and e.g.
272-
* some mac os x filesystems only offer that.
273-
*
274-
* @throws InterruptedException if interrupted.
274+
* @throws InterruptedException if {@code interrupt()} was called for this Thread while it was sleeping.
275275
*/
276-
public static void sleepTillNextFullSecond() throws InterruptedException {
277-
sleep(1001 - (System.currentTimeMillis() % 1000));
276+
public static void sleepToNextSecond() throws InterruptedException {
277+
sleep(1001 - System.currentTimeMillis() % 1000);
278278
}
279279

280280
private TestUtils() {

0 commit comments

Comments
 (0)