Skip to content

Commit

Permalink
Merge pull request #333 from snuyanzin/JLINE_332
Browse files Browse the repository at this point in the history
[JLINE3-332] Throw IllegalArgumentException in case there is no times…
  • Loading branch information
gnodet committed Nov 20, 2018
2 parents 5a781bb + ac87e85 commit f65e68b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.*;
import java.nio.file.*;
import java.time.DateTimeException;
import java.time.Instant;
import java.util.*;

Expand Down Expand Up @@ -100,12 +101,19 @@ public void load() throws IOException {
protected void addHistoryLine(Path path, String line) {
if (reader.isSet(LineReader.Option.HISTORY_TIMESTAMPED)) {
int idx = line.indexOf(':');
final String badHistoryFileSyntax = "Bad history file syntax! " +
"The history file `" + path + "` may be an older history: " +
"please remove it or use a different history file.";
if (idx < 0) {
throw new IllegalArgumentException("Bad history file syntax! " +
"The history file `" + path + "` may be an older history: " +
"please remove it or use a different history file.");
throw new IllegalArgumentException(badHistoryFileSyntax);
}
Instant time = Instant.ofEpochMilli(Long.parseLong(line.substring(0, idx)));
Instant time;
try {
time = Instant.ofEpochMilli(Long.parseLong(line.substring(0, idx)));
} catch (DateTimeException | NumberFormatException e) {
throw new IllegalArgumentException(badHistoryFileSyntax);
}

String unescaped = unescape(line.substring(idx + 1));
internalAdd(time, unescaped);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Tests for {@link DefaultHistory}.
Expand Down Expand Up @@ -148,4 +150,30 @@ public void testTrim() {
assertEquals("a", trimmed.get(2).line());
}

@Test
public void testAddHistoryLine() throws IOException {
final Path histFile = Files.createTempFile(null, null);

reader.setOpt(LineReader.Option.HISTORY_TIMESTAMPED);
try {
history.addHistoryLine(histFile, ":test");
fail("Wrong handling of timestamped history");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Bad history file syntax!"));
}

try {
history.addHistoryLine(histFile, "test:test");
fail("Wrong handling of timestamped history");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Bad history file syntax!"));
}

try {
history.addHistoryLine(histFile, "123456789123456789123456789:test");
fail("Wrong handling of timestamped history ");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Bad history file syntax!"));
}
}
}

0 comments on commit f65e68b

Please sign in to comment.