0

I have the following code that I'm executing from a Jenkins non-System Groovy Script using Groovy 2.4.3

private void readJsonFile() {
    if (testsValid == false) {
        def jsonSlurper = new JsonSlurper()
        try {                   
            println "FileReader("+jsonFileName+")"
            def fr = new FileReader(jsonFileName)
            println "About to slurp from FileReader "+fr
            println "Class: " + fr.getClass()
            tests = jsonSlurper.parse(fr)
        } catch (FileNotFoundException e) {
            println "Properties File Not Found: "+jsonFileName
            e.printStackTrace()
        } catch (IllegalArgumentException e) {
            println "Illegal Argument: " + e.getCause()
            e.printStackTrace()
        }
    }
}

But, when I run it I get the following exception:

FileReader(/var/lib/jenkins/workspace/2TierUtmsLog2/resources/utmsdata.json)
About to slurp from FileReader java.io.FileReader@125d92c
Class: class java.io.FileReader
Caught: java.lang.IllegalArgumentException: argument type mismatch
java.lang.IllegalArgumentException: argument type mismatch
    at JenkinsJsonTestDao.readJsonFile(JenkinsJsonTestDao.groovy:82)
    at JenkinsJsonTestDao.readJenkinsUtmsTest(JenkinsJsonTestDao.groovy:51)
    at JenkinsTestDaoIf$readJenkinsUtmsTest.call(Unknown Source)
    at JenkinsTestLogger.logTestResult(JenkinsTestLogger.groovy:32)
    at JenkinsTestLoggerIf$logTestResult.call(Unknown Source)
    at JenkinsLog.logTest(JenkinsLog.groovy:76)
    at JenkinsLog$logTest.call(Unknown Source)
    at JenkinsLog.main(JenkinsLog.groovy:44)
Build step 'Execute Groovy script' marked build as failure

The JsonSlurper.parse(Reader r) method does take a Reader, so I don't understand why this isn't working. I've also tried using the JsonSlurper.parse(String s) method, but get the same error. I've also printed out the contents that is read via the FileReader and it's correct.

Can anyone spot what I'm doing wrong, or is this another Jenkins restriction I'm running up against.

Added requested info: jsonSlurper: groovy.json.JsonSlurper@19e733e Print of new File(...) { "job": [ { "jenkins_name": "ForceAResult-fail", "utmsEnabled": "1", "utmsCycleId": "1", "utmsProejct": "Unity", "utmsTeam": "CA-SS System Software", "utmsTestName": "Force_a_test", "utmsTestPlanId": "2", "utmsTestSetId": "3", }, { "jenkins_name": "ForceAResult-pass", "utmsEnabled": "1", "utmsCycleId": "1", "utmsProejct": "Unity", "utmsTeam": "CA-SS System Software", "utmsTestName": "Force_a_test", "utmsTestPlanId": "2", "utmsTestSetId": "3", } ]}

Uncaught Exception: java.lang.IllegalArgumentException: argument type mismatch org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.IllegalArgumentException: argument type mismatch at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2699)

Added info for Divesh

[EnvInject] - Loading node environment variables. Building on master in workspace /var/lib/jenkins/workspace/Groovy Version System Groovy Version: 1.8.9 PATH: /var/lib/ccache/bin:/c4shares/auto/tools/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/accurev/bin:/opt/CmTools/shbin:/re/bin:/re/thirdparty_tools/java/tags/linux/Production/bin:/opt/pegasus/bin:/re/thirdparty_tools/ant/tags/Production/bin:/c4shares/auto/devutils/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/NX/bin

GROOVY_HOME: null

[Groovy Version] $ /var/lib/jenkins/tools/hudson.plugins.groovy.GroovyInstallation/Groovy_2.4.3/bin/groovy "/var/lib/jenkins/workspace/Groovy Version/hudson6179818785298037506.groovy" Sep 24, 2015 8:11:25 AM org.codehaus.groovy.runtime.m12n.MetaInfExtensionModule newModule WARNING: Module [groovy-nio] - Unable to load extension class [org.codehaus.groovy.runtime.NioGroovyMethods] Execute Groovy script - non-System

