Java? Come on! :)

I’m working on a patch for Maven Release. Now Brett asked me to write an integration test, to prove I’m actually fixing something.

The project uses Maven Verifier, which runs a pom and then lets you test whether it did the right thing or not by checking the build log.

Check, if a file has a string in it

I copied this java-code (verify.bsh) from a similar integration test. It reads the build.log and checks if it contains some string.

import java.io.*;
import java.util.*;
import java.util.regex.*;

try
{   
    File buildLog = new File( basedir, "build.log" );
    
    System.out.println( "Checking logs.." );
    
    StringBuffer data = new StringBuffer( 1024 );
    BufferedReader reader = new BufferedReader( new FileReader( buildLog ) );
    char[] buf = new char[1024];
    int numRead = 0;
    while ( ( numRead = reader.read( buf ) ) != -1 )
    {
        String readData = String.valueOf( buf, 0, numRead );
        data.append( readData );
        buf = new char[1024];
    }
    reader.close();
    String contents = data.toString();

    String expected = "Executing goals 'clean verify' with arguments '-P profile-in-parent,it-repo'";
    
    if( contents.indexOf( expected ) != -1 )
    {
        return true;
    }            
}
catch( Throwable t )
{
    t.printStackTrace();
    return false;
}

System.out.println( "FAILED!" );
return false;

I now replaced this with following groovy code (verify.groovy):

expected = "[INFO] Executing goals 'clean verify' with arguments '-P it-repo,profile-in-parent'"

println "Expects \"$expected\" in build.log"
println "Checking logs..."

found = new File(basedir, "build.log" )
            .text
            .contains(expected)

println found ? "GOOD!" : "FAILED!"

return found

Happy coding!

Advertisements

antExec – Executing external processes in Groovy

Groovy is a nice language. I currently use it for writing maven plugins and for doing scripting tasks in the realm of build automation.

Poor mans integration with .NET or other systems is the command-line. Groovy has built-in support for that:

"myprog".execute()

And if you need parameters:

["cmd", "/C", "cd"].execute()

But there are some problems with how to wait for the process and retrieve the streams. Often you just want the output back – and you want to make sure that the command exited with no error.

There is also a ant-integration in groovy. So I wrote a little wrapper that makes using ant’s exec-task feasible:

mixinAntExec() // has to be called once
// prints the stdout
// if the error code is != 0, 
// it throws an exeption with 
// helpful information
println ("myprog".antExec())

def result = ["myprog", "/fail"].antExec(false)
println (result.exitValue) // error code
println (result.error) // err out
println (result.text) // std out

This script can either be pasted to the bottom, or included on the script path:

def mixinAntExec() {
    String.metaClass.antExec = {failOnError=true -> antExec([delegate], failOnError)}
    ArrayList.metaClass.antExec << {failOnError=true -> antExec(delegate, failOnError)}
}

def antExec(command, failOnError=true) {
    def commandLiteral = command.collect{it.contains(' ') ? '"' + it + '"' : it}.join(' ')
    
    println "ant exec with: ${commandLiteral}"
    
    def ant = new AntBuilder()
    ant.exec(outputproperty:"text",
             errorproperty: "error",
             resultproperty: "exitValue",
             failonerror: false,
             executable: command[0]) {
               if (command.size()>1)
                   arg(line:command[1..-1].collect{it.contains(' ') ? '"' + it + '"' : it}.join(' '))
             }
         
     def result = new Expando(
         text: ant.project.properties.text,
         error: ant.project.properties.error,
         exitValue: ant.project.properties.exitValue as Integer,
         toString: {text}
     )
     
     if (failOnError && result.exitValue != 0){
         throw new Exception("""command failed with ${result.exitValue}
executed: ${commandLiteral}
error: ${result.error}
text: ${result.text}""")

     }
     
     return result
}