Skip to content

Commit

Permalink
SONAR-6993 Add branch in analysis metadata holder
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Nov 12, 2015
1 parent 660e2ed commit dc752be
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 26 deletions.
Expand Up @@ -50,4 +50,10 @@ public interface AnalysisMetadataHolder {
*/ */
boolean isCrossProjectDuplicationEnabled(); boolean isCrossProjectDuplicationEnabled();


/**
* @throws IllegalStateException if branch has not been set
*/
@CheckForNull
String getBranch();

} }
Expand Up @@ -24,30 +24,31 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.server.computation.snapshot.Snapshot; import org.sonar.server.computation.snapshot.Snapshot;


import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;


public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder { public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
@CheckForNull @CheckForNull
private Long analysisDate; private InitializedProperty<Long> analysisDate = new InitializedProperty<>();


private boolean baseProjectSnapshotInitialized = false; private InitializedProperty<Snapshot> baseProjectSnapshot = new InitializedProperty<>();


@CheckForNull @CheckForNull
private Snapshot baseProjectSnapshot; private InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();


@CheckForNull private InitializedProperty<String> branch = new InitializedProperty<>();
private Boolean isCrossProjectDuplicationEnabled;


@Override @Override
public void setAnalysisDate(Date date) { public void setAnalysisDate(Date date) {
checkState(analysisDate == null, "Analysis date has already been set"); checkNotNull(date, "Date must not be null");
this.analysisDate = date.getTime(); checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
this.analysisDate.setProperty(date.getTime());
} }


@Override @Override
public Date getAnalysisDate() { public Date getAnalysisDate() {
checkState(analysisDate != null, "Analysis date has not been set"); checkState(analysisDate.isInitialized(), "Analysis date has not been set");
return new Date(this.analysisDate); return new Date(this.analysisDate.getProperty());
} }


@Override @Override
Expand All @@ -57,27 +58,58 @@ public boolean isFirstAnalysis() {


@Override @Override
public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) { public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
checkState(!baseProjectSnapshotInitialized, "Base project snapshot has already been set"); checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
this.baseProjectSnapshot = baseProjectSnapshot; this.baseProjectSnapshot.setProperty(baseProjectSnapshot);
this.baseProjectSnapshotInitialized = true;
} }


@Override @Override
@CheckForNull @CheckForNull
public Snapshot getBaseProjectSnapshot() { public Snapshot getBaseProjectSnapshot() {
checkState(baseProjectSnapshotInitialized, "Base project snapshot has not been set"); checkState(baseProjectSnapshot.isInitialized(), "Base project snapshot has not been set");
return baseProjectSnapshot; return baseProjectSnapshot.getProperty();
} }


@Override @Override
public void setIsCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) { public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
checkState(this.isCrossProjectDuplicationEnabled == null, "Cross project duplication flag has already been set"); checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
this.isCrossProjectDuplicationEnabled = isCrossProjectDuplicationEnabled; this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
} }


@Override @Override
public boolean isCrossProjectDuplicationEnabled() { public boolean isCrossProjectDuplicationEnabled() {
checkState(isCrossProjectDuplicationEnabled != null, "Cross project duplication flag has not been set"); checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
return isCrossProjectDuplicationEnabled; 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;
}
} }
} }
Expand Up @@ -39,6 +39,11 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
/** /**
* @throws IllegalStateException if cross project duplication flag has already been set * @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);


} }
Expand Up @@ -60,6 +60,16 @@ public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() {
new AnalysisMetadataHolderImpl().getAnalysisDate(); 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 @Test
public void setAnalysisDate_throws_ISE_when_called_twice() { public void setAnalysisDate_throws_ISE_when_called_twice() {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
Expand Down Expand Up @@ -117,7 +127,7 @@ public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
public void isCrossProjectDuplicationEnabled_return_true() { public void isCrossProjectDuplicationEnabled_return_true() {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();


underTest.setIsCrossProjectDuplicationEnabled(true); underTest.setCrossProjectDuplicationEnabled(true);


assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true); assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
} }
Expand All @@ -126,7 +136,7 @@ public void isCrossProjectDuplicationEnabled_return_true() {
public void isCrossProjectDuplicationEnabled_return_false() { public void isCrossProjectDuplicationEnabled_return_false() {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();


underTest.setIsCrossProjectDuplicationEnabled(false); underTest.setCrossProjectDuplicationEnabled(false);


assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false); assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
} }
Expand All @@ -142,10 +152,46 @@ public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initi
@Test @Test
public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() { public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
underTest.setIsCrossProjectDuplicationEnabled(true); underTest.setCrossProjectDuplicationEnabled(true);


expectedException.expect(IllegalStateException.class); expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Cross project duplication flag has already been set"); 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");
} }
} }
Expand Up @@ -66,7 +66,17 @@ public boolean isCrossProjectDuplicationEnabled() {
} }


@Override @Override
public void setIsCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) { public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
delegate.setIsCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled); delegate.setCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled);
}

@Override
public String getBranch() {
return delegate.getBranch();
}

@Override
public void setBranch(@Nullable String branch) {
delegate.setBranch(branch);
} }
} }

0 comments on commit dc752be

Please sign in to comment.