]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2693 Improve unit tests for I18n Harmcrest matcher
authorFabrice Bellingard <bellingard@gmail.com>
Fri, 2 Sep 2011 16:03:38 +0000 (18:03 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Fri, 2 Sep 2011 16:03:38 +0000 (18:03 +0200)
sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java
sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedTest.java
sonar-testing-harness/src/test/resources/org/sonar/l10n/core_fr_CA.properties [new file with mode: 0644]

index 1fd44ecfa6be28bc9af543d5008ece5abd8a946b..18351b9c70b73907a1736ee522c290106d3ecec2 100644 (file)
@@ -28,6 +28,7 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -35,6 +36,7 @@ import java.util.Collection;
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.io.IOUtils;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.sonar.test.TestUtils;
@@ -102,16 +104,35 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> {
       }
     }
     if ( !nonExistingKeys.isEmpty()) {
-      details.append("\n\nAlso, the following keys do not exist in the default bundle:");
+      details.append("\n\nThe following keys do not exist in the default bundle:");
       for (String key : nonExistingKeys) {
         details.append("\n\t- " + key);
       }
     }
     details.append("\n\n=======================");
 
+    printReport(details.toString());
+
     description.appendText(details.toString());
   }
 
+  private void printReport(String details) {
+    File dumpFile = new File("target/l10n/" + bundleName + ".report.txt");
+    if (dumpFile.exists()) {
+      dumpFile.delete();
+    }
+    dumpFile.getParentFile().mkdirs();
+    FileWriter writer = null;
+    try {
+      writer = new FileWriter(dumpFile);
+      writer.write(details);
+    } catch (IOException e) {
+      System.out.println("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'.");
+    } finally {
+      IOUtils.closeQuietly(writer);
+    }
+  }
+
   protected Collection<String> retrieveMissingKeys(File bundle, File defaultBundle) throws IOException {
     Collection<String> missingKeys = Lists.newArrayList();
 
index 0bac5a9047bcebb2915b18d1c50e6eace2fde961..73d6e3106890a9bbbdb46b70918c476cd636767b 100644 (file)
@@ -31,17 +31,17 @@ import java.io.File;
 import java.util.Collection;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.test.TestUtils;
 
 public class BundleSynchronizedTest {
 
+  private static final String GITHUB_RAW_FILE_PATH = "https://raw.github.com/SonarSource/sonar/master/sonar-testing-harness/src/test/resources/org/sonar/l10n/";
   private BundleSynchronizedMatcher matcher;
 
   @Before
   public void test() throws Exception {
-    matcher = new BundleSynchronizedMatcher("https://raw.github.com/SonarSource/sonar/master/sonar-testing-harness/src/test/resources/org/sonar/l10n/");
+    matcher = new BundleSynchronizedMatcher();
   }
 
   @Test
@@ -49,25 +49,34 @@ public class BundleSynchronizedTest {
   public void testBundlesInsideSonarPlugin() {
     // synchronized bundle
     assertThat("myPlugin_fr_CA.properties", isBundleSynchronized());
+    assertFalse(new File("target/l10n/myPlugin_fr_CA.properties.report.txt").exists());
     // missing keys
     try {
       assertThat("myPlugin_fr.properties", isBundleSynchronized());
+      assertTrue(new File("target/l10n/myPlugin_fr.properties.report.txt").exists());
     } catch (AssertionError e) {
       assertThat(e.getMessage(), containsString("Missing keys are:\n\t- second.prop"));
     }
     // unnecessary many keys
     try {
       assertThat("myPlugin_fr_QB.properties", isBundleSynchronized());
+      assertTrue(new File("target/l10n/myPlugin_fr_QB.properties.report.txt").exists());
     } catch (AssertionError e) {
-      assertThat(e.getMessage(), containsString("Also, the following keys do not exist in the default bundle:\n\t- fourth.prop"));
+      assertThat(e.getMessage(), containsString("The following keys do not exist in the default bundle:\n\t- fourth.prop"));
     }
   }
 
   @Test
-  @Ignore
   // The case of a Sonar Language Pack that translates the Core bundles
   public void testBundlesOfLanguagePack() {
-    assertThat("core_fr.properties", isBundleSynchronized());
+    // synchronized bundle
+    assertThat("core_fr_CA.properties", new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH));
+    // missing keys
+    try {
+      assertThat("core_fr.properties", new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH));
+    } catch (AssertionError e) {
+      assertThat(e.getMessage(), containsString("Missing keys are:\n\t- second.prop"));
+    }
   }
 
   @Test
@@ -81,8 +90,8 @@ public class BundleSynchronizedTest {
   }
 
   @Test
-  @Ignore
   public void testGetBundleFileFromGithub() throws Exception {
+    matcher = new BundleSynchronizedMatcher(GITHUB_RAW_FILE_PATH);
     matcher.getBundleFileFromGithub("core.properties");
     assertTrue(new File("target/l10n/download/core.properties").exists());
   }
diff --git a/sonar-testing-harness/src/test/resources/org/sonar/l10n/core_fr_CA.properties b/sonar-testing-harness/src/test/resources/org/sonar/l10n/core_fr_CA.properties
new file mode 100644 (file)
index 0000000..d182159
--- /dev/null
@@ -0,0 +1,4 @@
+## -------- Test file for the BundleSynchronizedMatcher -------- ##
+first.prop = This is my first property
+second.prop = This is my second property
+third.prop = This is my third property
\ No newline at end of file