2

Had a problem with retrieving returncode programmatically...following files. (did cut out some company-specific things). I got 2 files, one general file with a java-task in it and a specific file, which defines sime properties and a ant-task.

How can i get the build.rc in my executing java-test? The echo-message at the end of the file delivers the actual return-code, but the p.getProperty("build.rc") returns null.

specific.build.xml

<project basedir="../../../.." name="run-joblauncher">
     <property name="prop1" value="valProp1" />
     <ant antfile="./dev/script/general.xml" dir="${basedir}" target="RUN-MYCLASS" inheritAll="true"/>
</project>

general.xml

<project basedir="../../../.." default="RUN-MYCLASS" name="run-specifictest"> <target name="RUN-MYCLASS"> 
     <property name="returnCode" value="99"/>
     <target name="RUN-MYCLASS">            
          <java classname="my.company.class" fork="true" resultproperty="build.rc" failonerror="false">
                <arg value="${workdir}"/>
                <classpath>
                     <pathelement path="${java.class.path}"/>
                </classpath>
          </java>   
      </target> 
</project>

myJunitTest.java

public class TestAntScripts extends TestCase {

public void testMyAnt() throws IOException {

    File dir = new File("pathToSpecific.xml");

    Project p = null;
    try {
        File buildFile = new File(buildFileName);
        p = new Project();
        DefaultLogger consoleLogger = new DefaultLogger();
        consoleLogger.setErrorPrintStream(System.err);
        consoleLogger.setOutputPrintStream(System.out);
        consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
        p.addBuildListener(consoleLogger);
        p.addBuildListener(new CustomBuildListener(this));
        p.fireBuildStarted();
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.fireBuildFinished(null);
    } catch (BuildException e) {
        p.fireBuildFinished(e);
        e.printStackTrace();
        System.err.println("AntRunner for buildfile " + buildFileName
                + " failed with Exception ");
    }

    String rc = p.getProperty("returnCode");
    assertEquals(0, Integer.parseInt(rc));

    }


public void taskFinished(String buildRc) {
    assertEquals(0, buildRc);   
    }
}

EDIT:

how's the approach to assign a value to a already defined property? defined a var returnCode outside the java-target and try to assign the value like <var name="returnCode" value="${build.rc}"/> inside the target. not runnin'..

EDIT2: added a custom BuildListener and grabbing the build.rc over there.

  @Override
  public void targetFinished(BuildEvent event) {

    if (event.getTarget().getName().trim().equalsIgnoreCase("RUN-MYCLASS")) {
        String buildRc = event.getTarget().getProject().getProperty("build.rc");    
        tester.taskFinished(buildRc, jobname);
    }

tester is my JUnit-Test, which will be set in Constructor.

maxstreifeneder
  • 625
  • 1
  • 8
  • 19

2 Answers2

1

added a custom BuildListener and grabbing the build.rc over there.

@Override
public void targetFinished(BuildEvent event) {

if (event.getTarget().getName().trim().equalsIgnoreCase("RUN-MYCLASS")) {
    String buildRc = event.getTarget().getProject().getProperty("build.rc");    
    tester.setReturnCode(buildRc);
}

and then testing like

assertEquals(0, Integer.parseInt(this.returnCode));
maxstreifeneder
  • 625
  • 1
  • 8
  • 19
0

The ant manual mentions that

Property scopes exist at Apache Ant's various "block" levels. These include targets as well as the Parallel and Sequential task containers (including Macrodef bodies).

Pretty sure that build.rc is local and only valid in the target scope. So we can't access it after the target has been executed.

Try to define a global property (with the <property> element) and assign the result code from the java task to that property.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • ain'get macrodef neither unset running :( can you post any complete code of the according file? (general.xml or specific.xml..) – maxstreifeneder Aug 02 '12 at 14:35