diff options
| author | Gintas Grigelionis <gintas@apache.org> | 2018-02-28 07:55:51 +0100 |
|---|---|---|
| committer | Gintas Grigelionis <gintas@apache.org> | 2018-02-28 07:59:42 +0100 |
| commit | fb75550cdc6378f32e0b7aa7f6aa0a452b6fd6ca (patch) | |
| tree | 1a32223135236b79347f105ada6ac3ad1dbfbc71 /src/tutorial/tasks-filesets-properties/02-fileset | |
| parent | d5f4b681c48ca7b74fdd6ce13dae39f6684aa010 (diff) | |
| download | ant-fb75550cdc6378f32e0b7aa7f6aa0a452b6fd6ca.tar.gz | |
Modernize tutorials
Diffstat (limited to 'src/tutorial/tasks-filesets-properties/02-fileset')
3 files changed, 102 insertions, 58 deletions
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 @@ -<?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> 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<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); } diff --git a/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java b/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java index 819bb032a..5e70d7671 100644 --- a/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java +++ b/src/tutorial/tasks-filesets-properties/02-fileset/src/FindTest.java @@ -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")); } |
