Skip to content

Commit

Permalink
OAK-10596: Improve the test coverage of o.a.j.o.namepath.JcrPathParser
Browse files Browse the repository at this point in the history
Added and improved test cases
  • Loading branch information
mbaedke committed Jan 19, 2024
1 parent 0e541b2 commit 8669972
Showing 1 changed file with 114 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public boolean equals(Object obj) {
}
return false;
}

@Override
public String toString() {
return data == null ? "null" : data;
}
}

private static final ParserCallbackResult CALLBACKRESULT_ROOT = new ParserCallbackResult(ParserCallbackResultType.CALLBACK_ROOT, null, 0);
Expand Down Expand Up @@ -190,7 +195,7 @@ public void testEmptyPath() {
public void testUnexpectedOpeningSquareBracket() throws RepositoryException {
String path = "[";
TestListener listener = new TestListener(
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
Expand All @@ -199,7 +204,7 @@ public void testUnexpectedOpeningSquareBracket() throws RepositoryException {
path = "/[";
listener = new TestListener(
CALLBACKRESULT_ROOT,
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
Expand All @@ -208,7 +213,7 @@ public void testUnexpectedOpeningSquareBracket() throws RepositoryException {
path = "./[";
listener = new TestListener(
CALLBACKRESULT_CURRENT,
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
Expand All @@ -217,39 +222,40 @@ public void testUnexpectedOpeningSquareBracket() throws RepositoryException {
path = "../[";
listener = new TestListener(
CALLBACKRESULT_PARENT,
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = ".[";
listener = new TestListener(
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "..[";
listener = new TestListener(
CALLBACKRESULT_ERROR("'[' not allowed in name")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "{[";
path = "{[}";
listener = new TestListener(
CALLBACKRESULT_ERROR("'{[' is not a valid path. Missing '}'.")
CALLBACKRESULT_ERROR(errorCharacterNotAllowedInName('['))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "a[[";
listener = new TestListener(
CALLBACKRESULT_ERROR("'[' not allowed in name")
//the parser actually produces an error, but we should change the error message to something like this
CALLBACKRESULT_ERROR(errorClosingQuareBracketExpected(path))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
Expand All @@ -263,10 +269,108 @@ public void testMissingClosingSquareBracket() throws RepositoryException {
TestListener listener = new TestListener(
CALLBACKRESULT_ROOT,
//the parser actually produces an error, but we should change the error message to something like this
CALLBACKRESULT_ERROR("'/a[' is not a valid path. ']' expected after index.")
CALLBACKRESULT_ERROR(errorClosingQuareBracketExpected(path))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();
}

@Test
public void testUnxepectedClosingSquareBracket() throws RepositoryException {
String path = "]";
TestListener listener = new TestListener(
CALLBACKRESULT_ERROR(errorInvalidCharacterInName(path, ']'))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "/]";
listener = new TestListener(
CALLBACKRESULT_ROOT,
CALLBACKRESULT_ERROR(errorInvalidCharacterInName(path, ']'))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = ".]";
listener = new TestListener(
//TODO improve error message?
CALLBACKRESULT_ERROR(errorInvalidCharacterInName(path, ']'))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "..]";
listener = new TestListener(
//TODO improve error message?
CALLBACKRESULT_ERROR(errorInvalidCharacterInName(path, ']'))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "{]}";
listener = new TestListener(
CALLBACKRESULT_ERROR(errorInvalidCharacterInName(path, ']'))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();

path = "a[]]";
listener = new TestListener(
CALLBACKRESULT_ERROR(errorNumberFormatExceptionInIndex(path))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();
}

@Test
public void testUnxepectedOpeningCurlyBracket() throws RepositoryException {
String path = "/{";
TestListener listener = new TestListener(
CALLBACKRESULT_ROOT,
CALLBACKRESULT_ERROR(errorMissingClosingCurlyBracket(path))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();
}
@Test

public void testMissingClosingCurlyBracket() throws RepositoryException {
String path = "{a";
TestListener listener = new TestListener(
CALLBACKRESULT_ERROR(errorMissingClosingCurlyBracket(path))
);
assertFalse(JcrPathParser.validate(path));
assertFalse(JcrPathParser.parse(path, listener));
listener.evaluate();
}

//TODO replace with #errorInvalidCharacterInName() to make error messages consistent?
private static String errorCharacterNotAllowedInName(char c) {
return "'" + c + "' not allowed in name";
}

private static String errorInvalidCharacterInName(String path, char c) {
return "'" + path + "' is not a valid path. ']' not a valid name character.";
}

private static String errorClosingQuareBracketExpected(String path) {
return "'" + path + "' is not a valid path. ']' expected after index.";
}

private static String errorNumberFormatExceptionInIndex(String path) {
return "'" + path + "' is not a valid path. NumberFormatException in index: ";
}

private static String errorMissingClosingCurlyBracket(String path) {
return "'" + path + "' is not a valid path. Missing '}'.";
}
}

0 comments on commit 8669972

Please sign in to comment.