Skip to content

Commit

Permalink
Add tests for branch filters
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hugo committed Mar 14, 2016
1 parent fe7f56d commit 4dbbbc7
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 4dbbbc7

Please sign in to comment.