diff --git a/src/test/java/jenkins/scm/api/SCMCategoryTest.java b/src/test/java/jenkins/scm/api/SCMCategoryTest.java index 3d9cc9f4..1eff91de 100644 --- a/src/test/java/jenkins/scm/api/SCMCategoryTest.java +++ b/src/test/java/jenkins/scm/api/SCMCategoryTest.java @@ -25,6 +25,10 @@ package jenkins.scm.api; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + import edu.umd.cs.findbugs.annotations.NonNull; import java.util.Arrays; import java.util.Collections; @@ -38,52 +42,66 @@ import jenkins.scm.impl.UncategorizedSCMHeadCategory; import jenkins.util.NonLocalizable; import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.localizer.Localizable; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; +class SCMCategoryTest { -public class SCMCategoryTest { @Test - public void toDisplayName() throws Exception { - assertThat(SCMCategory.toDisplayName(UncategorizedSCMHeadCategory.DEFAULT, ChangeRequestSCMHeadCategory.DEFAULT, - TagSCMHeadCategory.DEFAULT).toString( - Locale.ENGLISH), is("Branches / Change requests / Tags")); - assertThat(SCMCategory.toDisplayName(UncategorizedSCMHeadCategory.DEFAULT, - TagSCMHeadCategory.DEFAULT).toString( - Locale.ENGLISH), is("Branches / Tags")); - assertThat(SCMCategory.toDisplayName(UncategorizedSCMHeadCategory.DEFAULT).toString( - Locale.ENGLISH), is("Branches")); + void toDisplayName() { + assertThat( + SCMCategory.toDisplayName( + UncategorizedSCMHeadCategory.DEFAULT, + ChangeRequestSCMHeadCategory.DEFAULT, + TagSCMHeadCategory.DEFAULT) + .toString(Locale.ENGLISH), + is("Branches / Change requests / Tags")); + assertThat( + SCMCategory.toDisplayName(UncategorizedSCMHeadCategory.DEFAULT, TagSCMHeadCategory.DEFAULT) + .toString(Locale.ENGLISH), + is("Branches / Tags")); + assertThat( + SCMCategory.toDisplayName(UncategorizedSCMHeadCategory.DEFAULT).toString(Locale.ENGLISH), + is("Branches")); } @Test - public void toDisplayName1() throws Exception { - assertThat(SCMCategory.toDisplayName(Arrays.asList(ChangeRequestSCMHeadCategory.DEFAULT, - TagSCMHeadCategory.DEFAULT, UncategorizedSCMHeadCategory.DEFAULT)).toString( - Locale.ENGLISH), is("Branches / Change requests / Tags")); - + void toDisplayName1() { + assertThat( + SCMCategory.toDisplayName(Arrays.asList( + ChangeRequestSCMHeadCategory.DEFAULT, + TagSCMHeadCategory.DEFAULT, + UncategorizedSCMHeadCategory.DEFAULT)) + .toString(Locale.ENGLISH), + is("Branches / Change requests / Tags")); } @Test - public void toShortUrl() throws Exception { - assertThat(SCMCategory.toShortUrl(UncategorizedSCMHeadCategory.DEFAULT, ChangeRequestSCMHeadCategory.DEFAULT, - TagSCMHeadCategory.DEFAULT), is("change-requests_default_tags")); - + void toShortUrl() { + assertThat( + SCMCategory.toShortUrl( + UncategorizedSCMHeadCategory.DEFAULT, + ChangeRequestSCMHeadCategory.DEFAULT, + TagSCMHeadCategory.DEFAULT), + is("change-requests_default_tags")); } @Test - public void toShortUrl1() throws Exception { - assertThat(SCMCategory.toShortUrl(Arrays.asList(ChangeRequestSCMHeadCategory.DEFAULT, - TagSCMHeadCategory.DEFAULT, UncategorizedSCMHeadCategory.DEFAULT)), is("change-requests_default_tags")); - assertThat(SCMCategory.toShortUrl(Arrays.asList(TagSCMHeadCategory.DEFAULT, UncategorizedSCMHeadCategory.DEFAULT)), is("default_tags")); + void toShortUrl1() { + assertThat( + SCMCategory.toShortUrl(Arrays.asList( + ChangeRequestSCMHeadCategory.DEFAULT, + TagSCMHeadCategory.DEFAULT, + UncategorizedSCMHeadCategory.DEFAULT)), + is("change-requests_default_tags")); + assertThat( + SCMCategory.toShortUrl(Arrays.asList(TagSCMHeadCategory.DEFAULT, UncategorizedSCMHeadCategory.DEFAULT)), + is("default_tags")); assertThat(SCMCategory.toShortUrl(Collections.singletonList(TagSCMHeadCategory.DEFAULT)), is("tags")); - } @Test - public void group() throws Exception { + void group() { UncategorizedSCMHeadCategory u1 = UncategorizedSCMHeadCategory.DEFAULT; UncategorizedSCMHeadCategory u2 = new UncategorizedSCMHeadCategory(new NonLocalizable("foo")); ChangeRequestSCMHeadCategory c1 = ChangeRequestSCMHeadCategory.DEFAULT; @@ -91,12 +109,12 @@ public void group() throws Exception { ChangeRequestSCMHeadCategory c3 = new ChangeRequestSCMHeadCategory(new NonLocalizable("manchu")); Map> result = SCMCategory.group(u1, c1, c2, c3, u2); assertThat(result.entrySet(), hasSize(2)); - assertThat(result.get("default"), Matchers.containsInAnyOrder(u1, u2)); - assertThat(result.get("change-requests"), Matchers.containsInAnyOrder(c1, c2, c3)); + assertThat(result.get("default"), Matchers.containsInAnyOrder(u1, u2)); + assertThat(result.get("change-requests"), Matchers.containsInAnyOrder(c1, c2, c3)); } @Test - public void group1() throws Exception { + void group1() { UncategorizedSCMHeadCategory u1 = UncategorizedSCMHeadCategory.DEFAULT; UncategorizedSCMHeadCategory u2 = new UncategorizedSCMHeadCategory(new NonLocalizable("foo")); ChangeRequestSCMHeadCategory c1 = ChangeRequestSCMHeadCategory.DEFAULT; @@ -105,30 +123,39 @@ public void group1() throws Exception { TagSCMHeadCategory t1 = new TagSCMHeadCategory(new NonLocalizable("foomanchu")); Map> result = SCMCategory.group(Arrays.asList(u1, c1, t1, c2, c3, u2)); assertThat(result.entrySet(), hasSize(3)); - assertThat(result.get("default"), Matchers.containsInAnyOrder(u1, u2)); - assertThat(result.get("change-requests"), Matchers.containsInAnyOrder(c1, c2, c3)); - assertThat(result.get("tags"), Matchers.contains(t1)); - + assertThat(result.get("default"), Matchers.containsInAnyOrder(u1, u2)); + assertThat(result.get("change-requests"), Matchers.containsInAnyOrder(c1, c2, c3)); + assertThat(result.get("tags"), Matchers.contains(t1)); } @Test - public void getName() throws Exception { - assertThat(new MySCMCategory("foomanchu", new NonLocalizable("Fu Manchu"), new NonLocalizable("Fu Manchu")).getName(), is("foomanchu")); + void getName() { + assertThat( + new MySCMCategory("foomanchu", new NonLocalizable("Fu Manchu"), new NonLocalizable("Fu Manchu")) + .getName(), + is("foomanchu")); } @Test - public void getDisplayName() throws Exception { - assertThat(new MySCMCategory("foomanchu", new NonLocalizable("Fu Manchu"), new NonLocalizable("Fu Manchu")).getDisplayName().toString(Locale.ENGLISH), is("Fu Manchu")); + void getDisplayName() { + assertThat( + new MySCMCategory("foomanchu", new NonLocalizable("Fu Manchu"), new NonLocalizable("Fu Manchu")) + .getDisplayName() + .toString(Locale.ENGLISH), + is("Fu Manchu")); } @Test - public void defaultDisplayName() throws Exception { - assertThat(new MySCMCategory("foomanchu", null, new NonLocalizable("Fu Manchu")) - .getDisplayName().toString(Locale.ENGLISH), is("Fu Manchu")); + void defaultDisplayName() { + assertThat( + new MySCMCategory("foomanchu", null, new NonLocalizable("Fu Manchu")) + .getDisplayName() + .toString(Locale.ENGLISH), + is("Fu Manchu")); } @Test - public void isMatch() throws Exception { + void isMatch() { UncategorizedSCMHeadCategory u = UncategorizedSCMHeadCategory.DEFAULT; ChangeRequestSCMHeadCategory c = ChangeRequestSCMHeadCategory.DEFAULT; TagSCMHeadCategory t = TagSCMHeadCategory.DEFAULT; @@ -173,12 +200,11 @@ public SCMHead getTarget() { assertThat(t.isMatch(mh, Arrays.asList(u, c, t)), is(false)); assertThat(t.isMatch(ch, Arrays.asList(c, u, t)), is(false)); assertThat(t.isMatch(th, Arrays.asList(c, u, t)), is(true)); - } private static class MySCMCategory extends SCMCategory { - private NonLocalizable defaultDisplayName; + private final NonLocalizable defaultDisplayName; public MySCMCategory(String name, NonLocalizable displayName, NonLocalizable defaultDisplayName) { super(name, displayName); diff --git a/src/test/java/jenkins/scm/api/SCMEventTest.java b/src/test/java/jenkins/scm/api/SCMEventTest.java index ae26a72d..dd83a49d 100644 --- a/src/test/java/jenkins/scm/api/SCMEventTest.java +++ b/src/test/java/jenkins/scm/api/SCMEventTest.java @@ -25,17 +25,6 @@ package jenkins.scm.api; -import edu.umd.cs.findbugs.annotations.NonNull; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import jakarta.servlet.http.HttpServletRequest; -import org.junit.Test; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; -import org.kohsuke.stapler.StaplerRequest2; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsString; @@ -48,22 +37,25 @@ import static org.hamcrest.Matchers.sameInstance; import static org.mockito.Mockito.*; -@RunWith(Theories.class) -public class SCMEventTest { +import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import javax.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.kohsuke.stapler.StaplerRequest2; - @DataPoints - public static SCMEvent.Type[] types() { - return SCMEvent.Type.values(); - } +class SCMEventTest { @Test - public void executorService() throws Exception { + void executorService() { assertThat(SCMEvent.executorService(), notNullValue()); assertThat(SCMEvent.executorService().isShutdown(), is(false)); } @Test - public void eventProcessingMetricsReturnsZeroWhenNoEvents() throws Exception { + void eventProcessingMetricsReturnsZeroWhenNoEvents() { SCMEvent.EventQueueMetrics eventProcessingMetrics = SCMEvent.getEventProcessingMetrics(); assertThat(eventProcessingMetrics, notNullValue()); assertThat(eventProcessingMetrics.getPoolSize(), is(0)); @@ -73,7 +65,7 @@ public void eventProcessingMetricsReturnsZeroWhenNoEvents() throws Exception { } @Test - public void eventProcessingMetrics() throws Exception { + void eventProcessingMetrics() { ThreadPoolExecutor executor = mock(ThreadPoolExecutor.class); when(executor.getPoolSize()).thenReturn(20); @@ -87,7 +79,6 @@ public void eventProcessingMetrics() throws Exception { when(executor.getQueue()).thenReturn(queue); when(executor.getCompletedTaskCount()).thenReturn(1000L); - SCMEvent.EventQueueMetrics eventProcessingMetrics = new SCMEvent.EventQueueMetrics(executor); assertThat(eventProcessingMetrics, notNullValue()); assertThat(eventProcessingMetrics.getPoolSize(), is(20)); @@ -96,13 +87,14 @@ public void eventProcessingMetrics() throws Exception { assertThat(eventProcessingMetrics.getCompletedTasks(), is(1000L)); } - @Theory - public void getType(SCMEvent.Type type) throws Exception { + @ParameterizedTest + @EnumSource(SCMEvent.Type.class) + void getType(SCMEvent.Type type) { assertThat(new MySCMEvent(type, new Object()).getType(), is(type)); } @Test - public void getTimestamp() throws Exception { + void getTimestamp() { long before = System.currentTimeMillis(); long after; MySCMEvent instance; @@ -115,14 +107,14 @@ public void getTimestamp() throws Exception { } @Test - public void getTimestamp2() throws Exception { + void getTimestamp2() { MySCMEvent instance; instance = new MySCMEvent(SCMEvent.Type.CREATED, 53L, new Object()); assertThat(instance.getTimestamp(), is(53L)); } @Test - public void getDate() throws Exception { + void getDate() { long before = System.currentTimeMillis(); long after; MySCMEvent instance; @@ -135,12 +127,12 @@ public void getDate() throws Exception { } @Test - public void getPayload() throws Exception { + void getPayload() { assertThat(new MySCMEvent(SCMEvent.Type.CREATED, this).getPayload(), sameInstance(this)); } @Test - public void equalityContract() throws Exception { + void equalityContract() { MySCMEvent a1 = new MySCMEvent(SCMEvent.Type.CREATED, 53L, "foo"); MySCMEvent a2 = new MySCMEvent(SCMEvent.Type.CREATED, 53L, new String("foo")); MySCMEvent a3 = new MySCMEvent(SCMEvent.Type.CREATED, Long.valueOf(53L), new String("foo")); @@ -160,22 +152,22 @@ public void equalityContract() throws Exception { } @Test - public void usefulToString() throws Exception { - assertThat(new MySCMEvent(SCMEvent.Type.REMOVED, 1479764915000L, "{\"name\":\"value\"}").toString(), - allOf(containsString(String.format("%tc", 1479764915000L)), + void usefulToString() { + assertThat( + new MySCMEvent(SCMEvent.Type.REMOVED, 1479764915000L, "{\"name\":\"value\"}").toString(), + allOf( + containsString(String.format("%tc", 1479764915000L)), containsString("REMOVED"), - containsString("{\"name\":\"value\"}") - ) - ); + containsString("{\"name\":\"value\"}"))); } @Test - public void originOfNull() throws Exception { + void originOfNull() { assertThat(SCMEvent.originOf((StaplerRequest2) null), is(nullValue())); } @Test - public void originOfSimpleRequest() throws Exception { + void originOfSimpleRequest() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("http"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -183,11 +175,12 @@ public void originOfSimpleRequest() throws Exception { when(req.getLocalPort()).thenReturn(80); when(req.getRemoteHost()).thenReturn("scm.example.com"); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ http://jenkins.example.com/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ http://jenkins.example.com/jenkins/notify")); } @Test - public void originOfSimpleTLSRequest() throws Exception { + void originOfSimpleTLSRequest() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("https"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -195,11 +188,12 @@ public void originOfSimpleTLSRequest() throws Exception { when(req.getLocalPort()).thenReturn(443); when(req.getRemoteHost()).thenReturn("scm.example.com"); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); } @Test - public void originOfSimpleRequestNonStdPort() throws Exception { + void originOfSimpleRequestNonStdPort() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("http"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -207,11 +201,13 @@ public void originOfSimpleRequestNonStdPort() throws Exception { when(req.getLocalPort()).thenReturn(8080); when(req.getRemoteHost()).thenReturn("scm.example.com"); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ http://jenkins.example.com:8080/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), + is("scm.example.com/203.0.113.1 ⇒ http://jenkins.example.com:8080/jenkins/notify")); } @Test - public void originOfSimpleTLSRequestNonStdPort() throws Exception { + void originOfSimpleTLSRequestNonStdPort() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("https"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -219,11 +215,13 @@ public void originOfSimpleTLSRequestNonStdPort() throws Exception { when(req.getLocalPort()).thenReturn(8443); when(req.getRemoteHost()).thenReturn("scm.example.com"); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com/203.0.113.1 ⇒ https://jenkins.example.com:8443/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), + is("scm.example.com/203.0.113.1 ⇒ https://jenkins.example.com:8443/jenkins/notify")); } @Test - public void originOfForwardedRequestSingleHop() throws Exception { + void originOfForwardedRequestSingleHop() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("http"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -234,11 +232,13 @@ public void originOfForwardedRequestSingleHop() throws Exception { when(req.getLocalPort()).thenReturn(8080); when(req.getRemoteHost()).thenReturn("proxy.example.com"); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com → proxy.example.com/203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), + is("scm.example.com → proxy.example.com/203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); } @Test - public void originOfForwardedRequestMultiHop() throws Exception { + void originOfForwardedRequestMultiHop() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getScheme()).thenReturn("http"); when(req.getServerName()).thenReturn("jenkins.example.com"); @@ -249,7 +249,10 @@ public void originOfForwardedRequestMultiHop() throws Exception { when(req.getRemotePort()).thenReturn(8080); when(req.getRemoteHost()).thenReturn(null); when(req.getRemoteAddr()).thenReturn("203.0.113.1"); - assertThat(SCMEvent.originOf(req), is("scm.example.com → gateway.example.com → proxy.example.com → 203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); + assertThat( + SCMEvent.originOf(req), + is( + "scm.example.com → gateway.example.com → proxy.example.com → 203.0.113.1 ⇒ https://jenkins.example.com/jenkins/notify")); } public static class MySCMEvent extends SCMEvent { @@ -278,5 +281,4 @@ private Object readResolve() { return this; } } - } diff --git a/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java b/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java index 5ada8c6d..cdac7222 100644 --- a/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java +++ b/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java @@ -25,9 +25,6 @@ package jenkins.scm.api; -import org.hamcrest.Matchers; -import org.junit.Test; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -39,9 +36,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class SCMHeadObserverTest { +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +class SCMHeadObserverTest { + @Test - public void allOf() throws Exception { + void allOf() throws Exception { SCMHead head1 = new SCMHead("bar"); SCMRevision revision1 = mock(SCMRevision.class); SCMHead head2 = new SCMHead("foo"); @@ -57,7 +58,7 @@ public void allOf() throws Exception { } @Test - public void first() throws Exception { + void first() throws Exception { SCMHeadObserver.OneFinished instance = SCMHeadObserver.first(SCMHeadObserver.collect(), SCMHeadObserver.any()); SCMHead head1 = new SCMHead("bar"); SCMRevision revision1 = mock(SCMRevision.class); @@ -68,7 +69,7 @@ public void first() throws Exception { } @Test - public void collect() throws Exception { + void collect() { SCMHead head1 = new SCMHead("bar"); SCMRevision revision1 = mock(SCMRevision.class); SCMHead head2 = new SCMHead("foo"); @@ -81,11 +82,10 @@ public void collect() throws Exception { instance.observe(head2, revision2); assertThat("Still observing", instance.isObserving(), is(true)); assertThat(instance.result(), Matchers.allOf(hasEntry(head1, revision1), hasEntry(head2, revision2))); - } @Test - public void select() throws Exception { + void select() { SCMHead head1 = new SCMHead("bar"); SCMRevision revision1 = mock(SCMRevision.class); SCMHead head2 = new SCMHead("foo"); @@ -101,26 +101,25 @@ public void select() throws Exception { } @Test - public void filter() throws Exception { + void filter() throws Exception { SCMHead head1 = new SCMHead("bar"); SCMRevision revision1 = mock(SCMRevision.class); SCMHead head2 = new SCMHead("foo"); SCMRevision revision2 = mock(SCMRevision.class); - SCMHeadObserver.Filter - instance = SCMHeadObserver.filter(SCMHeadObserver.collect(), head2); + SCMHeadObserver.Filter instance = + SCMHeadObserver.filter(SCMHeadObserver.collect(), head2); assertThat("Observing from the start", instance.isObserving(), is(true)); assertThat("Wants only selected head", instance.getIncludes(), contains(head2)); instance.observe(head1, revision1); assertThat("Still observing before match", instance.isObserving(), is(true)); instance.observe(head2, revision2); assertThat("Stops observing after selected observation", instance.isObserving(), is(false)); - assertThat(instance.unwrap().result(), hasEntry(head2,revision2)); + assertThat(instance.unwrap().result(), hasEntry(head2, revision2)); assertThat(instance.unwrap().result(), not(hasKey(head1))); - } @Test - public void named() throws Exception { + void named() { SCMHeadObserver.Named instance = SCMHeadObserver.named("foo"); assertThat("Observing from the start", instance.isObserving(), is(true)); assertThat("Wants everything", instance.getIncludes(), nullValue()); @@ -138,7 +137,7 @@ public void named() throws Exception { } @Test - public void any() throws Exception { + void any() { SCMHeadObserver.Any instance = SCMHeadObserver.any(); assertThat("Observing from the start", instance.isObserving(), is(true)); assertThat("Wants everything", instance.getIncludes(), nullValue()); @@ -148,5 +147,4 @@ public void any() throws Exception { assertThat("Stops observing after first observation", instance.isObserving(), is(false)); assertThat(instance.result(), is(revision)); } - } diff --git a/src/test/java/jenkins/scm/api/SCMHeadTest.java b/src/test/java/jenkins/scm/api/SCMHeadTest.java index b34bbba5..c065e86c 100644 --- a/src/test/java/jenkins/scm/api/SCMHeadTest.java +++ b/src/test/java/jenkins/scm/api/SCMHeadTest.java @@ -25,19 +25,19 @@ package jenkins.scm.api; -import edu.umd.cs.findbugs.annotations.NonNull; -import jenkins.scm.api.mixin.SCMHeadMixin; -import jenkins.scm.impl.mock.MockChangeRequestSCMHead; -import org.junit.Test; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; -public class SCMHeadTest { +import edu.umd.cs.findbugs.annotations.NonNull; +import jenkins.scm.api.mixin.SCMHeadMixin; +import jenkins.scm.impl.mock.MockChangeRequestSCMHead; +import org.junit.jupiter.api.Test; + +class SCMHeadTest { @Test - public void equality() { + void equality() { SCMHead h1 = new SCMHead("h1"); SCMHead h2 = new SCMHead("h1"); SCMHead x = new SCMHead("h2"); @@ -48,7 +48,7 @@ public void equality() { } @Test - public void mixinEquality() { + void mixinEquality() { SCMHead h1 = new MockChangeRequestSCMHead(1, "h1"); SCMHead h2 = new MockChangeRequestSCMHead(1, "h1"); SCMHead x = new MockChangeRequestSCMHead(1, "h2"); @@ -63,8 +63,8 @@ public void mixinEquality() { } @Test - public void crazyMixinEquality() { - SCMHead h1 = new CrazyHead("crazy", true, (byte)0, 'a', 0.1, 0.2f, 3, 4L, (short)5, "String"); + void crazyMixinEquality() { + SCMHead h1 = new CrazyHead("crazy", true, (byte) 0, 'a', 0.1, 0.2f, 3, 4L, (short) 5, "String"); SCMHead h2 = new CrazyHead("crazy", true, (byte) 0, 'a', 0.1, 0.2f, 3, 4L, (short) 5, "String"); SCMHead x0 = new CrazyHead("crazy", false, (byte) 0, 'a', 0.1, 0.2f, 3, 4L, (short) 5, "String"); SCMHead x1 = new CrazyHead("crazy", true, (byte) 1, 'a', 0.1, 0.2f, 3, 4L, (short) 5, "String"); @@ -122,8 +122,17 @@ public static class CrazyHead extends SCMHead implements CrazyMixin { private final short aShort; private final Object anObject; - public CrazyHead(@NonNull String name, boolean aBoolean, byte aByte, char aChar, double aDouble, float aFloat, - int anInt, long aLong, short aShort, Object anObject) { + public CrazyHead( + @NonNull String name, + boolean aBoolean, + byte aByte, + char aChar, + double aDouble, + float aFloat, + int anInt, + long aLong, + short aShort, + Object anObject) { super(name); this.aBoolean = aBoolean; this.aByte = aByte; @@ -181,5 +190,4 @@ public Object getAnObject() { return anObject; } } - } diff --git a/src/test/java/jenkins/scm/api/SCMNameTest.java b/src/test/java/jenkins/scm/api/SCMNameTest.java index 8b8f5267..8ce51772 100644 --- a/src/test/java/jenkins/scm/api/SCMNameTest.java +++ b/src/test/java/jenkins/scm/api/SCMNameTest.java @@ -24,53 +24,55 @@ package jenkins.scm.api; -import org.junit.Test; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -public class SCMNameTest { +import org.junit.jupiter.api.Test; + +class SCMNameTest { + @Test - public void given__url_without_hostname__when__naming__then__no_name_inferred() throws Exception { + void given__url_without_hostname__when__naming__then__no_name_inferred() { assertThat(SCMName.fromUrl("file:///some/random/file"), is(nullValue())); } @Test - public void given__url_with_hostname__when__naming__then__public_tld_removed() throws Exception { + void given__url_with_hostname__when__naming__then__public_tld_removed() { assertThat(SCMName.fromUrl("http://scm.example.com"), is("scm example")); } @Test - public void given__url_with_hostname__when__naming__then__public_sld_removed() throws Exception { + void given__url_with_hostname__when__naming__then__public_sld_removed() { assertThat(SCMName.fromUrl("http://scm.example.co.uk"), is("scm example")); } @Test - public void given__url_with_hostname__when__naming__then__prefix_is_removed() throws Exception { + void given__url_with_hostname__when__naming__then__prefix_is_removed() { assertThat(SCMName.fromUrl("http://scm.example.ie", "scm"), is("example")); } @Test - public void given__url_with_punycode__when__naming__then__hostname_is_decoded() throws Exception { - assertThat(SCMName.fromUrl("http://xn--e1afmkfd.xn--p1ai/"), + void given__url_with_punycode__when__naming__then__hostname_is_decoded() { + assertThat( + SCMName.fromUrl("http://xn--e1afmkfd.xn--p1ai/"), is("\u043F\u0440\u0438\u043C\u0435\u0440" /*пример*/)); } @Test - public void given__url_with_idn__when__naming__then__punycode_is_roundtripped() throws Exception { - assertThat(SCMName.fromUrl("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444" /*пример.рф*/), + void given__url_with_idn__when__naming__then__punycode_is_roundtripped() { + assertThat( + SCMName.fromUrl("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444" /*пример.рф*/), is("\u043F\u0440\u0438\u043C\u0435\u0440" /*пример*/)); } @Test - public void given__url_with_idn__when__naming__then__punycode_is_roundtripped2() throws Exception { + void given__url_with_idn__when__naming__then__punycode_is_roundtripped2() { assertThat(SCMName.fromUrl("http://\u4F8B\u5B50.\u4E2D\u56FD/"), is("\u4F8B\u5B50")); } @Test - public void given__url_with_ipv4address__when__naming__then__no_name_inferred() throws Exception { + void given__url_with_ipv4address__when__naming__then__no_name_inferred() { assertThat(SCMName.fromUrl("http://127.0.0.1/scm"), is(nullValue())); } - } diff --git a/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java b/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java index 21ce72c9..088d5b54 100644 --- a/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java +++ b/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java @@ -1,5 +1,8 @@ package jenkins.scm.api; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + import hudson.model.Action; import hudson.model.Actionable; import java.util.List; @@ -9,14 +12,12 @@ import jenkins.scm.impl.mock.MockSCMHead; import jenkins.scm.impl.mock.MockSCMRevision; import jenkins.scm.impl.mock.MockSCMSource; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +class SCMRevisionActionTest { -public class SCMRevisionActionTest { @Test - public void given__legacyData__when__gettingRevision__then__legacyReturned() throws Exception { + void given__legacyData__when__gettingRevision__then__legacyReturned() { try (MockSCMController c = MockSCMController.create()) { MockSCMSource source = new MockSCMSource(c, "test", new MockSCMDiscoverBranches()); SCMRevision revision = new MockSCMRevision(new MockSCMHead("head"), "hash"); @@ -27,13 +28,13 @@ public void given__legacyData__when__gettingRevision__then__legacyReturned() thr } @Test - public void given__mixedData__when__gettingRevision__then__legacyReturnedForUnmatched() throws Exception { + void given__mixedData__when__gettingRevision__then__legacyReturnedForUnmatched() { try (MockSCMController c = MockSCMController.create()) { MockSCMSource source1 = new MockSCMSource(c, "test", new MockSCMDiscoverBranches()); source1.setId("foo"); MockSCMSource source2 = new MockSCMSource(c, "test", new MockSCMDiscoverBranches()); source2.setId("bar"); - MockSCMSource source3 = new MockSCMSource( c, "test", new MockSCMDiscoverBranches()); + MockSCMSource source3 = new MockSCMSource(c, "test", new MockSCMDiscoverBranches()); source3.setId("manchu"); SCMRevision revision1 = new MockSCMRevision(new MockSCMHead("head1"), "hash1"); SCMRevision revision2 = new MockSCMRevision(new MockSCMHead("head2"), "hash2"); @@ -49,7 +50,7 @@ public void given__mixedData__when__gettingRevision__then__legacyReturnedForUnma } @Test - public void given__mixedData__when__gettingRevision__then__firstlegacyReturnedForUnmatched() throws Exception { + void given__mixedData__when__gettingRevision__then__firstlegacyReturnedForUnmatched() { try (MockSCMController c = MockSCMController.create()) { MockSCMSource source1 = new MockSCMSource(c, "test", new MockSCMDiscoverBranches()); source1.setId("foo"); @@ -84,7 +85,10 @@ public String getSearchUrl() { @SuppressWarnings("deprecation") // avoid TransientActionFactory @Override public List getActions(Class type) { - return getActions().stream().filter(type::isInstance).map(type::cast).collect(Collectors.toList()); + return getActions().stream() + .filter(type::isInstance) + .map(type::cast) + .collect(Collectors.toList()); } } } diff --git a/src/test/java/jenkins/scm/api/SCMUriTest.java b/src/test/java/jenkins/scm/api/SCMUriTest.java index 5273ad00..a271f04e 100644 --- a/src/test/java/jenkins/scm/api/SCMUriTest.java +++ b/src/test/java/jenkins/scm/api/SCMUriTest.java @@ -24,82 +24,90 @@ package jenkins.scm.api; -import java.util.Collections; -import org.junit.Test; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -public class SCMUriTest { +import java.util.Collections; +import org.junit.jupiter.api.Test; + +class SCMUriTest { + @Test - public void given__url_with_custom_scheme__when__normalizing__then__custom_default_port_elided() throws Exception { - assertThat(SCMUri.normalize("myscm://myscm.EXAMPLE.COM:6352/", Collections.singletonMap("myscm", 6352)), is("myscm://myscm.example.com")); + void given__url_with_custom_scheme__when__normalizing__then__custom_default_port_elided() { + assertThat( + SCMUri.normalize("myscm://myscm.EXAMPLE.COM:6352/", Collections.singletonMap("myscm", 6352)), + is("myscm://myscm.example.com")); assertThat(SCMUri.normalize("myscm://myscm.EXAMPLE.COM:6352/"), is("myscm://myscm.example.com:6352")); } @Test - public void given__url_without_hostname__when__normalizing__then__no_name_inferred() throws Exception { + void given__url_without_hostname__when__normalizing__then__no_name_inferred() { assertThat(SCMUri.normalize("FILE:///some/random/file"), is("file:///some/random/file")); } @Test - public void given__normalized_url__when__normalizing__then__verbatim() throws Exception { + void given__normalized_url__when__normalizing__then__verbatim() { assertThat(SCMUri.normalize("https://example.com/foo"), is("https://example.com/foo")); assertThat(SCMUri.normalize("https://example.com:8443/foo"), is("https://example.com:8443/foo")); assertThat(SCMUri.normalize("https://bob@example.com/foo"), is("https://bob@example.com/foo")); assertThat(SCMUri.normalize("https://bob:pass@example.com/foo"), is("https://bob:pass@example.com/foo")); assertThat(SCMUri.normalize("https://bob%25smith@example.com/foo"), is("https://bob%25smith@example.com/foo")); - assertThat(SCMUri.normalize("https://bob%25smith:pass@example.com/foo"), + assertThat( + SCMUri.normalize("https://bob%25smith:pass@example.com/foo"), is("https://bob%25smith:pass@example.com/foo")); } @Test - public void given__url_with_punycode__when__normalizing__then__punycode_retained() throws Exception { - assertThat(SCMUri.normalize("http://xn--e1afmkfd.xn--p1ai/"), + void given__url_with_punycode__when__normalizing__then__punycode_retained() { + assertThat( + SCMUri.normalize("http://xn--e1afmkfd.xn--p1ai/"), is("http://xn--e1afmkfd.xn--p1ai" /*http://пример.рф*/)); } @Test - public void given__url_with_idn__when__normalizing__then__hostname_is_decoded() throws Exception { - assertThat(SCMUri.normalize("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444/"), /*пример.рф*/ + void given__url_with_idn__when__normalizing__then__hostname_is_decoded() { + assertThat( + SCMUri.normalize("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444/"), /*пример.рф*/ is("http://xn--e1afmkfd.xn--p1ai")); } @Test - public void given__url_with_idn_and_port__when__normalizing__then__hostname_is_decoded() throws Exception { - assertThat(SCMUri.normalize("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:8080/"), /*пример.рф*/ + void given__url_with_idn_and_port__when__normalizing__then__hostname_is_decoded() { + assertThat( + SCMUri.normalize("http://\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:8080/"), /*пример.рф*/ is("http://xn--e1afmkfd.xn--p1ai:8080")); } @Test - public void given__url_with_user_and_idn__when__normalizing__then__hostname_is_decoded() throws Exception { - assertThat(SCMUri.normalize("http://bob@\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:8080/"), /*пример.рф*/ + void given__url_with_user_and_idn__when__normalizing__then__hostname_is_decoded() { + assertThat( + SCMUri.normalize("http://bob@\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:8080/"), /*пример.рф*/ is("http://bob@xn--e1afmkfd.xn--p1ai:8080")); } @Test - public void given__url_with_userpass_and_idn__when__normalizing__then__hostname_is_decoded() throws Exception { - assertThat(SCMUri.normalize("http://bob:secret@пример.рф/"), - is("http://bob:secret@xn--e1afmkfd.xn--p1ai")); + void given__url_with_userpass_and_idn__when__normalizing__then__hostname_is_decoded() { + assertThat(SCMUri.normalize("http://bob:secret@пример.рф/"), is("http://bob:secret@xn--e1afmkfd.xn--p1ai")); } @Test - public void given__url_with_userpass_and_idn_and_port__when__normalizing__then__hostname_is_decoded() throws Exception { - assertThat(SCMUri.normalize("http://bob:secret@пример.рф:8080/"), + void given__url_with_userpass_and_idn_and_port__when__normalizing__then__hostname_is_decoded() { + assertThat( + SCMUri.normalize("http://bob:secret@пример.рф:8080/"), is("http://bob:secret@xn--e1afmkfd.xn--p1ai:8080")); } @Test - public void given__url_with_everything_messed_up__when__normalizing__then__everything_is_fixed() throws Exception { + void given__url_with_everything_messed_up__when__normalizing__then__everything_is_fixed() { // the scheme is upper case, we expect that to be fixed // the user part should be ... // the host is IDN so that should be fixed // the port here is ๔๔๓ which is the Thai digits for 443, and as the scheme is https // the path should be normalized // the trailing slash should be removed - assertThat(SCMUri.normalize( - "HTTPS://böb:seçret@\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:\u0E54\u0E54\u0E53/foo/../"), + assertThat( + SCMUri.normalize( + "HTTPS://böb:seçret@\u043F\u0440\u0438\u043C\u0435\u0440.\u0440\u0444:\u0E54\u0E54\u0E53/foo/../"), is("https://b%C3%B6b:se%C3%A7ret@xn--e1afmkfd.xn--p1ai")); } - } diff --git a/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java b/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java index 4c9aef35..80cf0472 100644 --- a/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java +++ b/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java @@ -25,33 +25,36 @@ package jenkins.scm.impl; -import jenkins.scm.impl.mock.MockChangeRequestSCMHead; -import jenkins.scm.impl.mock.MockSCMHead; -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -@RunWith(Theories.class) -public class ChangeRequestSCMHeadCategoryTest { - - @DataPoint - public static ChangeRequestSCMHeadCategory defInstance = ChangeRequestSCMHeadCategory.DEFAULT; - - @DataPoint - public static ChangeRequestSCMHeadCategory custInstance = new ChangeRequestSCMHeadCategory(Messages._ChangeRequestSCMHeadCategory_DisplayName()); +import java.util.stream.Stream; +import jenkins.scm.impl.mock.MockChangeRequestSCMHead; +import jenkins.scm.impl.mock.MockSCMHead; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class ChangeRequestSCMHeadCategoryTest { + + static Stream instance() { + return Stream.of( + Arguments.of(Named.of("default", ChangeRequestSCMHeadCategory.DEFAULT)), + Arguments.of(Named.of( + "custom", + new ChangeRequestSCMHeadCategory(Messages._ChangeRequestSCMHeadCategory_DisplayName())))); + } - @Theory - public void given_changeRequestHead_when_isMatch_then_confirmMatch(ChangeRequestSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_changeRequestHead_when_isMatch_then_confirmMatch(ChangeRequestSCMHeadCategory instance) { assertThat(instance.isMatch(new MockChangeRequestSCMHead(1, "master")), is(true)); } - @Theory - public void given_regularHead_when_isMatch_then_rejectMatch(ChangeRequestSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_regularHead_when_isMatch_then_rejectMatch(ChangeRequestSCMHeadCategory instance) { assertThat(instance.isMatch(new MockSCMHead("master")), is(false)); } - } diff --git a/src/test/java/jenkins/scm/impl/NoOpProjectObserverTest.java b/src/test/java/jenkins/scm/impl/NoOpProjectObserverTest.java index 9d0a455d..df721070 100644 --- a/src/test/java/jenkins/scm/impl/NoOpProjectObserverTest.java +++ b/src/test/java/jenkins/scm/impl/NoOpProjectObserverTest.java @@ -25,23 +25,24 @@ package jenkins.scm.impl; -import jenkins.scm.api.SCMSource; -import org.junit.Test; -import org.mockito.InOrder; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; -public class NoOpProjectObserverTest { +import jenkins.scm.api.SCMSource; +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; + +class NoOpProjectObserverTest { + @Test - public void instance() throws Exception { + void instance() { assertThat(NoOpProjectObserver.instance(), notNullValue()); } @Test - public void addSource() throws Exception { + void addSource() { SCMSource source = mock(SCMSource.class); InOrder seq = inOrder(source); NoOpProjectObserver.instance().addSource(source); @@ -49,13 +50,12 @@ public void addSource() throws Exception { } @Test - public void addAttribute() throws Exception { + void addAttribute() { NoOpProjectObserver.instance().addAttribute("key", "value"); } @Test - public void complete() throws Exception { + void complete() throws Exception { NoOpProjectObserver.instance().complete(); } - } diff --git a/src/test/java/jenkins/scm/impl/NullSCMSourceTest.java b/src/test/java/jenkins/scm/impl/NullSCMSourceTest.java index e89c776c..f7c55a2d 100644 --- a/src/test/java/jenkins/scm/impl/NullSCMSourceTest.java +++ b/src/test/java/jenkins/scm/impl/NullSCMSourceTest.java @@ -25,14 +25,6 @@ package jenkins.scm.impl; -import hudson.scm.NullSCM; -import jenkins.scm.api.SCMHead; -import jenkins.scm.api.SCMHeadObserver; -import jenkins.scm.api.SCMRevision; -import org.junit.ClassRule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.hasProperty; @@ -43,37 +35,34 @@ import static org.mockito.Mockito.verify; import static org.mockito.hamcrest.MockitoHamcrest.argThat; -public class NullSCMSourceTest { +import hudson.scm.NullSCM; +import jenkins.scm.api.SCMHead; +import jenkins.scm.api.SCMHeadObserver; +import jenkins.scm.api.SCMRevision; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class NullSCMSourceTest { @Test - public void given_instance_when_fetch_then_noRevisionObserved() throws Exception { + void given_instance_when_fetch_then_noRevisionObserved(JenkinsRule r) throws Exception { SCMHeadObserver observer = mock(SCMHeadObserver.class); NullSCMSource instance = new NullSCMSource(); instance.fetch(null, observer, null); - verify(observer, never()).observe( - argThat( - allOf( - instanceOf(SCMHead.class), - hasProperty("name", is("the-name")) - ) - ), - argThat( - allOf( + verify(observer, never()) + .observe( + argThat(allOf(instanceOf(SCMHead.class), hasProperty("name", is("the-name")))), + argThat(allOf( instanceOf(SCMRevision.class), hasProperty("head", hasProperty("name", is("the-name"))), - hasProperty("deterministic", is(false)) - ) - ) - ); + hasProperty("deterministic", is(false))))); } @Test - public void given_instance_when_fetchingNonObservedHead_then_nullScmReturned() throws Exception { + void given_instance_when_fetchingNonObservedHead_then_nullScmReturned(JenkinsRule r) { NullSCMSource instance = new NullSCMSource(); assertThat(instance.build(new SCMHead("foo"), mock(SCMRevision.class)), instanceOf(NullSCM.class)); } - } diff --git a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java index 178cd5a6..c8a556ab 100644 --- a/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java +++ b/src/test/java/jenkins/scm/impl/SCMFileSystemTest.java @@ -24,64 +24,62 @@ package jenkins.scm.impl; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.model.Item; import hudson.scm.SCM; import hudson.scm.SCMDescriptor; +import java.io.IOException; import jenkins.scm.api.SCMFileSystem; import jenkins.scm.api.SCMRevision; import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceDescriptor; import jenkins.scm.impl.mock.MockSCM; import jenkins.scm.impl.mock.MockSCMSource; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import java.io.IOException; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class SCMFileSystemTest { - @Rule - public JenkinsRule r = new JenkinsRule(); +@WithJenkins +class SCMFileSystemTest { @Issue("JENKINS-52964") @Test - public void filesystem_supports_false_by_default_for_descriptor() { + void filesystem_supports_false_by_default_for_descriptor(JenkinsRule r) { SCMSourceDescriptor descriptor = r.jenkins.getDescriptorByType(MockSCMSource.DescriptorImpl.class); assertFalse(SCMFileSystem.supports(descriptor)); - SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); + SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); assertFalse(SCMFileSystem.supports(scmDescriptor)); } @Issue("JENKINS-52964") @Test - public void filesystem_supports_true_implementation_for_descriptor() { + void filesystem_supports_true_implementation_for_descriptor(JenkinsRule r) { SCMSourceDescriptor descriptor = r.jenkins.getDescriptorByType(MockSCMSource.DescriptorImpl.class); assertTrue(SCMFileSystem.supports(descriptor)); - SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); + SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); assertTrue(SCMFileSystem.supports(scmDescriptor)); } @Issue("JENKINS-52964") @Test - public void filesystem_supports_false_implementation_for_descriptor() { + void filesystem_supports_false_implementation_for_descriptor(JenkinsRule r) { SCMSourceDescriptor descriptor = r.jenkins.getDescriptorByType(MockSCMSource.DescriptorImpl.class); assertFalse(SCMFileSystem.supports(descriptor)); - SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); + SCMDescriptor scmDescriptor = r.jenkins.getDescriptorByType(MockSCM.DescriptorImpl.class); assertFalse(SCMFileSystem.supports(scmDescriptor)); } @@ -116,7 +114,6 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull throws IOException, InterruptedException { return null; } - } @TestExtension("filesystem_supports_false_implementation_for_descriptor") @@ -147,6 +144,5 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull throws IOException, InterruptedException { return null; } - } } diff --git a/src/test/java/jenkins/scm/impl/SingleSCMNavigatorTest.java b/src/test/java/jenkins/scm/impl/SingleSCMNavigatorTest.java index a924f467..91ae9884 100644 --- a/src/test/java/jenkins/scm/impl/SingleSCMNavigatorTest.java +++ b/src/test/java/jenkins/scm/impl/SingleSCMNavigatorTest.java @@ -25,14 +25,6 @@ package jenkins.scm.impl; -import java.util.Arrays; -import java.util.Collections; -import java.util.Random; -import jenkins.scm.api.SCMSource; -import jenkins.scm.api.SCMSourceObserver; -import org.junit.Test; -import org.mockito.InOrder; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.emptyCollectionOf; @@ -45,9 +37,18 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; -public class SingleSCMNavigatorTest { +import java.util.Arrays; +import java.util.Collections; +import java.util.Random; +import jenkins.scm.api.SCMSource; +import jenkins.scm.api.SCMSourceObserver; +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; + +class SingleSCMNavigatorTest { + @Test - public void getName() throws Exception { + void getName() { Random entropy = new Random(); String name = "foo-" + entropy.nextLong(); SingleSCMNavigator instance = new SingleSCMNavigator(name, Collections.emptyList()); @@ -55,28 +56,27 @@ public void getName() throws Exception { } @Test - public void getSources_empty() throws Exception { - assertThat(new SingleSCMNavigator("foo", Collections.emptyList()).getSources(), emptyCollectionOf(SCMSource.class)); + void getSources_empty() { + assertThat( + new SingleSCMNavigator("foo", Collections.emptyList()).getSources(), + emptyCollectionOf(SCMSource.class)); } @Test - public void getSources_one() throws Exception { + void getSources_one() { SCMSource s1 = mock(SCMSource.class); - assertThat(new SingleSCMNavigator("foo", Collections.singletonList(s1)).getSources(), - contains(s1)); + assertThat(new SingleSCMNavigator("foo", Collections.singletonList(s1)).getSources(), contains(s1)); } @Test - public void getSources_two() throws Exception { + void getSources_two() { SCMSource s1 = mock(SCMSource.class); SCMSource s2 = mock(SCMSource.class); - assertThat(new SingleSCMNavigator("foo", Arrays.asList(s1, s2)).getSources(), - contains(s1, s2)); - + assertThat(new SingleSCMNavigator("foo", Arrays.asList(s1, s2)).getSources(), contains(s1, s2)); } @Test - public void visitSources_empty() throws Exception { + void visitSources_empty() throws Exception { SCMSourceObserver mock = mock(SCMSourceObserver.class); SCMSourceObserver.ProjectObserver observer = mock(SCMSourceObserver.ProjectObserver.class); InOrder seq = inOrder(mock, observer); @@ -90,7 +90,7 @@ public void visitSources_empty() throws Exception { } @Test - public void visitSources_one() throws Exception { + void visitSources_one() throws Exception { SCMSource s1 = mock(SCMSource.class); SCMSourceObserver mock = mock(SCMSourceObserver.class); SCMSourceObserver.ProjectObserver observer = mock(SCMSourceObserver.ProjectObserver.class); @@ -105,7 +105,7 @@ public void visitSources_one() throws Exception { } @Test - public void visitSources_two() throws Exception { + void visitSources_two() throws Exception { SCMSource s1 = mock(SCMSource.class); SCMSource s2 = mock(SCMSource.class); SCMSourceObserver mock = mock(SCMSourceObserver.class); @@ -120,5 +120,4 @@ public void visitSources_two() throws Exception { seq.verify(observer, times(1)).complete(); seq.verifyNoMoreInteractions(); } - } diff --git a/src/test/java/jenkins/scm/impl/SingleSCMSourceTest.java b/src/test/java/jenkins/scm/impl/SingleSCMSourceTest.java index d3cc630f..f9f33e13 100644 --- a/src/test/java/jenkins/scm/impl/SingleSCMSourceTest.java +++ b/src/test/java/jenkins/scm/impl/SingleSCMSourceTest.java @@ -24,6 +24,20 @@ package jenkins.scm.impl; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.hamcrest.MockitoHamcrest.argThat; + import edu.umd.cs.findbugs.annotations.NonNull; import hudson.model.AbstractProject; import hudson.model.Descriptor; @@ -42,56 +56,26 @@ import jenkins.scm.impl.mock.MockSCM; import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMHead; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.DataBoundConstructor; import org.mockito.InOrder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.hamcrest.MockitoHamcrest.argThat; - -public class SingleSCMSourceTest { - - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class SingleSCMSourceTest { @Test - public void configRoundtrip() throws Exception { + void configRoundtrip(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); - SingleSCMSource source = new SingleSCMSource( - "the-name", - new MockSCM( - c, - "foo", - new MockSCMHead("master"), - null - ) - ); + SingleSCMSource source = + new SingleSCMSource("the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); source.setId("the-id"); SCMSourceBuilder builder = new SCMSourceBuilder(source); - SingleSCMSource expected = new SingleSCMSource( - "the-name", - new MockSCM( - c, - "foo", - new MockSCMHead("master"), - null - ) - ); + SingleSCMSource expected = + new SingleSCMSource("the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); expected.setId("the-id"); SCMSource actual = r.configRoundtrip(builder).scm; System.out.printf("expected=%s%nactual=%s%n", expected, actual); @@ -100,67 +84,52 @@ public void configRoundtrip() throws Exception { } @Test - public void given_instance_when_fetch_then_revisionObserved() throws Exception { + void given_instance_when_fetch_then_revisionObserved(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); SCMHeadObserver observer = mock(SCMHeadObserver.class); - SingleSCMSource instance = new SingleSCMSource("the-id", "the-name", - new MockSCM(c, "foo", new MockSCMHead("master"), null)); + SingleSCMSource instance = + new SingleSCMSource("the-id", "the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); instance.fetch(null, observer, null); - verify(observer).observe( - argThat( - allOf( - instanceOf(SCMHead.class), - hasProperty("name", is("the-name")) - ) - ), - argThat( - allOf( + verify(observer) + .observe( + argThat(allOf(instanceOf(SCMHead.class), hasProperty("name", is("the-name")))), + argThat(allOf( instanceOf(SCMRevision.class), hasProperty("head", hasProperty("name", is("the-name"))), - hasProperty("deterministic", is(false)) - ) - ) - ); + hasProperty("deterministic", is(false))))); } } @Test - public void given_instance_when_fetchWithCriteria_then_criteriaIgnoredAndRevisionObserved() throws Exception { + void given_instance_when_fetchWithCriteria_then_criteriaIgnoredAndRevisionObserved(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); SCMHeadObserver observer = mock(SCMHeadObserver.class); SCMSourceCriteria criteria = mock(SCMSourceCriteria.class); InOrder seq = inOrder(observer, criteria); - SingleSCMSource instance = new SingleSCMSource("the-id", "the-name", - new MockSCM(c, "foo", new MockSCMHead("master"), null)); + SingleSCMSource instance = + new SingleSCMSource("the-id", "the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); instance.fetch(criteria, observer, null); - seq.verify(observer).observe( - argThat( - allOf( - instanceOf(SCMHead.class), - hasProperty("name", is("the-name")) - ) - ), - argThat( - allOf( + seq.verify(observer) + .observe( + argThat(allOf(instanceOf(SCMHead.class), hasProperty("name", is("the-name")))), + argThat(allOf( instanceOf(SCMRevision.class), hasProperty("head", hasProperty("name", is("the-name"))), - hasProperty("deterministic", is(false)) - ) - ) - ); + hasProperty("deterministic", is(false))))); seq.verifyNoMoreInteractions(); } } @Test - public void given_instance_when_fetchingObservedHead_then_scmReturned() throws Exception { + void given_instance_when_fetchingObservedHead_then_scmReturned(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); - SingleSCMSource instance = new SingleSCMSource("the-id", "the-name", - new MockSCM(c, "foo", new MockSCMHead("master"), null)); - Map result = instance.fetch(SCMHeadObserver.collect(), null).result(); + SingleSCMSource instance = + new SingleSCMSource("the-id", "the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); + Map result = + instance.fetch(SCMHeadObserver.collect(), null).result(); assertThat(result.entrySet(), hasSize(1)); Map.Entry entry = result.entrySet().iterator().next(); assertThat(instance.build(entry.getKey(), entry.getValue()), instanceOf(MockSCM.class)); @@ -168,22 +137,23 @@ public void given_instance_when_fetchingObservedHead_then_scmReturned() throws E } @Test - public void given_instance_when_fetchingNonObservedHead_then_nullScmReturned() throws Exception { + void given_instance_when_fetchingNonObservedHead_then_nullScmReturned(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); - SingleSCMSource instance = new SingleSCMSource("the-id", "the-name", - new MockSCM(c, "foo", new MockSCMHead("master"), null)); + SingleSCMSource instance = + new SingleSCMSource("the-id", "the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); assertThat(instance.build(new SCMHead("foo"), mock(SCMRevision.class)), instanceOf(NullSCM.class)); } } @Test - public void scmRevisionImpl() throws Exception { + void scmRevisionImpl(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); - SingleSCMSource instance = new SingleSCMSource("the-id", "the-name", - new MockSCM(c, "foo", new MockSCMHead("master"), null)); - Map result = instance.fetch(SCMHeadObserver.collect(), null).result(); + SingleSCMSource instance = + new SingleSCMSource("the-id", "the-name", new MockSCM(c, "foo", new MockSCMHead("master"), null)); + Map result = + instance.fetch(SCMHeadObserver.collect(), null).result(); SCMRevision revision = result.values().iterator().next(); assertThat(revision.isDeterministic(), is(false)); assertThat(revision.equals(revision), is(true)); @@ -193,20 +163,20 @@ public void scmRevisionImpl() throws Exception { } @Test - public void getSCMDescriptors() throws Exception { + void getSCMDescriptors(JenkinsRule r) { TopLevelItemDescriptor descriptor = mock(TopLevelItemDescriptor.class); TopLevelSCMOwner owner = mock(TopLevelSCMOwner.class); when(owner.getDescriptor()).thenReturn(descriptor); when(descriptor.isApplicable(any(Descriptor.class))).thenReturn(true); - assertThat(SingleSCMSource.DescriptorImpl.getSCMDescriptors(owner), + assertThat( + SingleSCMSource.DescriptorImpl.getSCMDescriptors(owner), hasItem(instanceOf(MockSCM.DescriptorImpl.class))); } - public interface TopLevelSCMOwner extends TopLevelItem, SCMSourceOwner { - } + public interface TopLevelSCMOwner extends TopLevelItem, SCMSourceOwner {} /** - * Helper for {@link #configRoundtrip()}. + * Helper for configRoundtrip(). */ public static class SCMSourceBuilder extends Builder { @@ -226,14 +196,10 @@ public String getDisplayName() { return "SCMSourceBuilder"; } - @SuppressWarnings("rawtypes") @Override public boolean isApplicable(Class jobType) { return true; } - } - } - } diff --git a/src/test/java/jenkins/scm/impl/SymbolAnnotationsTest.java b/src/test/java/jenkins/scm/impl/SymbolAnnotationsTest.java index d7beff2e..8f12beab 100644 --- a/src/test/java/jenkins/scm/impl/SymbolAnnotationsTest.java +++ b/src/test/java/jenkins/scm/impl/SymbolAnnotationsTest.java @@ -25,8 +25,14 @@ package jenkins.scm.impl; -import java.util.Collections; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; +import java.util.Collections; import jenkins.scm.impl.mock.MockSCM; import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMDiscoverBranches; @@ -41,158 +47,175 @@ import jenkins.scm.impl.trait.WildcardSCMHeadFilterTrait; import jenkins.scm.impl.trait.WildcardSCMSourceFilterTrait; import org.jenkinsci.plugins.structs.describable.DescribableModel; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.startsWith; - -public class SymbolAnnotationsTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class SymbolAnnotationsTest { @Test - public void given__mockScm__when__uninstantiating__then__noRaw$class() throws Exception { + void given__mockScm__when__uninstantiating__then__noRaw$class(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("test"); - MockSCM instance = new MockSCM(c, "test", new MockSCMHead("master"), new MockSCMRevision( - new MockSCMHead("master"), c.getRevision("test", "master"))); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), allOf( - startsWith("@"), - not(containsString(", $")), - not(containsString("=$")), - not(containsString("[$")), - not(containsString("{$")) - )); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@mockScm(" - + "controllerId=" + c.getId() + "," - + "head=master," - + "repository=test," - + "revision=" + c.getRevision("test", "master") - + ")")); + MockSCM instance = new MockSCM( + c, + "test", + new MockSCMHead("master"), + new MockSCMRevision(new MockSCMHead("master"), c.getRevision("test", "master"))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + allOf( + startsWith("@"), + not(containsString(", $")), + not(containsString("=$")), + not(containsString("[$")), + not(containsString("{$")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + is("@mockScm(" + + "controllerId=" + c.getId() + "," + + "head=master," + + "repository=test," + + "revision=" + c.getRevision("test", "master") + + ")")); } } @Test - public void given__mockScmSource__when__uninstantiating__then__noRaw$class() throws Exception { + void given__mockScmSource__when__uninstantiating__then__noRaw$class(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("test"); - MockSCMSource instance = new MockSCMSource(c, "test", new MockSCMDiscoverBranches(), - new MockSCMDiscoverChangeRequests(), new MockSCMDiscoverTags(), - new WildcardSCMHeadFilterTrait("*", "ignore"), new RegexSCMHeadFilterTrait("i.*")); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), allOf( - startsWith("@"), - not(containsString(", $")), - not(containsString("=$")), - not(containsString("[$")), - not(containsString("{$")) - )); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@mockScm(" - + "controllerId=" + c.getId() + "," - + "id=" + instance.getId() + "," - + "repository=test," - + "traits=[" - + "@discoverBranches$MockSCMDiscoverBranches(), " - + "@discoverChangeRequests$MockSCMDiscoverChangeRequests(strategiesStr=HEAD, ), " - + "@discoverTags$MockSCMDiscoverTags(), " - + "@headWildcardFilter$WildcardSCMHeadFilterTrait(excludes=ignore,includes=*), " - + "@headRegexFilter$RegexSCMHeadFilterTrait(regex=i.*)" - + "]" - + ")")); + MockSCMSource instance = new MockSCMSource( + c, + "test", + new MockSCMDiscoverBranches(), + new MockSCMDiscoverChangeRequests(), + new MockSCMDiscoverTags(), + new WildcardSCMHeadFilterTrait("*", "ignore"), + new RegexSCMHeadFilterTrait("i.*")); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + allOf( + startsWith("@"), + not(containsString(", $")), + not(containsString("=$")), + not(containsString("[$")), + not(containsString("{$")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + is("@mockScm(" + + "controllerId=" + c.getId() + "," + + "id=" + instance.getId() + "," + + "repository=test," + + "traits=[" + + "@discoverBranches$MockSCMDiscoverBranches(), " + + "@discoverChangeRequests$MockSCMDiscoverChangeRequests(strategiesStr=HEAD, ), " + + "@discoverTags$MockSCMDiscoverTags(), " + + "@headWildcardFilter$WildcardSCMHeadFilterTrait(excludes=ignore,includes=*), " + + "@headRegexFilter$RegexSCMHeadFilterTrait(regex=i.*)" + + "]" + + ")")); } } @Test - public void given__mockScmNavigator__when__uninstantiating__then__noRaw$class() throws Exception { + void given__mockScmNavigator__when__uninstantiating__then__noRaw$class(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("test"); - MockSCMNavigator instance = new MockSCMNavigator(c, + MockSCMNavigator instance = new MockSCMNavigator( + c, new MockSCMDiscoverBranches(), new MockSCMDiscoverChangeRequests(), new MockSCMDiscoverTags(), new WildcardSCMHeadFilterTrait("*", "ignore"), new RegexSCMHeadFilterTrait("i.*"), new WildcardSCMSourceFilterTrait("i*", "ignored"), - new RegexSCMSourceFilterTrait("ig.*") - ); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), allOf( - startsWith("@"), - not(containsString(", $")), - not(containsString("=$")), - not(containsString("[$")), - not(containsString("{$")) - )); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@mockScm(" - + "controllerId=" + c.getId() + ",traits=[" - + "@discoverBranches$MockSCMDiscoverBranches(), " - + "@discoverChangeRequests$MockSCMDiscoverChangeRequests(strategiesStr=HEAD, ), " - + "@discoverTags$MockSCMDiscoverTags(), " - + "@headWildcardFilter$WildcardSCMHeadFilterTrait(excludes=ignore,includes=*), " - + "@headRegexFilter$RegexSCMHeadFilterTrait(regex=i.*), " - + "@sourceWildcardFilter$WildcardSCMSourceFilterTrait(excludes=ignored,includes=i*), " - + "@sourceRegexFilter$RegexSCMSourceFilterTrait(regex=ig.*)" - + "]" - + ")")); + new RegexSCMSourceFilterTrait("ig.*")); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + allOf( + startsWith("@"), + not(containsString(", $")), + not(containsString("=$")), + not(containsString("[$")), + not(containsString("{$")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + is("@mockScm(" + + "controllerId=" + c.getId() + ",traits=[" + + "@discoverBranches$MockSCMDiscoverBranches(), " + + "@discoverChangeRequests$MockSCMDiscoverChangeRequests(strategiesStr=HEAD, ), " + + "@discoverTags$MockSCMDiscoverTags(), " + + "@headWildcardFilter$WildcardSCMHeadFilterTrait(excludes=ignore,includes=*), " + + "@headRegexFilter$RegexSCMHeadFilterTrait(regex=i.*), " + + "@sourceWildcardFilter$WildcardSCMSourceFilterTrait(excludes=ignored,includes=i*), " + + "@sourceRegexFilter$RegexSCMSourceFilterTrait(regex=ig.*)" + + "]" + + ")")); } } @Test - public void given__singleScmNavigator__when__uninstantiating__then__noRaw$class() throws Exception { + void given__singleScmNavigator__when__uninstantiating__then__noRaw$class(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("test"); - SingleSCMNavigator instance = new SingleSCMNavigator("foo", - Collections.singletonList(new MockSCMSource(c, "test")) - ); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), allOf( - startsWith("@"), - not(containsString(", $")), - not(containsString("=$")), - not(containsString("[$")), - not(containsString("{$")) - )); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@fromSource(name=foo," - + "sources=[@mockScm$MockSCMSource(" - + "controllerId=" + c.getId() + "," - + "id=" + instance.getSources().get(0).getId() + "," - + "repository=test," - + "traits=[]" - + ")" - + "]" - + ")")); + SingleSCMNavigator instance = + new SingleSCMNavigator("foo", Collections.singletonList(new MockSCMSource(c, "test"))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + allOf( + startsWith("@"), + not(containsString(", $")), + not(containsString("=$")), + not(containsString("[$")), + not(containsString("{$")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + is("@fromSource(name=foo," + + "sources=[@mockScm$MockSCMSource(" + + "controllerId=" + c.getId() + "," + + "id=" + instance.getSources().get(0).getId() + "," + + "repository=test," + + "traits=[]" + + ")" + + "]" + + ")")); } } @Test - public void given__singleScmSource__when__uninstantiating__then__noRaw$class() throws Exception { + void given__singleScmSource__when__uninstantiating__then__noRaw$class(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("test"); - SingleSCMSource instance = new SingleSCMSource("foo", "foo", new MockSCM(c, "test", - new MockSCMHead("master"), new MockSCMRevision( - new MockSCMHead("master"), c.getRevision("test", "master"))) - ); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), allOf( - startsWith("@"), - not(containsString(", $")), - not(containsString("=$")), - not(containsString("[$")), - not(containsString("{$")) - )); - assertThat(DescribableModel.uninstantiate2_(instance).toString(), is("@fromScm(" - + "id=foo," - + "name=foo," - + "scm=@mockScm$MockSCM(" - + "controllerId=" + c.getId() + "," - + "head=master," - + "repository=test," - + "revision=" + c.getRevision("test", "master") - + ")" - + ")")); + SingleSCMSource instance = new SingleSCMSource( + "foo", + "foo", + new MockSCM( + c, + "test", + new MockSCMHead("master"), + new MockSCMRevision(new MockSCMHead("master"), c.getRevision("test", "master")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + allOf( + startsWith("@"), + not(containsString(", $")), + not(containsString("=$")), + not(containsString("[$")), + not(containsString("{$")))); + assertThat( + DescribableModel.uninstantiate2_(instance).toString(), + is("@fromScm(" + + "id=foo," + + "name=foo," + + "scm=@mockScm$MockSCM(" + + "controllerId=" + c.getId() + "," + + "head=master," + + "repository=test," + + "revision=" + c.getRevision("test", "master") + + ")" + + ")")); } } - } diff --git a/src/test/java/jenkins/scm/impl/TagSCMHeadCategoryTest.java b/src/test/java/jenkins/scm/impl/TagSCMHeadCategoryTest.java index 3e832b0e..df1d58fd 100644 --- a/src/test/java/jenkins/scm/impl/TagSCMHeadCategoryTest.java +++ b/src/test/java/jenkins/scm/impl/TagSCMHeadCategoryTest.java @@ -25,39 +25,41 @@ package jenkins.scm.impl; -import jenkins.scm.impl.mock.MockChangeRequestSCMHead; -import jenkins.scm.impl.mock.MockSCMHead; -import jenkins.scm.impl.mock.MockTagSCMHead; -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -@RunWith(Theories.class) -public class TagSCMHeadCategoryTest { +import java.util.stream.Stream; +import jenkins.scm.impl.mock.MockChangeRequestSCMHead; +import jenkins.scm.impl.mock.MockSCMHead; +import jenkins.scm.impl.mock.MockTagSCMHead; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; - @DataPoint - public static TagSCMHeadCategory defInstance = TagSCMHeadCategory.DEFAULT; +class TagSCMHeadCategoryTest { - @DataPoint - public static TagSCMHeadCategory custInstance = new TagSCMHeadCategory(Messages._TagSCMHeadCategory_DisplayName()); + static Stream instance() { + return Stream.of( + Arguments.of(Named.of("default", TagSCMHeadCategory.DEFAULT)), + Arguments.of(Named.of("custom", new TagSCMHeadCategory(Messages._TagSCMHeadCategory_DisplayName())))); + } - @Theory - public void given_tagHead_when_isMatch_then_confirmMatch(TagSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_tagHead_when_isMatch_then_confirmMatch(TagSCMHeadCategory instance) { assertThat(instance.isMatch(new MockTagSCMHead("1.0", 0L)), is(true)); } - @Theory - public void given_regularHead_when_isMatch_then_rejectMatch(TagSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_regularHead_when_isMatch_then_rejectMatch(TagSCMHeadCategory instance) { assertThat(instance.isMatch(new MockSCMHead("master")), is(false)); } - @Theory - public void given_changeRequestHead_when_isMatch_then_rejectMatch(TagSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_changeRequestHead_when_isMatch_then_rejectMatch(TagSCMHeadCategory instance) { assertThat(instance.isMatch(new MockChangeRequestSCMHead(1, "master")), is(false)); } - } diff --git a/src/test/java/jenkins/scm/impl/UncategorizedSCMHeadCategoryTest.java b/src/test/java/jenkins/scm/impl/UncategorizedSCMHeadCategoryTest.java index cbba4191..40bb9d84 100644 --- a/src/test/java/jenkins/scm/impl/UncategorizedSCMHeadCategoryTest.java +++ b/src/test/java/jenkins/scm/impl/UncategorizedSCMHeadCategoryTest.java @@ -25,39 +25,43 @@ package jenkins.scm.impl; -import jenkins.scm.impl.mock.MockChangeRequestSCMHead; -import jenkins.scm.impl.mock.MockSCMHead; -import jenkins.scm.impl.mock.MockTagSCMHead; -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -@RunWith(Theories.class) -public class UncategorizedSCMHeadCategoryTest { +import java.util.stream.Stream; +import jenkins.scm.impl.mock.MockChangeRequestSCMHead; +import jenkins.scm.impl.mock.MockSCMHead; +import jenkins.scm.impl.mock.MockTagSCMHead; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; - @DataPoint - public static UncategorizedSCMHeadCategory defInstance = UncategorizedSCMHeadCategory.DEFAULT; +class UncategorizedSCMHeadCategoryTest { - @DataPoint - public static UncategorizedSCMHeadCategory custInstance = new UncategorizedSCMHeadCategory(Messages._UncategorizedSCMHeadCategory_DisplayName()); + static Stream instance() { + return Stream.of( + Arguments.of(Named.of("default", UncategorizedSCMHeadCategory.DEFAULT)), + Arguments.of(Named.of( + "custom", + new UncategorizedSCMHeadCategory(Messages._UncategorizedSCMHeadCategory_DisplayName())))); + } - @Theory - public void given_tagHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_tagHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) { assertThat(instance.isMatch(new MockTagSCMHead("1.0", 0L)), is(true)); } - @Theory - public void given_regularHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_regularHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) { assertThat(instance.isMatch(new MockSCMHead("master")), is(true)); } - @Theory - public void given_changeRequestHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_changeRequestHead_when_isMatch_then_confirmMatch(UncategorizedSCMHeadCategory instance) { assertThat(instance.isMatch(new MockChangeRequestSCMHead(1, "master")), is(true)); } - } diff --git a/src/test/java/jenkins/scm/impl/UncategorizedSCMSourceCategoryTest.java b/src/test/java/jenkins/scm/impl/UncategorizedSCMSourceCategoryTest.java index 3f1ee0f7..60268871 100644 --- a/src/test/java/jenkins/scm/impl/UncategorizedSCMSourceCategoryTest.java +++ b/src/test/java/jenkins/scm/impl/UncategorizedSCMSourceCategoryTest.java @@ -25,29 +25,31 @@ package jenkins.scm.impl; -import jenkins.scm.api.SCMSource; -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.mock; -@RunWith(Theories.class) -public class UncategorizedSCMSourceCategoryTest { - - @DataPoint - public static UncategorizedSCMSourceCategory defInstance = UncategorizedSCMSourceCategory.DEFAULT; - - @DataPoint - public static UncategorizedSCMSourceCategory custInstance = new UncategorizedSCMSourceCategory(Messages._UncategorizedSCMSourceCategory_DisplayName()); +import java.util.stream.Stream; +import jenkins.scm.api.SCMSource; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class UncategorizedSCMSourceCategoryTest { + + static Stream instance() { + return Stream.of( + Arguments.of(Named.of("default", UncategorizedSCMSourceCategory.DEFAULT)), + Arguments.of(Named.of( + "custom", + new UncategorizedSCMSourceCategory(Messages._UncategorizedSCMSourceCategory_DisplayName())))); + } - @Theory - public void given_source_when_isMatch_then_confirmMatch(UncategorizedSCMSourceCategory instance) throws Exception { + @ParameterizedTest + @MethodSource("instance") + void given_source_when_isMatch_then_confirmMatch(UncategorizedSCMSourceCategory instance) { SCMSource mock = mock(SCMSource.class); assertThat(instance.isMatch(mock), is(true)); } - } diff --git a/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java b/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java index 82767011..926ce7fd 100644 --- a/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java +++ b/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java @@ -24,6 +24,14 @@ package jenkins.scm.impl.avatars; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.startsWith; + import hudson.ExtensionList; import hudson.model.UnprotectedRootAction; import hudson.util.HttpResponses; @@ -38,26 +46,17 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.HttpResponse; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.startsWith; - -public class AvatarCacheTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class AvatarCacheTest { @Test - public void fakeUrl() throws Exception { + void fakeUrl(JenkinsRule r) throws Exception { // start by adding a block of tasks to the worker so we can prevent the image from being fetched CountDownLatch requestsPending = new CountDownLatch(AvatarCache.CONCURRENT_REQUEST_LIMIT + 1); CountDownLatch blockReady = new CountDownLatch(AvatarCache.CONCURRENT_REQUEST_LIMIT); @@ -67,7 +66,7 @@ public void fakeUrl() throws Exception { blockReady.await(10, TimeUnit.SECONDS); // now we generate the URL - String url = callBuildUrl("about:blank", "32x32"); + String url = callBuildUrl(r, "about:blank", "32x32"); assertThat(url, is(r.getURL().toString() + "avatar-cache/0ffc09cf03c14f063afa7f03a5f4b074.png?size=32x32")); // now we can test the pending responses @@ -116,7 +115,7 @@ public void fakeUrl() throws Exception { } @Test - public void realUrl() throws Exception { + void realUrl(JenkinsRule r) throws Exception { // start by adding a block of tasks to the worker so we can prevent the image from being fetched CountDownLatch requestsPending = new CountDownLatch(AvatarCache.CONCURRENT_REQUEST_LIMIT + 1); CountDownLatch blockReady = new CountDownLatch(AvatarCache.CONCURRENT_REQUEST_LIMIT); @@ -126,7 +125,7 @@ public void realUrl() throws Exception { blockReady.await(10, TimeUnit.SECONDS); // now we generate the URL - String url = callBuildUrl(r.getURL().toString() + "plugin/scm-api/test-avatar.png", "32x32"); + String url = callBuildUrl(r, r.getURL().toString() + "plugin/scm-api/test-avatar.png", "32x32"); assertThat(url, allOf(startsWith(r.getURL().toString() + "avatar-cache/"), endsWith(".png?size=32x32"))); // now we can test the pending responses @@ -183,14 +182,11 @@ private ExecutorService worker() { private List> fakeWork(final CountDownLatch latch, final CountDownLatch running) { final List> tasks = new ArrayList<>(); for (int i = 0; i < AvatarCache.CONCURRENT_REQUEST_LIMIT; i++) { - tasks.add(new Callable<>() { - @Override - public Void call() throws Exception { - running.countDown(); - latch.countDown(); - latch.await(10, TimeUnit.SECONDS); - return null; - } + tasks.add(() -> { + running.countDown(); + latch.countDown(); + latch.await(10, TimeUnit.SECONDS); + return null; }); } return tasks; @@ -199,12 +195,13 @@ public Void call() throws Exception { /** * Invokes {@link AvatarCache#buildUrl(String, String)} from a request thread. * + * @param r the jenkins rule. * @param url the url. * @param size the size. * @return the response * @throws IOException if there is an error. */ - public String callBuildUrl(String url, String size) throws IOException { + public String callBuildUrl(JenkinsRule r, String url, String size) throws IOException { ProbeAction a = ExtensionList.lookup(UnprotectedRootAction.class).get(ProbeAction.class); assertThat(a, notNullValue()); synchronized (a) { @@ -214,7 +211,8 @@ public String callBuildUrl(String url, String size) throws IOException { (HttpURLConnection) new URL(r.getURL().toString() + a.getUrlName() + "/").openConnection(); try { c.connect(); - return IOUtils.toString(c.getInputStream(), StandardCharsets.UTF_8).trim(); + return IOUtils.toString(c.getInputStream(), StandardCharsets.UTF_8) + .trim(); } finally { c.disconnect(); } @@ -246,5 +244,4 @@ public HttpResponse doIndex() { return HttpResponses.text(AvatarCache.buildUrl(url, size)); } } - } diff --git a/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java b/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java index 04d81d5d..e6b2ef1c 100644 --- a/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java +++ b/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java @@ -33,7 +33,6 @@ import java.util.Arrays; import java.util.List; import org.apache.commons.io.FileUtils; -import static jenkins.scm.impl.mock.AbstractSampleRepoRule.run; /** * Rule tailored to a DVCS which may be initialized and cloned from a local directory. diff --git a/src/test/java/jenkins/scm/impl/mock/MockSCMControllerTest.java b/src/test/java/jenkins/scm/impl/mock/MockSCMControllerTest.java index defb4db0..ecc5b0f6 100644 --- a/src/test/java/jenkins/scm/impl/mock/MockSCMControllerTest.java +++ b/src/test/java/jenkins/scm/impl/mock/MockSCMControllerTest.java @@ -1,15 +1,14 @@ package jenkins.scm.impl.mock; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.security.MessageDigest; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; - -public class MockSCMControllerTest { +class MockSCMControllerTest { @Test - public void toHexBinary() throws Exception { + void toHexBinary() throws Exception { final MessageDigest msg = MessageDigest.getInstance("SHA-1"); msg.update("blah".getBytes()); assertEquals("5bf1fd927dfb8679496a2e6cf00cbe50c1c87145", MockSCMController.toHexBinary(msg.digest())); diff --git a/src/test/java/jenkins/scm/impl/trait/RegexSCMHeadFilterTraitTest.java b/src/test/java/jenkins/scm/impl/trait/RegexSCMHeadFilterTraitTest.java index ed613ebc..c8798a02 100644 --- a/src/test/java/jenkins/scm/impl/trait/RegexSCMHeadFilterTraitTest.java +++ b/src/test/java/jenkins/scm/impl/trait/RegexSCMHeadFilterTraitTest.java @@ -24,6 +24,9 @@ package jenkins.scm.impl.trait; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -33,27 +36,25 @@ import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMDiscoverBranches; import jenkins.scm.impl.mock.MockSCMSource; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; - -public class RegexSCMHeadFilterTraitTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class RegexSCMHeadFilterTraitTest { @Test - public void given_sourceWithRegexRule_when_scanning_then_ruleApplied() throws Exception { + void given_sourceWithRegexRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createBranch("foo", "fork"); c.createBranch("foo", "alt"); - MockSCMSource src = new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new RegexSCMHeadFilterTrait("[fm].*")); - Map result = src.fetch(null, SCMHeadObserver.collect(), null, null).result(); + MockSCMSource src = + new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new RegexSCMHeadFilterTrait("[fm].*")); + Map result = + src.fetch(null, SCMHeadObserver.collect(), null, null).result(); Set names = new TreeSet<>(); - for (SCMHead h: result.keySet()) { + for (SCMHead h : result.keySet()) { names.add(h.getName()); } assertThat(names, containsInAnyOrder("master", "fork")); diff --git a/src/test/java/jenkins/scm/impl/trait/RegexSCMSourceFilterTraitTest.java b/src/test/java/jenkins/scm/impl/trait/RegexSCMSourceFilterTraitTest.java index b63bfbfc..e767f9f3 100644 --- a/src/test/java/jenkins/scm/impl/trait/RegexSCMSourceFilterTraitTest.java +++ b/src/test/java/jenkins/scm/impl/trait/RegexSCMSourceFilterTraitTest.java @@ -24,6 +24,9 @@ package jenkins.scm.impl.trait; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import hudson.model.TaskListener; @@ -38,19 +41,15 @@ import jenkins.scm.impl.NoOpProjectObserver; import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMNavigator; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; - -public class RegexSCMSourceFilterTraitTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class RegexSCMSourceFilterTraitTest { @Test - public void given_navigatorWithIncludeRegexRule_when_scanning_then_ruleApplied() throws Exception { + void given_navigatorWithIncludeRegexRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createRepository("bar"); @@ -65,8 +64,7 @@ public void given_navigatorWithIncludeRegexRule_when_scanning_then_ruleApplied() private static class SimpleSCMSourceObserver extends SCMSourceObserver { Set names = new HashSet<>(); LogTaskListener listener = - new LogTaskListener(Logger.getLogger(WildcardSCMSourceFilterTrait.class.getName()), - Level.INFO); + new LogTaskListener(Logger.getLogger(RegexSCMSourceFilterTrait.class.getName()), Level.INFO); @NonNull @Override @@ -90,8 +88,7 @@ public ProjectObserver observe(@NonNull String projectName) @Override public void addAttribute(@NonNull String key, @Nullable Object value) - throws IllegalArgumentException, ClassCastException { - } + throws IllegalArgumentException, ClassCastException {} public Set getNames() { return names; diff --git a/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java b/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java index 0ff12166..ff790f56 100644 --- a/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java +++ b/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java @@ -24,6 +24,9 @@ package jenkins.scm.impl.trait; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -33,27 +36,25 @@ import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMDiscoverBranches; import jenkins.scm.impl.mock.MockSCMSource; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; - -public class WildcardSCMHeadFilterTraitTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class WildcardSCMHeadFilterTraitTest { @Test - public void given_sourceWithIncludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_sourceWithIncludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createBranch("foo", "fork"); c.createBranch("foo", "alt"); - MockSCMSource src = new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("master fo*", "")); - Map result = src.fetch(null, SCMHeadObserver.collect(), null, null).result(); + MockSCMSource src = new MockSCMSource( + c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("master fo*", "")); + Map result = + src.fetch(null, SCMHeadObserver.collect(), null, null).result(); Set names = new TreeSet<>(); - for (SCMHead h: result.keySet()) { + for (SCMHead h : result.keySet()) { names.add(h.getName()); } assertThat(names, containsInAnyOrder("master", "fork")); @@ -61,31 +62,36 @@ public void given_sourceWithIncludeWildcardRule_when_scanning_then_ruleApplied() } @Test - public void given_sourceWithExcludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_sourceWithExcludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createBranch("foo", "fork"); c.createBranch("foo", "alt"); - MockSCMSource src = new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("*", "fo*")); - Map result = src.fetch(null, SCMHeadObserver.collect(), null, null).result(); + MockSCMSource src = new MockSCMSource( + c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("*", "fo*")); + Map result = + src.fetch(null, SCMHeadObserver.collect(), null, null).result(); Set names = new TreeSet<>(); - for (SCMHead h: result.keySet()) { + for (SCMHead h : result.keySet()) { names.add(h.getName()); } assertThat(names, containsInAnyOrder("master", "alt")); } } + @Test - public void given_sourceWithIncludeExcludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_sourceWithIncludeExcludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createBranch("foo", "fork"); c.createBranch("foo", "foo"); c.createBranch("foo", "alt"); - MockSCMSource src = new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("master fo*", "foo")); - Map result = src.fetch(null, SCMHeadObserver.collect(), null, null).result(); + MockSCMSource src = new MockSCMSource( + c, "foo", new MockSCMDiscoverBranches(), new WildcardSCMHeadFilterTrait("master fo*", "foo")); + Map result = + src.fetch(null, SCMHeadObserver.collect(), null, null).result(); Set names = new TreeSet<>(); - for (SCMHead h: result.keySet()) { + for (SCMHead h : result.keySet()) { names.add(h.getName()); } assertThat(names, containsInAnyOrder("master", "fork")); diff --git a/src/test/java/jenkins/scm/impl/trait/WildcardSCMSourceFilterTraitTest.java b/src/test/java/jenkins/scm/impl/trait/WildcardSCMSourceFilterTraitTest.java index 08fcd0fc..205044da 100644 --- a/src/test/java/jenkins/scm/impl/trait/WildcardSCMSourceFilterTraitTest.java +++ b/src/test/java/jenkins/scm/impl/trait/WildcardSCMSourceFilterTraitTest.java @@ -24,6 +24,9 @@ package jenkins.scm.impl.trait; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import hudson.model.TaskListener; @@ -38,19 +41,15 @@ import jenkins.scm.impl.NoOpProjectObserver; import jenkins.scm.impl.mock.MockSCMController; import jenkins.scm.impl.mock.MockSCMNavigator; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; - -public class WildcardSCMSourceFilterTraitTest { - @ClassRule - public static JenkinsRule r = new JenkinsRule(); +@WithJenkins +class WildcardSCMSourceFilterTraitTest { @Test - public void given_navigatorWithIncludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_navigatorWithIncludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createRepository("bar"); @@ -63,7 +62,7 @@ public void given_navigatorWithIncludeWildcardRule_when_scanning_then_ruleApplie } @Test - public void given_navigatorWithExcludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_navigatorWithExcludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createRepository("bar"); @@ -74,8 +73,9 @@ public void given_navigatorWithExcludeWildcardRule_when_scanning_then_ruleApplie assertThat(observer.getNames(), containsInAnyOrder("bar", "manchu")); } } + @Test - public void given_navigatorWithIncludeExcludeWildcardRule_when_scanning_then_ruleApplied() throws Exception { + void given_navigatorWithIncludeExcludeWildcardRule_when_scanning_then_ruleApplied(JenkinsRule r) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); c.createRepository("fu"); @@ -91,8 +91,7 @@ public void given_navigatorWithIncludeExcludeWildcardRule_when_scanning_then_rul private static class SimpleSCMSourceObserver extends SCMSourceObserver { Set names = new HashSet<>(); LogTaskListener listener = - new LogTaskListener(Logger.getLogger(WildcardSCMSourceFilterTrait.class.getName()), - Level.INFO); + new LogTaskListener(Logger.getLogger(WildcardSCMSourceFilterTrait.class.getName()), Level.INFO); @NonNull @Override @@ -116,8 +115,7 @@ public ProjectObserver observe(@NonNull String projectName) @Override public void addAttribute(@NonNull String key, @Nullable Object value) - throws IllegalArgumentException, ClassCastException { - } + throws IllegalArgumentException, ClassCastException {} public Set getNames() { return names;