Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tests should be independent from EOL in resources

  • Loading branch information...
commit 55d60c0ffdc0c634eb20b152b4e76351faa5d9bd 1 parent cfae388
@Godin Godin authored
View
4 .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
View
46 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.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 void testExportProfile() throws IOException, SAXException {
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 void testExportXPathRule() {
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)
@@ -145,7 +169,7 @@ public Rule findByKey(String repositoryKey, String key) {
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;
}
}
View
39 sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java
@@ -26,8 +26,9 @@
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.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 void javaToHtml() throws IOException {
@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>");
@@ -97,7 +90,7 @@ public void getCss() {
}
@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 void mustBeThreadsafe() throws FileNotFoundException, InterruptedExceptio
Reader java;
- ColorizerTask() throws FileNotFoundException {
+ ColorizerTask() throws IOException {
this.java = readFile("/org/sonar/colorizer/samples/Sample.java");
}
@@ -129,8 +122,22 @@ public String call() throws Exception {
}
}
- 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) {
View
24 ...est-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 "";
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.