this.endUnit = builder.endUnit;
}
- /**
- * @deprecated since 2.14 use {@link #builder()}
- */
- @Deprecated
- public Block(String resourceId, ByteArray blockHash, int indexInFile, int startLine, int endLine) {
- this.resourceId = resourceId;
- this.blockHash = blockHash;
- this.indexInFile = indexInFile;
- this.startLine = startLine;
- this.endLine = endLine;
- }
-
public String getHashHex() {
return getBlockHash().toString();
}
// stored in a0. Clones are only reported, if tuples are lost in
// Line 12, as otherwise all current clones could be prolonged
// by one statement. Clone reporting matches tuples that, after
- // correction of the statement index, appear in both c(i) and a;
+ // correction of the statement index, appear in both c(i) and a,
// each matched pair corresponds to a single clone. Its location
// can be extracted from the filename and info fields.
*/
package org.sonar.duplications.index;
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Comparator;
-
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.TreeMultimap;
+import java.util.Collection;
public class MemoryCloneIndex implements CloneIndex {
- private final TreeMultimap<String, Block> filenameIndex;
- private final HashMultimap<ByteArray, Block> sequenceHashIndex;
-
- private static final class ValueComparator implements Comparator<Block>, Serializable {
-
- private static final long serialVersionUID = 6048010242032502222L;
-
- public int compare(Block o1, Block o2) {
- if (o2.getResourceId().equals(o1.getResourceId())) {
- return o1.getIndexInFile() - o2.getIndexInFile();
- }
- return o1.getResourceId().compareTo(o2.getResourceId());
- }
- }
-
- private static final class KeyComparator implements Comparator<String>, Serializable {
-
- private static final long serialVersionUID = 8705841881237170539L;
-
- public int compare(String o1, String o2) {
- return o1.compareTo(o2);
- }
- }
-
- public MemoryCloneIndex() {
- filenameIndex = TreeMultimap.create(new KeyComparator(), new ValueComparator());
- sequenceHashIndex = HashMultimap.create();
- }
-
- public Collection<String> getAllUniqueResourceId() {
- return filenameIndex.keySet();
- }
-
- public boolean containsResourceId(String resourceId) {
- return filenameIndex.containsKey(resourceId);
- }
+ private Multimap<String, Block> byResource = ArrayListMultimap.create();
+ private Multimap<ByteArray, Block> byHash = ArrayListMultimap.create();
- public Collection<Block> getByResourceId(String fileName) {
- return filenameIndex.get(fileName);
+ public Collection<Block> getByResourceId(String resourceId) {
+ return byResource.get(resourceId);
}
public Collection<Block> getBySequenceHash(ByteArray sequenceHash) {
- return sequenceHashIndex.get(sequenceHash);
+ return byHash.get(sequenceHash);
}
- public void insert(Block tuple) {
- filenameIndex.put(tuple.getResourceId(), tuple);
- sequenceHashIndex.put(tuple.getBlockHash(), tuple);
+ public void insert(Block block) {
+ byResource.put(block.getResourceId(), block);
+ byHash.put(block.getBlockHash(), block);
}
}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * 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.duplications.index;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
-import org.sonar.duplications.block.Block;
-import org.sonar.duplications.block.ByteArray;
-
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-
-public class MemoryCloneIndex2 extends AbstractCloneIndex {
-
- private Map<String, List<Block>> byResource = Maps.newHashMap();
- private Map<ByteArray, List<Block>> byHash = Maps.newHashMap();
-
- public Collection<Block> getByResourceId(String resourceId) {
- return get(byResource, resourceId);
- }
-
- public Collection<Block> getBySequenceHash(ByteArray sequenceHash) {
- return get(byHash, sequenceHash);
- }
-
- public void insert(Block block) {
- put(byResource, block.getResourceId(), block);
- put(byHash, block.getBlockHash(), block);
- }
-
- private static <T> List<Block> get(Map<T, List<Block>> map, T key) {
- List<Block> blocks = map.get(key);
- return blocks != null ? blocks : Collections.EMPTY_LIST;
- }
-
- private static <T> void put(Map<T, List<Block>> map, T key, Block value) {
- List<Block> blocks = map.get(key);
- if (blocks == null) {
- blocks = Lists.newLinkedList();
- map.put(key, blocks);
- }
- blocks.add(value);
- }
-
-}
import org.junit.Test;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.*;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.Assert.assertThat;
public class BlockTest {
assertThat(block.getEndUnit(), is(5));
}
- @Test
- public void fieldsTest() {
- String fileName = "someFile";
- int statementIndex = 4;
- ByteArray hash = new ByteArray(12345);
- Block tuple = new Block(fileName, hash, statementIndex, 0, 10);
- assertThat(tuple.getResourceId(), equalTo(fileName));
- assertThat(tuple.getIndexInFile(), equalTo(statementIndex));
- assertEquals(tuple.getBlockHash(), hash);
- }
-
- @Test
- public void tupleEqualsTest() {
- Block tuple1 = new Block("somefile", new ByteArray(123), 1, 1, 10);
- Block tuple2 = new Block("somefile", new ByteArray(123), 1, 1, 10);
- Block tupleArr = new Block("somefile", new ByteArray(333), 1, 1, 10);
- Block tupleIndex = new Block("somefile", new ByteArray(123), 2, 1, 10);
- Block tupleName = new Block("other", new ByteArray(123), 1, 1, 10);
-
- assertTrue(tuple1.equals(tuple2));
- assertThat(tuple1.toString(), is(tuple2.toString()));
-
- assertFalse(tuple1.equals(tupleArr));
- assertThat(tuple1.toString(), not(equalTo(tupleArr.toString())));
-
- assertFalse(tuple1.equals(tupleIndex));
- assertThat(tuple1.toString(), not(equalTo(tupleIndex.toString())));
-
- assertFalse(tuple1.equals(tupleName));
- assertThat(tuple1.toString(), not(equalTo(tupleName.toString())));
- }
-
- @Test
- public void hashCodeTest() {
- String[] files = {"file1", "file2"};
- int[] unitIndexes = {1, 2};
- ByteArray[] arrays = {new ByteArray(123), new ByteArray(321)};
-
- // fileName is in hashCode()
- int defaultTupleHashCode = new Block(files[0], arrays[0], unitIndexes[0], 1, 10).hashCode();
- int fileNameTupleHashCode = new Block(files[1], arrays[0], unitIndexes[0], 1, 10).hashCode();
- assertThat(defaultTupleHashCode, not(equalTo(fileNameTupleHashCode)));
-
- // statementIndex is in hashCode()
- int indexTupleHashCode = new Block(files[0], arrays[0], unitIndexes[1], 1, 10).hashCode();
- assertThat(defaultTupleHashCode, not(equalTo(indexTupleHashCode)));
-
- // sequenceHash is in hashCode()
- int sequenceHashTupleHashCode = new Block(files[0], arrays[1], unitIndexes[0], 1, 10).hashCode();
- assertThat(defaultTupleHashCode, not(equalTo(sequenceHashTupleHashCode)));
- }
}
*/
package org.sonar.duplications.detector;
-import static org.hamcrest.Matchers.hasItem;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.sameInstance;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup;
-
-import java.util.*;
-
+import com.google.common.collect.Lists;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.index.MemoryCloneIndex;
import org.sonar.duplications.junit.TestNamePrinter;
-import com.google.common.collect.Lists;
+import java.util.*;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.*;
+import static org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup;
public abstract class DetectorTestCase {
* so we can simply use index and hash.
*/
protected static Block newBlock(String resourceId, ByteArray hash, int index) {
- return new Block(resourceId, hash, index, index, index + LINES_PER_BLOCK);
+ return Block.builder()
+ .setResourceId(resourceId)
+ .setBlockHash(hash)
+ .setIndexInFile(index)
+ .setLines(index, index + LINES_PER_BLOCK)
+ .build();
}
protected static ClonePart newClonePart(String resourceId, int unitStart, int cloneUnitLength) {
@Test
public void same_lines_but_different_indexes() {
CloneIndex cloneIndex = createIndex();
+ Block.Builder block = Block.builder()
+ .setResourceId("a")
+ .setLines(0, 1);
List<Block> fileBlocks = Arrays.asList(
- new Block("a", new ByteArray("1".getBytes()), 0, 0, 1),
- new Block("a", new ByteArray("2".getBytes()), 1, 0, 1),
- new Block("a", new ByteArray("1".getBytes()), 2, 0, 1));
+ 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());
List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
print(clones);
*/
package org.sonar.duplications.detector.original;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Test;
import org.sonar.duplications.block.Block;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
public class BlocksGroupTest {
/**
* {@link BlocksGroup} uses only resourceId and index from block, thus we can simplify testing.
*/
private static Block newBlock(String resourceId, int indexInFile) {
- return new Block(resourceId, null, indexInFile, indexInFile, indexInFile);
+ return Block.builder()
+ .setResourceId(resourceId)
+ .setIndexInFile(indexInFile)
+ .setLines(indexInFile, indexInFile)
+ .build();
}
public static BlocksGroup newBlocksGroup(Block... blocks) {
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * 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.duplications.index;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Collection;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.duplications.block.Block;
-import org.sonar.duplications.block.ByteArray;
-
-public class MemoryCloneIndexTest {
-
- private CloneIndex cloneIndex;
-
- @Before
- public void initialize() {
- cloneIndex = new MemoryCloneIndex();
- }
-
- @Test
- public void byFileName() {
- Block tuple1 = new Block("a", new ByteArray(0), 0, 0, 10);
- Block tuple2 = new Block("a", new ByteArray(0), 1, 10, 20);
-
- assertThat(cloneIndex.getByResourceId("a").size(), is(0));
-
- cloneIndex.insert(tuple1);
- assertThat(cloneIndex.getByResourceId("a").size(), is(1));
-
- cloneIndex.insert(tuple2);
- assertThat(cloneIndex.getByResourceId("a").size(), is(2));
- }
-
- @Test
- public void bySequenceHash() {
- Block tuple1 = new Block("a", new ByteArray(0), 0, 0, 5);
- Block tuple2 = new Block("a", new ByteArray(0), 1, 1, 6);
-
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(0));
-
- cloneIndex.insert(tuple1);
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(1));
-
- cloneIndex.insert(tuple2);
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(2));
- }
-
- @Test
- public void insertSame() {
- Block tuple = new Block("a", new ByteArray(0), 0, 0, 5);
- Block tupleSame = new Block("a", new ByteArray(0), 0, 0, 5);
-
- assertThat(cloneIndex.getByResourceId("a").size(), is(0));
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(0));
-
- cloneIndex.insert(tuple);
- assertThat(cloneIndex.getByResourceId("a").size(), is(1));
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(1));
-
- cloneIndex.insert(tupleSame);
- assertThat(cloneIndex.getByResourceId("a").size(), is(1));
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(0)).size(), is(1));
- }
-
- @Test
- public void testSorted() {
- for (int i = 0; i < 10; i++) {
- cloneIndex.insert(new Block("a", new ByteArray(1), 10 - i, i, i + 5));
- }
- assertThat(cloneIndex.getByResourceId("a").size(), is(10));
- assertThat(cloneIndex.getBySequenceHash(new ByteArray(1)).size(), is(10));
-
- Collection<Block> set = cloneIndex.getByResourceId("a");
- int prevStatementIndex = 0;
- for (Block tuple : set) {
- assertTrue(tuple.getIndexInFile() > prevStatementIndex);
- prevStatementIndex = tuple.getIndexInFile();
- }
- }
-}
*/
package org.sonar.duplications.index;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.sameInstance;
-import static org.junit.Assert.assertThat;
-
-import java.util.Collection;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
+import java.util.Collection;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.Assert.assertThat;
+
public class PackedMemoryCloneIndexTest {
private PackedMemoryCloneIndex index;
}
private static Block newBlock(String resourceId, long hash) {
- return new Block(resourceId, new ByteArray(hash), 1, 1, 1);
+ return Block.builder()
+ .setResourceId(resourceId)
+ .setBlockHash(new ByteArray(hash))
+ .setIndexInFile(1)
+ .setLines(1, 2)
+ .build();
}
}