Browse Source

Modernize tutorials

master
Gintas Grigelionis 7 years ago
parent
commit
fb75550cdc
19 changed files with 604 additions and 412 deletions
  1. +19
    -8
      src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml
  2. +17
    -0
      src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java
  3. +31
    -7
      src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java
  4. +19
    -7
      src/tutorial/tasks-filesets-properties/02-fileset/build.xml
  5. +30
    -14
      src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java
  6. +53
    -37
      src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java
  7. +19
    -5
      src/tutorial/tasks-filesets-properties/03-paths/build.xml
  8. +29
    -16
      src/tutorial/tasks-filesets-properties/03-paths/src/Find.java
  9. +53
    -37
      src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java
  10. +19
    -7
      src/tutorial/tasks-filesets-properties/04-lists/build.xml
  11. +38
    -25
      src/tutorial/tasks-filesets-properties/04-lists/src/Find.java
  12. +55
    -41
      src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java
  13. +20
    -8
      src/tutorial/tasks-filesets-properties/final/build.xml
  14. +18
    -2
      src/tutorial/tasks-filesets-properties/final/find.html
  15. +35
    -77
      src/tutorial/tasks-filesets-properties/final/src/Find.java
  16. +52
    -91
      src/tutorial/tasks-filesets-properties/final/src/FindTest.java
  17. +21
    -8
      src/tutorial/tasks-start-writing/build.xml
  18. +25
    -8
      src/tutorial/tasks-start-writing/src/HelloWorld.java
  19. +51
    -14
      src/tutorial/tasks-start-writing/src/HelloWorldTest.java

+ 19
- 8
src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="FindTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -36,8 +52,7 @@
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
</target>


<target name="use.init" description="Taskdef´ the Find-Task" depends="jar">
<target name="use.init" description="Taskdef the Find-Task" depends="jar">
<taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
</target>

@@ -46,8 +61,6 @@
<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}" />
@@ -72,8 +85,6 @@

<target name="test"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>

description="Runs unit tests and creates a report"/>

</project>

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

@@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;



+ 31
- 7
src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java View File

@@ -1,16 +1,40 @@
import org.apache.tools.ant.BuildFileTest;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class FindTest extends BuildFileTest {
import static org.junit.Assert.assertEquals;

public FindTest(String name) {
super(name);
}
public class FindTest {

@Rule
public BuildFileRule rule = new BuildFileRule();

@Before
public void setUp() {
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testSimple() {
expectLog("use.simple", "test-value");
rule.executeTarget("use.simple");
assertEquals("test-value", rule.getLog());
}
}

+ 19
- 7
src/tutorial/tasks-filesets-properties/02-fileset/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="FindTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -36,8 +52,7 @@
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
</target>


<target name="use.init" description="Taskdef´ the Find-Task" depends="jar">
<target name="use.init" description="Taskdef the Find-Task" depends="jar">
<taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
</target>

@@ -59,7 +74,6 @@
</find>
</target>


<target name="junit" description="Runs the unit tests" depends="jar">
<delete dir="${junit.out.dir.xml}" />
<mkdir dir="${junit.out.dir.xml}" />
@@ -96,8 +110,6 @@

<target name="test2"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>

description="Runs unit tests and creates a report"/>

</project>

+ 30
- 14
src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java View File

@@ -1,17 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
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.util.ArrayList;
import java.util.List;
import java.io.File;

public class Find extends Task {

private String file;
private String location;
private Vector filesets = new Vector();
private List<FileSet> filesets = new ArrayList<>();

public void setFile(String file) {
this.file = file;
@@ -26,29 +43,28 @@ public class Find extends Task {
}

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");
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();
for (FileSet fs : filesets) {
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)) {
for (String includedFile : includedFiles) {
String filename = includedFile.replace('\\','/');
filename = filename.substring(filename.lastIndexOf("/") + 1);
if (foundLocation == null && file.equals(filename)) {
File base = ds.getBasedir();
File found = new File(base, includedFiles[i]);
File found = new File(base, includedFile);
foundLocation = found.getAbsolutePath();
}
}
}
if (foundLocation!=null)
if (foundLocation != null)
getProject().setNewProperty(location, foundLocation);
}



+ 53
- 37
src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java View File

@@ -1,66 +1,82 @@
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Property;
import java.io.File;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class FindTest extends BuildFileTest {
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public FindTest(String name) {
super(name);
}
public class FindTest {

@Rule
public BuildFileRule rule = new BuildFileRule();

@Rule
public ExpectedException tried = ExpectedException.none();

@Before
public void setUp() {
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testMissingFile() {
tried.expect(BuildException.class);
tried.expectMessage("file not set");
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());
}
find.execute();
}

@Test
public void testMissingLocation() {
tried.expect(BuildException.class);
tried.expectMessage("location not set");
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());
}
find.execute();
}

@Test
public void testMissingFileset() {
tried.expect(BuildException.class);
tried.expectMessage("fileset not set");
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());
}
find.execute();
}

