]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6990 add ComponentImpl.equals and hashcode based on uuid
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 6 Nov 2015 14:42:50 +0000 (15:42 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 9 Nov 2015 15:34:20 +0000 (16:34 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java

index e2a65f0c142632077bedc31db3a936b58211f7c6..ad6a5c8e9ec239f66e714e317e9ddc32f4b5ab3e 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList;
 import java.util.ArrayList;
 import java.util.List;
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
 import org.sonar.batch.protocol.Constants;
 import org.sonar.batch.protocol.output.BatchReport;
@@ -66,17 +67,11 @@ public class ComponentImpl implements Component {
 
   @Override
   public String getUuid() {
-    if (uuid == null) {
-      throw new UnsupportedOperationException(String.format("Component uuid of ref '%s' has not be fed yet", this.reportAttributes.getRef()));
-    }
     return uuid;
   }
 
   @Override
   public String getKey() {
-    if (key == null) {
-      throw new UnsupportedOperationException(String.format("Component key of ref '%s' has not be fed yet", this.reportAttributes.getRef()));
-    }
     return key;
   }
 
@@ -207,4 +202,21 @@ public class ComponentImpl implements Component {
       ", reportAttributes=" + reportAttributes +
       '}';
   }
+
+  @Override
+  public boolean equals(@Nullable Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    ComponentImpl component = (ComponentImpl) o;
+    return uuid.equals(component.uuid);
+  }
+
+  @Override
+  public int hashCode() {
+    return uuid.hashCode();
+  }
 }
index fde6c340d669c4fdcc227d3836a1c335653791e9..a3d3823df35af3a199837fdf734935b08742828c 100644 (file)
@@ -159,4 +159,20 @@ public class ComponentImplTest {
     return builder(reportComponent).setKey(KEY).setUuid(UUID).build();
   }
 
+  @Test
+  public void equals_compares_on_uuid_only() {
+    ComponentImpl.Builder builder = builder(BatchReport.Component.newBuilder().build()).setUuid(UUID);
+
+    assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("1").build());
+    assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("2").build());
+  }
+
+  @Test
+  public void hashCode_is_hashcode_of_uuid() {
+    ComponentImpl.Builder builder = builder(BatchReport.Component.newBuilder().build()).setUuid(UUID);
+
+    assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("1").build().hashCode());
+    assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("2").build().hashCode());
+    assertThat(builder.setKey("1").build().hashCode()).isEqualTo(UUID.hashCode());
+  }
 }