diff options
author | Gintas Grigelionis <gintas@apache.org> | 2018-02-28 06:06:58 +0100 |
---|---|---|
committer | Gintas Grigelionis <gintas@apache.org> | 2018-02-28 07:59:42 +0100 |
commit | d5f4b681c48ca7b74fdd6ce13dae39f6684aa010 (patch) | |
tree | 14ad88eb8c0f3612df90bd62547b72bca336756f /src/tutorial | |
parent | 2e788849136bfd75e88ce0d959810f1191d43a33 (diff) | |
download | ant-d5f4b681c48ca7b74fdd6ce13dae39f6684aa010.tar.gz |
Add tutorial sources
Diffstat (limited to 'src/tutorial')
19 files changed, 1621 insertions, 0 deletions
diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml b/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml new file mode 100644 index 000000000..73b26ad7f --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="FindTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the Find-Task" depends="jar"> + <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/> + </target> + + <target name="use.simple" depends="use.init"> + <find property="test" value="test-value"/> + <find print="test"/> + </target> + + + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + + +</project> diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java new file mode 100644 index 000000000..a2f44ddfa --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java @@ -0,0 +1,33 @@ +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; + +public class Find extends Task { + + private String property; + private String value; + private String print; + + public void setProperty(String property) { + this.property = property; + } + + public void setValue(String value) { + this.value = value; + } + + public void setPrint(String print) { + this.print = print; + } + + public void execute() { + if (print != null) { + String propValue = getProject().getProperty(print); + log(propValue); + } else { + if (property == null) throw new BuildException("property not set"); + if (value == null) throw new BuildException("value not set"); + getProject().setNewProperty(property, value); + } + } + +} diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java new file mode 100644 index 000000000..1e88a3c40 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java @@ -0,0 +1,16 @@ +import org.apache.tools.ant.BuildFileTest; + +public class FindTest extends BuildFileTest { + + public FindTest(String name) { + super(name); + } + + public void setUp() { + configureProject("build.xml"); + } + + public void testSimple() { + expectLog("use.simple", "test-value"); + } +} diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/build.xml b/src/tutorial/tasks-filesets-properties/02-fileset/build.xml new file mode 100644 index 000000000..81ba60b2a --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/02-fileset/build.xml @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="FindTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the Find-Task" depends="jar"> + <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/> + </target> + + <target name="use.simple" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </find> + </target> + + <target name="testFileNotPresent" depends="use.init"> + <find file="a-not-existing-file.jar" location="location.ant-jar"> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </find> + </target> + + <target name="testFilePresent" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </find> + </target> + + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" depends="jar"> + <junit printsummary="no" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="brief" usefile="false"/> + <batchtest fork="true" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="test2" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + + +</project> diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java b/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java new file mode 100644 index 000000000..04067bb0f --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java @@ -0,0 +1,55 @@ +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.DirectoryScanner; + +import java.util.Vector; +import java.util.Iterator; +import java.io.File; + +public class Find extends Task { + + private String file; + private String location; + private Vector filesets = new Vector(); + + public void setFile(String file) { + this.file = file; + } + + public void setLocation(String location) { + this.location = location; + } + + public void addFileset(FileSet fileset) { + filesets.add(fileset); + } + + protected void validate() { + if (file==null) throw new BuildException("file not set"); + if (location==null) throw new BuildException("location not set"); + if (filesets.size()<1) throw new BuildException("fileset not set"); + } + + public void execute2() { + validate(); + String foundLocation = null; + for(Iterator itFSets = filesets.iterator(); itFSets.hasNext(); ) { + FileSet fs = (FileSet)itFSets.next(); + DirectoryScanner ds = fs.getDirectoryScanner(getProject()); + String[] includedFiles = ds.getIncludedFiles(); + for(int i=0; i<includedFiles.length; i++) { + String filename = includedFiles[i].replace('\\','/'); + filename = filename.substring(filename.lastIndexOf("/")+1); + if (foundLocation==null && file.equals(filename)) { + File base = ds.getBasedir(); + File found = new File(base, includedFiles[i]); + foundLocation = found.getAbsolutePath(); + } + } + } + if (foundLocation!=null) + getProject().setNewProperty(location, foundLocation); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java b/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java new file mode 100644 index 000000000..819bb032a --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java @@ -0,0 +1,68 @@ +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.taskdefs.Property; +import java.io.File; + +public class FindTest extends BuildFileTest { + + public FindTest(String name) { + super(name); + } + + public void setUp() { + configureProject("build.xml"); + } + + public void testMissingFile() { + Find find = new Find(); + try { + find.execute(); + fail("No 'no-file'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "file not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingLocation() { + Find find = new Find(); + find.setFile("ant.jar"); + try { + find.execute(); + fail("No 'no-location'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "location not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingFileset() { + Find find = new Find(); + find.setFile("ant.jar"); + find.setLocation("location.ant-jar"); + try { + find.execute(); + fail("No 'no-fileset'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "fileset not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testFileNotPresent() { + executeTarget("testFileNotPresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNull("Property set to wrong value.", result); + } + + public void testFilePresent() { + executeTarget("testFilePresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNotNull("Property not set.", result); + assertTrue("Wrong file found.", result.endsWith("ant.jar")); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/03-paths/build.xml b/src/tutorial/tasks-filesets-properties/03-paths/build.xml new file mode 100644 index 000000000..ee0812a47 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/03-paths/build.xml @@ -0,0 +1,108 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="FindTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the Find-Task" depends="jar"> + <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/> + </target> + + <target name="use.simple" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + <echo>ant.jar found on ${location.ant-jar}</echo> + </target> + + <target name="testFileNotPresent" depends="use.init"> + <find file="a-not-existing-file.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="testFilePresent" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" depends="jar"> + <junit printsummary="no" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="brief" usefile="false"/> + <batchtest fork="true" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="test2" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + +</project> diff --git a/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java b/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java new file mode 100644 index 000000000..a05ac5814 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java @@ -0,0 +1,52 @@ +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.DirectoryScanner; + +import java.util.Vector; +import java.util.Iterator; +import java.io.File; + +public class Find extends Task { + + private String file; + private String location; + private Vector paths = new Vector(); + + public void setFile(String file) { + this.file = file; + } + + public void setLocation(String location) { + this.location = location; + } + + public void addPath(Path path) { + paths.add(path); + } + + protected void validate() { + if (file==null) throw new BuildException("file not set"); + if (location==null) throw new BuildException("location not set"); + if (paths.size()<1) throw new BuildException("path not set"); + } + + public void execute() { + validate(); + String foundLocation = null; + for(Iterator itPaths = paths.iterator(); itPaths.hasNext(); ) { + Path path = (Path)itPaths.next(); + String[] includedFiles = path.list(); + for(int i=0; i<includedFiles.length; i++) { + String filename = includedFiles[i].replace('\\','/'); + filename = filename.substring(filename.lastIndexOf("/")+1); + if (foundLocation==null && file.equals(filename)) { + foundLocation = includedFiles[i]; + } + } + } + if (foundLocation!=null) + getProject().setNewProperty(location, foundLocation); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java b/src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java new file mode 100644 index 000000000..667094929 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java @@ -0,0 +1,68 @@ +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.taskdefs.Property; +import java.io.File; + +public class FindTest extends BuildFileTest { + + public FindTest(String name) { + super(name); + } + + public void setUp() { + configureProject("build.xml"); + } + + public void testMissingFile() { + Find find = new Find(); + try { + find.execute(); + fail("No 'no-file'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "file not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingLocation() { + Find find = new Find(); + find.setFile("ant.jar"); + try { + find.execute(); + fail("No 'no-location'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "location not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingPath() { + Find find = new Find(); + find.setFile("ant.jar"); + find.setLocation("location.ant-jar"); + try { + find.execute(); + fail("No 'no-fileset'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "path not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testFileNotPresent() { + executeTarget("testFileNotPresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNull("Property set to wrong value.", result); + } + + public void testFilePresent() { + executeTarget("testFilePresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNotNull("Property not set.", result); + assertTrue("Wrong file found.", result.endsWith("ant.jar")); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/04-lists/build.xml b/src/tutorial/tasks-filesets-properties/04-lists/build.xml new file mode 100644 index 000000000..39c18a2d6 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/04-lists/build.xml @@ -0,0 +1,162 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="FindTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the Find-Task" depends="jar"> + <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/> + </target> + + <target name="use.simple" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + <fileset dir="c:/seu" includes="ant*/**/*.jar"/> + </path> + </find> + <echo>ant.jar found on ${location.ant-jar}</echo> + </target> + + <target name="use.simple2" depends="use.init"> + <find file="ant.jar" location="location.ant-jar" delimiter=";"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + <fileset dir="c:/seu" includes="ant*/**/*.jar"/> + </path> + </find> + <echo>ant.jar found on ${location.ant-jar}</echo> + </target> + + <target name="testFileNotPresent" depends="use.init"> + <find file="a-not-existing-file.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="testFilePresent" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="test.init"> + <mkdir dir="test1/dir11/dir111"/> + <mkdir dir="test1/dir11/dir112"/> + <mkdir dir="test1/dir12/"/> + <mkdir dir="test1/dir13/dir131"/> + <mkdir dir="test1/dir13/dir132"/> + <touch file="test1/dir11/dir111/test"/> + <touch file="test1/dir11/dir111/not"/> + <touch file="test1/dir11/dir111/not2"/> + <touch file="test1/dir11/dir112/test"/> + <touch file="test1/dir11/dir112/not"/> + <touch file="test1/dir11/dir112/not2"/> + <touch file="test1/dir12/test"/> + <touch file="test1/dir12/not"/> + <touch file="test1/dir12/not2"/> + <touch file="test1/dir13/dir131/test"/> + <touch file="test1/dir13/dir131/not"/> + <touch file="test1/dir13/dir131/not2"/> + <touch file="test1/dir13/dir132/test"/> + <touch file="test1/dir13/dir132/not"/> + <touch file="test1/dir13/dir132/not2"/> + <mkdir dir="test2"/> + <copy todir="test2"> + <fileset dir="test1"/> + </copy> + </target> + + <target name="testMultipleFiles" depends="use.init,test.init"> + <touch file="test1/dir11/dir111/test"/> + <find file="test" location="location.test" delimiter=";"> + <path> + <fileset dir="test1"/> + <fileset dir="test2"/> + </path> + </find> + <delete> + <fileset dir="test1"/> + <fileset dir="test2"/> + </delete> + </target> + + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" depends="jar"> + <junit printsummary="no" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="brief" usefile="false"/> + <batchtest fork="true" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="test2" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + + +</project> diff --git a/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java new file mode 100644 index 000000000..c608af9e3 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java @@ -0,0 +1,81 @@ +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.DirectoryScanner; + +import java.util.Vector; +import java.util.Iterator; +import java.io.File; + +public class Find extends Task { + + // ===== internal attributes ===== + + private Vector foundFiles = new Vector(); + + // ===== attribute support ===== + + private String file; + private String location; + private Vector paths = new Vector(); + private String delimiter = null; + + public void setFile(String file) { + this.file = file; + } + + public void setLocation(String location) { + this.location = location; + } + + public void addPath(Path path) { + paths.add(path); + } + + public void setDelimiter(String delim) { + delimiter = delim; + } + + // ===== the tasks work ===== + + protected void validate() { + if (file==null) throw new BuildException("file not set"); + if (location==null) throw new BuildException("location not set"); + if (paths.size()<1) throw new BuildException("path not set"); + } + + public void execute() { + validate(); + for(Iterator itPaths = paths.iterator(); itPaths.hasNext(); ) { + Path path = (Path)itPaths.next(); + String[] includedFiles = path.list(); + for(int i=0; i<includedFiles.length; i++) { + String filename = includedFiles[i].replace('\\','/'); + filename = filename.substring(filename.lastIndexOf("/")+1); + if (file.equals(filename) && !foundFiles.contains(includedFiles[i])) { + foundFiles.add(includedFiles[i]); + } + } + } + String rv = null; + if (foundFiles.size() > 0) { + if (delimiter==null) { + // only the first + rv = (String)foundFiles.elementAt(0); + } else { + // create list + StringBuffer list = new StringBuffer(); + for(Iterator it=foundFiles.iterator(); it.hasNext(); ) { + list.append(it.next()); + if (it.hasNext()) list.append(delimiter); + } + rv = list.toString(); + } + } + + + if (rv!=null) + getProject().setNewProperty(location, rv); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java b/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java new file mode 100644 index 000000000..54c7fc2b3 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java @@ -0,0 +1,78 @@ +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.taskdefs.Property; +import java.io.File; + +public class FindTest extends BuildFileTest { + + public FindTest(String name) { + super(name); + } + + public void setUp() { + configureProject("build.xml"); + } + + public void testMissingFile() { + Find find = new Find(); + try { + find.execute(); + fail("No 'no-file'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "file not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingLocation() { + Find find = new Find(); + find.setFile("ant.jar"); + try { + find.execute(); + fail("No 'no-location'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "location not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingPath() { + Find find = new Find(); + find.setFile("ant.jar"); + find.setLocation("location.ant-jar"); + try { + find.execute(); + fail("No 'no-fileset'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "path not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testFileNotPresent() { + executeTarget("testFileNotPresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNull("Property set to wrong value.", result); + } + + public void testFilePresent() { + executeTarget("testFilePresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNotNull("Property not set.", result); + assertTrue("Wrong file found.", result.endsWith("ant.jar")); + } + + + public void testMultipleFiles() { + executeTarget("testMultipleFiles"); + String result = getProject().getProperty("location.test"); + assertNotNull("Property not set.", result); + assertTrue("Only one file found.", result.indexOf(";") > -1); + //assertTrue("Wrong file found.", result.endsWith("ant.jar")); + } + + +} diff --git a/src/tutorial/tasks-filesets-properties/final/build.xml b/src/tutorial/tasks-filesets-properties/final/build.xml new file mode 100644 index 000000000..39c18a2d6 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/final/build.xml @@ -0,0 +1,162 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="FindTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the Find-Task" depends="jar"> + <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/> + </target> + + <target name="use.simple" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + <fileset dir="c:/seu" includes="ant*/**/*.jar"/> + </path> + </find> + <echo>ant.jar found on ${location.ant-jar}</echo> + </target> + + <target name="use.simple2" depends="use.init"> + <find file="ant.jar" location="location.ant-jar" delimiter=";"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + <fileset dir="c:/seu" includes="ant*/**/*.jar"/> + </path> + </find> + <echo>ant.jar found on ${location.ant-jar}</echo> + </target> + + <target name="testFileNotPresent" depends="use.init"> + <find file="a-not-existing-file.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="testFilePresent" depends="use.init"> + <find file="ant.jar" location="location.ant-jar"> + <path> + <fileset dir="${ant.home}" includes="**/*.jar"/> + </path> + </find> + </target> + + <target name="test.init"> + <mkdir dir="test1/dir11/dir111"/> + <mkdir dir="test1/dir11/dir112"/> + <mkdir dir="test1/dir12/"/> + <mkdir dir="test1/dir13/dir131"/> + <mkdir dir="test1/dir13/dir132"/> + <touch file="test1/dir11/dir111/test"/> + <touch file="test1/dir11/dir111/not"/> + <touch file="test1/dir11/dir111/not2"/> + <touch file="test1/dir11/dir112/test"/> + <touch file="test1/dir11/dir112/not"/> + <touch file="test1/dir11/dir112/not2"/> + <touch file="test1/dir12/test"/> + <touch file="test1/dir12/not"/> + <touch file="test1/dir12/not2"/> + <touch file="test1/dir13/dir131/test"/> + <touch file="test1/dir13/dir131/not"/> + <touch file="test1/dir13/dir131/not2"/> + <touch file="test1/dir13/dir132/test"/> + <touch file="test1/dir13/dir132/not"/> + <touch file="test1/dir13/dir132/not2"/> + <mkdir dir="test2"/> + <copy todir="test2"> + <fileset dir="test1"/> + </copy> + </target> + + <target name="testMultipleFiles" depends="use.init,test.init"> + <touch file="test1/dir11/dir111/test"/> + <find file="test" location="location.test" delimiter=";"> + <path> + <fileset dir="test1"/> + <fileset dir="test2"/> + </path> + </find> + <delete> + <fileset dir="test1"/> + <fileset dir="test2"/> + </delete> + </target> + + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" depends="jar"> + <junit printsummary="no" haltonfailure="no"> + <classpath refid="classpath.test"/> + <sysproperty key="ant.home" value="${ant.home}"/> + <formatter type="brief" usefile="false"/> + <batchtest fork="true" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="test2" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + + +</project> diff --git a/src/tutorial/tasks-filesets-properties/final/find.html b/src/tutorial/tasks-filesets-properties/final/find.html new file mode 100644 index 000000000..7f781a618 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/final/find.html @@ -0,0 +1,68 @@ +<html> + +<head> +<meta http-equiv="Content-Language" content="en-us"> +<title> Find Task</title> +</head> + +<body> + +<h2><a name="find">Find</a></h2> +<h3>Description</h3> +<p>Searchs in a given path for a file and returns the absolute to it as property. +If delimiter is set this task returns all found locations.</p> + +<h3>Parameters</h3> +<table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">file</td> + <td valign="top">The name of the file to search.</td> + <td align="center" valign="top">yes</td> + </tr> + <tr> + <td valign="top">location</td> + <td valign="top">The name of the property where to store the location</td> + <td align="center" valign="top">yes</td> + </tr> + <tr> + <td valign="top">delimiter</td> + <td valign="top">A delimiter to use when returning the list</td> + <td align="center" valign="top">only if the list is required</td> + </tr> +</table> + +<h3>Parameters specified as nested elements</h3> + +<h4>path</h4> +<p>The path where to search the file.</p> + +<h3>Examples</h3> +<pre> + <find file="ant.jar" location="loc"> + <path> + <fileset dir="${ant.home}"/> + <path> + </find> +</pre> +Searches in Ants home directory for a file <i>ant.jar</i> and stores its location in +property <i>loc</i> (should be ANT_HOME/bin/ant.jar). + +<pre> + <find file="ant.jar" location="loc" delimiter=";"> + <path> + <fileset dir="C:/"/> + <path> + </find> + <echo>ant.jar found in: ${loc}</echo> +</pre> +Searches in Windows C: drive for all <i>ant.jar</i> and stores their locations in +property <i>loc</i> delimited with <i>';'</i>. (should need a long time :-) +After that it prints out the result (e.g. C:/ant-1.5.4/bin/ant.jar;C:/ant-1.6/bin/ant.jar). + +</body> +</html> diff --git a/src/tutorial/tasks-filesets-properties/final/src/Find.java b/src/tutorial/tasks-filesets-properties/final/src/Find.java new file mode 100644 index 000000000..96fc06f02 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/final/src/Find.java @@ -0,0 +1,141 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Ant" and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.tools.ant.taskdefs; + + +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.DirectoryScanner; + +import java.util.Vector; +import java.util.Iterator; +import java.io.File; + +public class Find extends Task { + + // ===== internal attributes ===== + + private Vector foundFiles = new Vector(); + + // ===== attribute support ===== + + private String file; + private String location; + private Vector paths = new Vector(); + private String delimiter = null; + + public void setFile(String file) { + this.file = file; + } + + public void setLocation(String location) { + this.location = location; + } + + public void addPath(Path path) { + paths.add(path); + } + + public void setDelimiter(String delim) { + delimiter = delim; + } + + // ===== the tasks work ===== + + protected void validate() { + if (file==null) throw new BuildException("file not set"); + if (location==null) throw new BuildException("location not set"); + if (paths.size()<1) throw new BuildException("path not set"); + } + + public void execute() { + validate(); + // find all files + for(Iterator itPaths = paths.iterator(); itPaths.hasNext(); ) { + Path path = (Path)itPaths.next(); + String[] includedFiles = path.list(); + for(int i=0; i<includedFiles.length; i++) { + String filename = includedFiles[i].replace('\\','/'); + filename = filename.substring(filename.lastIndexOf("/")+1); + if (file.equals(filename) && !foundFiles.contains(includedFiles[i])) { + foundFiles.add(includedFiles[i]); + } + } + } + + // create the return value (list/single) + String rv = null; + if (foundFiles.size() > 0) { + if (delimiter==null) { + // only the first + rv = (String)foundFiles.elementAt(0); + } else { + // create list + StringBuffer list = new StringBuffer(); + for(Iterator it=foundFiles.iterator(); it.hasNext(); ) { + list.append(it.next()); + if (it.hasNext()) list.append(delimiter); + } + rv = list.toString(); + } + } + + // create the property + if (rv!=null) + getProject().setNewProperty(location, rv); + } + +} diff --git a/src/tutorial/tasks-filesets-properties/final/src/FindTest.java b/src/tutorial/tasks-filesets-properties/final/src/FindTest.java new file mode 100644 index 000000000..117505347 --- /dev/null +++ b/src/tutorial/tasks-filesets-properties/final/src/FindTest.java @@ -0,0 +1,134 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Ant" and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + */ + +package org.apache.tools.ant.taskdefs; + + +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.taskdefs.Property; +import java.io.File; + +public class FindTest extends BuildFileTest { + + public FindTest(String name) { + super(name); + } + + public void setUp() { + configureProject("build.xml"); + } + + public void testMissingFile() { + Find find = new Find(); + try { + find.execute(); + fail("No 'no-file'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "file not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingLocation() { + Find find = new Find(); + find.setFile("ant.jar"); + try { + find.execute(); + fail("No 'no-location'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "location not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testMissingPath() { + Find find = new Find(); + find.setFile("ant.jar"); + find.setLocation("location.ant-jar"); + try { + find.execute(); + fail("No 'no-fileset'-exception thrown."); + } catch (Exception e) { + // exception expected + String expected = "path not set"; + assertEquals("Wrong exception message.", expected, e.getMessage()); + } + } + + public void testFileNotPresent() { + executeTarget("testFileNotPresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNull("Property set to wrong value.", result); + } + + public void testFilePresent() { + executeTarget("testFilePresent"); + String result = getProject().getProperty("location.ant-jar"); + assertNotNull("Property not set.", result); + assertTrue("Wrong file found.", result.endsWith("ant.jar")); + } + + + public void testMultipleFiles() { + executeTarget("testMultipleFiles"); + String result = getProject().getProperty("location.test"); + assertNotNull("Property not set.", result); + assertTrue("Only one file found.", result.indexOf(";") > -1); + } + + +} diff --git a/src/tutorial/tasks-start-writing/build.xml b/src/tutorial/tasks-start-writing/build.xml new file mode 100644 index 000000000..8e502c1b6 --- /dev/null +++ b/src/tutorial/tasks-start-writing/build.xml @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project name="MyTask" basedir="." default="test"> + + <property name="src.dir" value="src"/> + <property name="classes.dir" value="classes"/> + + <property name="ant.test.lib" value="ant-testutil.jar"/> + <property name="report.dir" value="report"/> + <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/> + <property name="junit.out.dir.html" value="${report.dir}/junit/html"/> + + <path id="classpath.run"> + <path path="${java.class.path}"/> + <path location="${ant.project.name}.jar"/> + </path> + + <path id="classpath.test"> + <path refid="classpath.run"/> + <path location="${ant.test.lib}"/> + </path> + + <target name="clean" description="Delete all generated files"> + <delete failonerror="false" includeEmptyDirs="true"> + <fileset dir="." includes="${ant.project.name}.jar"/> + <fileset dir="${classes.dir}"/> + <fileset dir="${report.dir}"/> + </delete> + </target> + + <target name="compile" description="Compiles the Task"> + <mkdir dir="${classes.dir}"/> + <javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/> + </target> + + <target name="jar" description="JARs the Task" depends="compile"> + <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/> + </target> + + + <target name="use.init" description="Taskdef´ the HelloWorld-Task" depends="jar"> + <taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/> + </target> + + + <target name="use.without" description="Use without any" depends="use.init"> + <helloworld/> + </target> + + <target name="use.message" description="Use with attribute 'message'" depends="use.init"> + <helloworld message="attribute-text"/> + </target> + + <target name="use.fail" description="Use with attribute 'fail'" depends="use.init"> + <helloworld fail="true"/> + </target> + + <target name="use.nestedText" description="Use with nested text" depends="use.init"> + <helloworld>nested-text</helloworld> + </target> + + <target name="use.nestedElement" description="Use with nested 'message'" depends="use.init"> + <helloworld> + <message msg="Nested Element 1"/> + <message msg="Nested Element 2"/> + </helloworld> + </target> + + + <target name="use" + description="Try all (w/out use.fail)" + depends="use.without,use.message,use.nestedText,use.nestedElement" + /> + + + <target name="junit" description="Runs the unit tests" depends="jar"> + <delete dir="${junit.out.dir.xml}" /> + <mkdir dir="${junit.out.dir.xml}" /> + <junit printsummary="yes" haltonfailure="no"> + <classpath refid="classpath.test"/> + <formatter type="xml"/> + <batchtest fork="yes" todir="${junit.out.dir.xml}"> + <fileset dir="${src.dir}" includes="**/*Test.java"/> + </batchtest> + </junit> + </target> + + <target name="junitreport" description="Create a report for the rest result"> + <mkdir dir="${junit.out.dir.html}" /> + <junitreport todir="${junit.out.dir.html}"> + <fileset dir="${junit.out.dir.xml}"> + <include name="*.xml"/> + </fileset> + <report format="frames" todir="${junit.out.dir.html}"/> + </junitreport> + </target> + + <target name="test" + depends="junit,junitreport" + description="Runs unit tests and creates a report" + /> + + +</project> diff --git a/src/tutorial/tasks-start-writing/src/HelloWorld.java b/src/tutorial/tasks-start-writing/src/HelloWorld.java new file mode 100644 index 000000000..eaa42d6ad --- /dev/null +++ b/src/tutorial/tasks-start-writing/src/HelloWorld.java @@ -0,0 +1,70 @@ +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import java.util.Vector; +import java.util.Iterator; + +/** + * The task of the tutorial. + * Prints a message or let the build fail. + * @author Jan Matérne + * @since 2003-08-19 + */ +public class HelloWorld extends Task { + + + /** The message to print. As attribute. */ + String message; + public void setMessage(String msg) { + message = msg; + } + + /** Should the build fail? Defaults to <i>false</i>. As attribute. */ + boolean fail = false; + public void setFail(boolean b) { + fail = b; + } + + /** Support for nested text. */ + public void addText(String text) { + message = text; + } + + + /** Do the work. */ + public void execute() { + // handle attribute 'fail' + if (fail) throw new BuildException("Fail requested."); + + // handle attribute 'message' and nested text + if (message!=null) log(message); + + // handle nested elements + for (Iterator it=messages.iterator(); it.hasNext(); ) { + Message msg = (Message)it.next(); + log(msg.getMsg()); + } + } + + + /** Store nested 'message's. */ + Vector messages = new Vector(); + + /** Factory method for creating nested 'message's. */ + public Message createMessage() { + Message msg = new Message(); + messages.add(msg); + return msg; + } + + /** A nested 'message'. */ + public class Message { + // Bean constructor + public Message() {} + + /** Message to print. */ + String msg; + public void setMsg(String msg) { this.msg = msg; } + public String getMsg() { return msg; } + } + +}
\ No newline at end of file diff --git a/src/tutorial/tasks-start-writing/src/HelloWorldTest.java b/src/tutorial/tasks-start-writing/src/HelloWorldTest.java new file mode 100644 index 000000000..87a7e3142 --- /dev/null +++ b/src/tutorial/tasks-start-writing/src/HelloWorldTest.java @@ -0,0 +1,40 @@ +import org.apache.tools.ant.BuildFileTest; + +public class HelloWorldTest extends BuildFileTest { + + public HelloWorldTest(String s) { + super(s); + } + + public void setUp() { + // initialize Ant + configureProject("build.xml"); + } + + public void testWithout() { + executeTarget("use.without"); + assertEquals("Message was logged but should not.", getLog(), ""); + } + + public void testMessage() { + // execute target 'use.nestedText' and expect a message + // 'attribute-text' in the log + expectLog("use.message", "attribute-text"); + } + + public void testFail() { + // execute target 'use.fail' and expect a BuildException + // with text 'Fail requested.' + expectBuildException("use.fail", "Fail requested."); + } + + public void testNestedText() { + expectLog("use.nestedText", "nested-text"); + } + + public void testNestedElement() { + executeTarget("use.nestedElement"); + assertLogContaining("Nested Element 1"); + assertLogContaining("Nested Element 2"); + } +} |