]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6993 Add branch in analysis metadata holder
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 10 Nov 2015 15:56:43 +0000 (16:56 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 10:01:29 +0000 (11:01 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/AnalysisMetadataHolder.java
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/AnalysisMetadataHolderImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/MutableAnalysisMetadataHolder.java
server/sonar-server/src/test/java/org/sonar/server/computation/analysis/AnalysisMetadataHolderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/analysis/MutableAnalysisMetadataHolderRule.java

index 4a78f052c86101e331390e1cb2720e9e6d6bcf65..c916fc8feb8e449d3485974460937064062e9710 100644 (file)
@@ -50,4 +50,10 @@ public interface AnalysisMetadataHolder {
    */
   boolean isCrossProjectDuplicationEnabled();
 
+  /**
+   * @throws IllegalStateException if branch has not been set
+   */
+  @CheckForNull
+  String getBranch();
+
 }
index 429451abb0c5dc733c1ae9bf2c85c711b03d1867..81f10bef2ca13a0d657606d8be0977c4519ab95f 100644 (file)
@@ -24,30 +24,31 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.server.computation.snapshot.Snapshot;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
   @CheckForNull
-  private Long analysisDate;
+  private InitializedProperty<Long> analysisDate = new InitializedProperty<>();
 
-  private boolean baseProjectSnapshotInitialized = false;
+  private InitializedProperty<Snapshot> baseProjectSnapshot = new InitializedProperty<>();
 
   @CheckForNull
-  private Snapshot baseProjectSnapshot;
+  private InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
 
-  @CheckForNull
-  private Boolean isCrossProjectDuplicationEnabled;
+  private InitializedProperty<String> branch = new InitializedProperty<>();
 
   @Override
   public void setAnalysisDate(Date date) {
-    checkState(analysisDate == null, "Analysis date has already been set");
-    this.analysisDate = date.getTime();
+    checkNotNull(date, "Date must not be null");
+    checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
+    this.analysisDate.setProperty(date.getTime());
   }
 
   @Override
   public Date getAnalysisDate() {
-    checkState(analysisDate != null, "Analysis date has not been set");
-    return new Date(this.analysisDate);
+    checkState(analysisDate.isInitialized(), "Analysis date has not been set");
+    return new Date(this.analysisDate.getProperty());
   }
 
   @Override
@@ -57,27 +58,58 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
 
   @Override
   public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
-    checkState(!baseProjectSnapshotInitialized, "Base project snapshot has already been set");
-    this.baseProjectSnapshot = baseProjectSnapshot;
-    this.baseProjectSnapshotInitialized = true;
+    checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
+    this.baseProjectSnapshot.setProperty(baseProjectSnapshot);
   }
 
   @Override
   @CheckForNull
   public Snapshot getBaseProjectSnapshot() {
-    checkState(baseProjectSnapshotInitialized, "Base project snapshot has not been set");
-    return baseProjectSnapshot;
+    checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
+    return baseProjectSnapshot.getProperty();
   }
 
   @Override
-  public void setIsCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
-    checkState(this.isCrossProjectDuplicationEnabled == null, "Cross project duplication flag has already been set");
-    this.isCrossProjectDuplicationEnabled = isCrossProjectDuplicationEnabled;
+  public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+    checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
+    this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
   }
 
   @Override
   public boolean isCrossProjectDuplicationEnabled() {
-    checkState(isCrossProjectDuplicationEnabled != null, "Cross project duplication flag has not been set");
-    return isCrossProjectDuplicationEnabled;
+    checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
+    return crossProjectDuplicationEnabled.getProperty();
+  }
+
+  @Override
+  public void setBranch(@Nullable String branch) {
+    checkState(!this.branch.isInitialized(), "Branch has already been set");
+    this.branch.setProperty(branch);
+  }
+
+  @Override
+  public String getBranch() {
+    checkState(branch.isInitialized(), "Branch has not been set");
+    return branch.getProperty();
+  }
+
+  private static class InitializedProperty<E> {
+    private E property;
+    private boolean initialized = false;
+
+    public InitializedProperty setProperty(@Nullable E property) {
+      this.property = property;
+      this.initialized = true;
+      return this;
+    }
+
+    @CheckForNull
+    public E getProperty() {
+      return property;
+    }
+
+    public boolean isInitialized() {
+      return initialized;
+    }
   }
 }
