<?xml version="1.0" encoding="UTF-8"?>

<project name="PEtALS integration EIP component standalone tests" default="prepare" basedir=".">

	<path id="client.classpath">
		<pathelement path="${classpath}" />
		<fileset dir="client">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<path id="ant.classpath">
		<pathelement path="${classpath}" />
		<fileset dir="lib">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<!-- Petals JMX RMI port and hosts -->
	<property name="host1.port" value="7700" />
	<property name="host1.ip" value="localhost" />
	<property name="host1.username" value="petals"/>
	<property name="host1.password" value="petals"/>

	<!-- Define path -->
	<condition property="basepath" value="file:///">
		<and>
			<or>
				<os family="windows" />
				<os family="dos" />
			</or>
		</and>
	</condition>
	<condition property="basepath" value="file://">
		<os family="unix" />
	</condition>

	<taskdef resource="org/ow2/petals/ant/petals-ant.properties" classpathref="ant.classpath" loaderref="ant.classpath.loader" />

	<!-- Targets -->
	<target name="prepare">
		<echo message="Preparing EIP component standalone tests... " />
		<unzip src="deployables/petals-se-rmi.zip" dest="deployables/petals-se-rmi_temp"/>
		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<rmi:componentcontextname>RMIComponentContext</rmi:componentcontextname>]]></replacetoken>
			<replacevalue><![CDATA[<rmi:componentcontextname>RMIComponentContext_EIP_1</rmi:componentcontextname>]]></replacevalue>
		</replace>
		<zip basedir="deployables/petals-se-rmi_temp" destfile="deployables/petals-se-rmi.zip" />

		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<name>petals-se-rmi</name>]]></replacetoken>
			<replacevalue><![CDATA[<name>petals-se-rmi2</name>]]></replacevalue>
		</replace>
		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<rmi:embeddedregistry>true</rmi:embeddedregistry>]]></replacetoken>
			<replacevalue><![CDATA[<rmi:embeddedregistry>false</rmi:embeddedregistry>]]></replacevalue>
		</replace>
		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<rmi:componentcontextname>RMIComponentContext_EIP_1</rmi:componentcontextname>]]></replacetoken>
			<replacevalue><![CDATA[<rmi:componentcontextname>RMIComponentContext_EIP_2</rmi:componentcontextname>]]></replacevalue>
		</replace>
		<zip basedir="deployables/petals-se-rmi_temp" destfile="deployables/petals-se-rmi2.zip" />

		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<name>petals-se-rmi2</name>]]></replacetoken>
			<replacevalue><![CDATA[<name>petals-se-rmi3</name>]]></replacevalue>
		</replace>
		<replace file="deployables/petals-se-rmi_temp/META-INF/jbi.xml">
			<replacetoken><![CDATA[<rmi:componentcontextname>RMIComponentContext_EIP_2</rmi:componentcontextname>]]></replacetoken>
			<replacevalue><![CDATA[<rmi:componentcontextname>RMIComponentContext_EIP_3</rmi:componentcontextname>]]></replacevalue>
		</replace>
		<zip basedir="deployables/petals-se-rmi_temp" destfile="deployables/petals-se-rmi3.zip" />

		<delete dir="deployables/petals-se-rmi_temp" />
	</target>

	<target name="deploy">
		<echo message="Installing components..." />
		<parallel>
			<jbi-install-component file="${basepath}${basedir}/deployables/petals-se-rmi.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-install-component file="${basepath}${basedir}/deployables/petals-se-rmi2.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-install-component file="${basepath}${basedir}/deployables/petals-se-rmi3.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-install-component file="${basepath}${basedir}/deployables/petals-se-eip.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
		</parallel>

		<echo message="Starting components..." />
		<!-- Start first the first RMI component to start the RMI registry and wait 1 second -->
		<jbi-start-component name="petals-se-rmi" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
		<parallel>
			<jbi-start-component name="petals-se-rmi2" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-start-component name="petals-se-rmi3" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-start-component name="petals-se-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
		</parallel>

		<echo message="Deploying service assemblies..." />
		<parallel>
			<jbi-deploy-service-assembly file="${basepath}${basedir}/deployables/sa-eip.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}"
 password="${host1.password}" />
			<jbi-deploy-service-assembly file="${basepath}${basedir}/deployables/sa-eip-integration.zip" port="${host1.port}" host="${host1.ip}" username="${host1.username}"
 password="${host1.password}" />
		</parallel>

		<echo message="Starting service assemblies..." />
		<parallel>
			<jbi-start-service-assembly name="sa-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
			<jbi-start-service-assembly name="sa-eip-integration" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" />
		</parallel>
	</target>


	<target name="run">
		<echo message="Launching EIP component standalone tests..." />
		<parallel>
			<!--Start first providers then consumers-->
			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result12">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration2.xml' -properties '${basepath}${basedir}/resources/rmi2.properties'"/>
			</java>

			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result13">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration3.xml' -properties '${basepath}${basedir}/resources/rmi3.properties'"/>
			</java>
			<sequential>
				<sleep seconds="3" />
				<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result11">
					<classpath refid="client.classpath" />
					<arg line="-integration '${basepath}${basedir}/resources/integration1.xml' -properties '${basepath}${basedir}/resources/rmi1.properties'"/>
				</java>
			</sequential>
		</parallel>
		<parallel>
			<!--Start first providers then consumers-->
			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result22">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration2.xml' -properties '${basepath}${basedir}/resources/rmi2.properties'"/>
			</java>

			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result23">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration3.xml' -properties '${basepath}${basedir}/resources/rmi3.properties'"/>
			</java>
			<sequential>
				<sleep seconds="3" />
				<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result21">
					<classpath refid="client.classpath" />
					<arg line="-integration '${basepath}${basedir}/resources/integration1.xml' -properties '${basepath}${basedir}/resources/rmi1.properties'"/>
				</java>
			</sequential>
		</parallel>
		<parallel>
			<!--Start first providers then consumers-->
			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result32">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration2.xml' -properties '${basepath}${basedir}/resources/rmi2.properties'"/>
			</java>

			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result33">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration3.xml' -properties '${basepath}${basedir}/resources/rmi3.properties'"/>
			</java>
			<sequential>
				<sleep seconds="3" />
				<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result31">
					<classpath refid="client.classpath" />
					<arg line="-integration '${basepath}${basedir}/resources/integration1.xml' -properties '${basepath}${basedir}/resources/rmi1.properties'"/>
				</java>
			</sequential>
		</parallel>
		<parallel>
			<!--Start first providers then consumers-->
			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result42">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration2.xml' -properties '${basepath}${basedir}/resources/rmi2.properties'"/>
			</java>

			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result43">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration3.xml' -properties '${basepath}${basedir}/resources/rmi3.properties'"/>
			</java>
			<sequential>
				<sleep seconds="3" />
				<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result41">
					<classpath refid="client.classpath" />
					<arg line="-integration '${basepath}${basedir}/resources/integration1.xml' -properties '${basepath}${basedir}/resources/rmi1.properties'"/>
				</java>
			</sequential>
		</parallel>
		<parallel>
			<!--Start first providers then consumers-->
			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result52">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration2.xml' -properties '${basepath}${basedir}/resources/rmi2.properties'"/>
			</java>

			<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result53">
				<classpath refid="client.classpath" />
				<arg line="-integration '${basepath}${basedir}/resources/integration3.xml' -properties '${basepath}${basedir}/resources/rmi3.properties'"/>
			</java>
			<sequential>
				<sleep seconds="3" />
				<java classname="com.ebmwebsoucing.integration.client.rmi.RMIClient" fork="true" resultproperty="result51">
					<classpath refid="client.classpath" />
					<arg line="-integration '${basepath}${basedir}/resources/integration1.xml' -properties '${basepath}${basedir}/resources/rmi1.properties'"/>
				</java>
			</sequential>
		</parallel>
		<fail message="EIP component integration-test 11 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result11}" />
						<!--For ant 1.7 only <isfailure code="${result11}"/> -->
					</and>
				</not>
			</condition>
		</fail>

		<fail message="EIP component integration-test 12 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result12}" />
						<!--For ant 1.7 only <isfailure code="${result12}"/> -->					
					</and>
				</not>
			</condition>
		</fail>

		<fail message="EIP component integration-test 13 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result13}" />
						<!--For ant 1.7 only <isfailure code="${result13}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		
		<fail message="EIP component integration-test 21 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result21}" />
						<!--For ant 1.7 only <isfailure code="${result21}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		
		<fail message="EIP component integration-test 22 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result22}" />
						<!--For ant 1.7 only <isfailure code="${result22}"/> -->
					</and>
				</not>
			</condition>
		</fail>
						
		<fail message="EIP component integration-test 23 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result23}" />
						<!--For ant 1.7 only <isfailure code="${result23}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		
		<fail message="EIP component integration-test 31 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result31}" />
						<!--For ant 1.7 only <isfailure code="${result31}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 32 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result32}" />
						<!--For ant 1.7 only <isfailure code="${result32}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 33 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result33}" />
						<!--For ant 1.7 only <isfailure code="${result33}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 41 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result41}" />
						<!--For ant 1.7 only <isfailure code="${result41}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 42 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result42}" />
						<!--For ant 1.7 only <isfailure code="${result42}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 43 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result43}" />
						<!--For ant 1.7 only <isfailure code="${result43}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 51 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result51}" />
						<!--For ant 1.7 only <isfailure code="${result51}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 52 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result52}" />
						<!--For ant 1.7 only <isfailure code="${result52}"/> -->
					</and>
				</not>
			</condition>
		</fail>
		<fail message="EIP component integration-test 53 failed!">
			<condition>
				<not>
					<and>
						<equals arg1="0" arg2="${result53}" />
						<!--For ant 1.7 only <isfailure code="${result53}"/> -->
					</and>
				</not>
			</condition>
		</fail>
	</target>

	<target name="cleanAll">
			<echo message="Cleaning Mail component tests..." />

			<petals-stop-all-service-assemblies port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />
			<petals-shut-down-all-service-assemblies port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />
			<petals-undeploy-all-service-assemblies force="true" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />

			<petals-stop-all-components port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />
			<petals-shut-down-all-components port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />
			<petals-uninstall-all-components port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />

			<petals-unload-all-installers port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="true" />
	</target>
	
	<target name="clean">
		<echo message="Cleaning EIP component tests..." />

		<parallel>
			<jbi-stop-service-assembly name="sa-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-stop-service-assembly name="sa-eip-integration" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
		</parallel>
		<parallel>
			<jbi-shut-down-service-assembly name="sa-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-shut-down-service-assembly name="sa-eip-integration" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
		</parallel>
		<parallel>
			<jbi-undeploy-service-assembly name="sa-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-undeploy-service-assembly name="sa-eip-integration" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
		</parallel>
		<parallel>
			<jbi-stop-component name="petals-se-rmi" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-stop-component name="petals-se-rmi2" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-stop-component name="petals-se-rmi3" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-stop-component name="petals-se-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />	
		</parallel>
		<parallel>
			<jbi-shut-down-component name="petals-se-rmi" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-shut-down-component name="petals-se-rmi2" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-shut-down-component name="petals-se-rmi3" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-shut-down-component name="petals-se-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
		</parallel>
		<parallel>
			<jbi-uninstall-component name="petals-se-rmi" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-uninstall-component name="petals-se-rmi2" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-uninstall-component name="petals-se-rmi3" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
			<jbi-uninstall-component name="petals-se-eip" port="${host1.port}" host="${host1.ip}" username="${host1.username}" password="${host1.password}" failOnError="false" />
		</parallel>
	</target>

	<target name="all">
		<antcall target="cleanAll" />
		<antcall target="prepare"/>
		<antcall target="deploy"/>
		<antcall target="run"/>
		<antcall target="clean"/>
		<echo message="All task finished!" />
	</target>
</project>
