<?xml version="1.0"?>
<project xmlns:antcontrib="antlib:net.sf.antcontrib"
    xmlns:ivy="antlib:org.apache.ivy.ant"
    name="vaadin-server" 
	default="deploy-and-start" basedir=".">
    
	 <!-- Import common targets  -->
	<import file="../build/common.xml" />

	<target name="check-params">
		<fail unless="output-dir" message="The 'output-dir' (usually build/result/vaadin-xxx) should be given to test script." />
		<fail unless="package.name" message="The 'package.name' property must be defined." />
		<fail unless="package.filename" message="The 'package.filename' property must be defined." />
		<fail unless="testing.testarea" message="The 'testing.testarea' property must be defined." />
		<property name="webroot" value="${testing.testarea}/${package.name}/WebContent" />		
	</target>
	
    <target name="prepare-testing-area" depends="check-params">
        <echo> Package name: ${package.name}</echo>
        <echo> Package filename: ${package.filename}</echo>
        <echo> Testing area: ${testing.testarea}</echo>    
        <echo> test-output-dir: ${test-output-dir}</echo>

        <delete dir="${testing.testarea}/${package.name}" />
        <mkdir dir="${testing.testarea}/${package.name}/WebContent" />
        <echo>Extracting Vaadin package ${package.filename} to testing area ${testing.testarea}</echo>
        <unzip src="${package.filename}" dest="${testing.testarea}/${package.name}/WebContent" />   
    </target>

    <target name="deploy-and-start" depends="init-deps, prepare-testing-area">
    	<property name="webroot" value="${testing.testarea}/${package.name}/WebContent"></property>
    	
    	<ivy:resolve file="ivy.xml"/>
    	<ivy:cachepath pathid="server" conf="server" />
        <java classname="com.vaadin.launcher.DemoLauncher" fork="yes" dir="${testing.testarea}/${package.name}" resultproperty="server.start.result">
            <arg value="--nogui=1" />
            <classpath>
            	<path location="${webroot}/WEB-INF/classes" />
            	<fileset dir="${webroot}/WEB-INF/lib/" includes="*.jar" />                            
                <path refid="server" />
            </classpath>
            <jvmarg value="-Demma.coverage.out.file=${testing.testarea}/${package.name}/war.ec"/>
            <jvmarg value="-Djava.awt.headless=true"/>
        </java>
    </target>
    
    <target name="wait-for-startup">
        <echo>Waiting for Servlet Container to start up.</echo>
        <waitfor maxwait="60" maxwaitunit="second" checkevery="5" checkeveryunit="second" timeoutproperty="server.start.failed">
            <http url="http://localhost:8888"/>
        </waitfor>
        <fail if="server.start.failed" message="Server startup failed" />
    </target>
   
</project>