index 5d0b20918b472c1e87545b1a5f594f04223d96d5..8c3f0ca98ecb56fb81bc44e846f04b20cde80b17 100644 (file)
@@ -39,6 +39,11 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
   /**
    * @throws IllegalStateException if cross project duplication flag has already been set
    */
-  void setIsCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
+  void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
+
+  /**
+   * @throws IllegalStateException if branch has already been set
+   */
+  void setBranch(@Nullable String branch);
 
 }
index a1925060be78dcb16059c7314716f79d7eb0dad1..f0aca8636a847186d6a09bc72fb0b106db4c2576 100644 (file)
@@ -60,6 +60,16 @@ public class AnalysisMetadataHolderImplTest {
     new AnalysisMetadataHolderImpl().getAnalysisDate();
   }
 
+  @Test
+  public void setAnalysisDate_throws_NPE_when_date_is_null() {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("Date must not be null");
+
+    underTest.setAnalysisDate(null);
+  }
+
   @Test
   public void setAnalysisDate_throws_ISE_when_called_twice() {
     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
@@ -117,7 +127,7 @@ public class AnalysisMetadataHolderImplTest {
   public void isCrossProjectDuplicationEnabled_return_true() {
     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
 
-    underTest.setIsCrossProjectDuplicationEnabled(true);
+    underTest.setCrossProjectDuplicationEnabled(true);
 
     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
   }
@@ -126,7 +136,7 @@ public class AnalysisMetadataHolderImplTest {
   public void isCrossProjectDuplicationEnabled_return_false() {
     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
 
-    underTest.setIsCrossProjectDuplicationEnabled(false);
+    underTest.setCrossProjectDuplicationEnabled(false);
 
     assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
   }
@@ -142,10 +152,46 @@ public class AnalysisMetadataHolderImplTest {
   @Test
   public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
-    underTest.setIsCrossProjectDuplicationEnabled(true);
+    underTest.setCrossProjectDuplicationEnabled(true);
 
     expectedException.expect(IllegalStateException.class);
     expectedException.expectMessage("Cross project duplication flag has already been set");
-    underTest.setIsCrossProjectDuplicationEnabled(false);
+    underTest.setCrossProjectDuplicationEnabled(false);
+  }
+
+  @Test
+  public void set_branch() {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+
+    underTest.setBranch("origin/master");
+
+    assertThat(underTest.getBranch()).isEqualTo("origin/master");
+  }
+
+  @Test
+  public void set_no_branch() {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+
+    underTest.setBranch(null);
+
+    assertThat(underTest.getBranch()).isNull();
+  }
+
+  @Test
+  public void branch_throws_ISE_when_holder_is_not_initialized() {
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Branch has not been set");
+
+    new AnalysisMetadataHolderImpl().getBranch();
+  }
+
+  @Test
+  public void setBranch_throws_ISE_when_called_twice() {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+    underTest.setBranch("origin/master");
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Branch has already been set");
+    underTest.setBranch("origin/master");
   }
 }
index 476be72f60d16cbd0ee9a9df28d7bdd83e79be04..fed4344a683d5bd4254ac8d235a9af3ba13c4ca1 100644 (file)
@@ -66,7 +66,17 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   }
 
   @Override
-  public void setIsCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
-    delegate.setIsCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled);
+  public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+    delegate.setCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled);
+  }
+
+  @Override
+  public String getBranch() {
+    return delegate.getBranch();
+  }
+
+  @Override
+  public void setBranch(@Nullable String branch) {
+    delegate.setBranch(branch);
   }
 }