aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel/src/test/java
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-channel/src/test/java
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-channel/src/test/java')
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java119
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java125
2 files changed, 244 insertions, 0 deletions
diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
new file mode 100644
index 00000000000..eedf28ae0ad
--- /dev/null
+++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.channel;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class CodeBufferTest {
+
+ @Test
+ public void testPop() {
+ CodeBuffer code = new CodeBuffer("pa");
+ assertThat((char) code.pop(), is('p'));
+ assertThat((char) code.pop(), is('a'));
+ assertThat(code.pop(), is( -1));
+ }
+
+ @Test
+ public void testPeek() {
+ CodeBuffer code = new CodeBuffer("pa");
+ assertThat((char) code.peek(), is('p'));
+ assertThat((char) code.peek(), is('p'));
+ code.pop();
+ assertThat((char) code.peek(), is('a'));
+ code.pop();
+ assertThat(code.peek(), is( -1));
+ }
+
+ @Test
+ public void testLastCharacter() {
+ CodeReader reader = new CodeReader("bar");
+ assertThat(reader.lastChar(), is( -1));
+ reader.pop();
+ assertThat((char) reader.lastChar(), is('b'));
+ }
+
+ @Test
+ public void testGetColumnAndLinePosition() {
+ CodeReader reader = new CodeReader("pa\nc\r\ns\r\n\r\n");
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(1));
+ reader.pop(); // p
+ reader.pop(); // a
+ assertThat(reader.getColumnPosition(), is(2));
+ assertThat(reader.getLinePosition(), is(1));
+ reader.peek(); // \n
+ reader.lastChar(); // a
+ assertThat(reader.getColumnPosition(), is(2));
+ assertThat(reader.getLinePosition(), is(1));
+ reader.pop(); // \n
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(2));
+ reader.pop(); // c
+ assertThat(reader.getColumnPosition(), is(1));
+ assertThat(reader.getLinePosition(), is(2));
+ reader.pop(); // \r
+ reader.pop(); // \n
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(3));
+ assertThat((char) reader.pop(), is('s'));
+ reader.pop(); // \r
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(4));
+ reader.pop(); // \n
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(4));
+ reader.pop(); // \r
+ reader.pop(); // \n
+ assertThat(reader.getColumnPosition(), is(0));
+ assertThat(reader.getLinePosition(), is(5));
+ }
+
+ @Test
+ public void testStartAndStopRecording() {
+ CodeReader reader = new CodeReader("123456");
+ reader.pop();
+ assertEquals("", reader.stopRecording().toString());
+
+ reader.startRecording();
+ reader.pop();
+ reader.pop();
+ reader.peek();
+ assertEquals("23", reader.stopRecording().toString());
+ assertEquals("", reader.stopRecording().toString());
+ }
+
+ @Test
+ public void testCharAt() {
+ CodeReader reader = new CodeReader("123456");
+ assertEquals('1', reader.charAt(0));
+ assertEquals('6', reader.charAt(5));
+ }
+
+ @Test
+ public void testCharAtIndexOutOfBoundsException() {
+ CodeReader reader = new CodeReader("12345");
+ assertEquals(reader.charAt(5), (char) -1);
+ }
+}
diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java
new file mode 100644
index 00000000000..7c195bff527
--- /dev/null
+++ b/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.channel;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.number.OrderingComparisons.lessThan;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import java.io.StringReader;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.junit.Test;
+
+public class CodeReaderTest {
+
+ @Test
+ public void testPopWithAppendable() {
+ CodeReader reader = new CodeReader("package org.sonar;");
+
+ StringBuilder sw = new StringBuilder();
+ reader.pop(sw);
+ assertEquals("p", sw.toString());
+ reader.pop(sw);
+ assertEquals("pa", sw.toString());
+
+ }
+
+ @Test
+ public void testPeekACharArray() {
+ CodeReader reader = new CodeReader(new StringReader("bar"));
+ char[] chars = reader.peek(2);
+ assertThat(chars.length, is(2));
+ assertThat(chars[0], is('b'));
+ assertThat(chars[1], is('a'));
+ }
+
+ @Test
+ public void testPeekTo() {
+ CodeReader reader = new CodeReader(new StringReader("package org.sonar;"));
+ StringBuilder result = new StringBuilder();
+ reader.peekTo(new EndMatcher() {
+
+ public boolean match(int endFlag) {
+ return 'r' == (char) endFlag;
+ }
+ }, result);
+ assertEquals("package o", result.toString());
+ assertThat(reader.peek(), is((int) 'p')); // never called pop()
+ }
+
+ @Test
+ public void testPopTo() {
+ CodeReader reader = new CodeReader(new StringReader("package org.sonar;"));
+ StringBuilder result = new StringBuilder();
+ reader.popTo(new EndMatcher() {
+
+ public boolean match(int endFlag) {
+ return 'r' == (char) endFlag;
+ }
+ }, result);
+ assertThat(result.toString(), is("package o"));
+ assertThat(reader.peek(), is((int) 'r'));
+ CodeReader.Cursor previousCursor = reader.getPreviousCursor();
+ assertThat(previousCursor.getColumn(), is(0));
+ assertThat(previousCursor.getLine(), is(1));
+ }
+
+ @Test
+ public void testPeekToAndReachingTheBufferLimit() {
+ CodeReader reader = new CodeReader("word1 word2 word2", 10);
+ for (int i = 0; i < 6; i++) {
+ reader.pop();
+ }
+ StringBuilder result = new StringBuilder();
+ reader.peekTo(new EndMatcher() {
+
+ public boolean match(int endFlag) {
+ return ' ' == (char) endFlag;
+ }
+ }, result);
+ assertEquals("word2", result.toString());
+ }
+
+ @Test
+ public void testPopToWithRegex() {
+ CodeReader reader = new CodeReader(new StringReader("123ABC"));
+ StringBuilder token = new StringBuilder();
+ assertEquals(3, reader.popTo(Pattern.compile("\\d+").matcher(new String()), token));
+ assertEquals("123", token.toString());
+ assertEquals( -1, reader.popTo(Pattern.compile("\\d+").matcher(new String()), token));
+ assertEquals(3, reader.popTo(Pattern.compile("\\w+").matcher(new String()), token));
+ assertEquals("123ABC", token.toString());
+ assertEquals( -1, reader.popTo(Pattern.compile("\\w+").matcher(new String()), token));
+ }
+
+ @Test
+ public void testPopToWithRegexAndFollowingMatcher() {
+ Matcher digitMatcher = Pattern.compile("\\d+").matcher(new String());
+ Matcher alphabeticMatcher = Pattern.compile("[a-zA-Z]").matcher(new String());
+ StringBuilder token = new StringBuilder();
+ assertEquals( -1, new CodeReader(new StringReader("123 ABC")).popTo(digitMatcher, alphabeticMatcher, token));
+ assertEquals("", token.toString());
+ assertEquals(3, new CodeReader(new StringReader("123ABC")).popTo(digitMatcher, alphabeticMatcher, token));
+ assertEquals("123", token.toString());
+ }
+}