]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 3 Feb 2012 12:14:58 +0000 (16:14 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 3 Feb 2012 21:58:31 +0000 (01:58 +0400)
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java
sonar-duplications/src/main/java/net/sourceforge/pmd/cpd/Language.java
sonar-duplications/src/main/java/org/sonar/duplications/detector/original/OriginalCloneDetectionAlgorithm.java
sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/DuplicationsCollector.java
sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java
sonar-duplications/src/test/java/org/sonar/duplications/detector/original/FilterTest.java

index 2b6c2b39a4e059b143c131260e62289f614b492c..434049fdbd6af82dbd2b052411688c9deb8e0f86 100644 (file)
  */
 package org.sonar.plugins.cpd;
 
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.SensorContext;
@@ -39,6 +29,14 @@ import org.sonar.api.test.IsMeasure;
 import org.sonar.duplications.index.CloneGroup;
 import org.sonar.duplications.index.ClonePart;
 
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.*;
+
 public class SonarEngineTest {
 
   private SensorContext context;
@@ -132,7 +130,7 @@ public class SonarEngineTest {
   }
 
   private CloneGroup newCloneGroup(ClonePart... parts) {
-    return new CloneGroup(0, parts[0], Arrays.asList(parts));
+    return CloneGroup.builder().setLength(0).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build();
   }
 
 }
index 6b1e11c643c6c290ee6452e0ee29409760fde8d5..8fb352cfe847bad6272c5857585f90a60c303b06 100644 (file)
@@ -27,8 +27,6 @@ import java.io.FilenameFilter;
 
 public interface Language {
 
-  String fileSeparator = System.getProperty("file.separator");
-
   Tokenizer getTokenizer();
 
   FilenameFilter getFileFilter();
index bef9f4e80a4ef5374ca82cd6d5f4080856e2e6f1..afefdef5c07ede7496ab04e7f4e369d63007d0ad 100644 (file)
@@ -223,7 +223,7 @@ public final class OriginalCloneDetectionAlgorithm {
       parts.add(part);
     }
 
-    filter.add(new CloneGroup(cloneLength, origin, parts));
+    filter.add(CloneGroup.builder().setLength(cloneLength).setOrigin(origin).setParts(parts).build());
   }
 
 }
index 6c05737240bf4228a215aee404eb51da529e4e00..b36f5b3c94fa4bc2b875609de903ee324cbf4cc0 100644 (file)
@@ -80,8 +80,10 @@ public class DuplicationsCollector extends Search.Collector {
    */
   @Override
   public void endOfGroup() {
-    int lengthInUnits = 0;
     ClonePart origin = null;
+
+    CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
+
     List<ClonePart> parts = Lists.newArrayListWithCapacity(count);
     for (int[] b : blockNumbers) {
       Block firstBlock = text.getBlock(b[0]);
@@ -97,7 +99,7 @@ public class DuplicationsCollector extends Search.Collector {
         if (origin == null) {
           origin = part;
           // To calculate length important to use the origin, because otherwise block may come from DB without required data
-          lengthInUnits = lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1;
+          builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
         } else if (part.getUnitStart() < origin.getUnitStart()) {
           origin = part;
         }
@@ -107,11 +109,9 @@ public class DuplicationsCollector extends Search.Collector {
     }
 
     Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
+    builder.setOrigin(origin).setParts(parts);
 
-    CloneGroup group = new CloneGroup(length, origin, parts);
-    group.setLengthInUnits(lengthInUnits);
-
-    filter(group);
+    filter(builder.build());
 
     reset();
   }
index dfb86bb93d69a9e42a90a02427e376ea8446f9db..eda0b40b8cd4ddc4820bab94078e691ef10473f7 100644 (file)
@@ -38,10 +38,52 @@ public class CloneGroup {
    */
   private int hash;
 
-  public CloneGroup(int cloneLength, ClonePart origin, List<ClonePart> parts) {
-    this.cloneLength = cloneLength;
-    this.originPart = origin;
-    this.parts = ImmutableList.copyOf(parts);
+  /**
+   * @since 2.14
+   */
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  /**
+   * @since 2.14
+   */
+  public static final class Builder {
+    private ClonePart origin;
+    private int length;
+    private int lengthInUnits;
+    private List<ClonePart> parts;
+
+    public Builder setLength(int length) {
+      this.length = length;
+      return this;
+    }
+
+    public Builder setOrigin(ClonePart origin) {
+      this.origin = origin;
+      return this;
+    }
+
+    public Builder setParts(List<ClonePart> parts) {
+      this.parts = ImmutableList.copyOf(parts);
+      return this;
+    }
+
+    public Builder setLengthInUnits(int length) {
+      this.lengthInUnits = length;
+      return this;
+    }
+
+    public CloneGroup build() {
+      return new CloneGroup(this);
+    }
+  }
+
+  private CloneGroup(Builder builder) {
+    this.cloneLength = builder.length;
+    this.originPart = builder.origin;
+    this.parts = builder.parts;
+    this.length = builder.lengthInUnits;
   }
 
   public ClonePart getOriginPart() {
@@ -60,17 +102,6 @@ public class CloneGroup {
     return length;
   }
 
-  /**
-   * TODO get rid of this method, otherwise class is not immutable
-   *
-   * @since 2.14
-   * @see #getLengthInUnits()
-   */
-  @Beta
-  public void setLengthInUnits(int length) {
-    this.length = length;
-  }
-
   /**
    * @return clone length in {@link org.sonar.duplications.block.Block}s
    */
index bf60290a6ab7f2ddcc181af34ff4559f2e6c9187..6ed2a9e1a4150dcda5766f9cd375f49fe6c261b9 100644 (file)
  */
 package org.sonar.duplications.detector.original;
 
-import static org.hamcrest.Matchers.is;
-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 java.util.Arrays;
-
 import org.junit.Test;
 import org.sonar.duplications.index.CloneGroup;
 import org.sonar.duplications.index.ClonePart;
 
+import java.util.Arrays;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.*;
+
 public class FilterTest {
 
   /**
@@ -187,7 +185,7 @@ public class FilterTest {
    * Creates new group from list of parts, origin - is a first part from list.
    */
   private CloneGroup newCloneGroup(int len, ClonePart... parts) {
-    return new CloneGroup(len, parts[0], Arrays.asList(parts));
+    return CloneGroup.builder().setLength(len).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build();
   }
 
 }