<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is imported in the archetype pom.xml files for integration tests -->
<project name="plugin-test" default="test-all">

    <property name="version" value="2.0-m5-SNAPSHOT"/>
    <property name="archetype" value="${archetype}"/>
    <property name="test.dir" value="${basedir}/target"/>

    <target name="test-jsf-basic" description="tests plugin with jsf basic archetype">
        <create-gen-basic archetype="appfuse-basic-jsf" name="basic-jsf"/>
    </target>

    <target name="test-spring-basic" description="tests plugin with spring basic archetype">
        <create-gen-basic archetype="appfuse-basic-spring" name="basic-spring"/>
    </target>

    <target name="test-struts-basic" description="tests plugin with struts basic archetype">
        <create-gen-basic archetype="appfuse-basic-struts" name="basic-struts"/>
    </target>

    <target name="test-tapestry-basic" description="tests plugin with tapestry basic archetype">
        <create-gen-basic archetype="appfuse-basic-tapestry" name="basic-tapestry"/>
    </target>

    <target name="test-basic" description="Runs all basic archetype tests using Ant">
        <antcall target="test-jsf-basic"/>
        <antcall target="test-spring-basic"/>
        <antcall target="test-struts-basic"/>
        <antcall target="test-tapestry-basic"/>
    </target>

    <target name="test-jsf-modular" description="tests plugin with jsf modular archetype">
        <create-gen-modular archetype="appfuse-modular-jsf" name="modular-jsf"/>
    </target>

    <target name="test-spring-modular" description="tests plugin with spring modular archetype">
        <create-gen-modular archetype="appfuse-modular-spring" name="modular-spring"/>
    </target>

    <target name="test-struts-modular" description="tests plugin with struts modular archetype">
        <create-gen-modular archetype="appfuse-modular-struts" name="modular-struts"/>
    </target>

    <target name="test-tapestry-modular" description="tests plugin with tapestry modular archetype">
        <create-gen-modular archetype="appfuse-modular-tapestry" name="modular-tapestry"/>
    </target>

    <target name="test-modular" description="Runs all modular archetype tests using Ant">
        <antcall target="test-jsf-modular"/>
        <antcall target="test-spring-modular"/>
        <antcall target="test-struts-modular"/>
        <antcall target="test-tapestry-modular"/>
    </target>

    <target name="test-all" depends="test-basic,test-modular"/>

    <macrodef name="create-gen-basic">
        <attribute name="archetype"/>
        <attribute name="version" default="${version}"/>
        <attribute name="name"/>

        <sequential>
            <delete dir="${test.dir}/@{name}"/>
            <mkdir dir="${test.dir}"/>
            <maven dir="${test.dir}" archetype="@{archetype}" version="@{version}" name="@{name}"/>

            <!-- copy Person.java to src -->
            <copy todir="${test.dir}/@{name}/src/main/java/com">
                <fileset dir="src/test/java/com" includes="**/*.java"/>
            </copy>

            <!-- add Person to hibernate.cfg.xml -->
            <replace file="${test.dir}/@{name}/src/main/resources/hibernate.cfg.xml">
                <replacetoken><![CDATA[<mapping class="org.appfuse.model.Role"/>]]></replacetoken>
                <replacevalue><![CDATA[<mapping class="org.appfuse.model.Role"/>
        <mapping class="com.company.model.Person"/>]]></replacevalue>
            </replace>

            <!-- run gen install integration-test -->
            <maven dir="${test.dir}/@{name}" command="appfuse:gen -Dentity=Person"/>
            <maven dir="${test.dir}/@{name}" command="appfuse:install -Dentity=Person"/>
            <maven dir="${test.dir}/@{name}" command="integration-test"/>

            <!-- test w/o generic core -->
            <maven dir="${test.dir}/@{name}" command="appfuse:gen -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}" command="appfuse:install -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}" command="integration-test"/>
        </sequential>
    </macrodef>

    <macrodef name="create-gen-modular">
        <attribute name="archetype"/>
        <attribute name="version" default="${version}"/>
        <attribute name="name"/>

        <sequential>
            <delete dir="${test.dir}/@{name}"/>
            <mkdir dir="${test.dir}"/>
            <maven dir="${test.dir}" archetype="@{archetype}" version="@{version}" name="@{name}"/>

            <!-- copy Person.java to core/src -->
            <copy todir="${test.dir}/@{name}/core/src/main/java/com">
                <fileset dir="src/test/java/com" includes="**/*.java"/>
            </copy>

            <!-- add Person to hibernate.cfg.xml -->
            <replace file="${test.dir}/@{name}/core/src/main/resources/hibernate.cfg.xml">
                <replacetoken><![CDATA[<mapping class="org.appfuse.model.Role"/>]]></replacetoken>
                <replacevalue><![CDATA[<mapping class="org.appfuse.model.Role"/>
        <mapping class="com.company.model.Person"/>]]></replacevalue>
            </replace>

            <maven dir="${test.dir}/@{name}" command="install -N"/>
            
            <!-- gen and test core -->
            <maven dir="${test.dir}/@{name}/core" command="appfuse:gen -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}/core" command="appfuse:install -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}/core" command="install"/>

            <!-- copy hibernate.cfg.xml to web project (todo: read from core.jar) -->
            <copy file="${test.dir}/@{name}/core/src/main/resources/hibernate.cfg.xml"
                  todir="${test.dir}/@{name}/web/src/main/resources"/>

            <!-- get and test web -->
            <maven dir="${test.dir}/@{name}/web" command="appfuse:gen -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}/web" command="appfuse:install -Dentity=Person -Damp.genericCore=false"/>
            <maven dir="${test.dir}/@{name}/web" command="install"/>
        </sequential>
    </macrodef>

    <target name="test-basic-source" description="Tests extracting source into basic archetypes">
        <antcall target="test-jsf-basic-source"/>
        <antcall target="test-spring-basic-source"/>
        <antcall target="test-struts-basic-source"/>
        <antcall target="test-tapestry-basic-source"/>
    </target>

    <target name="test-jsf-basic-source" description="tests full-source plugin with jsf basic archetype">
        <full-source-basic archetype="appfuse-basic-jsf" name="basic-jsf-source"/>
    </target>

     <target name="test-spring-basic-source" description="tests full-source plugin with spring basic archetype">
        <full-source-basic archetype="appfuse-basic-spring" name="basic-spring-source"/>
    </target>

     <target name="test-struts-basic-source" description="tests full-source plugin with struts basic archetype">
        <full-source-basic archetype="appfuse-basic-struts" name="basic-struts-source"/>
    </target>

     <target name="test-tapestry-basic-source" description="tests full-source plugin with tapestry basic archetype">
        <full-source-basic archetype="appfuse-basic-tapestry" name="basic-tapestry-source"/>
    </target>
    
    <macrodef name="full-source-basic">
        <attribute name="archetype"/>
        <attribute name="version" default="${version}"/>
        <attribute name="name"/>

        <sequential>
            <delete dir="${test.dir}/@{name}"/>
            <mkdir dir="${test.dir}"/>
            <maven dir="${test.dir}" archetype="@{archetype}" version="@{version}" name="@{name}"/>

            <!-- run full-source integration-test -->
            <maven dir="${test.dir}/@{name}" command="appfuse:full-source"/>
            <maven dir="${test.dir}/@{name}" command="integration-test"/>
        </sequential>
    </macrodef>

    <macrodef name="maven">
        <attribute name="dir"/>
        <attribute name="name" default=""/>
        <attribute name="archetype" default=""/>
        <attribute name="version" default=""/>
        <attribute name="command" default="archetype:create -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=@{archetype} -DarchetypeVersion=@{version} -DgroupId=com.company -DartifactId=@{name}"/>

        <sequential>
            <exec dir="@{dir}" executable="mvn.bat" os="Windows XP" failonerror="true">
                <arg line="@{command}"/>
            </exec>
            <exec dir="@{dir}" executable="mvn" os="Mac OS X" failonerror="true">
                <arg line="@{command}"/>
            </exec>
            <exec dir="@{dir}" executable="mvn" os="Linux" failonerror="true">
                <arg line="@{command}"/>
            </exec>
        </sequential>
    </macrodef>
</project>
