From e7f88146baf87dc96d723255e522bf51471f6701 Mon Sep 17 00:00:00 2001 From: Valentina Armenise Date: Fri, 22 Jan 2016 17:58:23 +0100 Subject: [PATCH] [JENKINS-32328] handling tool Installers which do not have the default schema --- .../tools/DownloadFromUrlInstaller.java | 28 +++++++++- .../hudson/model/DownloadServiceTest.java | 34 ++++++++++++ .../hudson.plugins.cmake.CmakeInstaller1.json | 54 +++++++++++++++++++ .../hudson.plugins.cmake.CmakeInstaller2.json | 48 +++++++++++++++++ ...on.plugins.cmake.CmakeInstallerResult.json | 54 +++++++++++++++++++ 5 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller1.json create mode 100644 test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller2.json create mode 100644 test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstallerResult.json diff --git a/core/src/main/java/hudson/tools/DownloadFromUrlInstaller.java b/core/src/main/java/hudson/tools/DownloadFromUrlInstaller.java index f71df4a387..2aa3579823 100644 --- a/core/src/main/java/hudson/tools/DownloadFromUrlInstaller.java +++ b/core/src/main/java/hudson/tools/DownloadFromUrlInstaller.java @@ -136,13 +136,39 @@ public abstract class DownloadFromUrlInstaller extends ToolInstaller { final DownloadFromUrlInstaller.DescriptorImpl delegate = (DownloadFromUrlInstaller.DescriptorImpl)this; return new Downloadable(getId()) { public JSONObject reduce(List jsonList) { - return delegate.reduce(jsonList); + if (isDefualtSchema(jsonList)) { + return delegate.reduce(jsonList); + } else { + //if it's not default schema fall back to the super class implementation + return super.reduce(jsonList); + } } }; } return new Downloadable(getId()); } + /** + * this function checks is the update center tool has the default schema + * @param jsonList the list of Update centers json files + * @return true if the schema is the default one (id, name, url), false otherwise + */ + private boolean isDefualtSchema(List jsonList) { + JSONObject jsonToolInstallerList = jsonList.get(0); + ToolInstallerList toolInstallerList = (ToolInstallerList) JSONObject.toBean(jsonToolInstallerList, ToolInstallerList.class); + + if (toolInstallerList != null) { + ToolInstallerEntry[] entryList = toolInstallerList.list; + ToolInstallerEntry sampleEntry = entryList[0]; + if (sampleEntry != null) { + if (sampleEntry.id != null && sampleEntry.name != null && sampleEntry.url != null) { + return true; + } + } + } + return false; + } + private JSONObject reduce(List jsonList) { List reducedToolEntries = new LinkedList<>(); //collect all tool installers objects from the multiple json objects diff --git a/test/src/test/java/hudson/model/DownloadServiceTest.java b/test/src/test/java/hudson/model/DownloadServiceTest.java index 78315cbcd5..9ded01620a 100644 --- a/test/src/test/java/hudson/model/DownloadServiceTest.java +++ b/test/src/test/java/hudson/model/DownloadServiceTest.java @@ -9,7 +9,9 @@ import java.util.Set; import java.util.TreeSet; import hudson.tasks.Maven; +import hudson.tools.DownloadFromUrlInstaller; import hudson.tools.JDKInstaller; +import hudson.tools.ToolInstallation; import jenkins.model.DownloadSettings; import net.sf.json.JSONObject; import org.jvnet.hudson.test.Issue; @@ -127,4 +129,36 @@ public class DownloadServiceTest extends HudsonTestCase { JSONObject expectedResultJson = JSONObject.fromObject(DownloadService.loadJSON(expectedResult)); assertEquals(reducedJson, expectedResultJson); } + + public void testReduceFunctionWithNotDefaultSchemaJsons() throws Exception { + URL resource1 = DownloadServiceTest.class.getResource("hudson.plugins.cmake.CmakeInstaller1.json"); + URL resource2 = DownloadServiceTest.class.getResource("hudson.plugins.cmake.CmakeInstaller2.json"); + JSONObject json1 = JSONObject.fromObject(DownloadService.loadJSON(resource1)); + JSONObject json2 = JSONObject.fromObject(DownloadService.loadJSON(resource2)); + List jsonObjectList = new ArrayList<>(); + jsonObjectList.add(json1); + jsonObjectList.add(json2); + Downloadable downloadable = new GenericDownloadFromUrlInstaller.DescriptorImpl().createDownloadable(); + JSONObject reducedJson = downloadable.reduce(jsonObjectList); + URL expectedResult = DownloadServiceTest.class.getResource("hudson.plugins.cmake.CmakeInstallerResult.json"); + JSONObject expectedResultJson = JSONObject.fromObject(DownloadService.loadJSON(expectedResult)); + assertEquals(reducedJson, expectedResultJson); + } + + private static class GenericDownloadFromUrlInstaller extends DownloadFromUrlInstaller { + protected GenericDownloadFromUrlInstaller(String id) { + super(id); + } + + public static final class DescriptorImpl extends DownloadFromUrlInstaller.DescriptorImpl { + public String getDisplayName() { + return ""; + } + + @Override + public boolean isApplicable(Class toolType) { + return true; + } + } + } } diff --git a/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller1.json b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller1.json new file mode 100644 index 0000000000..f8061acdea --- /dev/null +++ b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller1.json @@ -0,0 +1,54 @@ +downloadService.post('hudson.plugins.cmake.CmakeInstaller',{"list": [ + { + "id": "3.4.2", + "name": "3.4.2", + "variants": [ + { + "arch": "x86", + "os": "win32", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-win32-x86.zip" + }, + { + "arch": "x86_64", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Linux-x86_64.tar.gz" + }, + { + "arch": "i386", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Linux-i386.tar.gz" + }, + { + "arch": "x86_64", + "os": "Darwin", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Darwin-x86_64.tar.gz" + } + ] + }, + { + "id": "3.4.1", + "name": "3.4.1", + "variants": [ + { + "arch": "x86", + "os": "win32", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-win32-x86.zip" + }, + { + "arch": "x86_64", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Linux-x86_64.tar.gz" + }, + { + "arch": "i386", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Linux-i386.tar.gz" + }, + { + "arch": "x86_64", + "os": "Darwin", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Darwin-x86_64.tar.gz" + } + ] + } +]}) \ No newline at end of file diff --git a/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller2.json b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller2.json new file mode 100644 index 0000000000..96de0bc846 --- /dev/null +++ b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstaller2.json @@ -0,0 +1,48 @@ +downloadService.post('hudson.plugins.cmake.CmakeInstaller',{"list": [ + { + "id": "2.6.1", + "name": "2.6.1", + "variants": [ + { + "arch": "x86", + "os": "win32", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-win32-x86.zip" + }, + { + "arch": "sparc", + "os": "SunOS", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-SunOS-sparc.tar.gz" + }, + { + "arch": "i386", + "os": "Linux", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-Linux-i386.tar.gz" + }, + { + "arch": "n32", + "os": "IRIX64", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-IRIX64-n32.tar.gz" + }, + { + "arch": "64", + "os": "IRIX64", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-IRIX64-64.tar.gz" + }, + { + "arch": "9000_785", + "os": "HP-UX", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-HP-UX-9000_785.tar.gz" + }, + { + "arch": "universal", + "os": "Darwin", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-Darwin-universal.tar.gz" + }, + { + "arch": "powerpc", + "os": "AIX", + "url": "https://cmake.org/files/v2.6/cmake-2.6.1-AIX-powerpc.tar.gz" + } + ] + } +]}) \ No newline at end of file diff --git a/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstallerResult.json b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstallerResult.json new file mode 100644 index 0000000000..f8061acdea --- /dev/null +++ b/test/src/test/resources/hudson/model/hudson.plugins.cmake.CmakeInstallerResult.json @@ -0,0 +1,54 @@ +downloadService.post('hudson.plugins.cmake.CmakeInstaller',{"list": [ + { + "id": "3.4.2", + "name": "3.4.2", + "variants": [ + { + "arch": "x86", + "os": "win32", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-win32-x86.zip" + }, + { + "arch": "x86_64", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Linux-x86_64.tar.gz" + }, + { + "arch": "i386", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Linux-i386.tar.gz" + }, + { + "arch": "x86_64", + "os": "Darwin", + "url": "https://cmake.org/files/v3.4/cmake-3.4.2-Darwin-x86_64.tar.gz" + } + ] + }, + { + "id": "3.4.1", + "name": "3.4.1", + "variants": [ + { + "arch": "x86", + "os": "win32", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-win32-x86.zip" + }, + { + "arch": "x86_64", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Linux-x86_64.tar.gz" + }, + { + "arch": "i386", + "os": "Linux", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Linux-i386.tar.gz" + }, + { + "arch": "x86_64", + "os": "Darwin", + "url": "https://cmake.org/files/v3.4/cmake-3.4.1-Darwin-x86_64.tar.gz" + } + ] + } +]}) \ No newline at end of file -- GitLab