<?xml version="1.0"?>

<project name="junit examples" default="help" >

  <property name="build.sysclasspath" value="ignore"/>
  <property name="junit.timeout" value="180000"/> <!-- 3 minutes -->

  <property file="./build.properties"/>

   <!-- Test if JDK5 is available -->
   <available classname="java.lang.Enum" property="HAVE_JDK_1.5"/>

  <!-- set local properties for this build -->
   <path id="lib.classpath">
       <fileset dir="${lib}">
         <include name="**/*.jar" />
       </fileset>
   </path>

   <path id="lib.classpath.50">
      <fileset dir="${lib}">
        <include name="**/*.jar" />
         <exclude name="jboss-cache.jar"/>
         <exclude name="jboss-aop.jar"/>
      </fileset>

      <fileset dir="${lib-50}">
        <include name="**/*.jar" />
      </fileset>
   </path>

   <!-- Used for aop classloading and aopc -->
   <path id="aop.classpath">
       <fileset dir="${lib}">
          <include name="*.jar" />
          <exclude name="jboss-cache.jar" />
          <exclude name="**/j*unit.jar" />
          <!-- exclude name="**/jgroups.jar" /-->
          <exclude name="**/bsh*.jar" />
       </fileset>
   </path>

   <path id="cache.classpath">
       <fileset dir="${lib}">
         <include name="**/jboss-cache.jar" />
       </fileset>
   </path>

   <path id="cache.classpath.50">
       <fileset dir="${lib-50}">
         <include name="**/jboss-cache.jar" />
       </fileset>
   </path>

   <path id="build.classpath">
       <pathelement path="${build}"/>
   </path>

   <path id="build.classpath.50">
      <!-- NOTE that the order of build-50 nd build is IMPORTANT!!!
      This is because we have duplicated org.jboss.cache.aop.test (one for 50 and one
      for 1.4)
      -->
      <pathelement path="${build-50}"/>
      <pathelement path="${build}"/>
   </path>

  <target name="help" description="help page">
      <echo message="build.sh &lt;command&gt; where command is:" />
      <echo message="         compile       -- compile the examples code" />
      <echo message="         clobber       -- clean up the whole directory" />
      <echo message="         run.batch     -- run batch examples" />
      <echo message="         one.test      -- run one single test case. Need -Dtest=org.jboss.cache.??Test" />
     <echo message="         one.test.aop  -- run one single aop test case. Need -Dtest=org.jboss.cache.aop.??Test" />
     <echo message="         one.test.aop50  -- run one single aop test case using JDK50 annotation. Need -Dtest=org.jboss.cache.aop.??Test" />
      <echo message="         run.aop.batch -- run batch aop examples" />
      <echo message="         run.aopc.batch -- run batch aop examples with annoc and aopc pre-compiler" />
      <echo message="         run.demo      -- run demo gui for plain cache" />
      <echo message="         run.aop.demo  -- run demo gui for aop cache" />
  </target>

  <!-- set up directory -->
  <target name="init" description="Prepare to build.">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile
    and copy the deployment descriptors into it-->
     <mkdir dir="${build}"/>
     <mkdir dir="${build-50}"/>
    <mkdir dir="${reports}"/>
  </target>

  <target name="clean" description="Clean up compiled classes">
    <delete dir="${output}"/>
  </target>

  <target name="clobber" depends="clean" />

  <!-- compile directory -->
   <target name="compile" depends="compile-14, compile-50" description="Compiles all Java files">
   </target>

   <target name="compile-14" depends="init" description="Build unit testing class">
     <javac srcdir="${source}"
            destdir="${build}"
            includes="org/jboss/**"
            target="1.4"
            source="1.4"
            debug="true"
            deprecation="true">
       <classpath>
         <path refid="lib.classpath" />
       </classpath>
     </javac>

	  <copy todir="${build}">
	    <fileset dir="${source}">
	       <include name="**/*.clazz"/>
	    </fileset>
	  </copy>
      <copy todir="${output}/etc">
        <fileset dir="${etc}">
           <include name="**/*"/>
        </fileset>
      </copy>
      <copy todir="${output}/resources">
        <fileset dir="${resources}">
           <include name="**/*"/>
        </fileset>
      </copy>
   </target>

   <target name="compile-50" depends="init" description="Build unit testing class for JDK50" if="HAVE_JDK_1.5">
     <javac srcdir="${source-50}"
            destdir="${build-50}"
            includes="org/jboss/**"
            debug="true"
            deprecation="true">
       <classpath>
         <path refid="lib.classpath.50" />
       </classpath>
     </javac>
   </target>

   <!-- eg. ant run.examples -Dtest=org.jboss.test.cache.test.local.NoTxUnitTestCase -->
   <target name="one.test" depends="compile" description="run one junit test case.">
     <junit printsummary="true" fork="yes" >
       <classpath refid="lib.classpath" />
       <classpath path="${build}" />
       <classpath path="${output}/etc" />
       <formatter type="xml" />
       <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
       <test name="${test}" todir="${reports}" />
 <!--
       <jvmarg value="-Xdebug"/>
       <jvmarg value="-Xnoagent"/>
       <jvmarg value="-Djava.compiler=NONE"/>
       <jvmarg value="-Xrunjdwp:transport=dt_socket,server=n,suspend=n,address=localhost:2401"/>
 -->
     </junit>

      <junitreport todir="${reports}">
         <fileset dir="${reports}">
            <include name="TEST-*.xml" />
         </fileset>
         <report format="frames" todir="${reports}/html"/>
      </junitreport>
    </target>

  <target name="run.batch" depends="compile" description="run junit test cases.">
    <junit printsummary="true"
      timeout="${junit.timeout}"
      fork="yes" >
      <jvmarg value="-Xmx128M"/>
