diff --git a/pom.xml b/pom.xml index a1cdc0b32..b9bb7a4be 100644 --- a/pom.xml +++ b/pom.xml @@ -147,7 +147,7 @@ junit junit - 4.11 + 4.12 test diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/AllBranchesFilterTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/AllBranchesFilterTest.java new file mode 100644 index 000000000..8c2d8f55f --- /dev/null +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/AllBranchesFilterTest.java @@ -0,0 +1,31 @@ +package com.dabsquared.gitlabjenkins.trigger.filter; + +import org.apache.commons.lang.RandomStringUtils; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +/** + * @author Robin Müller + */ +public class AllBranchesFilterTest { + + @Test + public void isRandomBranchNameAllowed() { + String randomBranchName = RandomStringUtils.random(10, true, false); + + assertThat(new AllBranchesFilter().isBranchAllowed(randomBranchName), is(true)); + } + + @Test + public void getConfig() { + BranchFilterConfig config = new AllBranchesFilter().getConfig(); + + assertThat(config.getType(), is(BranchFilterType.All)); + assertThat(config.getExcludeBranchesSpec(), nullValue()); + assertThat(config.getIncludeBranchesSpec(), nullValue()); + assertThat(config.getTargetBranchRegex(), nullValue()); + } +} diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/BranchFilterFactoryTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/BranchFilterFactoryTest.java new file mode 100644 index 000000000..bd51b18c3 --- /dev/null +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/BranchFilterFactoryTest.java @@ -0,0 +1,60 @@ +package com.dabsquared.gitlabjenkins.trigger.filter; + +import org.junit.Test; + +import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +/** + * @author Robin Müller + */ +public class BranchFilterFactoryTest { + + @Test + public void getAllBranchesFilter() { + BranchFilter branchFilter = BranchFilterFactory.newBranchFilter(branchFilterConfig() + .withIncludeBranchesSpec("master") + .withExcludeBranchesSpec("develop") + .withTargetBranchRegex(".*") + .build(BranchFilterType.All)); + + assertThat(branchFilter, instanceOf(AllBranchesFilter.class)); + assertThat(branchFilter.getConfig().getIncludeBranchesSpec(), nullValue()); + assertThat(branchFilter.getConfig().getExcludeBranchesSpec(), nullValue()); + assertThat(branchFilter.getConfig().getTargetBranchRegex(), nullValue()); + } + + @Test + public void getNameBasedFilterFilter() { + String includeBranchesSpec = "master"; + String excludeBranchesSpec = "develop"; + BranchFilter branchFilter = BranchFilterFactory.newBranchFilter(branchFilterConfig() + .withIncludeBranchesSpec(includeBranchesSpec) + .withExcludeBranchesSpec(excludeBranchesSpec) + .withTargetBranchRegex(".*") + .build(BranchFilterType.NameBasedFilter)); + + assertThat(branchFilter, instanceOf(NameBasedFilter.class)); + assertThat(branchFilter.getConfig().getIncludeBranchesSpec(), is(includeBranchesSpec)); + assertThat(branchFilter.getConfig().getExcludeBranchesSpec(), is(excludeBranchesSpec)); + assertThat(branchFilter.getConfig().getTargetBranchRegex(), nullValue()); + } + + @Test + public void getRegexBasedFilterFilter() { + String regex = ".*"; + BranchFilter branchFilter = BranchFilterFactory.newBranchFilter(branchFilterConfig() + .withIncludeBranchesSpec("master") + .withExcludeBranchesSpec("develop") + .withTargetBranchRegex(regex) + .build(BranchFilterType.RegexBasedFilter)); + + assertThat(branchFilter, instanceOf(RegexBasedFilter.class)); + assertThat(branchFilter.getConfig().getIncludeBranchesSpec(), nullValue()); + assertThat(branchFilter.getConfig().getExcludeBranchesSpec(), nullValue()); + assertThat(branchFilter.getConfig().getTargetBranchRegex(), is(regex)); + } +} diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/NameBasedFilterTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/NameBasedFilterTest.java new file mode 100644 index 000000000..dccd960a3 --- /dev/null +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/NameBasedFilterTest.java @@ -0,0 +1,52 @@ +package com.dabsquared.gitlabjenkins.trigger.filter; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +/** + * @author Robin Müller + */ +public class NameBasedFilterTest { + + @Test + public void includeBranches() { + NameBasedFilter nameBasedFilter = new NameBasedFilter("master, develop", ""); + + assertThat(nameBasedFilter.isBranchAllowed("master"), is(true)); + assertThat(nameBasedFilter.isBranchAllowed("develop"), is(true)); + } + + @Test + public void excludeBranches() { + NameBasedFilter nameBasedFilter = new NameBasedFilter("", "master, develop"); + + assertThat(nameBasedFilter.isBranchAllowed("master"), is(false)); + assertThat(nameBasedFilter.isBranchAllowed("develop"), is(false)); + assertThat(nameBasedFilter.isBranchAllowed("not-excluded-branch"), is(true)); + } + + @Test + public void includeAndExcludeBranches() { + NameBasedFilter nameBasedFilter = new NameBasedFilter("master", "develop"); + + assertThat(nameBasedFilter.isBranchAllowed("master"), is(true)); + assertThat(nameBasedFilter.isBranchAllowed("develop"), is(false)); + assertThat(nameBasedFilter.isBranchAllowed("not-excluded-and-not-included-branch"), is(false)); + } + + @Test + public void getConfig() { + String includedBranches = "master, develop"; + String excludedBranches = "hotfix/test"; + + BranchFilterConfig config = new NameBasedFilter(includedBranches, excludedBranches).getConfig(); + + assertThat(config.getType(), is(BranchFilterType.NameBasedFilter)); + assertThat(config.getIncludeBranchesSpec(), is(includedBranches)); + assertThat(config.getExcludeBranchesSpec(), is(excludedBranches)); + assertThat(config.getTargetBranchRegex(), nullValue()); + } +} diff --git a/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/RegexBasedFilterTest.java b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/RegexBasedFilterTest.java new file mode 100644 index 000000000..b1bb3c710 --- /dev/null +++ b/src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/RegexBasedFilterTest.java @@ -0,0 +1,51 @@ +package com.dabsquared.gitlabjenkins.trigger.filter; + +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +/** + * @author Robin Müller + */ +@RunWith(Theories.class) +public class RegexBasedFilterTest { + + @DataPoints("matching-branches") + public static String[] matchingBranchNames = {"feature/test", "feature/awesome-feature"}; + + @DataPoints("not-matching-branches") + public static String[] notMatchingBranchNames = {"hotfix/test", "hotfix/awesome-feature", "master", "develop"}; + + @Theory + public void isRegexBranchAllowed(@FromDataPoints("matching-branches") String branchName) { + RegexBasedFilter featureBranches = new RegexBasedFilter("feature/.*"); + + assertThat(featureBranches.isBranchAllowed(branchName), is(true)); + } + + @Theory + public void isRegexBranchNotAllowed(@FromDataPoints("not-matching-branches") String branchName) { + RegexBasedFilter featureBranches = new RegexBasedFilter("feature/.*"); + + assertThat(featureBranches.isBranchAllowed(branchName), is(false)); + } + + @Test + public void getConfig() { + String regex = "test.*"; + + BranchFilterConfig config = new RegexBasedFilter(regex).getConfig(); + + assertThat(config.getType(), is(BranchFilterType.RegexBasedFilter)); + assertThat(config.getIncludeBranchesSpec(), nullValue()); + assertThat(config.getExcludeBranchesSpec(), nullValue()); + assertThat(config.getTargetBranchRegex(), is(regex)); + } +}