@Test
public void testFileNotPresent() {
executeTarget("testFileNotPresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFileNotPresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNull("Property set to wrong value.", result);
}

@Test
public void testFilePresent() {
executeTarget("testFilePresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFilePresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNotNull("Property not set.", result);
assertTrue("Wrong file found.", result.endsWith("ant.jar"));
}


+ 19
- 5
src/tutorial/tasks-filesets-properties/03-paths/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="FindTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -36,8 +52,7 @@
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
</target>


<target name="use.init" description="Taskdef´ the Find-Task" depends="jar">
<target name="use.init" description="Taskdef the Find-Task" depends="jar">
<taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
</target>

@@ -102,7 +117,6 @@

<target name="test2"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>
description="Runs unit tests and creates a report"/>

</project>

+ 29
- 16
src/tutorial/tasks-filesets-properties/03-paths/src/Find.java View File

@@ -1,17 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
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;
import java.util.ArrayList;
import java.util.List;

public class Find extends Task {

private String file;
private String location;
private Vector paths = new Vector();
private List<Path> paths = new ArrayList<>();

public void setFile(String file) {
this.file = file;
@@ -26,26 +41,24 @@ public class Find extends Task {
}

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");
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)) {
for (Path path : paths) {
for (String includedFile : path.list()) {
String filename = includedFile.replace('\\','/');
filename = filename.substring(filename.lastIndexOf("/") + 1);
if (foundLocation == null && file.equals(filename)) {
foundLocation = includedFiles[i];
}
}
}
if (foundLocation!=null)
if (foundLocation != null)
getProject().setNewProperty(location, foundLocation);
}



+ 53
- 37
src/tutorial/tasks-filesets-properties/03-paths/src/FindTest.java View File

@@ -1,66 +1,82 @@
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Property;
import java.io.File;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class FindTest extends BuildFileTest {
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public FindTest(String name) {
super(name);
}
public class FindTest {

@Rule
public BuildFileRule rule = new BuildFileRule();

@Rule
public ExpectedException tried = ExpectedException.none();

@Before
public void setUp() {
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testMissingFile() {
tried.expect(BuildException.class);
tried.expectMessage("file not set");
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());
}
find.execute();
}

@Test
public void testMissingLocation() {
tried.expect(BuildException.class);
tried.expectMessage("location not set");
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());
}
find.execute();
}

@Test
public void testMissingPath() {
tried.expect(BuildException.class);
tried.expectMessage("path not set");
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());
}
find.execute();
}

