alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Ant example source code file (symlink.xml)

This example Ant source code file (symlink.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Ant tags/keywords

creates, deletes, dir, error, error, file, file, link, link, os, os, should, test, this

The symlink.xml source code

<?xml version="1.0"?>

<!--

/*
 * Since the initial version of this file was deveolped on the clock on
 * an NSF grant I should say the following boilerplate:
 *
 * This material is based upon work supported by the National Science
 * Foundaton under Grant No. EIA-0196404. Any opinions, findings, and
 * conclusions or recommendations expressed in this material are those
 * of the author and do not necessarily reflect the views of the
 * National Science Foundation.
 */

-->

<project name="symlink-test" basedir="." default="all">

  <!-- 
       Since the symlink task and some of these targets rely on
       calls to exec, it may be possible for the JVM to outrun the 
       execution of the command line  system calls, so this value is
       the number of seconds we give the operating system to
       catch up before executing a task that depends on the 
       completion of previous tasks. This delay is also added to
       the end of each target so junit doesn't go testing things
       before they have finnished (hopefully). Tweak if needed.
  -->

  <property name="delay" value="3"/>
 
  <property name="tdir" value="${basedir}/test-working"/>

  <target name="setup">
      <delete dir="${tdir}"/>
      <mkdir dir="${tdir}"/>
  </target>

  <target name="all"
	      depends="setup, test-single, test-delete, test-record, test-recreate, teardown"/>
	
  <!-- test for action = single -->
  <!-- 
    Creates:
         File: ${tdir}/symlink.test
         Link: ${tdir}/singletest
  -->
  <target name="test-single">
    <touch file="${tdir}/symlink.test"/>
    <symlink resource="${tdir}/symlink.test" 
             link="${tdir}/singletest" 
             failonerror="yes"/>
    <sleep seconds="${delay}"/> 
    <available file="${tdir}/symlink.test" 
               property="test.single.file.created"/>
    <available file="${tdir}/singletest" 
               property="test.single.link.created"/>
  </target>



  <!-- test for action = delete  (no calls to command line so no sleep) -->

  <!-- 
    Creates:
         (none)
    Deletes:
         Link: ${tdir}/singletest
  -->

  <target name="test-delete">
    <touch file="${tdir}/symlink.test"/>
    <symlink resource="${tdir}/symlink.test" 
             link="${tdir}/singletest" 
             failonerror="yes"/>
    <sleep seconds="${delay}"/> 

    <symlink action="delete" link="${tdir}/singletest" failonerror="yes"/>
    <symlink action="delete" link="${tdir}/symlink.test" failonerror="no"/>
    <sleep seconds="${delay}"/> 

    <available file="${tdir}/symlink.test" 
               property="test.delete.file.still.there"/>
    <available file="${tdir}/singletest" 
               property="test.delete.link.still.there"
               value="ERROR: link deletion failed"/>
    
  </target>



  <!-- test for action = record -->

  <!-- 
    Creates:
         Dir:  ${tdir}/symtest1
         Dir:  ${tdir}/symtest1/symtest2
         Dir:  ${tdir}/symtest1/symtest3
         File: ${tdir}/symtest1/file1
         File: ${tdir}/symtest1/symtest2/file2
         File: ${tdir}/symtest1/symtest3/fileA
         File: ${tdir}/symtest1/symtest3/fileB
         File: ${tdir}/symtest1/symtest3/fileC
         Link: ${tdir}/symtest1/link1==>${tdir}/symtest1/file1
         Link: ${tdir}/symtest1/link2==>${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/symtest2/link3==>
                                           ${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/dirlink==>${tdir}/symtest1/symtest3
         Link: ${tdir}/symtest1/dirlink2==>${tdir}/symtest1/symtest3
         Link: ${tdir}/symtest1/dirlink3==>${tdir}/symtest1/symtest3
         File: ${tdir}/symtest1/recorded.links
         File: ${tdir}/symtest1/symtest2/recorded.links
    Deletes:
         (none)
  -->

  <target name="test-record">

    <mkdir dir="${tdir}/symtest1"/>
    <mkdir dir="${tdir}/symtest1/symtest2"/>
    <mkdir dir="${tdir}/symtest1/symtest3"/>
    <touch file="${tdir}/symtest1/file1"/>
    <touch file="${tdir}/symtest1/symtest2/file2"/>

    <touch file="${tdir}/symtest1/symtest3/fileA"/>
    <touch file="${tdir}/symtest1/symtest3/fileB"/>
    <touch file="${tdir}/symtest1/symtest3/fileC"/>

    <symlink resource="${tdir}/symtest1/file1" 
             link="${tdir}/symtest1/link1" 
             failonerror="no" />
    <symlink resource="${tdir}/symtest1/symtest2/file2" 
             link="${tdir}/symtest1/link2" 
             failonerror="no" />
    <symlink resource="${tdir}/symtest1/symtest2/file2" 
             link="${tdir}/symtest1/symtest2/link3" 
             failonerror="no" />
    <symlink resource="${tdir}/symtest1/symtest3"
             link="${tdir}/symtest1/dirlink"
             failonerror="no" />
    <symlink resource="${tdir}/symtest1/symtest3" 
             link="${tdir}/symtest1/dirlink2"
             failonerror="no"/>
    <symlink resource="${tdir}/symtest1/symtest3" 
             link="${tdir}/symtest1/dirlink3"
             failonerror="no"/>

    <sleep seconds="${delay}"/> 

    <symlink action="record" linkfilename="recorded.links">
       <fileset dir="${tdir}/symtest1" includes="**/**"/>
    </symlink>

    <sleep seconds="${delay}"/> 

    <!-- Test to see if the directories were created -->

    <available file="${tdir}/symtest1"
               type="dir"
               property="test.record.dir1.created"/>

    <available file="${tdir}/symtest1/symtest2"
               type="dir"
               property="test.record.dir2.created"/>

    <available file="${tdir}/symtest1/symtest3"
               type="dir"
               property="test.record.dir3.created"/>

    <!-- Test to see if the Files were created -->

    <available file="${tdir}/symtest1/file1"
               property="test.record.file1.created"/>

    <available file="${tdir}/symtest1/symtest2/file2"
               property="test.record.file2.created"/>

    <available file="${tdir}/symtest1/symtest3/fileA"
               property="test.record.fileA.created"/>

    <available file="${tdir}/symtest1/symtest3/fileB"
               property="test.record.fileB.created"/>

    <available file="${tdir}/symtest1/symtest3/fileC"
               property="test.record.fileC.created"/>

    <!-- Test to see if the links were created -->

    <available file="${tdir}/symtest1/link1"
               property="test.record.link1.created"/>

    <available file="${tdir}/symtest1/link2"
               property="test.record.link2.created"/>

    <available file="${tdir}/symtest1/symtest2/link3"
               property="test.record.link3.created"/>

    <available file="${tdir}/symtest1/dirlink"
               property="test.record.dirlink.created"/>

    <!-- this is redundant for this test, but used in the recreate test -->

    <available file="${tdir}/symtest1/dirlink2" 
               property="test.record.dirlink2.created"/>

    <!-- Test to see if the linkfiles were created -->

    <available file="${tdir}/symtest1/recorded.links"
               property="test.record.dir1.recorded"/>

    <available file="${tdir}/symtest1/symtest2/recorded.links"
               property="test.record.dir2.recorded"/>

    <!-- THIS should not be set -->

    <available file="${tdir}/symtest1/symtest3/recorded.links"
               property="test.record.dir3.recorded"
               value="ERROR: symtest3/recorded.links should not exist"/>


  </target>

  <!-- test for action = recreate -->

  <!-- 
    Deletes:
         Link: ${tdir}/symtest1/link1==>${tdir}/symtest1/file1
         Link: ${tdir}/symtest1/link2==>${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/symtest2/link3==>
                                           ${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/dirlink==>${tdir}/symtest1/symtest3
         Link: ${tdir}/symtest1/dirlink3==>${tdir}/symtest1/symtest3

    Creates
         Link: ${tdir}/symtest1/dirlink3==>${tdir}/symtest1/symtest2

    Recreates:
         Link: ${tdir}/symtest1/link1==>${tdir}/symtest1/file1
         Link: ${tdir}/symtest1/link2==>${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/symtest2/link3==>
                                           ${tdir}/symtest1/symtest2/file2
         Link: ${tdir}/symtest1/dirlink==>${tdir}/symtest1/symtest3

    Should Change:
         Link: ${tdir}/symtest1/dirlink3==>${tdir}/symtest1/symtest2
               to
               ${tdir}/symtest1/dirlink3==>${tdir}/symtest1/symtest3

    Should Not Create (bug 25181):
         Link: ${tdir}/symtest1/symtest3/dirlink2==>${tdir}/symtest1/symtest3
  -->

  <target name="test-recreate" depends="test-record">

    <symlink action="delete" link="${tdir}/symtest1/link1"/>
    <symlink action="delete" link="${tdir}/symtest1/link2"/>
    <symlink action="delete" link="${tdir}/symtest1/symtest2/link3"/>
    <symlink action="delete" link="${tdir}/symtest1/dirlink"/>
    <!-- dirlink2 intentionally not deleted to test bug 25181 -->
    <symlink action="delete" link="${tdir}/symtest1/dirlink3"/>

    <sleep seconds="${delay}"/>  

    <symlink resource="${tdir}/symtest1/symtest2" 
             link="${tdir}/symtest1/dirlink3"
             failonerror="no"/>

    <sleep seconds="${delay}"/>  

    <available file="${tdir}/symtest1/link1"
               property="test.recreate.link1.not.removed"
               value="ERROR: rm -f symtest1/link1 failed"/>

    <available file="${tdir}/symtest1/link2"
               property="test.recreate.link2.not.removed"
               value="ERROR: rm -f symtest1/link2 failed"/>

    <available file="${tdir}/symtest1/symtest2/link3"
               property="test.recreate.link3.not.removed"
               value="ERROR: rm -f symtest1/symtest2/link3 failed"/>

     <available file="${tdir}/symtest1/zdirlink"
               property="test.recreate.zdirlink.not.removed"
               value="ERROR: rm -f symtest1/zdirlink failed"/>

    <sleep seconds="${delay}"/>  

    <symlink action="recreate">
      <fileset dir="${tdir}/symtest1" includes="**/recorded.links"/>
    </symlink>

    <sleep seconds="${delay}"/>  

    <available file="${tdir}/symtest1/link1"
               property="test.recreate.link1.recreated"/>

    <available file="${tdir}/symtest1/link2"
               property="test.recreate.link2.recreated"/>

    <available file="${tdir}/symtest1/symtest2/link3"
               property="test.recreate.link3.recreated"/>

    <available file="${tdir}/symtest1/dirlink"
               property="test.recreate.dirlink.recreated"/>

    <!-- this should not get set -->
    <available file="${tdir}/symtest1/symtest3/symtest3" 
               property="test.recreate.dirlink2.recreated.twice"
               value="ERROR: dirlink2 was created a second time (bug 25181)"/>

    <touch file="${tdir}/symtest1/dirlink3/WhereAmI"/>

    <sleep seconds="${delay}"/>  

    <available file="${tdir}/symtest1/symtest3/WhereAmI" 
               property="test.recreate.dirlink3.was.altered"/>
  </target>


<!-- CALL THIS to clean things up afterwards -->

  <target name="teardown">
    <delete dir="${tdir}"/>
  </target>

</project>

Other Ant examples (source code examples)

Here is a short list of links related to this Ant symlink.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.