<!--
      <jvmarg value="-Dbind.address=172.16.129.57"/>
-->
      <classpath refid="lib.classpath" />
      <classpath path="${build}" />
      <classpath path="${output}/etc" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <formatter type="xml" />

      <batchtest todir="${reports}"
                 haltonerror="no"
                 haltonfailure="no"
                 fork="yes">

        <fileset dir="${build}">
          <include name="**/*Test.class"/>
          <exclude name="**/aop/**/*Test.class"/>
        </fileset>
      </batchtest>

    </junit>

    <junitreport todir="${reports}">
       <fileset dir="${reports}">
          <include name="TEST-*.xml" />
       </fileset>
       <report format="frames" todir="${reports}/html"/>
    </junitreport>
  </target>

  <target name="run.demo" depends="compile" description="run tree cache demo.">
    <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
      <jvmarg value="-Xmx128M"/>
      <classpath refid="lib.classpath" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <classpath path="${output}/etc" />
      <classpath path="${build}" />
    </java>
  </target>


   <target name="run.demo.async" depends="compile" description="run tree cache demo.">
      <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
      <jvmarg value="-Xmx128M"/>
        <arg value="-config"/>
        <arg value="META-INF/replAsync-service.xml"/>
       <classpath refid="lib.classpath" />
       <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
       <classpath path="${output}/etc" />
       <classpath path="${build}" />
     </java>
   </target>


   <target name="run.demo.async.shared.cacheloader" depends="compile" description="run tree cache demo.">
      <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
         <jvmarg value="-Xmx128M"/>
         <arg value="-config"/>
         <arg value="META-INF/replAsyncSharedCacheLoader-service.xml"/>
         <classpath refid="lib.classpath" />
         <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
         <classpath path="${output}/etc" />
         <classpath path="${build}" />
      </java>
   </target>


   <target name="run.demo.unshared.node1" depends="compile" description="run tree cache demo.">
      <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
         <jvmarg value="-Xmx128M"/>
         <arg value="-config"/>
         <arg value="META-INF/node1.xml"/>
         <classpath refid="lib.classpath" />
         <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
         <classpath path="${output}/etc" />
         <classpath path="${build}" />
      </java>
   </target>

   <target name="run.demo.unshared.node2" depends="compile" description="run tree cache demo.">
      <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
         <jvmarg value="-Xmx128M"/>
         <arg value="-config"/>
         <arg value="META-INF/node2.xml"/>
         <classpath refid="lib.classpath" />
         <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
         <classpath path="${output}/etc" />
         <classpath path="${build}" />
      </java>
   </target>

  <target name="run.server" depends="compile" description="run standalone server replicator.">
    <java classname="org.jboss.cache.Server" fork="yes" >
      <classpath refid="lib.classpath" />
      <classpath path="${build}" />
      <classpath path="${output}/etc" />
    </java>
  </target>

   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   AOP section
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->

   <!-- pre-compile directory with aopc -->
    <!-- define ant target for aopc. It pre-compile your Pojo classes with decarlation in a jboss-aop.xml file.
    To check whether it is successfully pre-compiled, you can either check for extra class files output or use
    a de-compiler like Cavj to examine the new pojo classes.
    Note that you direcoty name can't contain white spaces!!
  -->
   <taskdef name="aopc" classname="org.jboss.aop.ant.AopC" classpathref="aop.classpath"/>
   <target name="aopc" depends="compile" description="Precompile aop class">
      <aopc compilerclasspathref="aop.classpath" verbose="true">
         <src path="${build}"/>
         <include name="org/jboss/cache/aop/test/**/*.class"/>
         <aoppath path="${output}/etc/jboss-aop.xml"/>
         <classpath path="${build}"/>
         <classpath refid="lib.classpath"/>
      </aopc>
   </target>

   <target name="aopc50" depends="compile" description="Precompile aop class">
      <aopc compilerclasspathref="aop.classpath" verbose="true">
         <src path="${build-50}"/>
         <include name="org/jboss/cache/aop/test/**/*.class"/>
         <aoppath path="${output}/resources/jboss-aop.xml"/>
         <classpath path="${build-50}"/>
         <classpath refid="lib.classpath.50"/>
      </aopc>
   </target>

   <!-- pre-compile directory with annotationc using jdk1.4 -->
   <target name="annoc" depends="compile" description="Annotation precompiler for aop class">
      <!-- Define a new ant target. This is the 1.4 annotation pre-compiler. After running this step,
           you still need to run the aopc step again, if you are not using system class loader.
      -->
       <taskdef name="annotationc" classname="org.jboss.aop.ant.AnnotationC" classpathref="aop.classpath"/>
       <annotationc compilerclasspathref="aop.classpath" bytecode="true">
          <classpath refid="lib.classpath"/>
          <classpath path="${build}"/>
          <!--System wide jboss-aop.xml is located here. -->
          <classpath path="${output.resources.dir}"/>
          <src path="${source}"/>
          <include name="org/jboss/cache/aop/test/**/*.java"/>
       </annotationc>
   </target>

    <!-- pre-compile directory with aopc -->
    <target name="aopcWithAnnoc" depends="annoc" description="Precompile aop test classes after annoc">
       <aopc compilerclasspathref="aop.classpath" classpathref="aop.classpath" verbose="true">
          <!-- We can also include directly this way as well. -->
          <src path="${build}/org/jboss/cache/aop/test"/>
          <!--Still need to include system-wide jboss-aop.xml  -->
          <aoppath path="${output}/resources/jboss-aop.xml"/>
          <classpath path="${build}"/>
          <classpath refid="lib.classpath"/>
       </aopc>
    </target>

   <!--
   This is new target since 1.2.4. Since the old way using SystemClassLoader ie error prone, it has been replcaed in
   JBossAop 1.3.1 for the new GeneratedInstrumentedClassLoader. But for this to work, we will need this target
   to generate a temporarily ClassLoader under gen-bootclasspath directory
   -->
   <target name="generateClassLoader" description="Generate a new modified class loader so we can perform load time instrumentation">
      <property name="build.bootclasspath" value="${output}/gen-bootclasspath"/>
      <java classname="org.jboss.aop.hook.GenerateInstrumentedClassLoader">
         <classpath>
            <path refid="aop.classpath"/>
         </classpath>
         <arg value="${build.bootclasspath}"/>
      </java>
      <path id="bootclasspath">
         <pathelement location="${build.bootclasspath}"/>
         <path refid="aop.classpath"/>
      </path>
      <property name="bootclasspath" refid="bootclasspath"/>
   </target>

   <!-- eg. ant run.examples -Dtest=org.jboss.test.cache.test.local.NoTxUnitTestCase -->
   <target name="one.test.aop" depends="compile, generateClassLoader" description="run one junit test case.">

        <!--
       <junit printsummary="true" fork="yes" >
         <classpath refid="lib.classpath" />
         <classpath path="${build}" />
          <jvmarg value="-Djboss.aop.path=${output}/etc/META-INF/jboss-aop.xml"/>
          <jvmarg value="-Xbootclasspath/p:${bootclasspath}"/>
         <formatter type="xml" />
         <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
         <test name="${test}" todir="${reports}" />
         <jvmarg value="-Xdebug"/>
         <jvmarg value="-Xnoagent"/>
         <jvmarg value="-Djava.compiler=NONE"/>
         <jvmarg value="-Xrunjdwp:transport=dt_socket,server=n,suspend=n,address=localhost:2401"/>
       </junit>
   -->

        <junit printsummary="yes" timeout="${junit.timeout}" fork="yes">
           <jvmarg value="-Djboss.aop.path=${output}/etc/jboss-aop.xml"/>
           <jvmarg value="-Xbootclasspath/p:${bootclasspath}"/>
           <!-- jvmarg value="-Dbind.address=${bind.address}"/ -->
           <classpath path="${output}/etc" />
           <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml" />
           <classpath refid="lib.classpath"/>
           <classpath path="${build}"/>
           <formatter type="xml" usefile="true"/>
           <test name="${test}" todir="${reports}"/>
        </junit>

   <junitreport todir="${reports}">
      <fileset dir="${reports}">
         <include name="TEST-*.xml" />
      </fileset>
      <report format="frames" todir="${reports}/html"/>
   </junitreport>
 </target>

   <!-- eg. ant run.examples -Dtest=org.jboss.test.cache.test.local.NoTxUnitTestCase -->
   <target name="one.test.aop50" depends="compile, generateClassLoader" description="run one junit test case.">
     <junit printsummary="yes" timeout="${junit.timeout}" fork="yes">
        <jvmarg value="-Djboss.aop.path=${output}/resources/jboss-aop.xml"/>
        <!--
        <jvmarg value="-Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader"/>
        -->
        <jvmarg value="-javaagent:${lib-50}/jboss-aop-jdk50.jar"/>
        <!-- jvmarg value="-Dbind.address=${bind.address}"/ -->
        <classpath path="${output}/etc" />
        <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml" />
        <classpath refid="lib.classpath.50"/>
        <classpath refid="build.classpath.50"/>
        <formatter type="xml" usefile="true"/>
        <test name="${test}" todir="${reports}"/>
     </junit>

      <junitreport todir="${reports}">
         <fileset dir="${reports}">
            <include name="TEST-*.xml" />
         </fileset>
         <report format="frames" todir="${reports}/html"/>
      </junitreport>
 </target>

   <!-- Works only with aop package -->
   <!-- This one uses the aopc +annoc option. You can also use aopc in depends as well. -->
  <!-- eg. ant run.aopc.examples -Dtest=org.jboss.test.cache.test.local.NoTxUnitTestCase -->
  <target name="one.test.aopc" depends="aopcWithAnnoc" description="run junit test cases.">
    <junit printsummary="true" fork="yes" >
      <classpath refid="lib.classpath" />
      <classpath path="${build}" />
      <classpath path="${output}/etc" />
      <formatter type="xml" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <test name="${test}" todir="${reports}" />
      <jvmarg value="-Xmx128M"/>
