*/
package org.sonar.ce.task.projectanalysis.analysis;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.Arrays;
+import java.util.Optional;
+import java.util.stream.Stream;
+import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
+import org.sonar.core.platform.PlatformEditionProvider;
import org.sonar.db.component.BranchType;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.project.Project;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.sonar.core.platform.EditionProvider.Edition;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
+@RunWith(DataProviderRunner.class)
public class AnalysisMetadataHolderImplTest {
private static Analysis baseProjectAnalysis = new Analysis.Builder()
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
+ private AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
@Test
public void getOrganization_throws_ISE_if_organization_is_not_set() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Analysis date has not been set");
- new AnalysisMetadataHolderImpl().getAnalysisDate();
+ new AnalysisMetadataHolderImpl(editionProvider).getAnalysisDate();
}
@Test
public void setAnalysisDate_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setAnalysisDate(SOME_DATE);
expectedException.expect(IllegalStateException.class);
@Test
public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
- assertThat(new AnalysisMetadataHolderImpl().hasAnalysisDateBeenSet()).isFalse();
+ assertThat(new AnalysisMetadataHolderImpl(editionProvider).hasAnalysisDateBeenSet()).isFalse();
}
@Test
public void hasAnalysisDateBeenSet_returns_true_when_holder_date_is_set() {
- AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl(editionProvider);
holder.setAnalysisDate(46532);
assertThat(holder.hasAnalysisDateBeenSet()).isTrue();
}
@Test
public void isFirstAnalysis_return_true() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBaseAnalysis(null);
assertThat(underTest.isFirstAnalysis()).isTrue();
@Test
public void isFirstAnalysis_return_false() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBaseAnalysis(baseProjectAnalysis);
assertThat(underTest.isFirstAnalysis()).isFalse();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Base project snapshot has not been set");
- new AnalysisMetadataHolderImpl().isFirstAnalysis();
+ new AnalysisMetadataHolderImpl(editionProvider).isFirstAnalysis();
}
@Test
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Base project snapshot has not been set");
- new AnalysisMetadataHolderImpl().getBaseAnalysis();
+ new AnalysisMetadataHolderImpl(editionProvider).getBaseAnalysis();
}
@Test
public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBaseAnalysis(baseProjectAnalysis);
expectedException.expect(IllegalStateException.class);
@Test
public void isCrossProjectDuplicationEnabled_return_true() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setCrossProjectDuplicationEnabled(true);
@Test
public void isCrossProjectDuplicationEnabled_return_false() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setCrossProjectDuplicationEnabled(false);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Cross project duplication flag has not been set");
- new AnalysisMetadataHolderImpl().isCrossProjectDuplicationEnabled();
+ new AnalysisMetadataHolderImpl(editionProvider).isCrossProjectDuplicationEnabled();
}
@Test
public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setCrossProjectDuplicationEnabled(true);
expectedException.expect(IllegalStateException.class);
@Test
public void set_branch() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(new DefaultBranchImpl("master"));
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Branch has not been set");
- new AnalysisMetadataHolderImpl().getBranch();
+ new AnalysisMetadataHolderImpl(editionProvider).getBranch();
}
@Test
public void setBranch_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(new DefaultBranchImpl("master"));
expectedException.expect(IllegalStateException.class);
underTest.setBranch(new DefaultBranchImpl("master"));
}
+ @Test
+ @UseDataProvider("anyEditionIncludingNone")
+ public void setBranch_does_not_fail_if_main_branch_on_any_edition(@Nullable Edition edition) {
+ when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
+ Branch branch = mock(Branch.class);
+ when(branch.isMain()).thenReturn(true);
+ when(branch.isLegacyFeature()).thenReturn(false);
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
+
+ underTest.setBranch(branch);
+
+ assertThat(underTest.getBranch()).isSameAs(branch);
+ }
+
+ @Test
+ @UseDataProvider("anyEditionIncludingNone")
+ public void setBranch_does_not_fail_if_legacy_branch_on_any_edition(@Nullable Edition edition) {
+ when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
+ Branch branch = mock(Branch.class);
+ when(branch.isMain()).thenReturn(false);
+ when(branch.isLegacyFeature()).thenReturn(true);
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
+
+ underTest.setBranch(branch);
+
+ assertThat(underTest.getBranch()).isSameAs(branch);
+ }
+
+ @Test
+ @UseDataProvider("anyEditionIncludingNoneButCommunity")
+ public void setBranch_does_not_fail_if_non_main_non_legacy_branch_on_any_edition_but_Community(@Nullable Edition edition) {
+ when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
+ Branch branch = mock(Branch.class);
+ when(branch.isMain()).thenReturn(false);
+ when(branch.isLegacyFeature()).thenReturn(false);
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
+
+ underTest.setBranch(branch);
+
+ assertThat(underTest.getBranch()).isSameAs(branch);
+ }
+
+ @Test
+ public void setBranch_fails_if_non_main_non_legacy_branch_on_Community_edition() {
+ when(editionProvider.get()).thenReturn(Optional.of(Edition.COMMUNITY));
+ Branch branch = mock(Branch.class);
+ when(branch.isMain()).thenReturn(false);
+ when(branch.isLegacyFeature()).thenReturn(false);
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Branches and Pull Requests are not supported in Community Edition");
+
+ underTest.setBranch(branch);
+ }
+
+ @DataProvider
+ public static Object[][] anyEditionIncludingNone() {
+ return Stream.concat(
+ Stream.of((Edition) null),
+ Arrays.stream(Edition.values()))
+ .map(t -> new Object[] {t})
+ .toArray(Object[][]::new);
+ }
+
+ @DataProvider
+ public static Object[][] anyEditionIncludingNoneButCommunity() {
+ return Stream.concat(
+ Stream.of((Edition) null),
+ Arrays.stream(Edition.values())).filter(t -> t != Edition.COMMUNITY)
+ .map(t -> new Object[] {t})
+ .toArray(Object[][]::new);
+ }
+
@Test
public void setPullRequestId() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
String pullRequestId = "pr-123";
underTest.setPullRequestKey(pullRequestId);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Pull request key has not been set");
- new AnalysisMetadataHolderImpl().getPullRequestKey();
+ new AnalysisMetadataHolderImpl(editionProvider).getPullRequestKey();
}
@Test
public void setPullRequestId_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setPullRequestKey("pr-123");
expectedException.expect(IllegalStateException.class);
@Test
public void set_and_get_project() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
Project project = Project.from(newPrivateProjectDto(newOrganizationDto()));
underTest.setProject(project);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Project has not been set");
- new AnalysisMetadataHolderImpl().getProject();
+ new AnalysisMetadataHolderImpl(editionProvider).getProject();
}
@Test
public void setProject_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setProject(Project.from(newPrivateProjectDto(newOrganizationDto())));
expectedException.expect(IllegalStateException.class);
@Test
public void getRootComponentRef() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setRootComponentRef(10);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Root component ref has not been set");
- new AnalysisMetadataHolderImpl().getRootComponentRef();
+ new AnalysisMetadataHolderImpl(editionProvider).getRootComponentRef();
}
@Test
public void setRootComponentRef_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setRootComponentRef(10);
expectedException.expect(IllegalStateException.class);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Branch has not been set");
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.isShortLivingBranch();
}
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.SHORT);
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(branch);
assertThat(underTest.isShortLivingBranch()).isTrue();
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.SHORT);
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(branch);
assertThat(underTest.isSLBorPR()).isTrue();
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.LONG);
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(branch);
assertThat(underTest.isSLBorPR()).isFalse();
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setBranch(branch);
assertThat(underTest.isPullRequest()).isTrue();
@Test
public void setScmRevision_throws_ISE_when_called_twice() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setScmRevision("bd56dab");
expectedException.expect(IllegalStateException.class);
@Test
public void getScmRevision_returns_empty_if_scmRevision_is_not_initialized() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
assertThat(underTest.getScmRevision()).isNotPresent();
}
@Test
public void getScmRevision_returns_scmRevision_if_scmRevision_is_initialized() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setScmRevision("bd56dab");
assertThat(underTest.getScmRevision()).hasValue("bd56dab");
}
@Test
public void getScmRevision_does_not_return_empty_string() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setScmRevision("");
assertThat(underTest.getScmRevision()).isEmpty();
}
@Test
public void getScmRevision_does_not_return_blank_string() {
- AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
+ AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
underTest.setScmRevision(" ");
assertThat(underTest.getScmRevision()).isEmpty();
}