Browse Source

Add tutorial sources

master
Gintas Grigelionis 7 years ago
parent
commit
d5f4b681c4
19 changed files with 1621 additions and 0 deletions
  1. +79
    -0
      src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml
  2. +33
    -0
      src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java
  3. +16
    -0
      src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java
  4. +103
    -0
      src/tutorial/tasks-filesets-properties/02-fileset/build.xml
  5. +55
    -0
      src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java
  6. +68
    -0
      src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java
  7. +108
    -0
      src/tutorial/tasks-filesets-properties/03-paths/build.xml
  8. +52
    -0
      src/tutorial/tasks-filesets-properties/03-paths/src/Find.java
  9. +68
    -0
      src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java
  10. +162
    -0
      src/tutorial/tasks-filesets-properties/04-lists/build.xml
  11. +81
    -0
      src/tutorial/tasks-filesets-properties/04-lists/src/Find.java
  12. +78
    -0
      src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java
  13. +162
    -0
      src/tutorial/tasks-filesets-properties/final/build.xml
  14. +68
    -0
      src/tutorial/tasks-filesets-properties/final/find.html
  15. +141
    -0
      src/tutorial/tasks-filesets-properties/final/src/Find.java
  16. +134
    -0
      src/tutorial/tasks-filesets-properties/final/src/FindTest.java
  17. +103
    -0
      src/tutorial/tasks-start-writing/build.xml
  18. +70
    -0
      src/tutorial/tasks-start-writing/src/HelloWorld.java
  19. +40
    -0
      src/tutorial/tasks-start-writing/src/HelloWorldTest.java

+ 79
- 0
src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml View File

@@ -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>

+ 33
- 0
src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java View File

@@ -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);
}
}

}

+ 16
- 0
src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java View File

@@ -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");
}
}

+ 103
- 0
src/tutorial/tasks-filesets-properties/02-fileset/build.xml View File

@@ -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>

+ 55
- 0
src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java View File

@@ -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);
}

}

+ 68
- 0
src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java View File

@@ -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"));
}

}

+ 108
- 0
src/tutorial/tasks-filesets-properties/03-paths/build.xml View File

@@ -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>

+ 52
- 0
src/tutorial/tasks-filesets-properties/03-paths/src/Find.java View File

@@ -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);
}

}

+ 68
- 0
src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java View File

@@ -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"));
}

}

+ 162
- 0
src/tutorial/tasks-filesets-properties/04-lists/build.xml View File

@@ -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>

+ 81
- 0
src/tutorial/tasks-filesets-properties/04-lists/src/Find.java View File

@@ -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);
}

}

+ 78
- 0
src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java View File

@@ -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"));
}


}

+ 162
- 0
src/tutorial/tasks-filesets-properties/final/build.xml View File

@@ -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>

+ 68
- 0
src/tutorial/tasks-filesets-properties/final/find.html View File

@@ -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>
&lt;find file="ant.jar" location="loc">
&lt;path>
&lt;fileset dir="${ant.home}"/>
&lt;path>
&lt;/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>
&lt;find file="ant.jar" location="loc" delimiter=";">
&lt;path>
&lt;fileset dir="C:/"/>
&lt;path>
&lt;/find>
&lt;echo>ant.jar found in: ${loc}&lt;/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>

+ 141
- 0
src/tutorial/tasks-filesets-properties/final/src/Find.java View File

@@ -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);
}

}

+ 134
- 0
src/tutorial/tasks-filesets-properties/final/src/FindTest.java View File

@@ -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);
}


}

+ 103
- 0
src/tutorial/tasks-start-writing/build.xml View File

@@ -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>

+ 70
- 0
src/tutorial/tasks-start-writing/src/HelloWorld.java View File

@@ -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; }
}

}

+ 40
- 0
src/tutorial/tasks-start-writing/src/HelloWorldTest.java View File

@@ -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");
}
}

Loading…
Cancel
Save