Skip to content

Commit

Permalink
[xml] Add simple test case for -force-language
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Jul 31, 2021
1 parent 7426791 commit fea395c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pmd-xml/src/test/java/net/sourceforge/pmd/lang/xml/XmlCliTest.java
@@ -0,0 +1,66 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.xml;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;

import net.sourceforge.pmd.cli.BaseCLITest;

public class XmlCliTest extends BaseCLITest {
private static final String BASE_DIR = "src/test/resources/net/sourceforge/pmd/lang/xml/cli-tests/sampleproject";
private static final String RULE_MESSAGE = "A tags are not allowed";

private String[] createArgs(String directory, String ... args) {
List<String> arguments = new ArrayList<>();
arguments.add("-f");
arguments.add("text");
arguments.add("-no-cache");
arguments.add("-R");
arguments.add(BASE_DIR + "/ruleset.xml");
arguments.add("-d");
arguments.add(BASE_DIR + directory);
arguments.addAll(Arrays.asList(args));
return arguments.toArray(new String[0]);
}

@Test
public void analyzeSingleXmlWithoutForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext"), "analyzeSingleXmlWithoutForceLanguage", 0);
assertRuleMessage(0, resultFilename);
}

@Test
public void analyzeSingleXmlWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/file1.ext", "-force-language", "xml"),
"analyzeSingleXmlWithForceLanguage", 4);
assertRuleMessage(1, resultFilename);
}

@Test
public void analyzeDirectoryWithForceLanguage() {
String resultFilename = runTest(createArgs("/src/", "-force-language", "xml"),
"analyzeDirectoryWithForceLanguage", 4);
assertRuleMessage(3, resultFilename);
}

private void assertRuleMessage(int expectedCount, String resultFilename) {
try {
String result = FileUtils.readFileToString(new File(resultFilename), StandardCharsets.UTF_8);
Assert.assertEquals(expectedCount, StringUtils.countMatches(result, RULE_MESSAGE));
} catch (IOException e) {
throw new AssertionError(e);
}
}
}
@@ -0,0 +1,31 @@
<?xml version="1.0"?>

<ruleset name="sample"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

<description>
Sample
</description>

<rule name="A"
language="xml"
message="A tags are not allowed"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A tags are not allowed
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//a
]]>
</value>
</property>
</properties>
</rule>
</ruleset>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- BSD-style license; for more info see http://pmd.sourceforge.net/license.html -->
<file>
<a></a>
</file>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- BSD-style license; for more info see http://pmd.sourceforge.net/license.html -->
<file>
<a></a>
<a></a>
</file>
@@ -0,0 +1,3 @@
BSD-style license; for more info see http://pmd.sourceforge.net/license.html

Other file that is not a xml file.
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- BSD-style license; for more info see http://pmd.sourceforge.net/license.html -->
<file>
</file>

0 comments on commit fea395c

Please sign in to comment.