<!--
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xnoagent"/>
      <jvmarg value="-Djava.compiler=NONE"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,server=n,suspend=n,address=localhost:2401"/>
-->
    </junit>

    <junitreport todir="${reports}">
       <fileset dir="${reports}">
          <include name="TEST-*.xml" />
       </fileset>
       <report format="frames" todir="${reports}/html"/>
    </junitreport>
  </target>

   <target name="one.test.aopc50" depends="aopc50" description="run junit test cases.">
     <junit printsummary="true" fork="yes" >
       <classpath refid="lib.classpath.50" />
       <classpath refid="build.classpath.50" />
       <classpath path="${output}/etc" />
       <formatter type="xml" />
       <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
       <test name="${test}" todir="${reports}" />
       <jvmarg value="-Xmx128M"/>
     </junit>

     <junitreport todir="${reports}">
        <fileset dir="${reports}">
           <include name="TEST-*.xml" />
        </fileset>
        <report format="frames" todir="${reports}/html"/>
     </junitreport>
   </target>


   <!-- Works only with aop package -->
   <!-- This one uses the system classloader option -->
  <target name="run.aop.batch" depends="generateClassLoader, compile" description="run junit test cases.">
    <junit printsummary="true"
      timeout="${junit.timeout}"
      fork="yes" >
      <jvmarg value="-Xmx128M"/>
