diff options
author | Julien HENRY <henryju@yahoo.fr> | 2017-10-03 18:33:55 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-10-04 13:48:58 +0200 |
commit | 7aae33867472515bcec1d5161e6fca8d31d6c0bd (patch) | |
tree | 1b22504ab0daf84301c42ecbcdab0ce7ff38c0c2 /sonar-duplications/src/test/java | |
parent | b811d8eaa4bce4d6bdd5ec7164ecb4ad5fa979c8 (diff) | |
download | sonarqube-7aae33867472515bcec1d5161e6fca8d31d6c0bd.tar.gz sonarqube-7aae33867472515bcec1d5161e6fca8d31d6c0bd.zip |
SONAR-9111 Remove Guava dependency from sonar-duplications
Diffstat (limited to 'sonar-duplications/src/test/java')
5 files changed, 97 insertions, 98 deletions
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java index 2a3576936a8..86716386be7 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java @@ -19,14 +19,16 @@ */ package org.sonar.duplications.block; -import com.google.common.collect.Lists; -import org.junit.Test; -import org.sonar.duplications.statement.Statement; - +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.junit.Test; +import org.sonar.duplications.statement.Statement; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.sameInstance; import static org.junit.Assert.assertThat; /** @@ -131,7 +133,7 @@ public abstract class BlockChunkerTestCase { * Creates list of statements from Strings, each statement on a new line starting from 0. */ protected static List<Statement> createStatementsFromStrings(String... values) { - List<Statement> result = Lists.newArrayList(); + List<Statement> result = new ArrayList<>(); for (int i = 0; i < values.length; i++) { result.add(new Statement(i, i, values[i])); } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java index 29974b89a2b..ee3e8b32232 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java @@ -19,7 +19,10 @@ */ package org.sonar.duplications.detector; -import com.google.common.collect.Lists; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; import org.junit.Rule; import org.junit.Test; import org.sonar.duplications.block.Block; @@ -30,10 +33,6 @@ import org.sonar.duplications.index.ClonePart; import org.sonar.duplications.index.MemoryCloneIndex; import org.sonar.duplications.junit.TestNamePrinter; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; @@ -367,8 +366,7 @@ public abstract class DetectorTestCase { public void problemWithEndOfFile() { CloneIndex cloneIndex = createIndex( newBlocks("b", "1 2 3 4")); - Block[] fileBlocks = - newBlocks("a", "1 2 3"); + Block[] fileBlocks = newBlocks("a", "1 2 3"); List<CloneGroup> clones = detect(cloneIndex, fileBlocks); print(clones); @@ -399,7 +397,7 @@ public abstract class DetectorTestCase { Block.Builder block = Block.builder() .setResourceId("a") .setLines(0, 1); - Block[] fileBlocks = new Block[]{ + Block[] fileBlocks = new Block[] { block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(0).build(), block.setBlockHash(new ByteArray("2".getBytes())).setIndexInFile(1).build(), block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(2).build() @@ -426,7 +424,7 @@ public abstract class DetectorTestCase { } protected static Block[] newBlocks(String resourceId, String hashes) { - List<Block> result = Lists.newArrayList(); + List<Block> result = new ArrayList<>(); int indexInFile = 0; for (int i = 0; i < hashes.length(); i += 2) { Block block = newBlock(resourceId, new ByteArray("0" + hashes.charAt(i)), indexInFile); diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java index 694db8205e6..4f02f68ba83 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java @@ -19,7 +19,8 @@ */ package org.sonar.duplications.java; -import com.google.common.base.Joiner; +import java.util.Collection; +import java.util.List; import org.junit.Test; import org.sonar.duplications.block.Block; import org.sonar.duplications.block.BlockChunker; @@ -32,9 +33,8 @@ import org.sonar.duplications.statement.Statement; import org.sonar.duplications.statement.StatementChunker; import org.sonar.duplications.token.TokenChunker; -import java.util.Collection; -import java.util.List; - +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.joining; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -55,18 +55,18 @@ public class JavaDuplicationsFunctionalTest { @Test public void type1() { String fragment0 = source( - "if (a >= b) {", - " c = d + b; // Comment1", - " d = d + 1;}", - "else", - " c = d - a; // Comment2"); + "if (a >= b) {", + " c = d + b; // Comment1", + " d = d + 1;}", + "else", + " c = d - a; // Comment2"); String fragment1 = source( - "if (a>=b) {", - " // Comment1", - " c=d+b;", - " d=d+1;", - "} else // Comment2", - " c=d-a;"); + "if (a>=b) {", + " // Comment1", + " c=d+b;", + " d=d+1;", + "} else // Comment2", + " c=d-a;"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -83,17 +83,17 @@ public class JavaDuplicationsFunctionalTest { @Test public void type2_literals() { String fragment0 = source( - "if (a >= b) {", - " c = b + 1; // Comment1", - " d = '1';}", - "else", - " c = d - a; // Comment2"); + "if (a >= b) {", + " c = b + 1; // Comment1", + " d = '1';}", + "else", + " c = d - a; // Comment2"); String fragment1 = source( - "if (a >= b) {", - " c = b + 2; // Comment1", - " d = '2';}", - "else", - " c = d - a; // Comment2"); + "if (a >= b) {", + " c = b + 2; // Comment1", + " d = '2';}", + "else", + " c = d - a; // Comment2"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -104,18 +104,18 @@ public class JavaDuplicationsFunctionalTest { @Test public void type2() { String fragment0 = source( - "if (a >= b) {", - " c = d + b; // Comment1", - " d = d + 1;}", - "else", - " c = d - a; // Comment2"); + "if (a >= b) {", + " c = d + b; // Comment1", + " d = d + 1;}", + "else", + " c = d - a; // Comment2"); String fragment1 = source( - "if (m >= n) {", - " // Comment3", - " y = x + n; // Comment1", - " x = x + 5;}", - "else", - " y = x - m; // Comment2"); + "if (m >= n) {", + " // Comment3", + " y = x + n; // Comment1", + " x = x + 5;}", + "else", + " y = x - m; // Comment2"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(0)); } @@ -126,22 +126,21 @@ public class JavaDuplicationsFunctionalTest { @Test public void type3() { String fragment0 = source( - "public int getSoLinger() throws SocketException {", - " Object o = impl.getOption( SocketOptions.SO_LINGER);", - " if (o instanceof Integer) {", - " return((Integer) o).intValue();", - " }", - " else return -1;", - "}"); + "public int getSoLinger() throws SocketException {", + " Object o = impl.getOption( SocketOptions.SO_LINGER);", + " if (o instanceof Integer) {", + " return((Integer) o).intValue();", + " }", + " else return -1;", + "}"); String fragment1 = source( - "public synchronized int getSoTimeout() throws SocketException {", - " Object o = impl.getOption( SocketOptions.SO_TIMEOUT);", - " if (o instanceof Integer) {", - " return((Integer) o).intValue();", - " }", - " else return -0;", - "}" - ); + "public synchronized int getSoTimeout() throws SocketException {", + " Object o = impl.getOption( SocketOptions.SO_TIMEOUT);", + " if (o instanceof Integer) {", + " return((Integer) o).intValue();", + " }", + " else return -0;", + "}"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); @@ -150,7 +149,7 @@ public class JavaDuplicationsFunctionalTest { } private String source(String... lines) { - return Joiner.on('\n').join(lines); + return asList(lines).stream().collect(joining("\n")); } private static List<CloneGroup> detect2(String... fragments) { @@ -181,7 +180,7 @@ public class JavaDuplicationsFunctionalTest { private static BlockChunker BLOCK_CHUNKER = new BlockChunker(BLOCK_SIZE); private List<CloneGroup> detect(String... lines) { - String sourceCode = Joiner.on('\n').join(lines); + String sourceCode = asList(lines).stream().collect(joining("\n")); MemoryCloneIndex index = new MemoryCloneIndex(); List<Statement> statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode)); List<Block> blocks = BLOCK_CHUNKER.chunk("resourceId", statements); @@ -206,25 +205,25 @@ public class JavaDuplicationsFunctionalTest { @Test public void chainOfCases() { List<CloneGroup> duplications = detect( - "switch (a) {", - " case 'a': case 'b': case 'c':", - " doSomething();", - " case 'd': case 'e': case 'f':", - " doSomethingElse();", - "}"); + "switch (a) {", + " case 'a': case 'b': case 'c':", + " doSomething();", + " case 'd': case 'e': case 'f':", + " doSomethingElse();", + "}"); assertThat(duplications.size(), is(0)); } @Test public void literalsNormalization() { List<CloneGroup> duplications = detect( - "String s = \"abc\";", - "String s = \"def\";"); + "String s = \"abc\";", + "String s = \"def\";"); assertThat(duplications.size(), is(1)); duplications = detect( - "int i = 1;", - "int i = 2;"); + "int i = 1;", + "int i = 2;"); assertThat(duplications.size(), is(1)); } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java index 0468e5c2111..3ee629b73f9 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java @@ -19,24 +19,23 @@ */ package org.sonar.duplications.java; -import com.google.common.collect.Lists; -import org.apache.commons.io.IOUtils; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.sonar.duplications.DuplicationsTestUtil; -import org.sonar.duplications.token.Token; -import org.sonar.duplications.token.TokenChunker; -import org.sonar.duplications.token.TokenQueue; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.commons.io.IOUtils; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.sonar.duplications.DuplicationsTestUtil; +import org.sonar.duplications.token.Token; +import org.sonar.duplications.token.TokenChunker; +import org.sonar.duplications.token.TokenQueue; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -337,7 +336,9 @@ public class JavaTokenProducerTest { } private List<Token> chunk(String sourceCode) { - return Lists.newArrayList(chunker.chunk(sourceCode)); + List<Token> target = new ArrayList<>(); + chunker.chunk(sourceCode).forEach(target::add); + return target; } } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java index 3acd69f7c6e..a0746cbdd4f 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java @@ -19,20 +19,19 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.junit.Test; import org.sonar.duplications.token.Token; import org.sonar.duplications.token.TokenQueue; -import com.google.common.collect.Lists; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; public class ForgetLastTokenMatcherTest { @@ -40,7 +39,7 @@ public class ForgetLastTokenMatcherTest { public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); Token token = new Token("a", 0, 0); - List<Token> output = Lists.newArrayList(token); + List<Token> output = new ArrayList<>(Arrays.asList(token)); ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher(); assertThat(matcher.matchToken(tokenQueue, output), is(true)); |