Skip to content

Commit d631cc8

Browse files
committed
fix: don't use required code in assert
Replaced the usage of asserts with a new method called `validate()`. Fixes #6
1 parent b099ad0 commit d631cc8

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

src/main/java/org/codejive/properties/Cursor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,9 @@ public void remove() {
168168
public Cursor copy() {
169169
return Cursor.index(tokens, index);
170170
}
171+
172+
@Override
173+
public String toString() {
174+
return (hasToken() ? token() + " " : "") + "@" + position();
175+
}
171176
}

src/main/java/org/codejive/properties/Properties.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public String get(Object key) {
282282
public String getRaw(String rawKey) {
283283
Cursor pos = indexOf(unescape(rawKey));
284284
if (pos.hasToken()) {
285-
assert pos.nextIf(PropertiesParser.Type.KEY);
286-
assert pos.nextIf(PropertiesParser.Type.SEPARATOR);
287-
assert pos.isType(PropertiesParser.Type.VALUE);
285+
validate(pos.nextIf(PropertiesParser.Type.KEY), pos);
286+
validate(pos.nextIf(PropertiesParser.Type.SEPARATOR), pos);
287+
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
288288
return pos.raw();
289289
} else {
290290
return null;
@@ -343,22 +343,21 @@ public String putRaw(String rawKey, String rawValue) {
343343

344344
private void replaceValue(String key, String rawValue, String value) {
345345
Cursor pos = indexOf(key);
346-
assert pos.nextIf(PropertiesParser.Type.KEY);
347-
assert pos.nextIf(PropertiesParser.Type.SEPARATOR);
348-
assert pos.isType(PropertiesParser.Type.VALUE);
346+
validate(pos.nextIf(PropertiesParser.Type.KEY), pos);
347+
validate(pos.nextIf(PropertiesParser.Type.SEPARATOR), pos);
348+
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
349349
pos.replace(new PropertiesParser.Token(PropertiesParser.Type.VALUE, rawValue, value));
350350
}
351351

352352
// Add new tokens to the end of the list of tokens
353353
private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String value) {
354354
// Track back from end until we encounter the last VALUE token (if any)
355-
PropertiesParser.Token token;
356355
Cursor pos = last();
357356
while (pos.isType(PropertiesParser.Type.WHITESPACE, PropertiesParser.Type.COMMENT)) {
358357
pos.prev();
359358
}
360359
// Make sure we're either at the start or we've found a VALUE
361-
assert pos.atStart() || pos.isType(PropertiesParser.Type.VALUE);
360+
validate(pos.atStart() || pos.isType(PropertiesParser.Type.VALUE), pos);
362361
// Add a newline whitespace token if necessary
363362
if (pos.hasToken()) {
364363
pos.next();
@@ -397,11 +396,11 @@ public String remove(Object key) {
397396
private void removeItem(String skey) {
398397
setComment(skey, Collections.emptyList());
399398
Cursor pos = indexOf(skey);
400-
assert pos.isType(PropertiesParser.Type.KEY);
399+
validate(pos.isType(PropertiesParser.Type.KEY), pos);
401400
pos.remove();
402-
assert pos.isType(PropertiesParser.Type.SEPARATOR);
401+
validate(pos.isType(PropertiesParser.Type.SEPARATOR), pos);
403402
pos.remove();
404-
assert pos.isType(PropertiesParser.Type.VALUE);
403+
validate(pos.isType(PropertiesParser.Type.VALUE), pos);
405404
pos.remove();
406405
if (pos.isEol()) {
407406
pos.remove();
@@ -555,7 +554,7 @@ private List<Integer> findPropertyCommentLines(String key) {
555554
private List<Integer> findPropertyCommentLines(Cursor pos) {
556555
List<Integer> result = new ArrayList<>();
557556
Cursor fpos = pos.copy();
558-
assert fpos.isType(PropertiesParser.Type.KEY);
557+
validate(fpos.isType(PropertiesParser.Type.KEY), pos);
559558
fpos.prev();
560559
// Skip a single preceding whitespace if it is NOT an EOL token
561560
fpos.prevIf(PropertiesParser.Token::isWs);
@@ -796,4 +795,10 @@ private Cursor first() {
796795
private Cursor last() {
797796
return Cursor.last(tokens);
798797
}
798+
799+
private void validate(boolean ok, Cursor cursor) {
800+
if (!ok) {
801+
throw new IllegalStateException("Unexpected state detected at " + cursor);
802+
}
803+
}
799804
}

src/test/java/org/codejive/properties/TestProperties.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,26 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
305305
@Test
306306
void testPutNull() throws IOException, URISyntaxException {
307307
Properties p = new Properties();
308-
assertThatThrownBy(() -> {
309-
p.put("one", null);
310-
}).isInstanceOf(NullPointerException.class);
311-
assertThatThrownBy(() -> {
312-
p.setProperty("one", null);
313-
}).isInstanceOf(NullPointerException.class);
314-
assertThatThrownBy(() -> {
315-
p.put(null, "value");
316-
}).isInstanceOf(NullPointerException.class);
317-
assertThatThrownBy(() -> {
318-
p.setProperty(null, "value");
319-
}).isInstanceOf(NullPointerException.class);
308+
assertThatThrownBy(
309+
() -> {
310+
p.put("one", null);
311+
})
312+
.isInstanceOf(NullPointerException.class);
313+
assertThatThrownBy(
314+
() -> {
315+
p.setProperty("one", null);
316+
})
317+
.isInstanceOf(NullPointerException.class);
318+
assertThatThrownBy(
319+
() -> {
320+
p.put(null, "value");
321+
})
322+
.isInstanceOf(NullPointerException.class);
323+
assertThatThrownBy(
324+
() -> {
325+
p.setProperty(null, "value");
326+
})
327+
.isInstanceOf(NullPointerException.class);
320328
}
321329

322330
@Test

0 commit comments

Comments
 (0)