From aeadc1f9129274949daaa57738c7c4550bdfbc7b Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 6 Sep 2010 14:08:06 +0000 Subject: SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console --- sonar-testing-harness/pom.xml | 87 +++++++++++++ .../src/main/java/org/sonar/test/TestUtils.java | 140 +++++++++++++++++++++ .../org/sonar/test/channel/ChannelMatcher.java | 57 +++++++++ .../org/sonar/test/channel/ChannelMatchers.java | 18 +++ .../test/channel/ReaderHasNextCharMatcher.java | 47 +++++++ .../test/java/org/sonar/test/TestUtilsTest.java | 93 ++++++++++++++ .../sonar/test/channel/ChannelMatchersTest.java | 63 ++++++++++ .../sonar/test/TestUtilsTest/getResource/foo.txt | 1 + 8 files changed, 506 insertions(+) create mode 100644 sonar-testing-harness/pom.xml create mode 100644 sonar-testing-harness/src/main/java/org/sonar/test/TestUtils.java create mode 100644 sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatcher.java create mode 100644 sonar-testing-harness/src/main/java/org/sonar/test/channel/ChannelMatchers.java create mode 100644 sonar-testing-harness/src/main/java/org/sonar/test/channel/ReaderHasNextCharMatcher.java create mode 100644 sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java create mode 100644 sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java create mode 100644 sonar-testing-harness/src/test/resources/org/sonar/test/TestUtilsTest/getResource/foo.txt (limited to 'sonar-testing-harness') 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 @@ + + + 4.0.0 + + org.codehaus.sonar + sonar + 2.3-SNAPSHOT + + sonar-testing-harness + jar + Sonar :: Testing Harness + + + + + maven-deploy-plugin + + false + + + + + + + junit + junit + + + org.mockito + mockito-all + + + org.hamcrest + hamcrest-all + + + xmlunit + xmlunit + + + ch.qos.logback + logback-classic + + + org.codehaus.sonar + sonar-plugin-api + ${project.version} + + + org.dbunit + dbunit + + + + commons-lang + commons-lang + + + xerces + xmlParserAPIs + + + xerces + xercesImpl + + + + + hsqldb + hsqldb + + + org.codehaus.sonar + sonar-plugin-api + ${project.version} + compile + test-jar + + + org.codehaus.sonar + sonar-core + ${project.version} + compile + test-jar + + + 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 extends BaseMatcher> { + + 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 channel = (Channel) 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 ChannelMatcher consume(String sourceCode, OUTPUT output) { + return new ChannelMatcher(sourceCode, output); + } + + public static ChannelMatcher consume(CodeReader codeReader, OUTPUT output) { + return new ChannelMatcher(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 { + + 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("", ""); + + // order of attributes + assertSimilarXml("", ""); + + // whitespaces are ignored + assertSimilarXml(" ", ""); + + // attribute values are checked + assertFalse(isSimilarXml("", "").similar()); + + // different xml + assertFalse(isSimilarXml("", "").similar()); + + // order of nodes is important + assertFalse(isSimilarXml("", "").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 numberChannel = new Channel() { + + @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 -- cgit v1.2.3