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/21 13:26:00 UTC
[sling-org-apache-sling-repoinit-parser] branch master updated: SLING-11078 - Describe and Validate asRepoInitString Line Separator Requirement (#16)
This is an automated email from the ASF dual-hosted git repository.
dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git
The following commit(s) were added to refs/heads/master by this push:
new b568e14 SLING-11078 - Describe and Validate asRepoInitString Line Separator Requirement (#16)
b568e14 is described below
commit b568e14235fa6ea4ac7f710bbf4149b450f421d2
Author: Dan Klco <kl...@users.noreply.github.com>
AuthorDate: Fri Jan 21 08:25:56 2022 -0500
SLING-11078 - Describe and Validate asRepoInitString Line Separator Requirement (#16)
* SLING-11078 - Fixing regression in DeleteGroup where line separater no longer aded to asRepoInitString()
* SLING-11078 - Adding JavaDocs to describe the Operation.asRepoInitString() contract and validate line separate compliance
* Adding a method to get testcase suppliers to support having multiple tests within a parameterized test
* Cleaning up whitespace issues
---
.../repoinit/parser/operations/DeleteGroup.java | 2 +-
.../repoinit/parser/operations/Operation.java | 8 ++++++
.../repoinit/parser/operations/AsRepoInitTest.java | 27 +++++++++++++++----
.../sling/repoinit/parser/test/ParserTestCase.java | 30 +++++++++++++++++++++-
4 files changed, 60 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/DeleteGroup.java b/src/main/java/org/apache/sling/repoinit/parser/operations/DeleteGroup.java
index 3fc1d85..690cd4f 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/operations/DeleteGroup.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/DeleteGroup.java
@@ -47,7 +47,7 @@ public class DeleteGroup extends Operation {
@NotNull
@Override
public String asRepoInitString() {
- return String.format("delete group %s", QuotableStringUtil.forRepoInitString(groupname));
+ return String.format("delete group %s%n", QuotableStringUtil.forRepoInitString(groupname));
}
public String getGroupname() {
diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/Operation.java b/src/main/java/org/apache/sling/repoinit/parser/operations/Operation.java
index 9aa4157..aed4b43 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/operations/Operation.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/Operation.java
@@ -31,6 +31,14 @@ public abstract class Operation {
protected abstract String getParametersDescription();
+ /**
+ * Converts this operation instance to a RepoInit string representation
+ * including the current operation parameters. The representation must be
+ * parsable back into an equivalent operation and must end with a OS-compatible
+ * line separator.
+ *
+ * @return the repoinit string for the operation
+ */
@NotNull
public abstract String asRepoInitString();
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 fe12c50..e3e3406 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
@@ -25,10 +25,13 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
+import static org.junit.Assert.assertTrue;
+
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
@@ -37,15 +40,15 @@ 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()}) */
@@ -59,6 +62,20 @@ public class AsRepoInitTest {
@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..0dbf7c7 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