Start of Divesh's Request:

public java.lang.Object groovy.json.JsonSlurper.parse(byte[])
public java.lang.Object groovy.json.JsonSlurper.parse(char[])
public java.lang.Object groovy.json.JsonSlurper.parse(java.io.File)
public java.lang.Object groovy.json.JsonSlurper.parse(java.io.InputStream)
public java.lang.Object groovy.json.JsonSlurper.parse(java.io.Reader)
public java.lang.Object groovy.json.JsonSlurper.parse(java.net.URL)
public java.lang.Object groovy.json.JsonSlurper.parse(byte[],java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parse(java.io.File,java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parse(java.io.InputStream,java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parse(java.net.URL,java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parse(java.net.URL,java.util.Map)
public java.lang.Object groovy.json.JsonSlurper.parse(java.util.Map,java.net.URL)
public java.lang.Object groovy.json.JsonSlurper.parse(java.net.URL,java.util.Map,java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parse(java.util.Map,java.net.URL,java.lang.String)
public java.lang.Object groovy.json.JsonSlurper.parseText(java.lang.String)Finished: SUCCESS
Joel
  • 111
  • 13
  • Can you print out `jsonSlurper` and check `new File(jsonFileName).text` looks OK? – tim_yates Sep 22 '15 at 22:42
  • You're also never closing your reader. Try `new File(jsonFileName).withReader { fr -> ... Code... }` – tim_yates Sep 22 '15 at 22:50
  • 1
    The catch block for `IllegalArgumentException` prints "Illegal Argument:", but I do not see this string in your output. Typo? – divesh premdeep Sep 22 '15 at 23:47
  • Tim, I added the two print statements you requested. I've pasted the output below. Also, that's a good point about closing the reader. I'll be sure to add that in Divesh, I noticed that the "Illegal Argument" was not being printed out. I added a catch-all exception. The results are below too. – Joel Sep 23 '15 at 14:55
  • `jsonSlurper: groovy.json.JsonSlurper@19e733e Print of new File(...) { "job": [ { "jenkins_name": "ForceAResult-fail", .... }, { "jenkins_name": "ForceAResult-pass", .... } ]} Uncaught Exception: java.lang.IllegalArgumentException: argument type mismatch org.codehaus.groovy.runtime.InvokerInvocationException: ....` – Joel Sep 23 '15 at 14:57
  • Sorry for the poor formatting. I can't figure out how to get it right. – Joel Sep 23 '15 at 15:08
  • Add it to the question, it always looks rubbish in comments – tim_yates Sep 23 '15 at 15:36
  • Added requested info to the question – Joel Sep 23 '15 at 15:54
  • This is indeed weird; I can get your script to run on my machine. Could you run the following script (on the Jenkins box facing the problem) - `groovy -e "print (new groovy.json.JsonSlurper().getMetaClass().getMethods()*.toString().findAll{it ==~ /.*parse.*/}.join('\n'))"` – divesh premdeep Sep 23 '15 at 23:40
  • I'm trying to check if your Jenkins box is using the correct Groovy version and that the version of JsonSlurper being used indeed has a parse() method that takes a Reader as an argument. – divesh premdeep Sep 23 '15 at 23:41
  • I had to run it in a non-System Groovy script in the Jenkins job, since that's the only way to use the version of Groovy that I'm specifying. I also added information to printout the Groovy environment for both System and non-System Groovy Scripts – Joel Sep 24 '15 at 12:18
  • Just to be sure there wasn't a difference from running it in a jenkins groovy script file versus running it in a jenkins Groovy command, I created a script file with the getMetaClass() query. The results were the same. Both contained the Reader version of JsonSlurper.parse() – Joel Sep 24 '15 at 16:06
  • I took the code I was using and cut it down to just the JsonSlurper call that I was making and put it into a groovy script. This time it worked. So it's probably related to the class infrastructure I'm trying to run in the groovy script file. I'll continue investigating. When I find the answer I'll post it here. – Joel Sep 24 '15 at 20:05

0 Answers0