aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel/src/test/java
diff options
context:
space:
mode:
authorbellingard <bellingard@gmail.com>2010-10-14 13:45:29 +0000
committerbellingard <bellingard@gmail.com>2010-10-14 13:45:29 +0000
commit7b0cb741b188abd0be71b5dfacbe5f3a0a5cda9f (patch)
tree5fab661c104408d8dab61dba80ecc1775aecca37 /sonar-channel/src/test/java
parent4ccf48f4e76bfe9a1783aa9b17e3d4d1c5c278aa (diff)
downloadsonarqube-7b0cb741b188abd0be71b5dfacbe5f3a0a5cda9f.tar.gz
sonarqube-7b0cb741b188abd0be71b5dfacbe5f3a0a5cda9f.zip
[SONAR-1853] Create a new CodeReaderFilter mechanism to prevent logic duplications between Channel(s)
http://jira.codehaus.org/browse/SONAR-1853
Diffstat (limited to 'sonar-channel/src/test/java')
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java101
1 files changed, 96 insertions, 5 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
index eedf28ae0ad..05ac7704804 100644
--- a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
+++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
@@ -23,6 +23,10 @@ import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.regex.Pattern;
+
import org.junit.Test;
public class CodeBufferTest {
@@ -48,7 +52,7 @@ public class CodeBufferTest {
@Test
public void testLastCharacter() {
- CodeReader reader = new CodeReader("bar");
+ CodeBuffer reader = new CodeBuffer("bar");
assertThat(reader.lastChar(), is( -1));
reader.pop();
assertThat((char) reader.lastChar(), is('b'));
@@ -56,7 +60,7 @@ public class CodeBufferTest {
@Test
public void testGetColumnAndLinePosition() {
- CodeReader reader = new CodeReader("pa\nc\r\ns\r\n\r\n");
+ CodeBuffer reader = new CodeBuffer("pa\nc\r\ns\r\n\r\n");
assertThat(reader.getColumnPosition(), is(0));
assertThat(reader.getLinePosition(), is(1));
reader.pop(); // p
@@ -92,7 +96,7 @@ public class CodeBufferTest {
@Test
public void testStartAndStopRecording() {
- CodeReader reader = new CodeReader("123456");
+ CodeBuffer reader = new CodeBuffer("123456");
reader.pop();
assertEquals("", reader.stopRecording().toString());
@@ -106,14 +110,101 @@ public class CodeBufferTest {
@Test
public void testCharAt() {
- CodeReader reader = new CodeReader("123456");
+ CodeBuffer reader = new CodeBuffer("123456");
assertEquals('1', reader.charAt(0));
assertEquals('6', reader.charAt(5));
}
@Test
public void testCharAtIndexOutOfBoundsException() {
- CodeReader reader = new CodeReader("12345");
+ CodeBuffer reader = new CodeBuffer("12345");
assertEquals(reader.charAt(5), (char) -1);
}
+
+ @Test
+ public void testCodeReaderFilter() throws Exception {
+ CodeBuffer code = new CodeBuffer("abcd12efgh34", new ReplaceNumbersFilter());
+ // test #charAt
+ assertEquals('a', code.charAt(0));
+ assertEquals('-', code.charAt(4));
+ assertEquals('-', code.charAt(5));
+ assertEquals('e', code.charAt(6));
+ assertEquals('-', code.charAt(10));
+ assertEquals('-', code.charAt(11));
+ // test peek and pop
+ assertThat((char) code.peek(), is('a'));
+ assertThat((char) code.pop(), is('a'));
+ assertThat((char) code.pop(), is('b'));
+ assertThat((char) code.pop(), is('c'));
+ assertThat((char) code.pop(), is('d'));
+ assertThat((char) code.peek(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('e'));
+ assertThat((char) code.pop(), is('f'));
+ assertThat((char) code.pop(), is('g'));
+ assertThat((char) code.pop(), is('h'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ }
+
+ @Test
+ public void testSeveralCodeReaderFilter() throws Exception {
+ CodeBuffer code = new CodeBuffer("abcd12efgh34", new ReplaceNumbersFilter(), new ReplaceCharFilter());
+ // test #charAt
+ assertEquals('*', code.charAt(0));
+ assertEquals('-', code.charAt(4));
+ assertEquals('-', code.charAt(5));
+ assertEquals('*', code.charAt(6));
+ assertEquals('-', code.charAt(10));
+ assertEquals('-', code.charAt(11));
+ // test peek and pop
+ assertThat((char) code.peek(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.peek(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('*'));
+ assertThat((char) code.pop(), is('-'));
+ assertThat((char) code.pop(), is('-'));
+ }
+
+ class ReplaceNumbersFilter extends CodeReaderFilter {
+
+ private Pattern pattern = Pattern.compile("\\d");
+ private String REPLACEMENT = "-";
+
+ public int read(Reader in, char[] cbuf, int off, int len) throws IOException {
+ char[] tempBuffer = new char[cbuf.length];
+ int charCount = in.read(tempBuffer, off, len);
+ if (charCount != -1) {
+ String filteredString = pattern.matcher(new String(tempBuffer)).replaceAll(REPLACEMENT);
+ System.arraycopy(filteredString.toCharArray(), 0, cbuf, 0, tempBuffer.length);
+ }
+ return charCount;
+ }
+ }
+
+ class ReplaceCharFilter extends CodeReaderFilter {
+
+ private Pattern pattern = Pattern.compile("[a-zA-Z]");
+ private String REPLACEMENT = "*";
+
+ public int read(Reader in, char[] cbuf, int off, int len) throws IOException {
+ char[] tempBuffer = new char[cbuf.length];
+ int charCount = in.read(tempBuffer, off, len);
+ if (charCount != -1) {
+ String filteredString = pattern.matcher(new String(tempBuffer)).replaceAll(REPLACEMENT);
+ System.arraycopy(filteredString.toCharArray(), 0, cbuf, 0, tempBuffer.length);
+ }
+ return charCount;
+ }
+ }
+
}