Skip to content

Commit

Permalink
#402 added information about missing or extra new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk authored and krzyk committed Sep 19, 2015
1 parent ba335eb commit 627b07e
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions qulice-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
<pomInclude>duplicate-finder-violations</pomInclude>
<!--<pomInclude>duplicate-finder-ignore-deps</pomInclude>-->
<pomInclude>js-violations/pom.xml</pomInclude>
<pomInclude>xml-violations/pom.xml</pomInclude>
</pomIncludes>
<extraArtifacts>
<extraArtifact>${project.groupId}:qulice-checkstyle:${project.version}:jar</extraArtifact>
Expand Down
19 changes: 19 additions & 0 deletions qulice-maven-plugin/src/it/xml-violations/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014-2015, Xockets, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are NOT permitted.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
invoker.goals = clean verify
#invoker.buildResult = failure
3 changes: 3 additions & 0 deletions qulice-maven-plugin/src/it/xml-violations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<license>file:${basedir}/LICENSE.txt</license>
</configuration>
<executions>
<execution>
<goals>
Expand Down
43 changes: 43 additions & 0 deletions qulice-maven-plugin/src/it/xml-violations/src/site.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
* Copyright (c) 2014-2015, Foo, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
--><project xmlns="http://maven.apache.org/DECORATION/1.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.3.0 http://maven.apache.org/xsd/decoration-1.3.0.xsd"
name="foo-transport">
<skin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-skin</artifactId>
<version>1.5.1</version>
</skin>
<bannerLeft>
<name>xockets.com</name>
<src>http://foo.com/img/logo.png</src>
<href>http://www.foo.com/</href>
<width>100</width>
<height>50</height>
</bannerLeft>
<body>
<menu name="Overview">
<item name="Introduction" href="index.html"/>
<item name="Release History" href="https://github.com/foo/transport/releases"/>
</menu>
<menu ref="reports"/>
</body>
</project>
3 changes: 0 additions & 3 deletions qulice-maven-plugin/src/it/xml-violations/src/violations.xml

This file was deleted.

3 changes: 2 additions & 1 deletion qulice-maven-plugin/src/it/xml-violations/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
*/

def log = new File(basedir, 'build.log')
assert log.text.contains(' XML violations (see log above)')
assert log.text.contains('site.xml: to be validated')
assert !log.text.contains('The provided XML .* is not well formatted')
13 changes: 11 additions & 2 deletions qulice-xml/src/main/java/com/qulice/xml/XmlValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
)
public final class XmlValidator implements Validator {

/**
* New line escaped, for use in regex.
*/
private static final String ESCAPED_EOL = "\\n";

/**
* Should XML format be checked.
*/
Expand Down Expand Up @@ -131,7 +136,8 @@ private void formatting(final String name, final String before)
final String bnormalized = before.replace("\r\n", "\n");
if (!bnormalized.equals(after)) {
// @checkstyle MultipleStringLiteralsCheck (1 line)
final List<String> blines = Arrays.asList(bnormalized.split("\\n"));
final List<String> blines =
Arrays.asList(bnormalized.split(XmlValidator.ESCAPED_EOL, -1));
final int context = 5;
throw new ValidationException(
// @checkstyle LineLength (1 line)
Expand All @@ -141,7 +147,10 @@ private void formatting(final String name, final String before)
DiffUtils.generateUnifiedDiff(
"before", "after", blines,
DiffUtils.diff(
blines, Arrays.asList(after.split("\\n"))
blines,
Arrays.asList(
after.split(XmlValidator.ESCAPED_EOL, -1)
)
), context
), "\n"
)
Expand Down
30 changes: 30 additions & 0 deletions qulice-xml/src/test/java/com/qulice/xml/XmlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.qulice.spi.Environment;
import com.qulice.spi.ValidationException;
import com.qulice.spi.Validator;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand Down Expand Up @@ -69,6 +71,34 @@ public void passesValidationOnCorrectFile() throws Exception {
validator.validate(env);
}

/**
* XmlValidator can inform about missing end of line at the and of file.
* @throws Exception In case of error.
*/
@Test
public void informsAboutMissingEOLAtEOF() throws Exception {
final Environment env = new Environment.Mock().withFile(
"src/main/resources/valid5.xml",
// @checkstyle LineLength (1 line)
"<project xmlns=\"http://maven.apache.org/DECORATION/1.3.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/DECORATION/1.3.0 http://maven.apache.org/xsd/decoration-1.3.0.xsd\" name=\"xockets-hadoop-transport\">\n</project>"
);
final Validator validator = new XmlValidator(true);
String message = "";
try {
validator.validate(env);
} catch (final ValidationException ex) {
message = ex.getMessage();
}
MatcherAssert.assertThat(
message,
Matchers.allOf(
Matchers.containsString("--- before"),
Matchers.containsString("+++ after"),
Matchers.containsString("</project>\n+")
)
);
}

/**
* Should fail validation on incorrectly formatted file.
* @throws Exception If something wrong happens inside.
Expand Down

0 comments on commit 627b07e

Please sign in to comment.