Skip to content

Commit

Permalink
PluginManager#whichPlugin does not work for JenkinsRule (#6982)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Glick <[email protected]>
  • Loading branch information
basil and jglick authored Aug 15, 2022
1 parent 26c924b commit a837997
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
36 changes: 36 additions & 0 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -1324,6 +1327,39 @@ public PluginWrapper whichPlugin(Class c) {
oneAndOnly = p;
}
}
if (oneAndOnly == null && Main.isUnitTest) {
// compare jenkins.security.ClassFilterImpl
CodeSource cs = c.getProtectionDomain().getCodeSource();
if (cs != null) {
URL loc = cs.getLocation();
if (loc != null) {
if ("file".equals(loc.getProtocol())) {
File file;
try {
file = Paths.get(loc.toURI()).toFile();
} catch (InvalidPathException | URISyntaxException e) {
LOGGER.log(Level.WARNING, "could not inspect " + loc, e);
return null;
}
if (file.isFile()) { // ignore directories
try (JarFile jf = new JarFile(file)) {
Manifest mf = jf.getManifest();
if (mf != null) {
java.util.jar.Attributes attr = mf.getMainAttributes();
if (attr.getValue("Plugin-Version") != null) {
String shortName = attr.getValue("Short-Name");
LOGGER.fine(() -> "found " + shortName + " for " + c);
return getPlugin(shortName);
}
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "could not inspect " + loc, e);
}
}
}
}
}
}
return oneAndOnly;
}

Expand Down
45 changes: 45 additions & 0 deletions test/src/test/java/hudson/PluginManagerWhichTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* The MIT License
*
* Copyright 2022 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.cloudbees.hudson.plugins.folder.Folder;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class PluginManagerWhichTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void whichPlugin() throws Exception {
PluginWrapper expected = r.getPluginManager().getPlugin("cloudbees-folder");
assertNotNull(expected);
assertEquals(expected, r.getPluginManager().whichPlugin(Folder.class));
}
}

0 comments on commit a837997

Please sign in to comment.