2828import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBranch ;
2929import com .cloudbees .jenkins .plugins .bitbucket .trait .DiscardOldBranchTrait .ExcludeOldSCMHeadBranch ;
3030import java .util .Arrays ;
31- import java .util .Calendar ;
3231import java .util .Date ;
3332import jenkins .scm .api .SCMHead ;
3433import jenkins .scm .api .SCMHeadObserver ;
3534import jenkins .scm .api .trait .SCMHeadFilter ;
35+ import org .apache .commons .lang3 .time .DateUtils ;
3636import org .junit .jupiter .api .Test ;
3737
3838import static org .assertj .core .api .Assertions .assertThat ;
@@ -48,21 +48,15 @@ void verify_that_branch_is_not_excluded_if_has_recent_commits() throws Exception
4848 trait .decorateContext (ctx );
4949 assertThat (ctx .filters ()).hasAtLeastOneElementOfType (ExcludeOldSCMHeadBranch .class );
5050
51- long lastCommitDate = new Date (). getTime ();
51+ Date now = new Date ();
5252
5353 SCMHead head = mock (SCMHead .class );
5454 when (head .getName ()).thenReturn ("feature/release" );
5555
56- BitbucketBranch branch1 = mock (BitbucketBranch .class );
57- when (branch1 .getName ()).thenReturn ("feature/xyz" );
58- when (branch1 .getDateMillis ()).thenReturn (lastCommitDate );
59-
60- BitbucketBranch branch2 = mock (BitbucketBranch .class );
61- when (branch2 .getName ()).thenReturn ("feature/release" );
62- when (branch2 .getDateMillis ()).thenReturn (lastCommitDate );
63-
64- BitbucketSCMSourceRequest request = mock (BitbucketSCMSourceRequest .class );
65- when (request .getBranches ()).thenReturn (Arrays .asList (branch1 , branch2 ));
56+ BitbucketSCMSourceRequest request = prepareRequest (
57+ buildBranch ("feature/xyz" , DateUtils .addDays (now , -1 ).getTime ()),
58+ buildBranch ("feature/release" , DateUtils .addDays (now , -10 ).getTime ())
59+ );
6660
6761 for (SCMHeadFilter filter : ctx .filters ()) {
6862 assertThat (filter .isExcluded (request , head )).isFalse ();
@@ -76,27 +70,32 @@ void verify_that_branch_is_excluded_if_has_head_commit_older_than_specified() th
7670 trait .decorateContext (ctx );
7771 assertThat (ctx .filters ()).hasAtLeastOneElementOfType (ExcludeOldSCMHeadBranch .class );
7872
79- Calendar c = Calendar .getInstance ();
80- c .add (Calendar .DAY_OF_MONTH , -100 );
81- long lastCommitDate = c .getTimeInMillis ();
73+ Date now = new Date ();
8274
8375 SCMHead head = mock (SCMHead .class );
8476 when (head .getName ()).thenReturn ("feature/release" );
8577
86- BitbucketBranch branch1 = mock (BitbucketBranch .class );
87- when (branch1 .getName ()).thenReturn ("feature/xyz" );
88- when (branch1 .getDateMillis ()).thenReturn (lastCommitDate );
89-
90- BitbucketBranch branch2 = mock (BitbucketBranch .class );
91- when (branch2 .getName ()).thenReturn ("feature/release" );
92- when (branch2 .getDateMillis ()).thenReturn (lastCommitDate );
93-
94- BitbucketSCMSourceRequest request = mock (BitbucketSCMSourceRequest .class );
95- when (request .getBranches ()).thenReturn (Arrays .asList (branch1 , branch2 ));
78+ BitbucketSCMSourceRequest request = prepareRequest (
79+ buildBranch ("feature/xyz" , DateUtils .addDays (now , -6 ).getTime ()),
80+ buildBranch ("feature/release" , DateUtils .addDays (now , -10 ).getTime ())
81+ );
9682
9783 for (SCMHeadFilter filter : ctx .filters ()) {
9884 assertThat (filter .isExcluded (request , head )).isTrue ();
9985 }
10086 }
10187
88+ private BitbucketBranch buildBranch (String name , long date ) {
89+ BitbucketBranch branch = mock (BitbucketBranch .class );
90+ when (branch .getName ()).thenReturn (name );
91+ when (branch .getDateMillis ()).thenReturn (date );
92+ return branch ;
93+ }
94+
95+ private BitbucketSCMSourceRequest prepareRequest (BitbucketBranch ...branches ) {
96+ BitbucketSCMSourceRequest request = mock (BitbucketSCMSourceRequest .class );
97+ when (request .getBranches ()).thenReturn (Arrays .asList (branches ));
98+ return request ;
99+ }
100+
102101}
0 commit comments