From fb75550cdc6378f32e0b7aa7f6aa0a452b6fd6ca Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Wed, 28 Feb 2018 07:55:51 +0100 Subject: [PATCH] Modernize tutorials --- .../01-propertyaccess/build.xml | 27 +++- .../01-propertyaccess/src/Find.java | 17 +++ .../01-propertyaccess/src/FindTest.java | 38 ++++- .../02-fileset/build.xml | 26 +++- .../02-fileset/src/Find.java | 44 ++++-- .../02-fileset/src/FindTest.java | 90 ++++++----- .../03-paths/build.xml | 24 ++- .../03-paths/src/Find.java | 45 ++++-- .../03-paths/src/FindTest.java | 90 ++++++----- .../04-lists/build.xml | 26 +++- .../04-lists/src/Find.java | 63 +++++--- .../04-lists/src/FindTest.java | 96 +++++++----- .../tasks-filesets-properties/final/build.xml | 28 +++- .../tasks-filesets-properties/final/find.html | 20 ++- .../final/src/Find.java | 112 +++++--------- .../final/src/FindTest.java | 143 +++++++----------- src/tutorial/tasks-start-writing/build.xml | 29 +++- .../tasks-start-writing/src/HelloWorld.java | 33 +++- .../src/HelloWorldTest.java | 65 ++++++-- 19 files changed, 604 insertions(+), 412 deletions(-) diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml b/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml index 73b26ad7f..b217c22ce 100644 --- a/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/build.xml @@ -1,4 +1,20 @@ - + + @@ -36,8 +52,7 @@ - - + @@ -46,8 +61,6 @@ - - @@ -72,8 +85,6 @@ - + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java index a2f44ddfa..7e202bf05 100644 --- a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/Find.java @@ -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; diff --git a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java index 1e88a3c40..e5b5bacea 100644 --- a/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java +++ b/src/tutorial/tasks-filesets-properties/01-propertyaccess/src/FindTest.java @@ -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()); } } diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/build.xml b/src/tutorial/tasks-filesets-properties/02-fileset/build.xml index 81ba60b2a..a3b3cd71c 100644 --- a/src/tutorial/tasks-filesets-properties/02-fileset/build.xml +++ b/src/tutorial/tasks-filesets-properties/02-fileset/build.xml @@ -1,4 +1,20 @@ - + + @@ -36,8 +52,7 @@ - - + @@ -59,7 +74,6 @@ - @@ -96,8 +110,6 @@ - + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java b/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java index 04067bb0f..4e4fc1169 100644 --- a/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/02-fileset/src/Find.java @@ -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 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 + + @@ -36,8 +52,7 @@ - - + @@ -102,7 +117,6 @@ + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java b/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java index a05ac5814..d846fa74d 100644 --- a/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/03-paths/src/Find.java @@ -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 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 + + @@ -36,8 +52,7 @@ - - + @@ -118,7 +133,6 @@ - @@ -155,8 +169,6 @@ - + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java index c608af9e3..688c8de34 100644 --- a/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/04-lists/src/Find.java @@ -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 foundFiles = new ArrayList<>(); // ===== attribute support ===== private String file; private String location; - private Vector paths = new Vector(); + private List 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 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); } diff --git a/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java b/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java index 54c7fc2b3..836325f67 100644 --- a/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java +++ b/src/tutorial/tasks-filesets-properties/04-lists/src/FindTest.java @@ -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")); } - } diff --git a/src/tutorial/tasks-filesets-properties/final/build.xml b/src/tutorial/tasks-filesets-properties/final/build.xml index 39c18a2d6..ee695df8c 100644 --- a/src/tutorial/tasks-filesets-properties/final/build.xml +++ b/src/tutorial/tasks-filesets-properties/final/build.xml @@ -1,4 +1,20 @@ - + + @@ -29,15 +45,14 @@ - + - - + @@ -118,7 +133,6 @@ - @@ -155,8 +169,6 @@ - + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-filesets-properties/final/find.html b/src/tutorial/tasks-filesets-properties/final/find.html index 7f781a618..c37200c6a 100644 --- a/src/tutorial/tasks-filesets-properties/final/find.html +++ b/src/tutorial/tasks-filesets-properties/final/find.html @@ -1,3 +1,19 @@ + @@ -7,9 +23,9 @@ -

Find

+

Find

Description

-

Searchs in a given path for a file and returns the absolute to it as property. +

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.

Parameters

diff --git a/src/tutorial/tasks-filesets-properties/final/src/Find.java b/src/tutorial/tasks-filesets-properties/final/src/Find.java index 96fc06f02..8196c36d8 100644 --- a/src/tutorial/tasks-filesets-properties/final/src/Find.java +++ b/src/tutorial/tasks-filesets-properties/final/src/Find.java @@ -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 - * . */ +//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 foundFiles = new ArrayList<>(); // ===== attribute support ===== private String file; private String location; - private Vector paths = new Vector(); + private List 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 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); } diff --git a/src/tutorial/tasks-filesets-properties/final/src/FindTest.java b/src/tutorial/tasks-filesets-properties/final/src/FindTest.java index 117505347..0e0643782 100644 --- a/src/tutorial/tasks-filesets-properties/final/src/FindTest.java +++ b/src/tutorial/tasks-filesets-properties/final/src/FindTest.java @@ -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 - * . */ -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(";")); } - } diff --git a/src/tutorial/tasks-start-writing/build.xml b/src/tutorial/tasks-start-writing/build.xml index 8e502c1b6..3d5789c7b 100644 --- a/src/tutorial/tasks-start-writing/build.xml +++ b/src/tutorial/tasks-start-writing/build.xml @@ -1,4 +1,20 @@ - + + @@ -29,7 +45,7 @@ - + @@ -37,7 +53,7 @@ - + @@ -68,8 +84,7 @@ + depends="use.without,use.message,use.nestedText,use.nestedElement"/> @@ -96,8 +111,6 @@ - + description="Runs unit tests and creates a report"/> diff --git a/src/tutorial/tasks-start-writing/src/HelloWorld.java b/src/tutorial/tasks-start-writing/src/HelloWorld.java index eaa42d6ad..6b5163d42 100644 --- a/src/tutorial/tasks-start-writing/src/HelloWorld.java +++ b/src/tutorial/tasks-start-writing/src/HelloWorld.java @@ -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 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; } } -} \ No newline at end of file +} diff --git a/src/tutorial/tasks-start-writing/src/HelloWorldTest.java b/src/tutorial/tasks-start-writing/src/HelloWorldTest.java index 87a7e3142..fe9e90c13 100644 --- a/src/tutorial/tasks-start-writing/src/HelloWorldTest.java +++ b/src/tutorial/tasks-start-writing/src/HelloWorldTest.java @@ -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")); } }