2525
2626package hudson ;
2727
28- import static org .junit .Assert .assertNotNull ;
29- import static org .junit .Assert .assertTrue ;
30- import static org .junit .Assert .fail ;
28+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
29+ import static org .junit .jupiter . api . Assertions .assertTrue ;
30+ import static org .junit .jupiter . api . Assertions .fail ;
3131
3232import hudson .model .Hudson ;
3333import java .io .File ;
34+ import java .lang .reflect .Method ;
3435import java .net .URL ;
3536import java .util .Collection ;
3637import java .util .Enumeration ;
3738import java .util .LinkedHashSet ;
3839import java .util .Set ;
39- import org .junit .Rule ;
40- import org .junit .Test ;
41- import org .junit .experimental .categories .Category ;
40+ import org .junit .jupiter .api .Tag ;
41+ import org .junit .jupiter .api .Test ;
42+ import org .junit .jupiter .api .extension .ExtensionContext ;
43+ import org .junit .jupiter .api .extension .RegisterExtension ;
44+ import org .junit .runner .Description ;
45+ import org .junit .runners .model .Statement ;
4246import org .jvnet .hudson .test .Issue ;
4347import org .jvnet .hudson .test .JenkinsRecipe ;
4448import org .jvnet .hudson .test .JenkinsRule ;
45- import org .jvnet .hudson .test .SmokeTest ;
49+ import org .jvnet .hudson .test .junit . jupiter . JenkinsSessionExtension ;
4650import org .jvnet .hudson .test .recipes .LocalData ;
4751
4852/**
4953 * @author Alan Harder
5054 */
51- @ Category ( SmokeTest . class )
52- public class ClassicPluginStrategyTest {
55+ @ Tag ( " SmokeTest" )
56+ class ClassicPluginStrategyTest {
5357
54- @ Rule
55- public JenkinsRule j = new JenkinsRule () {
56- @ Override
57- protected Hudson newHudson () throws Exception {
58- File home = homeLoader .allocate ();
59-
60- for (JenkinsRecipe .Runner r : recipes ) {
61- r .decorateHome (this , home );
62- }
63- LocalPluginManager pluginManager = new LocalPluginManager (home ) {
64- @ Override
65- protected Collection <String > loadBundledPlugins () {
66- // Overriding so we can force loading of the detached plugins for testing
67- Set <String > names = new LinkedHashSet <>();
68- names .addAll (loadPluginsFromWar ("/WEB-INF/plugins" ));
69- names .addAll (loadPluginsFromWar ("/WEB-INF/detached-plugins" ));
70- return names ;
71- }
72- };
73- setPluginManager (pluginManager );
74- return new Hudson (home , createWebServer2 (), pluginManager );
75- }
76- };
58+ @ RegisterExtension
59+ private final JenkinsSessionExtension session = new CustomPluginManagerExtension ();
7760
7861 /**
7962 * Test finding resources via DependencyClassLoader.
8063 */
8164 @ LocalData
8265 @ Test
83- public void testDependencyClassLoader () throws Exception {
84- // Test data has: foo3 depends on foo2,foo1; foo2 depends on foo1
85- // (thus findResources from foo3 can find foo1 resources via 2 dependency paths)
86- PluginWrapper p = j .jenkins .getPluginManager ().getPlugin ("foo3" );
87- String res ;
88-
89- // In the current impl, the dependencies are the parent ClassLoader so resources
90- // are found there before checking the plugin itself. Adjust the expected results
91- // below if this is ever changed to check the plugin first.
92- Enumeration <URL > en = p .classLoader .getResources ("test-resource" );
93- for (int i = 0 ; en .hasMoreElements (); i ++) {
94- res = en .nextElement ().toString ();
95- if (i < 2 )
96- assertTrue ("In current impl, " + res + "should be foo1 or foo2" ,
97- res .contains ("/foo1/" ) || res .contains ("/foo2/" ));
98- else
99- assertTrue ("In current impl, " + res + "should be foo3" , res .contains ("/foo3/" ));
100- }
101- res = p .classLoader .getResource ("test-resource" ).toString ();
102- assertTrue ("In current impl, " + res + " should be foo1 or foo2" ,
103- res .contains ("/foo1/" ) || res .contains ("/foo2/" ));
66+ void testDependencyClassLoader () throws Throwable {
67+ session .then (j -> {
68+ // Test data has: foo3 depends on foo2,foo1; foo2 depends on foo1
69+ // (thus findResources from foo3 can find foo1 resources via 2 dependency paths)
70+ PluginWrapper p = j .jenkins .getPluginManager ().getPlugin ("foo3" );
71+ String res ;
72+
73+ // In the current impl, the dependencies are the parent ClassLoader so resources
74+ // are found there before checking the plugin itself. Adjust the expected results
75+ // below if this is ever changed to check the plugin first.
76+ Enumeration <URL > en = p .classLoader .getResources ("test-resource" );
77+ for (int i = 0 ; en .hasMoreElements (); i ++) {
78+ res = en .nextElement ().toString ();
79+ if (i < 2 )
80+ assertTrue (res .contains ("/foo1/" ) || res .contains ("/foo2/" ),
81+ "In current impl, " + res + "should be foo1 or foo2" );
82+ else
83+ assertTrue (res .contains ("/foo3/" ), "In current impl, " + res + "should be foo3" );
84+ }
85+ res = p .classLoader .getResource ("test-resource" ).toString ();
86+ assertTrue (res .contains ("/foo1/" ) || res .contains ("/foo2/" ),
87+ "In current impl, " + res + " should be foo1 or foo2" );
88+ });
10489 }
10590
10691 /**
@@ -110,17 +95,19 @@ public void testDependencyClassLoader() throws Exception {
11095 @ LocalData
11196 @ Issue ("JENKINS-18654" )
11297 @ Test
113- public void testDisabledDependencyClassLoader () throws Exception {
114- PluginWrapper p = j .jenkins .getPluginManager ().getPlugin ("foo4" );
115-
116- Enumeration <URL > en = p .classLoader .getResources ("test-resource" );
117- for (int i = 0 ; en .hasMoreElements (); i ++) {
118- String res = en .nextElement ().toString ();
119- if (i == 0 )
120- assertTrue ("expected foo4, found " + res , res .contains ("/foo4/" ));
121- else
122- fail ("disabled dependency should not be included" );
123- }
98+ void testDisabledDependencyClassLoader () throws Throwable {
99+ session .then (j -> {
100+ PluginWrapper p = j .jenkins .getPluginManager ().getPlugin ("foo4" );
101+
102+ Enumeration <URL > en = p .classLoader .getResources ("test-resource" );
103+ for (int i = 0 ; en .hasMoreElements (); i ++) {
104+ String res = en .nextElement ().toString ();
105+ if (i == 0 )
106+ assertTrue (res .contains ("/foo4/" ), "expected foo4, found " + res );
107+ else
108+ fail ("disabled dependency should not be included" );
109+ }
110+ });
124111 }
125112
126113 /**
@@ -130,12 +117,77 @@ public void testDisabledDependencyClassLoader() throws Exception {
130117 @ LocalData
131118 @ Issue ("JENKINS-27289" )
132119 @ Test
133- public void testMaskResourceClassLoader () throws Exception {
134- PluginWrapper pw = j .jenkins .getPluginManager ().getPlugin ("foo1" );
135- Class <?> clazz = pw .classLoader .loadClass ("org.apache.http.impl.io.SocketInputBuffer" );
136- ClassLoader cl = clazz .getClassLoader ();
137- URL url = cl .getResource ("org/apache/http/impl/io/SocketInputBuffer.class" );
138- assertNotNull (url );
139- assertTrue ("expected to find the class from foo1 plugin" , url .toString ().contains ("plugins/foo1" ));
120+ void testMaskResourceClassLoader () throws Throwable {
121+ session .then (j -> {
122+ PluginWrapper pw = j .jenkins .getPluginManager ().getPlugin ("foo1" );
123+ Class <?> clazz = pw .classLoader .loadClass ("org.apache.http.impl.io.SocketInputBuffer" );
124+ ClassLoader cl = clazz .getClassLoader ();
125+ URL url = cl .getResource ("org/apache/http/impl/io/SocketInputBuffer.class" );
126+ assertNotNull (url );
127+ assertTrue (url .toString ().contains ("plugins/foo1" ), "expected to find the class from foo1 plugin" );
128+ });
129+ }
130+
131+ private static final class CustomPluginManagerExtension extends JenkinsSessionExtension {
132+
133+ private int port ;
134+ private Description description ;
135+
136+ @ Override
137+ public void beforeEach (ExtensionContext context ) {
138+ super .beforeEach (context );
139+ description = Description .createTestDescription (
140+ context .getTestClass ().map (Class ::getName ).orElse (null ),
141+ context .getTestMethod ().map (Method ::getName ).orElse (null ),
142+ context .getTestMethod ().map (Method ::getAnnotations ).orElse (null ));
143+ }
144+
145+ @ Override
146+ public void then (Step s ) throws Throwable {
147+ CustomJenkinsRule r = new CustomJenkinsRule (getHome (), port );
148+ r .apply (
149+ new Statement () {
150+ @ Override
151+ public void evaluate () throws Throwable {
152+ port = r .getPort ();
153+ s .run (r );
154+ }
155+ },
156+ description
157+ ).evaluate ();
158+ }
159+
160+ private static final class CustomJenkinsRule extends JenkinsRule {
161+
162+ CustomJenkinsRule (File home , int port ) {
163+ with (() -> home );
164+ localPort = port ;
165+ }
166+
167+ int getPort () {
168+ return localPort ;
169+ }
170+
171+ @ Override
172+ protected Hudson newHudson () throws Exception {
173+ File home = homeLoader .allocate ();
174+
175+ for (JenkinsRecipe .Runner r : recipes ) {
176+ r .decorateHome (this , home );
177+ }
178+ LocalPluginManager pluginManager = new LocalPluginManager (home ) {
179+ @ Override
180+ protected Collection <String > loadBundledPlugins () {
181+ // Overriding so we can force loading of the detached plugins for testing
182+ Set <String > names = new LinkedHashSet <>();
183+ names .addAll (loadPluginsFromWar ("/WEB-INF/plugins" ));
184+ names .addAll (loadPluginsFromWar ("/WEB-INF/detached-plugins" ));
185+ return names ;
186+ }
187+ };
188+ setPluginManager (pluginManager );
189+ return new Hudson (home , createWebServer2 (), pluginManager );
190+ }
191+ }
140192 }
141193}
0 commit comments