aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness/src
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-04-27 14:49:10 +0200
committerDavid Gageot <david@gageot.net>2012-04-27 14:59:00 +0200
commit12c43f766d3c1dfd5217388b6648ce9114f42baa (patch)
tree0b42c172530203401436f7c860219e389e563003 /sonar-testing-harness/src
parent969cfa0ad87805e336a758454fe9200167da1c10 (diff)
downloadsonarqube-12c43f766d3c1dfd5217388b6648ce9114f42baa.tar.gz
sonarqube-12c43f766d3c1dfd5217388b6648ce9114f42baa.zip
Add test helpers and refactor TestUtils a bit.
Diffstat (limited to 'sonar-testing-harness/src')
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java45
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java29
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java67
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java52
4 files changed, 179 insertions, 14 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java b/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java
new file mode 100644
index 00000000000..3cfb3ae90ee
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java
@@ -0,0 +1,45 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.test;
+
+import com.google.common.base.CharMatcher;
+import org.fest.assertions.Condition;
+
+/**
+ * Conditions for use with FestAssert.
+ */
+public final class MoreConditions {
+ private static CharMatcher EOLS = CharMatcher.anyOf("\n\r");
+
+ private MoreConditions() {
+ // static utility class
+ }
+
+ public static Condition<String> equalsIgnoreEOL(String text) {
+ final String strippedText = EOLS.removeFrom(text);
+
+ return new Condition<String>() {
+ @Override
+ public boolean matches(String value) {
+ return EOLS.removeFrom(value).equals(strippedText);
+ }
+ }.as("equal to " + strippedText);
+ }
+}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
index a220b828726..2effa66eca8 100644
--- a/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
@@ -19,13 +19,9 @@
*/
package org.sonar.test;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import org.custommonkey.xmlunit.Diff;
@@ -37,6 +33,11 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
/**
* Utilities for unit tests
*
@@ -66,16 +67,16 @@ public final class TestUtils {
}
public static String getResourceContent(String path) {
- File file = getResource(path);
- if (file != null) {
- try {
- return FileUtils.readFileToString(file, CharEncoding.UTF_8);
+ URL url = TestUtils.class.getResource(path);
+ if (url == null) {
+ return null;
+ }
- } catch (IOException e) {
- throw new SonarException("Can not load the resource: " + path, e);
- }
+ try {
+ return Resources.toString(url, Charsets.UTF_8);
+ } catch (IOException e) {
+ throw new SonarException("Can not load the resource: " + path, e);
}
- return null;
}
/**
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java
new file mode 100644
index 00000000000..79e763dd1ae
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java
@@ -0,0 +1,67 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.test.i18n;
+
+import com.google.common.io.Closeables;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleRepository;
+import org.sonar.api.utils.SonarException;
+import org.sonar.test.TestUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Properties;
+
+public final class RuleRepositoryTestHelper {
+ private RuleRepositoryTestHelper() {
+ // Static utility class
+ }
+
+ public static List<Rule> createRulesWithNameAndDescription(String plugin, RuleRepository repository) {
+ Properties props = loadProperties(String.format("/org/sonar/l10n/%s.properties", plugin));
+
+ List<Rule> rules = repository.createRules();
+ for (Rule rule : rules) {
+ String name = props.getProperty(String.format("rule.%s.%s.name", repository.getKey(), rule.getKey()));
+ String description = TestUtils.getResourceContent(String.format("/org/sonar/l10n/%s/rules/%s/%s.html", plugin, repository.getKey(), rule.getKey()));
+
+ rule.setName(name);
+ rule.setDescription(description);
+ }
+
+ return rules;
+ }
+
+ private static Properties loadProperties(String resourcePath) {
+ Properties properties = new Properties();
+
+ InputStream input = null;
+ try {
+ input = TestUtils.class.getResourceAsStream(resourcePath);
+ properties.load(input);
+ return properties;
+ } catch (IOException e) {
+ throw new SonarException("Unable to read properties " + resourcePath, e);
+ } finally {
+ Closeables.closeQuietly(input);
+ }
+ }
+}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java
new file mode 100644
index 00000000000..86ba9114c6e
--- /dev/null
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java
@@ -0,0 +1,52 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.test;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.sonar.test.MoreConditions.equalsIgnoreEOL;
+
+public class MoreConditionsTest {
+ @Test
+ public void should_compare_equals_texts() {
+ assertThat("TEXT").satisfies(equalsIgnoreEOL("TEXT"));
+ }
+
+ @Test
+ public void should_ignore_line_feeds_and_carriage_returns() {
+ assertThat("BEFORE\nAFTER").satisfies(equalsIgnoreEOL("BEFOREAFTER"));
+ assertThat("BEFORE\rAFTER").satisfies(equalsIgnoreEOL("BEFOREAFTER"));
+ assertThat("BEFORE\n\rAFTER").satisfies(equalsIgnoreEOL("BEFOREAFTER"));
+ assertThat("BEFOREAFTER").satisfies(equalsIgnoreEOL("BEFORE\n\rAFTER"));
+ }
+
+ @Test
+ public void should_refuse_different_values() {
+ assertThat("TEXT").doesNotSatisfy(equalsIgnoreEOL("DIFFERENT"));
+ }
+
+ @Test
+ public void should_accept_empty_values() {
+ assertThat("").satisfies(equalsIgnoreEOL(""));
+ assertThat("").satisfies(equalsIgnoreEOL("\n\r"));
+ assertThat("\n\r").satisfies(equalsIgnoreEOL(""));
+ }
+}