]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6993 Add root component ref in analysis metadata holder
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 10 Nov 2015 15:59:46 +0000 (16:59 +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 c916fc8feb8e449d3485974460937064062e9710..595afb2817f40fc40228e01d5eb2fe5c323f027b 100644 (file)
@@ -56,4 +56,9 @@ public interface AnalysisMetadataHolder {
   @CheckForNull
   String getBranch();
 
+  /**
+   * @throws IllegalStateException if root component ref has not been set
+   */
+  int getRootComponentRef();
+
 }
index 81f10bef2ca13a0d657606d8be0977c4519ab95f..6cd49d3bd14becdf39d4a62e84ddbd550fe3ecf3 100644 (file)
@@ -28,16 +28,17 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
-  @CheckForNull
+
   private InitializedProperty<Long> analysisDate = new InitializedProperty<>();
 
   private InitializedProperty<Snapshot> baseProjectSnapshot = new InitializedProperty<>();
 
-  @CheckForNull
   private InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
 
   private InitializedProperty<String> branch = new InitializedProperty<>();
 
+  private InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
+
   @Override
   public void setAnalysisDate(Date date) {
     checkNotNull(date, "Date must not be null");
@@ -93,6 +94,18 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
     return branch.getProperty();
   }
 
+  @Override
+  public void setRootComponentRef(int rootComponentRef) {
+    checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
+    this.rootComponentRef.setProperty(rootComponentRef);
+  }
+
+  @Override
+  public int getRootComponentRef() {
+    checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
+    return rootComponentRef.getProperty();
+  }
+
   private static class InitializedProperty<E> {
     private E property;
     private boolean initialized = false;
@@ -112,4 +125,5 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
       return initialized;
     }
   }
+
 }
index 8c3f0ca98ecb56fb81bc44e846f04b20cde80b17..eec006c0591a3c89b958cdf0dd7f7d1b48bd6310 100644 (file)
@@ -46,4 +46,9 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
    */
   void setBranch(@Nullable String branch);
 
+  /**
+   * @throws IllegalStateException if root component ref has already been set
+   */
+  void setRootComponentRef(int rootComponentRef);
+
 }
index f0aca8636a847186d6a09bc72fb0b106db4c2576..a204ab73c57adf441e6772fd9f181df0af69ea3d 100644 (file)
@@ -194,4 +194,31 @@ public class AnalysisMetadataHolderImplTest {
     expectedException.expectMessage("Branch has already been set");
     underTest.setBranch("origin/master");
   }
+
+  @Test
+  public void getRootComponentRef() throws InterruptedException {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+
+    underTest.setRootComponentRef(10);
+
+    assertThat(underTest.getRootComponentRef()).isEqualTo(10);
+  }
+
+  @Test
+  public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Root component ref has not been set");
+
+    new AnalysisMetadataHolderImpl().getRootComponentRef();
+  }
+
+  @Test
+  public void setRootComponentRef_throws_ISE_when_called_twice() {
+    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+    underTest.setRootComponentRef(10);
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Root component ref has already been set");
+    underTest.setRootComponentRef(9);
+  }
 }
index fed4344a683d5bd4254ac8d235a9af3ba13c4ca1..51a9fb5295b628f45be6e2f58d0a1516fa43623a 100644 (file)
@@ -79,4 +79,14 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   public void setBranch(@Nullable String branch) {
     delegate.setBranch(branch);
   }
+
+  @Override
+  public void setRootComponentRef(int rootComponentRef) {
+    delegate.setRootComponentRef(rootComponentRef);
+  }
+
+  @Override
+  public int getRootComponentRef() {
+    return delegate.getRootComponentRef();
+  }
 }