2424
2525package hudson .model ;
2626
27+ import hudson .PluginWrapper ;
28+ import hudson .PluginWrapper .Dependency ;
2729import hudson .model .UpdateSite .Data ;
2830import hudson .util .FormValidation ;
2931import hudson .util .PersistedList ;
3234import java .io .IOException ;
3335import java .net .URISyntaxException ;
3436import java .net .URL ;
37+ import java .util .ArrayList ;
3538import java .util .Arrays ;
3639import java .util .Collections ;
3740import java .util .HashSet ;
41+ import java .util .jar .Attributes ;
42+ import java .util .jar .Manifest ;
3843
3944import javax .servlet .ServletException ;
4045import javax .servlet .http .HttpServletRequest ;
4146import javax .servlet .http .HttpServletResponse ;
4247
4348import static org .junit .Assert .*;
4449
50+ import jenkins .model .Jenkins ;
4551import jenkins .security .UpdateSiteWarningsConfiguration ;
4652import jenkins .security .UpdateSiteWarningsMonitor ;
4753import org .apache .commons .io .FileUtils ;
@@ -107,11 +113,9 @@ public void shutdownWebserver() throws Exception {
107113 }
108114
109115 @ Test public void relativeURLs () throws Exception {
110- PersistedList <UpdateSite > sites = j .jenkins .getUpdateCenter ().getSites ();
111- sites .clear ();
112116 URL url = new URL (baseUrl , "/plugins/tasks-update-center.json" );
113117 UpdateSite site = new UpdateSite (UpdateCenter .ID_DEFAULT , url .toString ());
114- sites . add (site );
118+ overrideUpdateSite (site );
115119 assertEquals (FormValidation .ok (), site .updateDirectly (false ).get ());
116120 Data data = site .getData ();
117121 assertNotNull (data );
@@ -124,6 +128,28 @@ public void shutdownWebserver() throws Exception {
124128 assertEquals ("Wrong name of plugin found" , "Task Scanner Plug-in" , tasksPlugin .getDisplayName ());
125129 }
126130
131+ @ Test public void wikiUrlFromSingleSite () throws Exception {
132+ UpdateSite site = getUpdateSite ("/plugins/tasks-update-center.json" );
133+ overrideUpdateSite (site );
134+ PluginWrapper wrapper = buildPluginWrapper ("dummy" , "https://wiki.jenkins.io/display/JENKINS/dummy" );
135+ assertEquals ("https://plugins.jenkins.io/dummy" , wrapper .getUrl ());
136+ }
137+
138+ @ Test public void wikiUrlFromMoreSites () throws Exception {
139+ UpdateSite site = getUpdateSite ("/plugins/tasks-update-center.json" );
140+ UpdateSite alternativeSite = getUpdateSite ("/plugins/alternative-update-center.json" , "alternative" );
141+ overrideUpdateSite (site , alternativeSite );
142+ // sites use different Wiki URL for dummy -> use URL from manifest
143+ PluginWrapper wrapper = buildPluginWrapper ("dummy" , "https://wiki.jenkins.io/display/JENKINS/dummy" );
144+ assertEquals ("https://wiki.jenkins.io/display/JENKINS/dummy" , wrapper .getUrl ());
145+ // sites use the same Wiki URL for tasks -> use it
146+ wrapper = buildPluginWrapper ("tasks" , "https://wiki.jenkins.io/display/JENKINS/tasks" );
147+ assertEquals ("https://plugins.jenkins.io/tasks" , wrapper .getUrl ());
148+ // only one site has it
149+ wrapper = buildPluginWrapper ("foo" , "https://wiki.jenkins.io/display/JENKINS/foo" );
150+ assertEquals ("https://plugins.jenkins.io/foo" , wrapper .getUrl ());
151+ }
152+
127153 @ Test public void updateDirectlyWithJson () throws Exception {
128154 UpdateSite us = new UpdateSite ("default" , new URL (baseUrl , "update-center.json" ).toExternalForm ());
129155 assertNull (us .getPlugin ("AdaptivePlugin" ));
@@ -141,12 +167,8 @@ public void shutdownWebserver() throws Exception {
141167 }
142168
143169 @ Test public void incompleteWarningsJson () throws Exception {
144- PersistedList <UpdateSite > sites = j .jenkins .getUpdateCenter ().getSites ();
145- sites .clear ();
146- URL url = new URL (baseUrl , "/plugins/warnings-update-center-malformed.json" );
147- UpdateSite site = new UpdateSite (UpdateCenter .ID_DEFAULT , url .toString ());
148- sites .add (site );
149- assertEquals (FormValidation .ok (), site .updateDirectly (false ).get ());
170+ UpdateSite site = getUpdateSite ("/plugins/warnings-update-center-malformed.json" );
171+ overrideUpdateSite (site );
150172 assertEquals ("number of warnings" , 7 , site .getData ().getWarnings ().size ());
151173 assertNotEquals ("plugin data is present" , Collections .emptyMap (), site .getData ().plugins );
152174 }
@@ -190,11 +212,38 @@ public void isPluginUpdateCompatible() throws Exception {
190212 assertFalse ("isLegacyDefault should be false with null url" ,new UpdateSite (null ,null ).isLegacyDefault ());
191213 }
192214
193-
194215 private UpdateSite getUpdateSite (String path ) throws Exception {
216+ return getUpdateSite (path , UpdateCenter .ID_DEFAULT );
217+ }
218+
219+ private UpdateSite getUpdateSite (String path , String id ) throws Exception {
195220 URL url = new URL (baseUrl , path );
196- UpdateSite site = new UpdateSite (UpdateCenter . ID_DEFAULT , url .toString ());
221+ UpdateSite site = new UpdateSite (id , url .toString ());
197222 assertEquals (FormValidation .ok (), site .updateDirectly (false ).get ());
198223 return site ;
199224 }
225+
226+ private void overrideUpdateSite (UpdateSite ... overrideSites ) {
227+ PersistedList <UpdateSite > sites = j .jenkins .getUpdateCenter ().getSites ();
228+ sites .clear ();
229+ sites .addAll (Arrays .asList (overrideSites ));
230+ }
231+
232+ private PluginWrapper buildPluginWrapper (String name , String wikiUrl ) {
233+ Manifest manifest = new Manifest ();
234+ Attributes attributes = manifest .getMainAttributes ();
235+ attributes .put (new Attributes .Name ("Short-Name" ), name );
236+ attributes .put (new Attributes .Name ("Plugin-Version" ), "1.0.0" );
237+ attributes .put (new Attributes .Name ("Url" ), wikiUrl );
238+ return new PluginWrapper (
239+ Jenkins .get ().getPluginManager (),
240+ new File ("/tmp/" + name + ".jpi" ),
241+ manifest ,
242+ null ,
243+ null ,
244+ new File ("/tmp/" + name + ".jpi.disabled" ),
245+ null ,
246+ new ArrayList <Dependency >()
247+ );
248+ }
200249}
0 commit comments