aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
commitaeadc1f9129274949daaa57738c7c4550bdfbc7b (patch)
tree08dadf5ef7474fc41d1d48f74648f1ba8b55f34d /sonar-testing-harness
downloadsonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.tar.gz
sonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.zip
SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console
Diffstat (limited to 'sonar-testing-harness')
-rw-r--r--sonar-testing-harness/pom.xml87
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java140
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java57
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java18
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java47
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java93
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java63
-rw-r--r--sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt1
8 files changed, 506 insertions, 0 deletions
diff --git a/sonar-testing-harness/pom.xml b/sonar-testing-harness/pom.xml
new file mode 100644
index 00000000000..2f0cebf0b15
--- /dev/null
+++ b/sonar-testing-harness/pom.xml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar</artifactId>
+ <version>2.3-SNAPSHOT</version>
+ </parent>
+ <artifactId>sonar-testing-harness</artifactId>
+ <packaging>jar</packaging>
+ <name>Sonar :: Testing Harness</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>xmlunit</groupId>
+ <artifactId>xmlunit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.dbunit</groupId>
+ <artifactId>dbunit</artifactId>
+ <exclusions>
+ <!-- removing commons lang 2.1 and xerces-->
+ <exclusion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>xerces</groupId>
+ <artifactId>xmlParserAPIs</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>xerces</groupId>
+ <artifactId>xercesImpl</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>${project.version}</version>
+ <scope>compile</scope>
+ <type>test-jar</type>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-core</artifactId>
+ <version>${project.version}</version>
+ <scope>compile</scope>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+</project>
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
new file mode 100644
index 00000000000..221684b9fa5
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java
@@ -0,0 +1,140 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.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;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.sonar.api.utils.SonarException;
+import org.xml.sax.SAXException;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.*;
+
+/**
+ * Utilities for unit tests
+ *
+ * @since 2.2
+ */
+public class TestUtils {
+
+ /**
+ * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
+ *
+ * @param path the starting slash is optional
+ * @return the resource. Null if resource not found
+ */
+ public static File getResource(String path) {
+ String resourcePath = path;
+ if ( !resourcePath.startsWith("/")) {
+ resourcePath = "/" + resourcePath;
+ }
+ URL url = TestUtils.class.getResource(resourcePath);
+ if (url != null) {
+ return FileUtils.toFile(url);
+ }
+ return null;
+ }
+
+ public static String getResourceContent(String path) {
+ File file = getResource(path);
+ if (file != null) {
+ try {
+ return FileUtils.readFileToString(file, CharEncoding.UTF_8);
+
+ } catch (IOException e) {
+ throw new SonarException("Can not load the resource: " + path, e);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Search for a resource in the classpath. For example calling the method getResource(getClass(), "myTestName/foo.txt") from
+ * the class org.sonar.Foo loads the file $basedir/src/test/resources/org/sonar/Foo/myTestName/foo.txt
+ *
+ * @return the resource. Null if resource not found
+ */
+ public static File getResource(Class baseClass, String path) {
+ String resourcePath = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/');
+ if ( !path.startsWith("/")) {
+ resourcePath += "/";
+ }
+ resourcePath += path;
+ return getResource(resourcePath);
+ }
+
+ /**
+ * Shortcut for getTestTempDir(baseClass, testName, true) : cleans the unit test directory
+ */
+ public static File getTestTempDir(Class baseClass, String testName) {
+ return getTestTempDir(baseClass, testName, true);
+ }
+
+ /**
+ * Create a temporary directory for unit tests.
+ * @param baseClass the unit test class
+ * @param testName the test name
+ * @param clean remove all the sub-directories and files ?
+ */
+ public static File getTestTempDir(Class baseClass, String testName, boolean clean) {
+ File dir = new File("target/test-tmp/" + baseClass.getCanonicalName() + "/" + testName);
+ if (clean && dir.exists()) {
+ try {
+ FileUtils.deleteDirectory(dir);
+ } catch (IOException e) {
+ throw new RuntimeException("Can not delete the directory " + dir);
+ }
+ }
+ try {
+ FileUtils.forceMkdir(dir);
+ } catch (IOException e) {
+ throw new RuntimeException("Can not create the directory " + dir);
+ }
+ return dir;
+ }
+
+ /**
+ * Checks that a file or a directory is not null and exists.
+ */
+ public static void assertExists(File file) {
+ assertNotNull(file);
+ assertThat(file.exists(), is(true));
+ }
+
+ public static void assertSimilarXml(String expectedXml, String xml) throws IOException, SAXException {
+ Diff diff = isSimilarXml(expectedXml, xml);
+ String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml;
+ assertTrue(message, diff.similar());
+ }
+
+ static Diff isSimilarXml(String expectedXml, String xml) throws IOException, SAXException {
+ XMLUnit.setIgnoreWhitespace(true);
+ Diff diff = XMLUnit.compareXML(xml, expectedXml);
+ return diff;
+ }
+}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java
new file mode 100644
index 00000000000..88e03987f0d
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java
@@ -0,0 +1,57 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.channel;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.sonar.channel.Channel;
+import org.sonar.channel.CodeReader;
+
+public class ChannelMatcher<OUTPUT> extends BaseMatcher<Channel<OUTPUT>> {
+
+ private final String sourceCode;
+ private final OUTPUT output;
+ private final CodeReader reader;
+
+ public ChannelMatcher(String sourceCode, OUTPUT output) {
+ this.sourceCode = sourceCode;
+ this.output = output;
+ this.reader = new CodeReader(sourceCode);
+ }
+
+ public ChannelMatcher(CodeReader reader, OUTPUT output) {
+ this.output = output;
+ this.sourceCode = new String(reader.peek(30));
+ this.reader = reader;
+ }
+
+ public boolean matches(Object arg0) {
+ if ( !(arg0 instanceof Channel)) {
+ return false;
+ }
+ Channel<OUTPUT> channel = (Channel<OUTPUT>) arg0;
+ return channel.consume(reader, output);
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("Channel consumes '" + sourceCode + "'");
+ }
+
+}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java
new file mode 100644
index 00000000000..0cead08afc3
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java
@@ -0,0 +1,18 @@
+package org.sonar.test.channel;
+
+import org.sonar.channel.CodeReader;
+
+public class ChannelMatchers {
+
+ public static <OUTPUT> ChannelMatcher<OUTPUT> consume(String sourceCode, OUTPUT output) {
+ return new ChannelMatcher<OUTPUT>(sourceCode, output);
+ }
+
+ public static <OUTPUT> ChannelMatcher<OUTPUT> consume(CodeReader codeReader, OUTPUT output) {
+ return new ChannelMatcher<OUTPUT>(codeReader, output);
+ }
+
+ public static ReaderHasNextCharMatcher hasNextChar(char nextChar) {
+ return new ReaderHasNextCharMatcher(nextChar);
+ }
+}
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java
new file mode 100644
index 00000000000..df4152c32b2
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java
@@ -0,0 +1,47 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.channel;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.sonar.channel.Channel;
+import org.sonar.channel.CodeReader;
+
+public class ReaderHasNextCharMatcher extends BaseMatcher<CodeReader> {
+
+ private final char nextChar;
+
+ public ReaderHasNextCharMatcher(char nextChar) {
+ this.nextChar = nextChar;
+ }
+
+ public boolean matches(Object arg0) {
+ if ( !(arg0 instanceof CodeReader)) {
+ return false;
+ }
+ CodeReader reader = (CodeReader) arg0;
+ return reader.peek() == nextChar;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("next char is '" + nextChar + "'");
+ }
+
+}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
new file mode 100644
index 00000000000..f0cefec7c38
--- /dev/null
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java
@@ -0,0 +1,93 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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 static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.sonar.test.TestUtils.assertExists;
+import static org.sonar.test.TestUtils.assertSimilarXml;
+import static org.sonar.test.TestUtils.getResource;
+import static org.sonar.test.TestUtils.getTestTempDir;
+import static org.sonar.test.TestUtils.isSimilarXml;
+
+import java.io.File;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Test;
+
+public class TestUtilsTest {
+
+ @Test
+ public void testResource() {
+ File file = getResource("org/sonar/test/TestUtilsTest/getResource/foo.txt");
+ TestUtils.assertExists(file);
+
+ file = getResource("/org/sonar/test/TestUtilsTest/getResource/foo.txt");
+ TestUtils.assertExists(file);
+
+ file = getResource(getClass(), "getResource/foo.txt");
+ TestUtils.assertExists(file);
+ }
+
+ @Test
+ public void testResourceNotFound() {
+ File file = getResource("org/sonar/test/TestUtilsTest/unknown.txt");
+ assertNull(file);
+ }
+
+ @Test
+ public void testTempDir() throws Exception {
+ File dir = getTestTempDir(getClass(), "testTempDir");
+ assertExists(dir);
+ assertThat(dir.isDirectory(), is(true));
+ assertThat(dir.listFiles().length, is(0));
+
+ FileUtils.writeStringToFile(new File(dir, "bar.txt"), "some text");
+ assertThat(dir.listFiles().length, is(1));
+
+ // the directory is cleaned
+ dir = getTestTempDir(getClass(), "testTempDir");
+ TestUtils.assertExists(dir);
+ assertThat(dir.isDirectory(), is(true));
+ assertThat(dir.listFiles().length, is(0));
+ }
+
+ @Test
+ public void testAssertSimilarXml() throws Exception {
+ assertSimilarXml("<foo></foo>", "<foo />");
+
+ // order of attributes
+ assertSimilarXml("<foo><bar id='1' key='one' /></foo>", "<foo><bar key='one' id='1' /></foo>");
+
+ // whitespaces are ignored
+ assertSimilarXml("<foo> <bar /> </foo>", "<foo><bar/></foo>");
+
+ // attribute values are checked
+ assertFalse(isSimilarXml("<foo id='1' />", "<foo id='2'/>").similar());
+
+ // different xml
+ assertFalse(isSimilarXml("<foo id='1' />", "<foo id='2'/>").similar());
+
+ // order of nodes is important
+ assertFalse(isSimilarXml("<foo><bar id='1' /><bar id='2' /></foo>", "<foo><bar id='2' /><bar id='1' /></foo>").similar());
+ }
+}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java
new file mode 100644
index 00000000000..0afd4f28c55
--- /dev/null
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java
@@ -0,0 +1,63 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.channel;
+
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.sonar.test.channel.ChannelMatchers.consume;
+import static org.sonar.test.channel.ChannelMatchers.hasNextChar;
+
+import org.junit.Test;
+import org.sonar.channel.Channel;
+import org.sonar.channel.CodeReader;
+
+public class ChannelMatchersTest {
+
+ @Test
+ public void testConsumeMatcher() {
+ Channel<StringBuilder> numberChannel = new Channel<StringBuilder>() {
+
+ @Override
+ public boolean consume(CodeReader code, StringBuilder output) {
+ if (Character.isDigit(code.peek())) {
+ output.append((char) code.pop());
+ return true;
+ }
+ return false;
+ }
+ };
+ StringBuilder output = new StringBuilder();
+ assertThat(numberChannel, consume("3", output));
+ assertThat(output.toString(), is("3"));
+ assertThat(numberChannel, consume(new CodeReader("333333"), output));
+
+ output = new StringBuilder();
+ assertThat(numberChannel, not(consume("n", output)));
+ assertThat(output.toString(), is(""));
+ assertThat(numberChannel, not(consume(new CodeReader("n"), output)));
+ }
+
+ @Test
+ public void testHasNextChar() {
+ assertThat(new CodeReader("123"), hasNextChar('1'));
+ assertThat(new CodeReader("123"), not(hasNextChar('n')));
+ }
+}
diff --git a/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt b/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt
new file mode 100644
index 00000000000..19102815663
--- /dev/null
+++ b/sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt
@@ -0,0 +1 @@
+foo \ No newline at end of file