Skip to content

Commit

Permalink
fix: can't have 2 @DataBoundConstructors
Browse files Browse the repository at this point in the history
Refactor to move default value for commentTrigger to config.jelly. Handle special case in BitbucketBuildRepository where trigger field is null within unit tests (since BitbucketBuildTrigger can't be mocked or initialized in the test).
  • Loading branch information
nblair committed Feb 18, 2017
1 parent 96e6cb6 commit 83260d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
* Created by nishio
*/
public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
/**
* Default value for {@link #getCommentTrigger()}.
*/
public static final String DEFAULT_COMMENT_TRIGGER = "test this please";

private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName());
private final String projectPath;
private final String cron;
Expand All @@ -55,30 +50,6 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
@Extension
public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor();

/**
* Sets {@link #getCommentTrigger()} to {@link #DEFAULT_COMMENT_TRIGGER}.
*/
@DataBoundConstructor
public BitbucketBuildTrigger(
String projectPath,
String cron,
String credentialsId,
String username,
String password,
String repositoryOwner,
String repositoryName,
String branchesFilter,
boolean branchesFilterBySCMIncludes,
String ciKey,
String ciName,
String ciSkipPhrases,
boolean checkDestinationCommit,
boolean approveIfSuccess
) throws ANTLRException {
this(projectPath, cron, credentialsId, username, password, repositoryOwner, repositoryName,
branchesFilter, branchesFilterBySCMIncludes, ciKey, ciName, ciSkipPhrases,
checkDestinationCommit, approveIfSuccess, DEFAULT_COMMENT_TRIGGER);
}
@DataBoundConstructor
public BitbucketBuildTrigger(
String projectPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class BitbucketRepository {
private static final String BUILD_REQUEST_DONE_MARKER = "ttp build flag";
private static final String BUILD_REQUEST_MARKER_TAG_SINGLE_RX = "\\#[\\w\\-\\d]+";
private static final String BUILD_REQUEST_MARKER_TAGS_RX = "\\[bid\\:\\s?(.*)\\]";
/**
* Default value for comment trigger.
*/
public static final String DEFAULT_COMMENT_TRIGGER = "test this please";

private String projectPath;
private BitbucketPullRequestsBuilder builder;
Expand Down Expand Up @@ -182,7 +186,12 @@ private void postBuildTagInTTPComment(String pullRequestId, String content, Stri
}

private boolean isTTPComment(String content) {
return content.toLowerCase().contains(trigger.getCommentTrigger().toLowerCase());
// special case: in unit tests, trigger is null and can't be mocked
String commentTrigger = DEFAULT_COMMENT_TRIGGER;
if(trigger != null && StringUtils.isNotBlank(trigger.getCommentTrigger())) {
commentTrigger = trigger.getCommentTrigger();
}
return content.toLowerCase().contains(commentTrigger);
}

private boolean isTTPCommentBuildTags(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
<f:checkbox />
</f:entry>
<f:entry title="Comment phrase to trigger build" field="commentTrigger">
<f:textbox />
<f:textbox default="test this please"/>
</f:entry>
</j:jelly>
6 changes: 4 additions & 2 deletions src/test/java/BitbucketBuildFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import antlr.ANTLRException;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildFilter;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildTrigger;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketCause;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketPullRequestsBuilder;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketRepository;
Expand Down Expand Up @@ -194,8 +196,8 @@ public void fromGitSCMFilter() {

@Test
@WithoutJenkins
public void filterPRComments() {
BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class);
public void filterPRComments() throws ANTLRException {
BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class);
EasyMock.expect(builder.getTrigger()).andReturn(null).anyTimes();
EasyMock.replay(builder);

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/BitbucketBuildRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void repositorySimpleUserPasswordTest() throws Exception {
"", true,
"", "", "",
true,
true
true,
BitbucketRepository.DEFAULT_COMMENT_TRIGGER
);

BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class);
Expand Down Expand Up @@ -148,7 +149,7 @@ public void repositoryCtorWithTriggerTest() throws Exception {
"", true,
"", "", "",
true,
true
true, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
);

BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class);
Expand Down Expand Up @@ -202,7 +203,7 @@ public void repositoryProjectIdTest() throws ANTLRException, NoSuchAlgorithmExce
"", true,
"jenkins", "Jenkins", "",
true,
true
true, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
);

BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class);
Expand Down Expand Up @@ -248,7 +249,7 @@ public void triggerLongCIKeyTest() throws ANTLRException, NoSuchAlgorithmExcepti
"", true,
"jenkins-too-long-ci-key", "Jenkins", "",
true,
true
true, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
);

final MessageDigest MD5 = MessageDigest.getInstance("MD5");
Expand Down

0 comments on commit 83260d5

Please sign in to comment.