<!--
      <jvmarg value="-Dbind.address=172.16.129.57"/>
-->
      <classpath refid="lib.classpath" />
      <classpath path="${build}" />
       <jvmarg value="-Djboss.aop.path=${output}/etc/jboss-aop.xml"/>
       <jvmarg value="-Xbootclasspath/p:${bootclasspath}"/>
      <classpath path="${output}/etc" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <formatter type="xml" />

      <batchtest todir="${reports}"
                 haltonerror="no"
                 haltonfailure="no"
                 fork="yes">

        <fileset dir="${build}">
          <include name="**/aop/**/*Test.class"/>
           <exclude name="org.jboss.cache.aop.eviction.AopLRUPolicyUpdateEvictionTest.class"/>
        </fileset>
      </batchtest>
    </junit>

    <junitreport todir="${reports}">
       <fileset dir="${reports}">
          <include name="TEST-*.xml" />
       </fileset>
       <report format="frames" todir="${reports}/html"/>
    </junitreport>
  </target>

   <!-- Works only with aop package -->
   <!-- This one uses the aopc+ annoc option -->
  <target name="run.aopc.batch" depends="aopcWithAnnoc" description="run junit test cases.">
    <junit printsummary="true"
      timeout="${junit.timeout}"
      fork="yes" >
      <jvmarg value="-Xmx128M"/>
      <classpath refid="lib.classpath" />
      <classpath path="${build}" />
      <classpath path="${output}/etc" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <formatter type="xml" />

      <batchtest todir="${reports}"
                 haltonerror="no"
                 haltonfailure="no"
                 fork="yes">

        <fileset dir="${build}">
           <include name="**/aop/**/*Test.class"/>
           <exclude name="org.jboss.cache.aop.eviction.AopLRUPolicyUpdateEvictionTest.class"/>
        </fileset>
      </batchtest>

    </junit>

    <junitreport todir="${reports}">
       <fileset dir="${reports}">
          <include name="TEST-*.xml" />
       </fileset>
       <report format="frames" todir="${reports}/html"/>
    </junitreport>
  </target>

   <!-- Works only with aop package -->
   <!-- This one uses the aopc+ annoc option -->
  <target name="run.aopc50.batch" depends="aopc50" description="run junit test cases.">
    <junit printsummary="true"
      timeout="${junit.timeout}"
      fork="yes" >
      <jvmarg value="-Xmx128M"/>
      <classpath refid="lib.classpath.50" />
      <classpath refid="build.classpath.50" />
      <classpath path="${output}/etc" />
      <formatter type="xml" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>

      <batchtest todir="${reports}"
                 haltonerror="no"
                 haltonfailure="no"
                 fork="yes">

         <fileset dir="${build-50}">
            <include name="**/aop/**/*Test.class"/>
            <exclude name="org.jboss.cache.aop.eviction.AopLRUPolicyUpdateEvictionTest.class"/>
         </fileset>
         <fileset dir="${build}">
            <include name="**/aop/**/*Test.class"/>
            <exclude name="org.jboss.cache.aop.eviction.AopLRUPolicyUpdateEvictionTest.class"/>
         </fileset>
      </batchtest>

    </junit>

    <junitreport todir="${reports}">
       <fileset dir="${reports}">
          <include name="TEST-*.xml" />
       </fileset>
       <report format="frames" todir="${reports}/html"/>
    </junitreport>
  </target>

  <target name="run.aop.demo" depends="compile" description="run tree cache demo.">
    <!--java classname="org.jboss.cache.aop.TreeCacheAopView" fork="yes"-->
      <java classname="org.jboss.cache.aop.TreeCacheView2" fork="yes">
      <jvmarg value="-Xmx128M"/>
<!--
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xnoagent"/>
      <jvmarg value="-Djava.compiler=NONE"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=2401"/>
-->

      <classpath refid="lib.classpath" />
      <sysproperty key="log4j.configuration" value="file:${output}/etc/log4j.xml"/>
      <classpath path="${output}/etc" />
      <classpath path="${build}" />
    </java>
  </target>

   <!-- Works only with aop package -->
   <!-- This one uses the system classloader option -->
   <target name="run.aop.server" depends="compile" description="run standalone server replicator.">
     <java classname="org.jboss.cache.aop.Server" fork="yes" >
       <jvmarg value="-Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader"/>
       <classpath refid="lib.classpath" />
       <classpath path="${build}" />
       <classpath path="${output}/etc" />
     </java>
   </target>


</project>