@Test
public void testFileNotPresent() {
executeTarget("testFileNotPresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFileNotPresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNull("Property set to wrong value.", result);
}

@Test
public void testFilePresent() {
executeTarget("testFilePresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFilePresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNotNull("Property not set.", result);
assertTrue("Wrong file found.", result.endsWith("ant.jar"));
}


+ 19
- 7
src/tutorial/tasks-filesets-properties/04-lists/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="FindTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -36,8 +52,7 @@
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
</target>


<target name="use.init" description="Taskdef´ the Find-Task" depends="jar">
<target name="use.init" description="Taskdef the Find-Task" depends="jar">
<taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
</target>

@@ -118,7 +133,6 @@
</delete>
</target>


<target name="junit" description="Runs the unit tests" depends="jar">
<delete dir="${junit.out.dir.xml}" />
<mkdir dir="${junit.out.dir.xml}" />
@@ -155,8 +169,6 @@

<target name="test2"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>

description="Runs unit tests and creates a report"/>

</project>

+ 38
- 25
src/tutorial/tasks-filesets-properties/04-lists/src/Find.java View File

@@ -1,23 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
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;
import java.util.ArrayList;
import java.util.List;

public class Find extends Task {

// ===== internal attributes =====

private Vector foundFiles = new Vector();
private List<String> foundFiles = new ArrayList<>();

// ===== attribute support =====

private String file;
private String location;
private Vector paths = new Vector();
private List<Path> paths = new ArrayList<>();
private String delimiter = null;

public void setFile(String file) {
@@ -39,42 +54,40 @@ public class Find extends Task {
// ===== 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");
if (file == null) throw new BuildException("file not set");
if (location == null) throw new BuildException("location not set");
if (paths.isEmpty()) 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]);
for (Path path : paths) {
for (String includedFile : path.list()) {
String filename = includedFile.replace('\\','/');
filename = filename.substring(filename.lastIndexOf("/") + 1);
if (file.equals(filename) && !foundFiles.contains(includedFile)) {
foundFiles.add(includedFile);
}
}
}
String rv = null;
if (foundFiles.size() > 0) {
if (delimiter==null) {
if (!foundFiles.isEmpty()) {
if (delimiter == null) {
// only the first
rv = (String)foundFiles.elementAt(0);
rv = foundFiles.get(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);
StringBuilder list = new StringBuilder();
for (String file : foundFiles) {
if (list.length() > 0) list.append(delimiter);
list.append(file);
}
rv = list.toString();
}
}


if (rv!=null)
if (rv != null)
getProject().setNewProperty(location, rv);
}



+ 55
- 41
src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java View File

@@ -1,78 +1,92 @@
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Property;
import java.io.File;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class FindTest extends BuildFileTest {
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public FindTest(String name) {
super(name);
}
public class FindTest {

@Rule
public BuildFileRule rule = new BuildFileRule();

@Rule
public ExpectedException tried = ExpectedException.none();

@Before
public void setUp() {
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testMissingFile() {
tried.expect(BuildException.class);
tried.expectMessage("file not set");
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());
}
find.execute();
}

@Test
public void testMissingLocation() {
tried.expect(BuildException.class);
tried.expectMessage("location not set");
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());
}
find.execute();
}

@Test
public void testMissingPath() {
tried.expect(BuildException.class);
tried.expectMessage("path not set");
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());
}
find.execute();
}

@Test
public void testFileNotPresent() {
executeTarget("testFileNotPresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFileNotPresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNull("Property set to wrong value.", result);
}

public void testFilePresent() {
executeTarget("testFilePresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFilePresent");
String result = rule.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");
rule.executeTarget("testMultipleFiles");
String result = rule.getProject().getProperty("location.test");
assertNotNull("Property not set.", result);
assertTrue("Only one file found.", result.indexOf(";") > -1);
assertTrue("Only one file found.", result.contains(";"));
//assertTrue("Wrong file found.", result.endsWith("ant.jar"));
}


}

+ 20
- 8
src/tutorial/tasks-filesets-properties/final/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="FindTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -29,15 +45,14 @@

<target name="compile" description="Compiles the Task">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/>
<javac includeantruntime="true" 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">
<target name="use.init" description="Taskdef the Find-Task" depends="jar">
<taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
</target>

@@ -118,7 +133,6 @@
</delete>
</target>


<target name="junit" description="Runs the unit tests" depends="jar">
<delete dir="${junit.out.dir.xml}" />
<mkdir dir="${junit.out.dir.xml}" />
@@ -155,8 +169,6 @@

<target name="test2"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>

description="Runs unit tests and creates a report"/>

</project>

+ 18
- 2
src/tutorial/tasks-filesets-properties/final/find.html View File

@@ -1,3 +1,19 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>

<head>
@@ -7,9 +23,9 @@

<body>

<h2><a name="find">Find</a></h2>
<h2 id="find">Find</h2>
<h3>Description</h3>
<p>Searchs in a given path for a file and returns the absolute to it as property.
<p>Searches 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>


+ 35
- 77
src/tutorial/tasks-filesets-properties/final/src/Find.java View File

@@ -1,80 +1,40 @@
/*
* The Apache Software License, Version 1.1
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 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;

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
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;
import java.util.ArrayList;
import java.util.List;

public class Find extends Task {

// ===== internal attributes =====

private Vector foundFiles = new Vector();
private List<String> foundFiles = new ArrayList<>();

// ===== attribute support =====

private String file;
private String location;
private Vector paths = new Vector();
private List<Path> paths = new ArrayList<>();
private String delimiter = null;

public void setFile(String file) {
@@ -96,45 +56,43 @@ public class Find extends Task {
// ===== 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");
if (file == null) throw new BuildException("file not set");
if (location == null) throw new BuildException("location not set");
if (paths.isEmpty()) 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]);
for (Path path : paths) {
for (String includedFile : path.list()) {
String filename = includedFile.replace('\\', '/');
filename = filename.substring(filename.lastIndexOf("/") + 1);
if (file.equals(filename) && !foundFiles.contains(includedFile)) {
foundFiles.add(includedFile);
}
}
}

// create the return value (list/single)
String rv = null;
if (foundFiles.size() > 0) {
if (delimiter==null) {
if (!foundFiles.isEmpty()) {
if (delimiter == null) {
// only the first
rv = (String)foundFiles.elementAt(0);
rv = foundFiles.get(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);
StringBuilder list = new StringBuilder();
for (String file : foundFiles) {
if (list.length() > 0) list.append(delimiter);
list.append(file);
}
rv = list.toString();
}
}

// create the property
if (rv!=null)
if (rv != null)
getProject().setNewProperty(location, rv);
}



+ 52
- 91
src/tutorial/tasks-filesets-properties/final/src/FindTest.java View File

@@ -1,134 +1,95 @@
/*
* The Apache Software License, Version 1.1
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 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;
//package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Property;
import java.io.File;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class FindTest extends BuildFileTest {
public class FindTest {

public FindTest(String name) {
super(name);
}
@Rule
public BuildFileRule rule = new BuildFileRule();

@Rule
public ExpectedException tried = ExpectedException.none();

@Before
public void setUp() {
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testMissingFile() {
tried.expect(BuildException.class);
tried.expectMessage("file not set");
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());
}
find.execute();
}

@Test
public void testMissingLocation() {
tried.expect(BuildException.class);
tried.expectMessage("location not set");
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());
}
find.execute();
}

@Test
public void testMissingPath() {
tried.expect(BuildException.class);
tried.expectMessage("path not set");
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());
}
find.execute();
}

@Test
public void testFileNotPresent() {
executeTarget("testFileNotPresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFileNotPresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNull("Property set to wrong value.", result);
}

@Test
public void testFilePresent() {
executeTarget("testFilePresent");
String result = getProject().getProperty("location.ant-jar");
rule.executeTarget("testFilePresent");
String result = rule.getProject().getProperty("location.ant-jar");
assertNotNull("Property not set.", result);
assertTrue("Wrong file found.", result.endsWith("ant.jar"));
}

@Test
public void testMultipleFiles() {
executeTarget("testMultipleFiles");
String result = getProject().getProperty("location.test");
rule.executeTarget("testMultipleFiles");
String result = rule.getProject().getProperty("location.test");
assertNotNull("Property not set.", result);
assertTrue("Only one file found.", result.indexOf(";") > -1);
assertTrue("Only one file found.", result.contains(";"));
}


}

+ 21
- 8
src/tutorial/tasks-start-writing/build.xml View File

@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="MyTask" basedir="." default="test">

<property name="src.dir" value="src"/>
@@ -29,7 +45,7 @@

<target name="compile" description="Compiles the Task">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/>
<javac includeantruntime="true" srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/>
</target>

<target name="jar" description="JARs the Task" depends="compile">
@@ -37,7 +53,7 @@
</target>


<target name="use.init" description="Taskdef´ the HelloWorld-Task" depends="jar">
<target name="use.init" description="Taskdef the HelloWorld-Task" depends="jar">
<taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/>
</target>

@@ -68,8 +84,7 @@

<target name="use"
description="Try all (w/out use.fail)"
depends="use.without,use.message,use.nestedText,use.nestedElement"
/>
depends="use.without,use.message,use.nestedText,use.nestedElement"/>


<target name="junit" description="Runs the unit tests" depends="jar">
@@ -96,8 +111,6 @@

<target name="test"
depends="junit,junitreport"
description="Runs unit tests and creates a report"
/>

description="Runs unit tests and creates a report"/>

</project>

+ 25
- 8
src/tutorial/tasks-start-writing/src/HelloWorld.java View File

@@ -1,11 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import java.util.Vector;
import java.util.Iterator;

import java.util.ArrayList;
import java.util.List;

/**
* The task of the tutorial.
* Prints a message or let the build fail.
* Prints a message or lets the build fail.
* @author Jan Matérne
* @since 2003-08-19
*/
@@ -36,18 +54,17 @@ public class HelloWorld extends Task {
if (fail) throw new BuildException("Fail requested.");

// handle attribute 'message' and nested text
if (message!=null) log(message);
if (message != null) log(message);

// handle nested elements
for (Iterator it=messages.iterator(); it.hasNext(); ) {
Message msg = (Message)it.next();
for (Message msg : messages) {
log(msg.getMsg());
}
}


/** Store nested 'message's. */
Vector messages = new Vector();
List<Message> messages = new ArrayList<>();

/** Factory method for creating nested 'message's. */
public Message createMessage() {
@@ -67,4 +84,4 @@ public class HelloWorld extends Task {
public String getMsg() { return msg; }
}

}
}

+ 51
- 14
src/tutorial/tasks-start-writing/src/HelloWorldTest.java View File

@@ -1,40 +1,77 @@
import org.apache.tools.ant.BuildFileTest;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class HelloWorldTest extends BuildFileTest {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public HelloWorldTest(String s) {
super(s);
}
public class HelloWorldTest {

@Rule
public BuildFileRule rule = new BuildFileRule();

@Rule
public ExpectedException tried = ExpectedException.none();

@Before
public void setUp() {
// initialize Ant
configureProject("build.xml");
rule.configureProject("build.xml");
}

@Test
public void testWithout() {
executeTarget("use.without");
assertEquals("Message was logged but should not.", getLog(), "");
rule.executeTarget("use.without");
assertEquals("Message was logged but should not.", "", rule.getLog());
}

@Test
public void testMessage() {
// execute target 'use.nestedText' and expect a message
// 'attribute-text' in the log
expectLog("use.message", "attribute-text");
rule.executeTarget("use.message");
assertEquals(rule.getLog(), "attribute-text");
}

@Test
public void testFail() {
tried.expect(BuildException.class);
tried.expectMessage("Fail requested.");
// execute target 'use.fail' and expect a BuildException
// with text 'Fail requested.'
expectBuildException("use.fail", "Fail requested.");
rule.executeTarget("use.fail");
}

@Test
public void testNestedText() {
expectLog("use.nestedText", "nested-text");
rule.executeTarget("use.nestedText");
assertEquals("nested-text", rule.getLog());
}

@Test
public void testNestedElement() {
executeTarget("use.nestedElement");
assertLogContaining("Nested Element 1");
assertLogContaining("Nested Element 2");
rule.executeTarget("use.nestedElement");
assertTrue(rule.getLog().contains("Nested Element 1"));
assertTrue(rule.getLog().contains("Nested Element 2"));
}
}

Loading…
Cancel
Save