diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-06 14:08:06 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-06 14:08:06 +0000 |
commit | aeadc1f9129274949daaa57738c7c4550bdfbc7b (patch) | |
tree | 08dadf5ef7474fc41d1d48f74648f1ba8b55f34d /sonar-testing-harness/src/test/java | |
download | sonarqube-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/src/test/java')
-rw-r--r-- | sonar-testing-harness/src/test/java/org/sonar/test/TestUtilsTest.java | 93 | ||||
-rw-r--r-- | sonar-testing-harness/src/test/java/org/sonar/test/channel/ChannelMatchersTest.java | 63 |
2 files changed, 156 insertions, 0 deletions
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'))); + } +} |