From 55d60c0ffdc0c634eb20b152b4e76351faa5d9bd Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 21 Apr 2011 03:17:53 +0400 Subject: [PATCH] Tests should be independent from EOL in resources --- .gitattributes | 4 -- .../plugins/pmd/PmdProfileExporterTest.java | 46 ++++++++++++++----- .../sonar/colorizer/CodeColorizerTest.java | 39 +++++++++------- .../encoding/CP1252Encoding.java | 24 +++++----- 4 files changed, 70 insertions(+), 43 deletions(-) diff --git a/.gitattributes b/.gitattributes index c56e61533ad..cabac405497 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1 @@ -plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_simple.xml eol=lf -plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_xpath_rules.xml eol=lf -sonar-colorizer/src/test/resources/org/sonar/colorizer/samples/Sample.java eol=lf -sonar-colorizer/src/test/resources/org/sonar/colorizer/samples/Sample.groovy eol=lf sonar-server/src/test/resources/org/sonar/server/configuration/PropertiesBackupTest/backup-with-multiline-property.xml eol=lf diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java index 3fc3a2dfcce..8c3d019c12c 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java @@ -19,7 +19,16 @@ */ 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; @@ -38,12 +47,6 @@ import java.io.StringWriter; 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(); @@ -61,7 +64,8 @@ public class PmdProfileExporterTest { 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 @@ -69,15 +73,35 @@ public class PmdProfileExporterTest { 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 { + 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) @@ -145,7 +169,7 @@ public class PmdProfileExporterTest { 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; } } diff --git a/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java b/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java index 9740e95b700..e069f67d2c2 100644 --- a/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java +++ b/sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java @@ -26,8 +26,9 @@ import static org.hamcrest.number.OrderingComparisons.greaterThan; 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; @@ -39,10 +40,6 @@ import java.util.concurrent.ExecutionException; 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 @@ -57,13 +54,9 @@ public class CodeColorizerTest { @Test public void shouldSupportWindowsEndOfLines() throws IOException { - StringBuilder windowsFile = new StringBuilder(); - List 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, "
public class Sample {
"); @@ -97,7 +90,7 @@ public class CodeColorizerTest { } @Test - public void mustBeThreadsafe() throws FileNotFoundException, InterruptedException, ExecutionException { + public void mustBeThreadsafe() throws InterruptedException, ExecutionException, IOException { final int taskCount = 50; final int threadCount = 5; @@ -105,7 +98,7 @@ public class CodeColorizerTest { Reader java; - ColorizerTask() throws FileNotFoundException { + ColorizerTask() throws IOException { this.java = readFile("/org/sonar/colorizer/samples/Sample.java"); } @@ -129,8 +122,22 @@ public class CodeColorizerTest { } } - 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) { diff --git a/sonar-plugin-api/test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/CP1252Encoding.java b/sonar-plugin-api/test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/CP1252Encoding.java index 1982fef8e78..7da17990464 100644 --- a/sonar-plugin-api/test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/CP1252Encoding.java +++ b/sonar-plugin-api/test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/CP1252Encoding.java @@ -1,13 +1,13 @@ -public class Car { - - public AClaèss() { - } - - public int explicação() { - return 1; - } - - public String getS() { - return ""; - } +public class Car { + + public AClaèss() { + } + + public int explicação() { + return 1; + } + + public String getS() { + return ""; + } } \ No newline at end of file -- 2.39.5