Author: cziegeler Date: Wed Aug 16 05:59:55 2017 New Revision: 1805139 URL: http://svn.apache.org/viewvc?rev=1805139&view=rev Log: SLING-7050 : Parser fails with trailing comments
Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt (with props) sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt (with props) sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt (with props) sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt (with props) Modified: sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java Modified: sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java?rev=1805139&r1=1805138&r2=1805139&view=diff ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java (original) +++ sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java Wed Aug 16 05:59:55 2017 @@ -20,6 +20,8 @@ package org.apache.sling.repoinit.parser import java.io.IOException; import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; import java.util.List; import org.apache.sling.repoinit.parser.RepoInitParser; @@ -36,11 +38,21 @@ import org.osgi.service.component.annota public class RepoInitParserService implements RepoInitParser { @Override - public List<Operation> parse(Reader r) throws RepoInitParsingException { - try { - return new RepoInitParserImpl(r).parse(); - } catch (ParseException pe) { - throw new RepoInitParsingException(pe.getMessage(), pe); + public List<Operation> parse(final Reader r) throws RepoInitParsingException { + // in order to avoid parsing problems with trailing comments we add a line feed at the end + try ( final StringWriter sw = new StringWriter()) { + final char[] buf = new char[2048]; + int l; + while ( (l = r.read(buf)) > 0 ) { + sw.write(buf, 0, l); + } + try (final StringReader sr = new StringReader(sw.toString().concat("\n")) ){ + return new RepoInitParserImpl(sr).parse(); + } catch (ParseException pe) { + throw new RepoInitParsingException(pe.getMessage(), pe); + } + } catch ( final IOException ioe ) { + throw new RepoInitParsingException(ioe.getMessage(), ioe); } finally { try { r.close(); Modified: sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java?rev=1805139&r1=1805138&r2=1805139&view=diff ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java (original) +++ sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java Wed Aug 16 05:59:55 2017 @@ -21,15 +21,17 @@ import static org.junit.Assert.assertEqu import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.PrintWriter; +import java.io.Reader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.io.IOUtils; -import org.apache.sling.repoinit.parser.impl.ParseException; -import org.apache.sling.repoinit.parser.impl.RepoInitParserImpl; +import org.apache.sling.repoinit.parser.RepoInitParsingException; +import org.apache.sling.repoinit.parser.impl.RepoInitParserService; import org.apache.sling.repoinit.parser.operations.Operation; import org.apache.sling.repoinit.parser.operations.OperationVisitor; import org.junit.Test; @@ -37,44 +39,49 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -/** Test the parser using our test-* input/expected output files. +/** Test the parser using our test-* input/expected output files. * The code of this class doesn't contain any actual tests, it * just looks for test-*.txt files, parses them and verifies the * results according to the test-*-output.txt files. */ @RunWith(Parameterized.class) public class ParserTest { - + public static final String DEFAULT_ENCODING = "UTF-8"; - + static class TestCase { - final InputStream input; + final Reader input; final String inputFilename; final InputStream expected; final String outputFilename; - - private static final String PREFIX = "/testcases/test-"; - + + private static final String PREFIX = "/testcases/test-"; + @Override public String toString() { return inputFilename; } - - private TestCase(int index) { + + private TestCase(int index) throws IOException { inputFilename = PREFIX + index + ".txt"; - input = getClass().getResourceAsStream(inputFilename); + final InputStream is = getClass().getResourceAsStream(inputFilename); + if ( is != null ) { + input = new InputStreamReader(is, "UTF-8"); + } else { + input = null; + } outputFilename = PREFIX + index + "-output.txt"; expected = getClass().getResourceAsStream(outputFilename); } - - static TestCase build(int index) { + + static TestCase build(int index) throws IOException { final TestCase result = new TestCase(index); if(result.input == null || result.expected == null) { return null; } return result; } - + void close() { try { input.close(); @@ -86,12 +93,12 @@ public class ParserTest { } } } - + private final TestCase tc; - + @Parameters(name="{0}") - public static Collection<Object[]> data() { - final List<Object []> result = new ArrayList<Object[]>(); + public static Collection<Object[]> data() throws IOException { + final List<Object []> result = new ArrayList<>(); for(int i=0; i < 100; i++) { final TestCase tc = TestCase.build(i); if(tc != null) { @@ -99,29 +106,29 @@ public class ParserTest { } } return result; - + } - + public ParserTest(TestCase tc) { this.tc = tc; } @Test - public void checkResult() throws ParseException, IOException { + public void checkResult() throws RepoInitParsingException, IOException { final String expected = IOUtils.toString(tc.expected, DEFAULT_ENCODING).trim(); try { final StringWriter sw = new StringWriter(); - final OperationVisitor v = new OperationToStringVisitor(new PrintWriter(sw)); - final List<Operation> result = new RepoInitParserImpl(tc.input).parse(); + final OperationVisitor v = new OperationToStringVisitor(new PrintWriter(sw)); + final List<Operation> result = new RepoInitParserService().parse(tc.input); for(Operation o : result) { o.accept(v); } sw.flush(); String actual = sw.toString().trim(); - + // normalize line endings to ensure tests run on windows as well actual = actual.replaceAll("\r\n", "\n"); - + assertEquals(expected, actual); } finally { tc.close(); Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt?rev=1805139&view=auto ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt (added) +++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt Wed Aug 16 05:59:55 2017 @@ -0,0 +1 @@ +CreateServiceUser comments_test_passed Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4-output.txt ------------------------------------------------------------------------------ svn:keywords = Id Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt?rev=1805139&view=auto ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt (added) +++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt Wed Aug 16 05:59:55 2017 @@ -0,0 +1,6 @@ +# trailing comments test +create service user comments_test_passed +# something + + + Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-4.txt ------------------------------------------------------------------------------ svn:keywords = Id Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt?rev=1805139&view=auto ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt (added) +++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt Wed Aug 16 05:59:55 2017 @@ -0,0 +1 @@ +CreateServiceUser comments_test_passed Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5-output.txt ------------------------------------------------------------------------------ svn:keywords = Id Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt?rev=1805139&view=auto ============================================================================== --- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt (added) +++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt Wed Aug 16 05:59:55 2017 @@ -0,0 +1,3 @@ +# trailing comments test without following blank lines +create service user comments_test_passed +# something \ No newline at end of file Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-5.txt ------------------------------------------------------------------------------ svn:keywords = Id