You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/20 14:39:43 UTC

[sling-org-apache-sling-repoinit-parser] 34/46: SLING-7050 : Parser fails with trailing comments

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git

commit bf352b64b1c5bb6e9f95295ff2cee039acd45a84
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Aug 16 05:59:55 2017 +0000

    SLING-7050 : Parser fails with trailing comments
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1805139 13f79535-47bb-0310-9956-ffa450edef68
---
 .../parser/impl/RepoInitParserService.java         | 22 ++++++--
 .../sling/repoinit/parser/test/ParserTest.java     | 59 ++++++++++++----------
 src/test/resources/testcases/test-4-output.txt     |  1 +
 src/test/resources/testcases/test-4.txt            |  6 +++
 src/test/resources/testcases/test-5-output.txt     |  1 +
 src/test/resources/testcases/test-5.txt            |  3 ++
 6 files changed, 61 insertions(+), 31 deletions(-)

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 c0a588b..696fbc2 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
@@ -20,6 +20,8 @@ package org.apache.sling.repoinit.parser.impl;
 
 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.annotations.Component;
 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();
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java
index 94c77e2..d51dd6d 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParserTest.java
@@ -21,15 +21,17 @@ import static org.junit.Assert.assertEquals;
 
 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();
diff --git a/src/test/resources/testcases/test-4-output.txt b/src/test/resources/testcases/test-4-output.txt
new file mode 100644
index 0000000..0178953
--- /dev/null
+++ b/src/test/resources/testcases/test-4-output.txt
@@ -0,0 +1 @@
+CreateServiceUser comments_test_passed
diff --git a/src/test/resources/testcases/test-4.txt b/src/test/resources/testcases/test-4.txt
new file mode 100644
index 0000000..9dd0a17
--- /dev/null
+++ b/src/test/resources/testcases/test-4.txt
@@ -0,0 +1,6 @@
+# trailing comments test
+create service user comments_test_passed
+# something
+
+
+
diff --git a/src/test/resources/testcases/test-5-output.txt b/src/test/resources/testcases/test-5-output.txt
new file mode 100644
index 0000000..0178953
--- /dev/null
+++ b/src/test/resources/testcases/test-5-output.txt
@@ -0,0 +1 @@
+CreateServiceUser comments_test_passed
diff --git a/src/test/resources/testcases/test-5.txt b/src/test/resources/testcases/test-5.txt
new file mode 100644
index 0000000..7f71efd
--- /dev/null
+++ b/src/test/resources/testcases/test-5.txt
@@ -0,0 +1,3 @@
+# trailing comments test without following blank lines
+create service user comments_test_passed
+# something
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.