Skip to content

fix: don't use required code in assert #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/codejive/properties/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ public void remove() {
public Cursor copy() {
return Cursor.index(tokens, index);
}

@Override
public String toString() {
return (hasToken() ? token() + " " : "") + "@" + position();
}
}
29 changes: 17 additions & 12 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ public String get(Object key) {
public String getRaw(String rawKey) {
Cursor pos = indexOf(unescape(rawKey));
if (pos.hasToken()) {
assert pos.nextIf(PropertiesParser.Type.KEY);
assert pos.nextIf(PropertiesParser.Type.SEPARATOR);
assert pos.isType(PropertiesParser.Type.VALUE);
validate(pos.nextIf(PropertiesParser.Type.KEY), pos);
validate(pos.nextIf(PropertiesParser.Type.SEPARATOR), pos);
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
return pos.raw();
} else {
return null;
Expand Down Expand Up @@ -343,22 +343,21 @@ public String putRaw(String rawKey, String rawValue) {

private void replaceValue(String key, String rawValue, String value) {
Cursor pos = indexOf(key);
assert pos.nextIf(PropertiesParser.Type.KEY);
assert pos.nextIf(PropertiesParser.Type.SEPARATOR);
assert pos.isType(PropertiesParser.Type.VALUE);
validate(pos.nextIf(PropertiesParser.Type.KEY), pos);
validate(pos.nextIf(PropertiesParser.Type.SEPARATOR), pos);
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
pos.replace(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
}

// Add new tokens to the end of the list of tokens
private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String value) {
// Track back from end until we encounter the last VALUE token (if any)
PropertiesParser.Token token;
Cursor pos = last();
while (pos.isType(PropertiesParser.Type.WHITESPACE, PropertiesParser.Type.COMMENT)) {
pos.prev();
}
// Make sure we're either at the start or we've found a VALUE
assert pos.atStart() || pos.isType(PropertiesParser.Type.VALUE);
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE), pos);
// Add a newline whitespace token if necessary
if (pos.hasToken()) {
pos.next();
Expand Down Expand Up @@ -397,11 +396,11 @@ public String remove(Object key) {
private void removeItem(String skey) {
setComment(skey, Collections.emptyList());
Cursor pos = indexOf(skey);
assert pos.isType(PropertiesParser.Type.KEY);
validate(pos.isType(PropertiesParser.Type.KEY), pos);
pos.remove();
assert pos.isType(PropertiesParser.Type.SEPARATOR);
validate(pos.isType(PropertiesParser.Type.SEPARATOR), pos);
pos.remove();
assert pos.isType(PropertiesParser.Type.VALUE);
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
pos.remove();
if (pos.isEol()) {
pos.remove();
Expand Down Expand Up @@ -555,7 +554,7 @@ private List<Integer> findPropertyCommentLines(String key) {
private List<Integer> findPropertyCommentLines(Cursor pos) {
List<Integer> result = new ArrayList<>();
Cursor fpos = pos.copy();
assert fpos.isType(PropertiesParser.Type.KEY);
validate(fpos.isType(PropertiesParser.Type.KEY), pos);
fpos.prev();
// Skip a single preceding whitespace if it is NOT an EOL token
fpos.prevIf(PropertiesParser.Token::isWs);
Expand Down Expand Up @@ -796,4 +795,10 @@ private Cursor first() {
private Cursor last() {
return Cursor.last(tokens);
}

private void validate(boolean ok, Cursor cursor) {
if (!ok) {
throw new IllegalStateException("Unexpected state detected at " + cursor);
}
}
}
32 changes: 20 additions & 12 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,26 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
@Test
void testPutNull() throws IOException, URISyntaxException {
Properties p = new Properties();
assertThatThrownBy(() -> {
p.put("one", null);
}).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> {
p.setProperty("one", null);
}).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> {
p.put(null, "value");
}).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> {
p.setProperty(null, "value");
}).isInstanceOf(NullPointerException.class);
assertThatThrownBy(
() -> {
p.put("one", null);
})
.isInstanceOf(NullPointerException.class);
assertThatThrownBy(
() -> {
p.setProperty("one", null);
})
.isInstanceOf(NullPointerException.class);
assertThatThrownBy(
() -> {
p.put(null, "value");
})
.isInstanceOf(NullPointerException.class);
assertThatThrownBy(
() -> {
p.setProperty(null, "value");
})
.isInstanceOf(NullPointerException.class);
}

@Test
Expand Down