Skip to content

Commit

Permalink
Tests should be independent from EOL in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Apr 22, 2011
1 parent cfae388 commit 55d60c0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 43 deletions.
4 changes: 0 additions & 4 deletions .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
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -61,23 +64,44 @@ 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
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)
Expand Down Expand Up @@ -145,7 +169,7 @@ public Collection<Rule> findAll(RuleQuery query) {
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;
}
}
Expand Down
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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>");
Expand Down Expand Up @@ -97,15 +90,15 @@ 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;

class ColorizerTask implements Callable<String> {

Reader java;

ColorizerTask() throws FileNotFoundException {
ColorizerTask() throws IOException {
this.java = readFile("/org/sonar/colorizer/samples/Sample.java");
}

Expand All @@ -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) {
Expand Down
@@ -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 "";
}
}

0 comments on commit 55d60c0

Please sign in to comment.