This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.scripting.sightly.compiler-1.0.10 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler.git
commit d4376d154029b9d18a2527a5d886e1163e1f224a Author: Radu Cotescu <[email protected]> AuthorDate: Mon Jan 9 14:38:45 2017 +0000 SLING-6445 - HTL scripts do not compile on Windows if the compiler needs to generate any warnings * applied patch from Vlad Băilescu * improved launchpad readiness test * updated HTL test dependencies (Closes #196) git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/scripting/sightly/compiler@1777973 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 14 +++++++- .../sightly/compiler/SightlyCompiler.java | 15 ++++++-- .../sightly/impl/compiler/SightlyCompilerTest.java | 41 ++++++++++++++++++++-- .../resources/missing-explicit-context-mac.html | 1 + .../resources/missing-explicit-context-win.html | 19 ++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 20dbf6b..6fd1459 100644 --- a/pom.xml +++ b/pom.xml @@ -234,7 +234,19 @@ <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-reflect</artifactId> - <version>1.5.5</version> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + <version>1.6.5</version> <scope>test</scope> </dependency> </dependencies> diff --git a/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java b/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java index ed5ef36..a24c940 100644 --- a/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java +++ b/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java @@ -166,8 +166,19 @@ public final class SightlyCompiler { int offendingInputIndex = documentFragment.indexOf(offendingInput); if (offendingInputIndex > -1) { String textBeforeError = documentFragment.substring(0, offendingInputIndex); - int line = lineOffset + textBeforeError.length() - textBeforeError.replaceAll(System.lineSeparator(), "").length(); - int column = textBeforeError.substring(textBeforeError.lastIndexOf(System.lineSeparator())).length(); + int line = lineOffset; + int lastNewLineIndex = 0; + for (String s : new String[] {"\r\n", "\r", "\n"}) { + int l = textBeforeError.split(s, -1).length - 1; + if (l + lineOffset > line) { + line = l + lineOffset; + int ix = textBeforeError.lastIndexOf(s); + if (ix > 0) { + lastNewLineIndex = ix + s.length() - 1; + } + } + } + int column = textBeforeError.substring(lastNewLineIndex).length(); if (column != columnOffset) { column +=columnOffset; } diff --git a/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java b/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java index 4af5713..e861b5f 100644 --- a/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java +++ b/src/test/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyCompilerTest.java @@ -26,10 +26,16 @@ import org.apache.sling.scripting.sightly.compiler.CompilerMessage; import org.apache.sling.scripting.sightly.compiler.SightlyCompiler; import org.apache.sling.scripting.sightly.impl.TestUtils; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +@RunWith(PowerMockRunner.class) +@PrepareForTest(SightlyCompiler.class) public class SightlyCompilerTest { private SightlyCompiler compiler = new SightlyCompiler(); @@ -42,18 +48,47 @@ public class SightlyCompilerTest { @Test public void testMissingExplicitContext() { - String script = "/missing-explicit-context.html"; + for (String s : new String[] {"", "-win", "-mac"}) { + String script = "/missing-explicit-context" + s + ".html"; + testMissingExplicitContext(script); + } + } + + private void testMissingExplicitContext(String script) { CompilationResult result = compile(script); List<CompilerMessage> warnings = result.getWarnings(); - assertTrue("Expected compilation warnings.", warnings.size() == 1); + assertTrue(script + ": Expected compilation warnings.", warnings.size() == 1); CompilerMessage warningMessage = warnings.get(0); - assertEquals("Expected warning on a different line.", 18, warningMessage.getLine()); + assertEquals(script + ": Expected warning on a different line.", 18, warningMessage.getLine()); + assertEquals(script + ": Expected warning on a different column.", 14, warningMessage.getColumn()); assertTrue(script.equals(warningMessage.getScriptName())); assertEquals("${some.value}: Element script requires that all expressions have an explicit context specified. The expression will" + " be replaced with an empty string.", warningMessage.getMessage()); } @Test + public void testMissingExplicitContextOnWindows() { + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.lineSeparator()).thenReturn("\r\n"); + + for (String s : new String[] {"", "-win", "-mac"}) { + String script = "/missing-explicit-context" + s + ".html"; + testMissingExplicitContext(script); + } + } + + @Test + public void testMissingExplicitContextOnMac() { + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.lineSeparator()).thenReturn("\r"); + + for (String s : new String[] {"", "-win", "-mac"}) { + String script = "/missing-explicit-context" + s + ".html"; + testMissingExplicitContext(script); + } + } + + @Test public void testSensitiveAttributes() { String script = "/sensitive-attributes.html"; CompilationResult result = compile(script); diff --git a/src/test/resources/missing-explicit-context-mac.html b/src/test/resources/missing-explicit-context-mac.html new file mode 100644 index 0000000..dbbe584 --- /dev/null +++ b/src/test/resources/missing-explicit-context-mac.html @@ -0,0 +1 @@ +<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ 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 ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ 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. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/--> <script> var a = '${so me.value}'; </script> \ No newline at end of file diff --git a/src/test/resources/missing-explicit-context-win.html b/src/test/resources/missing-explicit-context-win.html new file mode 100644 index 0000000..5b507e3 --- /dev/null +++ b/src/test/resources/missing-explicit-context-win.html @@ -0,0 +1,19 @@ +<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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 + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ 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. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/--> +<script> + var a = '${some.value}'; +</script> -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
