Archive

Archive for the ‘Groovy’ Category

Groovy – Easy XML Parsing with the XmlSlurper

August 4, 2014 Leave a comment

Groovy comes built-in with a cool feature for parsing XML, and easily “objectifying” it.
Its a one-liner:

def document = new XmlSlurper().parseText(xml); // Convert the XML into an Object Model

Usage example

import groovy.util.*;

public class GroovyXmlSlurperExample
{
   public static void main(String[] args)
   {
      def xml = "<root>\n\t<item name=\"a\" value=\"1\"/>\n\t<item name=\"b\" value=\"2\"/>\n</root>";
      System.out.println("XML is: \n" + xml);

      def document = new XmlSlurper().parseText(xml); // Convert the XML into an Object Model

      System.out.println("\nElements are:");
      for (item in document.item)
      {
         System.out.println(item.@name.text() + "=" + item.@value.text());
      }
        
      System.out.println("\nJust the value of the 'value' attributes: " + document."item".@value.text());
   }
}

Explanation

Line #10 – parses the XML from a String to an actual object.
Line #13 – iterates over all the elements of type “item” under the root.
Line #15 – shows how to get the value of attributes for a certain element, the ‘@name’ syntax refers to an attribute named “name” (... name="a" ...)

Further Reading & Experiments

Categories: Groovy, XML Tags: , , , , ,

Groovy – a cool problem

May 25, 2011 Leave a comment

So I got a small groovy script to maintain. This script is executed as part of a SoapUI project.
The target of the script was simple – iterate 5 times and execute a test-case.
Here was the script:

for( i in 1..5)
{
 log.info("Running Authentication - iteration <" + i + ">.")
 def step = testRunner.testCase.getTestStepByName( "authentication-fail" );
 step.run(testRunner, context);
}

The output was very simple:

Running Authentication - iteration <1>
Running Authentication - iteration <2>
Running Authentication - iteration <3>
Running Authentication - iteration <4>
Running Authentication - iteration <5>

But then I was asked to make ‘5‘ configurable, so I defined it as a parameter and made the following change to the code:

def maxCallsBeforeLock = testRunner.testCase.getPropertyValue("MaxCallsBeforeLock")
for( i in 1..maxCallsBeforeLock)
{
 log.info("Running Authentication - iteration <" + i + ">.")
 def step = testRunner.testCase.getTestStepByName( "authentication-fail" );
 step.run(testRunner, context);
}

Unfortunately, the result was not as I expected:

Running Authentication - iteration <1>
Running Authentication - iteration <2>
...
Running Authentication - iteration <52>
Running Authentication - iteration <53>

Since I’m familiar with the ASCII Code I immediately recognized that 53 is the ASCII code of ‘5‘ and the solution became apparent that I need to convert my fake integer to a real integer. So the revision to the code was simply to replace this:

def maxCallsBeforeLock = testRunner.testCase.getPropertyValue("MaxCallsBeforeLock")

With this:

def maxCallsBeforeLock = testRunner.testCase.getPropertyValue("MaxCallsBeforeLock").toInteger()

Which solved the problem.

Now I had just one thing left to do – to understand why this happened.
53 is the ASCII code of the character ‘5‘. It is also the hashCode of the string “5“.
After some investigation and the help of friends on Stack Overflow I got the answer that Strings with the length of 1 are converted to a single character. So when trying to use them in a range, the numerical value of the corresponding character is used.

Categories: Encoding, Groovy Tags:
%d bloggers like this: