*/
package org.sonar.plugins.pmd;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.CharSequenceReader;
import org.apache.commons.lang.StringUtils;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.profiles.RulesProfile;
import java.util.Collection;
import java.util.List;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
public class PmdProfileExporterTest {
private PmdProfileExporter exporter = new PmdProfileExporter();
StringWriter xmlOutput = new StringWriter();
exporter.exportProfile(rulesProfile, xmlOutput);
- assertEquals(TestUtils.getResourceContent("/org/sonar/plugins/pmd/export_simple.xml"), StringUtils.remove(xmlOutput.toString(), '\r'));
+
+ assertThat(xmlOutput.toString(), new IsEqualIgnoringEOL(TestUtils.getResourceContent("/org/sonar/plugins/pmd/export_simple.xml")));
}
@Test
StringWriter xmlOutput = new StringWriter();
RulesProfile profile = RulesProfile.create();
Rule xpathTemplate = Rule.create(PmdConstants.REPOSITORY_KEY, "MyOwnRule", "This is my own xpath rule.")
- .setConfigKey(PmdConstants.XPATH_CLASS).setPluginName(PmdConstants.REPOSITORY_KEY);
+ .setConfigKey(PmdConstants.XPATH_CLASS).setRepositoryKey(PmdConstants.REPOSITORY_KEY);
xpathTemplate.createParameter(PmdConstants.XPATH_EXPRESSION_PARAM);
xpathTemplate.createParameter(PmdConstants.XPATH_MESSAGE_PARAM);
ActiveRule xpath = profile.activateRule(xpathTemplate, null);
xpath.setParameter(PmdConstants.XPATH_EXPRESSION_PARAM, "//FieldDeclaration");
xpath.setParameter(PmdConstants.XPATH_MESSAGE_PARAM, "This is bad");
exporter.exportProfile(profile, xmlOutput);
- assertEquals(TestUtils.getResourceContent("/org/sonar/plugins/pmd/export_xpath_rules.xml"),
- StringUtils.remove(xmlOutput.toString(), '\r'));
+ assertThat(xmlOutput.toString(), new IsEqualIgnoringEOL(TestUtils.getResourceContent("/org/sonar/plugins/pmd/export_xpath_rules.xml")));
+ }
+
+ private static class IsEqualIgnoringEOL extends TypeSafeMatcher<CharSequence> {
+ private String expected;
+
+ public IsEqualIgnoringEOL(CharSequence expected) {
+ this.expected = normalize(expected);
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("string equal ").appendText(expected);
+ }
+
+ @Override
+ public boolean matchesSafely(CharSequence item) {
+ return StringUtils.equals(expected, normalize(item));
+ }
+
+ private static String normalize(CharSequence charSequence) {
+ return StringUtils.join(IOUtils.lineIterator(new CharSequenceReader(charSequence)), IOUtils.LINE_SEPARATOR_UNIX);
+ }
}
@Test(expected = SonarException.class)
public Rule find(RuleQuery query) {
for (Rule rule : rules) {
if (query.getConfigKey().equals(rule.getConfigKey())) {
- rule.setPluginName(PmdConstants.REPOSITORY_KEY);
+ rule.setRepositoryKey(PmdConstants.REPOSITORY_KEY);
return rule;
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.junit.Test;
-
public class CodeColorizerTest {
@Test
@Test
public void shouldSupportWindowsEndOfLines() throws IOException {
- StringBuilder windowsFile = new StringBuilder();
- List<String> lines = FileUtils.readLines(FileUtils.toFile(getClass().getResource("/org/sonar/colorizer/samples/Sample.java")));
- for (String line : lines) {
- windowsFile.append(line).append(IOUtils.LINE_SEPARATOR_WINDOWS);
- }
+ Reader windowsFile = readFile("/org/sonar/colorizer/samples/Sample.java", IOUtils.LINE_SEPARATOR_WINDOWS);
- String html = CodeColorizer.javaToHtml(new StringReader(windowsFile.toString()), HtmlOptions.DEFAULT);
+ String html = CodeColorizer.javaToHtml(windowsFile, HtmlOptions.DEFAULT);
assertHtml(html);
assertContains(html, "<pre><span class=\"k\">public</span> <span class=\"k\">class</span> Sample {</pre>");
}
@Test
- public void mustBeThreadsafe() throws FileNotFoundException, InterruptedException, ExecutionException {
+ public void mustBeThreadsafe() throws InterruptedException, ExecutionException, IOException {
final int taskCount = 50;
final int threadCount = 5;
Reader java;
- ColorizerTask() throws FileNotFoundException {
+ ColorizerTask() throws IOException {
this.java = readFile("/org/sonar/colorizer/samples/Sample.java");
}
}
}
- private FileReader readFile(String path) throws FileNotFoundException {
- return new FileReader(FileUtils.toFile(getClass().getResource(path)));
+ /**
+ * @return Reader for specified file with EOL normalized to specified one.
+ */
+ private Reader readFile(String path, String eol) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ for (String line : IOUtils.readLines(getClass().getResourceAsStream(path))) {
+ sb.append(line).append(eol);
+ }
+ return new StringReader(sb.toString());
+ }
+
+ /**
+ * @return Reader for specified file with EOL normalized to LF.
+ */
+ private Reader readFile(String path) throws IOException {
+ return readFile(path, IOUtils.LINE_SEPARATOR_UNIX);
}
private void assertHtml(String html) {