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

dulvac pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new b8946dc  SLING-9129 ResourceUtils add extra line break at the end of 
the file
b8946dc is described below

commit b8946dcf5a264d1602eeaa761b23881692f62e7b
Author: Thierry Ygé <[email protected]>
AuthorDate: Sun Feb 23 14:36:58 2020 +0100

    SLING-9129 ResourceUtils add extra line break at the end of the file
---
 pom.xml                                            |  9 +++++
 .../sling/testing/clients/util/ResourceUtil.java   |  9 +++--
 .../sling/testing/util/ResourceUtilTest.java       | 41 ++++++++++++++++++++++
 src/test/resources/resource-util-test-file.txt     |  7 ++++
 4 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index f8dde9a..45c6cc7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,6 +83,15 @@
                 <artifactId>maven-javadoc-plugin</artifactId>
                 <version>3.0.1</version>
             </plugin>
+            <plugin>
+                <groupId>org.apache.rat</groupId>
+                <artifactId>apache-rat-plugin</artifactId>
+                <configuration>
+                    <excludes combine.children="append">
+                            <exclude>src/test/resources/**/*.txt</exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
diff --git 
a/src/main/java/org/apache/sling/testing/clients/util/ResourceUtil.java 
b/src/main/java/org/apache/sling/testing/clients/util/ResourceUtil.java
index 5713ef1..b0eb5e2 100644
--- a/src/main/java/org/apache/sling/testing/clients/util/ResourceUtil.java
+++ b/src/main/java/org/apache/sling/testing/clients/util/ResourceUtil.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 public class ResourceUtil {
 
     /**
@@ -52,9 +54,12 @@ public class ResourceUtil {
             StringBuilder sb = new StringBuilder();
             String line;
             try {
-                BufferedReader reader = new BufferedReader(new 
InputStreamReader(resourceAsStream, "UTF-8"));
+                BufferedReader reader = new BufferedReader(new 
InputStreamReader(resourceAsStream, UTF_8.name()));
                 while ((line = reader.readLine()) != null) {
-                    sb.append(line).append("\n");
+                    if (sb.length() > 0) {
+                        sb.append("\n");
+                    }
+                    sb.append(line);
                 }
             } finally {
                 resourceAsStream.close();
diff --git a/src/test/java/org/apache/sling/testing/util/ResourceUtilTest.java 
b/src/test/java/org/apache/sling/testing/util/ResourceUtilTest.java
new file mode 100644
index 0000000..2d5f849
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/util/ResourceUtilTest.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
******************************************************************************/
+package org.apache.sling.testing.util;
+
+import org.apache.sling.testing.clients.util.ResourceUtil;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class ResourceUtilTest {
+
+    private static final long EXPECTED_NB_CARRIAGE_RETURNS = 6;
+
+    @Test
+    public void testReadResourceAsStringNoExtraLineBreak() throws IOException {
+        String input = 
ResourceUtil.readResourceAsString("/resource-util-test-file.txt");
+        assertNotNull(input);
+        long count = input.chars().filter(ch -> ch == '\n').count();
+        assertEquals("Expecting to have 6 line break in the given text file.", 
EXPECTED_NB_CARRIAGE_RETURNS, count);
+    }
+
+}
diff --git a/src/test/resources/resource-util-test-file.txt 
b/src/test/resources/resource-util-test-file.txt
new file mode 100644
index 0000000..ba70009
--- /dev/null
+++ b/src/test/resources/resource-util-test-file.txt
@@ -0,0 +1,7 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua.
+Odio facilisis mauris sit amet massa vitae. Ipsum a arcu cursus vitae congue 
mauris rhoncus.
+Sit amet consectetur adipiscing elit ut aliquam purus sit. Non pulvinar neque 
laoreet suspendisse interdum consectetur.
+Quisque id diam vel quam elementum pulvinar. Cras pulvinar mattis nunc sed 
blandit libero volutpat sed.
+Accumsan lacus vel facilisis volutpat est velit egestas dui id. Vivamus at 
augue eget arcu dictum.
+Turpis egestas integer eget aliquet nibh. Lobortis feugiat vivamus at augue 
eget arcu dictum.
+Lorem ipsum dolor sit amet consectetur adipiscing elit pellentesque.
\ No newline at end of file

Reply via email to