]> source.dussan.org Git - sonarqube.git/commitdiff
Tests should be independent from EOL in resources
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 20 Apr 2011 23:17:53 +0000 (03:17 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 22 Apr 2011 11:28:09 +0000 (15:28 +0400)
.gitattributes
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java
sonar-colorizer/src/test/java/org/sonar/colorizer/CodeColorizerTest.java
sonar-plugin-api/test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/CP1252Encoding.java

index c56e61533adb770da00ac9dc9f621875087d4acd..cabac4054971a265f0ec737e45cc6f685448bbf9 100644 (file)
@@ -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
index 3fc3a2dfcce9c4ed9ed15b3c6d40778d3b2c7324..8c3d019c12c53d68f07442a5f131ccce4494239a 100644 (file)
  */
 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<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 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;
         }
       }
index 9740e95b700ae46211d51fb6f4c3bd5e8f1402bf..e069f67d2c2e1c75918054a35c00ff64b6aa54ad 100644 (file)
@@ -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<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 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) {
index 1982fef8e7860706d5044d8392b2fb12c13f28be..7da17990464403b4f7f2eef0d0acc0fd8442af96 100644 (file)
@@ -1,13 +1,13 @@
-public class Car {\r
-\r
-       public AClaèss() {\r
-       }\r
-\r
-       public int explicação() {\r
-         return 1;\r
-       }\r
-\r
-       public String getS() {\r
-               return "";\r
-       }\r
+public class Car {
+
+       public AClaèss() {
+       }
+
+       public int explicação() {
+         return 1;
+       }
+
+       public String getS() {
+               return "";
+       }
 }
\ No newline at end of file