You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2022/01/20 20:06:08 UTC

[sling-org-apache-sling-repoinit-parser] branch SLING-11078 updated: Adding a method to get testcase suppliers to support having multiple tests within a parameterized test

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

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


The following commit(s) were added to refs/heads/SLING-11078 by this push:
     new a42aea0  Adding a method to get testcase suppliers to support having multiple tests within a parameterized test
a42aea0 is described below

commit a42aea00293976a91ec23e5d4c052fae2d93a4e3
Author: Dan Klco <kl...@adobe.com>
AuthorDate: Thu Jan 20 15:05:59 2022 -0500

    Adding a method to get testcase suppliers to support having multiple tests within a parameterized test
---
 .../repoinit/parser/operations/AsRepoInitTest.java | 33 ++++++++++++++--------
 .../sling/repoinit/parser/test/ParserTestCase.java | 30 +++++++++++++++++++-
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/src/test/java/org/apache/sling/repoinit/parser/operations/AsRepoInitTest.java b/src/test/java/org/apache/sling/repoinit/parser/operations/AsRepoInitTest.java
index 1c19cd2..3f8a834 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/operations/AsRepoInitTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/operations/AsRepoInitTest.java
@@ -31,6 +31,7 @@ import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 import java.util.Collection;
+import java.util.function.Supplier;
 
 /** Similar to {@link ParserTest} but uses {@link Operation#asRepoInitString()})
  *  to rebuild the input script after parsing it, to verify that that operation
@@ -39,33 +40,43 @@ import java.util.Collection;
 @RunWith(Parameterized.class)
 public class AsRepoInitTest {
 
-    private final ParserTestCase tc;
+    private final Supplier<ParserTestCase> testCaseSupplier;
 
     @Parameters(name="{0}")
     public static Collection<Object[]> data() throws IOException {
-        return ParserTestCase.buildTestData();
+        return ParserTestCase.buildTestDataSuppliers();
     }
 
-    public AsRepoInitTest(ParserTestCase tc) {
-        this.tc = tc;
+    public AsRepoInitTest(String testName, Supplier<ParserTestCase> testCaseSupplier) {
+        this.testCaseSupplier = testCaseSupplier;
     }
 
     /** Rebuild the input script using {@link Operation#asRepoInitString()}) */
     private static Reader rebuildInputScript(Reader input) throws Exception {
         StringBuilder sb = new StringBuilder();
         for (Operation o : new RepoInitParserService().parse(input)) {
-            String repoinitStatement = o.asRepoInitString();
-            assertTrue(
-                    "Operation.asRepoInitString() should always end with an-OS agnostic line separator. Not found for "
-                            + o.toString(),
-                    repoinitStatement.endsWith(System.lineSeparator()));
-            sb.append(repoinitStatement);
+            sb.append(o.asRepoInitString());
         }
         return new StringReader(sb.toString());
     }
 
     @Test
     public void checkResultAsRepoInit() throws Exception {
-        ParserTestCase.validate(rebuildInputScript(tc.input), tc.expected, tc);
+        try(ParserTestCase tc = testCaseSupplier.get()){
+            ParserTestCase.validate(rebuildInputScript(tc.input), tc.expected, tc);
+        }
+    }
+
+    @Test
+    public void checkRepoInitStatementNewline() throws Exception {
+        try (ParserTestCase tc = testCaseSupplier.get()) {
+            for (Operation o : new RepoInitParserService().parse(tc.input)) {
+                String repoinitStatement = o.asRepoInitString();
+                assertTrue(
+                        "Operation.asRepoInitString() should always end with an-OS compatible line separator. Not found for "
+                                + o.toString(),
+                        repoinitStatement.endsWith(System.lineSeparator()));
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParserTestCase.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParserTestCase.java
index 0c26a75..921b11f 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParserTestCase.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParserTestCase.java
@@ -30,6 +30,7 @@ import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.function.Supplier;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.repoinit.parser.RepoInitParsingException;
@@ -54,7 +55,7 @@ public class ParserTestCase implements Closeable {
     }
 
     private ParserTestCase(int index) throws IOException {
-        inputFilename = PREFIX + index + ".txt";
+        inputFilename = getFileName(index);
         final InputStream is = getClass().getResourceAsStream(inputFilename);
         if ( is != null ) {
             input = new InputStreamReader(is, "UTF-8");
@@ -73,6 +74,10 @@ public class ParserTestCase implements Closeable {
         return result;
     }
 
+    public static String getFileName(int index) {
+        return PREFIX + index + ".txt";
+    }
+
     public void close() {
         try {
             input.close();
@@ -118,4 +123,27 @@ public class ParserTestCase implements Closeable {
         }
         return result;
     }
+
+    public static Collection<Object[]> buildTestDataSuppliers() throws IOException {
+        final List<Object[]> result = new ArrayList<>();
+        for (int i = 0; i <= MAX_TEST_INDEX; i++) {
+            final int currentIdx = i;
+            Supplier<ParserTestCase> supplier = () -> {
+                try {
+                    return ParserTestCase.build(currentIdx);
+                } catch (IOException e) {
+                    return null;
+                }
+            };
+            try (ParserTestCase tc = supplier.get()) {
+                if(tc != null){
+                    result.add(new Object[] { ParserTestCase.getFileName(i), supplier });
+                }
+            }
+        }
+        if (result.size() < EXPECTED_TEST_COUNT) {
+            fail("Expected at least " + EXPECTED_TEST_COUNT + " test cases but got only " + result.size());
+        }
+        return result;
+    }
 }
\ No newline at end of file