SLING-11758 expose line and column in RepoInitParsingException (#29)
diff --git a/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java b/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
index fa40e52..451a5be 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
@@ -18,10 +18,44 @@
*/
package org.apache.sling.repoinit.parser;
+import org.osgi.annotation.versioning.ProviderType;
+
+@ProviderType
public class RepoInitParsingException extends Exception {
private static final long serialVersionUID = 1L;
+ private final int line;
+ private final int column;
+
public RepoInitParsingException(String reason, Throwable cause) {
super(reason, cause);
+ this.line = -1;
+ this.column = -1;
+ }
+
+ public RepoInitParsingException(Throwable cause) {
+ this(cause, -1, -1);
+ }
+
+ public RepoInitParsingException(Throwable cause, int line, int column) {
+ super(cause);
+ this.line = line;
+ this.column = column;
+ }
+
+ /**
+ *
+ * @return the line where the issue occurred or -1 if not known
+ */
+ public int getLine() {
+ return line;
+ }
+
+ /**
+ *
+ * @return the column where the issue occurred or -1 if not known
+ */
+ public int getColumn() {
+ return column;
}
}
diff --git a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
index d6ae70a..ca454b3 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
@@ -41,9 +41,11 @@
// in order to avoid parsing problems with trailing comments we add a line feed at the end
try (final Reader readerWrapper = new AddTailingLinefeedFilterReader(r)) {
return new RepoInitParserImpl(readerWrapper).parse();
- } catch ( final IOException | TokenMgrError | ParseException e ) {
- throw new RepoInitParsingException(e.getMessage(), e);
- }
+ } catch (ParseException e) {
+ throw new RepoInitParsingException(e, e.currentToken.next.beginLine, e.currentToken.next.beginColumn);
+ } catch (final IOException | TokenMgrError e ) {
+ throw new RepoInitParsingException(e);
+ }
}
private static final class AddTailingLinefeedFilterReader extends FilterReader {
diff --git a/src/main/java/org/apache/sling/repoinit/parser/package-info.java b/src/main/java/org/apache/sling/repoinit/parser/package-info.java
index 06ef5f6..252776b 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/package-info.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/package-info.java
@@ -15,6 +15,6 @@
* limitations under the License.
******************************************************************************/
-@org.osgi.annotation.versioning.Version("1.0.2")
+@org.osgi.annotation.versioning.Version("1.1.0")
package org.apache.sling.repoinit.parser;
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
index 1d7e9f7..21ee7d9 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
@@ -18,6 +18,7 @@
package org.apache.sling.repoinit.parser.test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import java.io.Reader;
import java.io.StringReader;
@@ -27,20 +28,38 @@
import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
import org.apache.sling.repoinit.parser.operations.CreateServiceUser;
import org.apache.sling.repoinit.parser.operations.Operation;
+import org.junit.Before;
import org.junit.Test;
public class ParserServiceTest {
+ private RepoInitParserService service;
+
+ @Before
+ public void setUp() {
+ service = new RepoInitParserService();
+ }
+
@Test
public void noErrors() throws RepoInitParsingException {
final Reader r = new StringReader("create service user foo");
- List<Operation> operations = new RepoInitParserService().parse(r);
+ List<Operation> operations = service.parse(r);
assertEquals(1, operations.size());
assertEquals(CreateServiceUser.class, operations.get(0).getClass());
}
-
- @Test(expected = RepoInitParsingException.class)
+
+ @Test
public void syntaxError() throws RepoInitParsingException {
final Reader r = new StringReader("not a valid statement");
- new RepoInitParserService().parse(r);
+ RepoInitParsingException exception = assertThrows(RepoInitParsingException.class, () -> service.parse(r));
+ assertEquals(1, exception.getLine());
+ assertEquals(1, exception.getColumn());
+ }
+
+ @Test
+ public void syntaxErrorInSecondLine() throws RepoInitParsingException {
+ final Reader r = new StringReader("create service user foo\n not a valid statement");
+ RepoInitParsingException exception = assertThrows(RepoInitParsingException.class, () -> service.parse(r));
+ assertEquals(2, exception.getLine());
+ assertEquals(3, exception.getColumn());
}
}
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
index 7e63c14..f319901 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
@@ -42,7 +42,7 @@
private final String input;
private final Class<? extends Throwable> expected;
- @Parameters
+ @Parameters(name="{0}")
public static Collection<Object[]> data() {
@SuppressWarnings("serial")
final List<Object []> result = new ArrayList<Object []>() {{