Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A servlet to export redirects to a TXT file to use with pipeline-free redirects #3484

Merged
merged 9 commits into from
Dec 13, 2024
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ACS AEM Commons
* ACS AEM Commons Bundle
*
* Copyright (C) 2013 - 2024 Adobe
*
Expand Down Expand Up @@ -62,16 +62,16 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r
}
Collection<RedirectRule> rules = RedirectFilter.getRules(request.getResource());
PrintWriter out = response.getWriter();
out.printf("# %s Redirects\n", statusCode == 0 ? "All" : "" + statusCode);
out.printf("# %s Redirects%n", statusCode == 0 ? "All" : "" + statusCode);
Copy link
Contributor

@krystiannowak krystiannowak Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YegorKozlov after starting to use %n as line separator, splitting still only by \n in tests by

String[] lines = response.getOutputAsString().split("\n");

in https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/3484/files#diff-36adc0c81c9585f4cadf9e45117faabfdda5ef6d7e197ade7d39e0a896c6853eR87
might not be precise enough anymore on Windows

Maybe by System.lineSeparator() as in:

diff --git a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java
index acb58e9c5..80799fb3f 100755
--- a/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java
+++ b/bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java
@@ -107,7 +107,7 @@ public class RewriteMapServletTest {
         servlet.doGet(request, response);
 
         assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType());
-        String[] lines = response.getOutputAsString().split("\n");
+        String[] lines = response.getOutputAsString().split(System.lineSeparator());
         assertEquals(2, lines.length); // header + 1 rule
         assertEquals("# 301 Redirects", lines[0]);
         String[] rule1 = lines[1].split(" ");
@@ -124,7 +124,7 @@ public class RewriteMapServletTest {
         servlet.doGet(request, response);
 
         assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType());
-        String[] lines = response.getOutputAsString().split("\n");
+        String[] lines = response.getOutputAsString().split(System.lineSeparator());
         assertEquals(3, lines.length); // header + notes + 1st rule
         assertEquals("# 302 Redirects", lines[0]);

would do?

for (RedirectRule rule : rules) {
if(statusCode != 0 && rule.getStatusCode() != statusCode) {
continue;
}
String note = rule.getNote();
if(note != null && !note.isEmpty()) {
out.printf("# %s\n", note);
out.printf("# %s%n", note);
}
out.printf("%s %s\n", rule.getSource(), rule.getTarget());
out.printf("%s %s%n", rule.getSource(), rule.getTarget());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ACS AEM Commons
* ACS AEM Commons Bundle
*
* Copyright (C) 2013 - 2024 Adobe
*
Expand Down Expand Up @@ -86,7 +86,7 @@
assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType());
String[] lines = response.getOutputAsString().split("\n");
assertEquals(4, lines.length); // header + 2 rules
assertEquals("# All Redirects", lines[0]);

Check failure on line 89 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 21)

RewriteMapServletTest.testGet

expected:<# All Redirects[]> but was:<# All Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# All Redirects[]> but was:<# All Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.testGet(RewriteMapServletTest.java:89)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)

Check failure on line 89 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 17)

RewriteMapServletTest.testGet

expected:<# All Redirects[]> but was:<# All Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# All Redirects[]> but was:<# All Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.testGet(RewriteMapServletTest.java:89)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)

Check failure on line 89 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 11)

RewriteMapServletTest.testGet

expected:<# All Redirects[]> but was:<# All Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# All Redirects[]> but was:<# All Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.testGet(RewriteMapServletTest.java:89)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
assertEquals("# note-1", lines[1]);

String[] rule1 = lines[2].split(" ");
Expand All @@ -109,7 +109,7 @@
assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType());
String[] lines = response.getOutputAsString().split("\n");
assertEquals(2, lines.length); // header + 1 rule
assertEquals("# 301 Redirects", lines[0]);

Check failure on line 112 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 21)

RewriteMapServletTest.test301Selector

expected:<# 301 Redirects[]> but was:<# 301 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 301 Redirects[]> but was:<# 301 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test301Selector(RewriteMapServletTest.java:112)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)

Check failure on line 112 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 17)

RewriteMapServletTest.test301Selector

expected:<# 301 Redirects[]> but was:<# 301 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 301 Redirects[]> but was:<# 301 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test301Selector(RewriteMapServletTest.java:112)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)

Check failure on line 112 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 11)

RewriteMapServletTest.test301Selector

expected:<# 301 Redirects[]> but was:<# 301 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 301 Redirects[]> but was:<# 301 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test301Selector(RewriteMapServletTest.java:112)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
String[] rule1 = lines[1].split(" ");
assertEquals("/content/three", rule1[0]);
assertEquals("/content/four", rule1[1]);
Expand All @@ -126,7 +126,7 @@
assertEquals(ContentType.TEXT_PLAIN.getMimeType(), response.getContentType());
String[] lines = response.getOutputAsString().split("\n");
assertEquals(3, lines.length); // header + notes + 1st rule
assertEquals("# 302 Redirects", lines[0]);

Check failure on line 129 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 21)

RewriteMapServletTest.test302Selector

expected:<# 302 Redirects[]> but was:<# 302 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 302 Redirects[]> but was:<# 302 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test302Selector(RewriteMapServletTest.java:129)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)

Check failure on line 129 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 17)

RewriteMapServletTest.test302Selector

expected:<# 302 Redirects[]> but was:<# 302 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 302 Redirects[]> but was:<# 302 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test302Selector(RewriteMapServletTest.java:129)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)

Check failure on line 129 in bundle/src/test/java/com/adobe/acs/commons/redirects/servlets/RewriteMapServletTest.java

View workflow job for this annotation

GitHub Actions / Test report (windows-latest, JDK 11)

RewriteMapServletTest.test302Selector

expected:<# 302 Redirects[]> but was:<# 302 Redirects[ ]>
Raw output
org.junit.ComparisonFailure: expected:<# 302 Redirects[]> but was:<# 302 Redirects[
]>
	at org.junit.Assert.assertEquals(Assert.java:117)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at com.adobe.acs.commons.redirects.servlets.RewriteMapServletTest.test302Selector(RewriteMapServletTest.java:129)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)

String[] rule1 = lines[2].split(" ");
assertEquals("/content/one", rule1[0]);
Expand Down
Loading