2

I have some test cases in ant and each one of them pings an URL, I have a java class which is doing the work of pinging the URL and taking care of the things I need for my test case and I am compiling and running that java class from my ant build file.

The URL is different for all the test cases so I want to pass the URL from the ant file to the java class but I am having trouble doing that, does anyone have any suggestions how can I pass a variable from ant to an java class. Right now I am passing the URL in the java file itself for one test case but I don't want to create different java files for all the test cases just for a different URL each time.

This is my testcase 1 target which calls ping target:

<target name="01">
    <description>
    @testlogic.group type="category" values="mats"
    Incremental response data stress test, multi-thread model
    </description>
    <antcall target="pingOHSJava">
    <param name="out.log" value="tiapwt01.log"/>
    <param name="ohs.name" value="ohs1"/>
    <param name="url" value="/waiter/servlet/ServletStressMulti?mode=1"/>
    <param name="para1" value="1"/>  
    <param name="para2" value="10"/>
    <param name="para3" value="100"/>
    <param name="para4" value="10"/>
    </antcall>

    <fail message="test FAILED found.. failing">
        <condition>
            <resourcecontains resource="${tiapwt.base.dir}/common/output/tiapwt_content.txt" substring="FAILED"/>
        </condition>
    </fail> 
</target>

This is the ping target:

<target name="pingOHSJava">
    <property name="srcdir" location="${functional.base.dir}/tiapwt/common/java" />
    <path id="classpath.test">
        <pathelement location="/scratch/skaneria/Dwnld/junit-4.10.jar" />
    </path>
    <property name="lib.dir" value="../common/jars" />
    <taskdef resource="org/testlogic/toolkit/asserts/antlib.xml">
        <classpath>
            <fileset dir="${lib.dir}" includes="tlt-asserts-0.3.jar" />
        </classpath>
    </taskdef>

    <delete verbose="true">
            <fileset dir="${srcdir}" includes="**/*.class" />
        </delete>
    <property name="urlPing" value="http://${ADMIN_HOST}:${APACHE_PORT}${url}"/>
    <trycatch property="pingOutput">
    <try>
    <javac srcdir="${srcdir}" destdir="${srcdir}">
        <classpath refid="classpath.test"/>
        </javac>
    <java classname="HttpURLConnectionExample" fork="true" failonerror="true">
    <classpath path="${srcdir}"/>

    </java>
    </try>
    <finally>
     <echo file="${twork.dir}/${out.log}" append="false">
URL: ${urlPing}
parameter 1 : mode=${para1}
parameter 2 : iterations=${para2}
parameter 3 : initial=${para3}
parameter 4 : increment=${para4}
------------------------------------------
${pingOutput}
       </echo>
    </finally>

    </trycatch>

    </target>

And this is a snippet from the java class which uses the URL:

private void sendGet() throws Exception {

        String url = "http://slc06mwj.us.oracle.com:7777/waiter/servlet/ServletStressMulti?mode=1&iterations=10&initial=100&increment=10000";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);
        try{
        int responseCode = con.getResponseCode();

        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        System.out.println("Response Message : " + con.getResponseMessage());
        } catch(Exception e){
            System.out.println("error");
        }
4
  • Could you post your code here Commented Nov 14, 2014 at 20:42
  • possible duplicate of Passing command line arguments to Java via ant build script Commented Nov 14, 2014 at 21:37
  • ya I already went through that question and it's answers but it doesn't answer how to use the variable which has been passed from ant target in the java class. Commented Nov 14, 2014 at 21:44
  • Thanks. I tried using the answer from the above link it's working. Commented Nov 14, 2014 at 22:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.