Repository: groovy Updated Branches: refs/heads/master 1eb1605d8 -> b5a8e6ad3
groovy-ant: refactor test directories to be under groovy.ant not groovy.util and split out an AntTestCase Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/b5a8e6ad Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/b5a8e6ad Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/b5a8e6ad Branch: refs/heads/master Commit: b5a8e6ad3b454d8a771bd2a41cad0193c3527ee5 Parents: 1eb1605 Author: Paul King <[email protected]> Authored: Sun Aug 19 22:12:36 2018 +1000 Committer: Paul King <[email protected]> Committed: Sun Aug 19 22:12:36 2018 +1000 ---------------------------------------------------------------------- .../spec/test/builder/AntBuilderSpecTest.groovy | 19 +- .../src/test-resources/groovy/ant/AntTest.xml | 47 +++ .../groovy/ant/AntTest_import.xml | 35 ++ .../src/test-resources/groovy/util/AntTest.xml | 47 --- .../groovy/util/AntTest_import.xml | 35 -- .../src/test/groovy/groovy/ant/AntTest.groovy | 319 +++++++++++++++++++ .../test/groovy/groovy/ant/AntTestCase.groovy | 34 ++ .../groovy/groovy/ant/FileNameFinderTest.groovy | 35 ++ .../src/test/groovy/groovy/ant/SpoofTask.java | 49 +++ .../groovy/groovy/ant/SpoofTaskContainer.java | 71 +++++ .../src/test/groovy/groovy/util/AntTest.groovy | 319 ------------------- .../groovy/util/FileNameFinderTest.groovy | 38 --- .../src/test/groovy/groovy/util/SpoofTask.java | 49 --- .../groovy/groovy/util/SpoofTaskContainer.java | 72 ----- 14 files changed, 597 insertions(+), 572 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy b/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy index a62e366..710d0c2 100644 --- a/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy +++ b/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy @@ -17,25 +17,20 @@ * under the License. */ package builder + +import groovy.ant.AntTestCase + /** * Test cases for the Ant builder documentation. */ -class AntBuilderSpecTest extends GroovyTestCase { +class AntBuilderSpecTest extends AntTestCase { - void doInTmpDir(Closure cl) { + void testEcho() { + /* // tag::create_zip_builder[] def ant = new AntBuilder() // end::create_zip_builder[] - def baseDir = File.createTempDir() - ant.project.baseDir = baseDir - try { - cl.call(ant, new FileTreeBuilder(baseDir)) - } finally { - baseDir.deleteDir() - } - } - - void testEcho() { + */ // tag::example_echo[] def ant = new AntBuilder() // <1> ant.echo('hello from Ant!') // <2> http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest.xml ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest.xml b/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest.xml new file mode 100644 index 0000000..4b1fbfa --- /dev/null +++ b/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest.xml @@ -0,0 +1,47 @@ +<!-- + + 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. + +--> +<!-- +does exactly the same as the AntBuilder in AntTest... just to be sure that ant behaves the same +as what we expect from the AntBuilder + --> +<project name="test" default="full"> + + <target name="full"> + <path id="task.path"> + <pathelement location="classes"/> + </path> + <taskdef name="spoofcontainer" classname="groovy.ant.SpoofTaskContainer" classpathref="task.path"/> + <taskdef name="spoof" classname="groovy.ant.SpoofTask" classpathref="task.path"/> + + <spoofcontainer> + <spoof foo="123"/> + </spoofcontainer> + </target> + + <target name="testAntBuilderWithinGroovyTask" description="Test for GROOVY-1602"> + <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/> + <echo message="before groovy task"/> + <groovy> + ant.echo(message: 'ant builder within groovy task') + </groovy> + <echo message="after groovy task"/> + </target> +</project> http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest_import.xml ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest_import.xml b/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest_import.xml new file mode 100644 index 0000000..c0b8e04 --- /dev/null +++ b/subprojects/groovy-ant/src/test-resources/groovy/ant/AntTest_import.xml @@ -0,0 +1,35 @@ +<!-- + + 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. + +--> +<!-- +Sample file to test AntBuilder import feature + --> +<project default="firstTarget"> + + <echo message="outside targets, at the top"/> + + <target name="firstTarget"> + <echo message="inside firstTarget"/> + </target> + + <target name="2ndTarget"> + <echo message="inside 2ndTarget"/> + </target> +</project> http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest.xml ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest.xml b/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest.xml deleted file mode 100644 index 9629399..0000000 --- a/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest.xml +++ /dev/null @@ -1,47 +0,0 @@ -<!-- - - 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. - ---> -<!-- -does exactly the same as the AntBuilder in AntTest... just to be sure that ant behaves the same -as what we expect from the AntBuilder - --> -<project name="test" default="full"> - - <target name="full"> - <path id="task.path"> - <pathelement location="classes"/> - </path> - <taskdef name="spoofcontainer" classname="groovy.util.SpoofTaskContainer" classpathref="task.path"/> - <taskdef name="spoof" classname="groovy.util.SpoofTask" classpathref="task.path"/> - - <spoofcontainer> - <spoof foo="123"/> - </spoofcontainer> - </target> - - <target name="testAntBuilderWithinGroovyTask" description="Test for GROOVY-1602"> - <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/> - <echo message="before groovy task"/> - <groovy> - ant.echo(message: 'ant builder within groovy task') - </groovy> - <echo message="after groovy task"/> - </target> -</project> http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest_import.xml ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest_import.xml b/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest_import.xml deleted file mode 100644 index c0b8e04..0000000 --- a/subprojects/groovy-ant/src/test-resources/groovy/util/AntTest_import.xml +++ /dev/null @@ -1,35 +0,0 @@ -<!-- - - 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. - ---> -<!-- -Sample file to test AntBuilder import feature - --> -<project default="firstTarget"> - - <echo message="outside targets, at the top"/> - - <target name="firstTarget"> - <echo message="inside firstTarget"/> - </target> - - <target name="2ndTarget"> - <echo message="inside 2ndTarget"/> - </target> -</project> http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTest.groovy b/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTest.groovy new file mode 100644 index 0000000..0df92ca --- /dev/null +++ b/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTest.groovy @@ -0,0 +1,319 @@ +/* + * 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. + */ +package groovy.ant + +import org.apache.tools.ant.BuildEvent +import org.apache.tools.ant.Project +import org.apache.tools.ant.ProjectHelper +import groovy.xml.NamespaceBuilder +import org.apache.tools.ant.UnknownElement +import org.junit.Assert + +/** + * Tests for the <groovy> task. + * + * @author Marc Guillemot + */ +class AntTest extends GroovyTestCase { + + void testAnt() { + def ant = new AntBuilder() + // let's just call one task + ant.echo('hello') + // here's an example of a block of Ant inside GroovyMarkup + ant.sequential { + echo('inside sequential') + def myDir = 'target/AntTest/' + mkdir(dir: myDir) + copy(todir: myDir) { + fileset(dir: 'src/test/groovy') { + include(name: '**/*.groovy') + } + } + echo('done') + } + // now let's do some normal Groovy again + def file = new File('target/AntTest/groovy/ant/AntTest.groovy') + assert file.exists() + } + + void testFileIteration() { + def ant = new AntBuilder() + // let's create a scanner of filesets + def scanner = ant.fileScanner { + fileset(dir: 'src/test/groovy') { + include(name: '**/Ant*.groovy') + } + } + // now let's iterate over + def found = false + for (f in scanner) { + println("Found file ${f}") + found = true + assert f instanceof File + assert f.name.endsWith('.groovy') + } + assert found + } + + void testJunitTask() { + def ant = new AntBuilder() + ant.junit { + test(name: 'groovy.ant.SomethingThatDoesNotExist') + } + } + + void testPathBuilding() { + def ant = new AntBuilder() + def value = ant.path { + fileset(dir: 'xdocs') { + include(name: '*.wiki') + } + } + assert value != null + assertEquals org.apache.tools.ant.types.Path, value.getClass() + } + + void testTaskContainerExecutionSequence() { + SpoofTaskContainer.getSpoof().length = 0 + def antFile = new File('src/test-resources/groovy/ant/AntTest.xml') + assertTrue "Couldn't find ant test script", antFile.exists() + // run it with ant, to be sure that our assumptions are correct + def project = new Project() + project.init() + ProjectHelper.projectHelper.parse(project, antFile) + project.executeTarget(project.defaultTarget) + + def expectedSpoof = + '''SpoofTaskContainer ctor +in addTask +configuring UnknownElement +SpoofTask ctor +begin SpoofTaskContainer execute +begin SpoofTask execute +tag name from wrapper: spoof +attributes map from wrapper: [foo:123] +param foo: 123 +end SpoofTask execute +end SpoofTaskContainer execute +''' + println SpoofTaskContainer.getSpoof().toString() + assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() + SpoofTaskContainer.spoof.length = 0 + + def ant = new AntBuilder() + def PATH = 'task.path' + + // and now run it with the AntBuilder + ant.path(id: PATH) { ant.pathelement(location: 'classes') } + ['spoofcontainer': SpoofTaskContainer, 'spoof': SpoofTask].each { pair -> + ant.taskdef(name: pair.key, classname: pair.value.name, classpathref: PATH) + } + ant.spoofcontainer { + ant.spoof(foo: 123) + } + assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() + + // now run it with AntBuilder using Namespaces (test for GROOVY-1070) + def antNS = new AntBuilder() + SpoofTaskContainer.resetSpoof() + + // and now run it with the AntBuilder + antNS.path(id: PATH) {antNS.pathelement(location: 'classes')} + ['spoofcontainer': SpoofTaskContainer, 'spoof': SpoofTask].each { pair -> + antNS.taskdef(name: pair.key, classname: pair.value.name, classpathref: PATH, + uri: 'testNS') + } + def testNS = NamespaceBuilder.newInstance(antNS, 'testNS', 'testNSprefix') + testNS.spoofcontainer { + testNS.spoof(foo: 123) + } + assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() + } + + /** Checks that we can access dynamically (through Ant's property task) defined properties in Groovy scriptlets */ + void testDynamicProperties() { + def antBuilder = new AntBuilder() + antBuilder.property(name: 'testProp1', value: 'TEST 1') + antBuilder.taskdef(name: 'groovy', classname: 'org.codehaus.groovy.ant.Groovy') + antBuilder.groovy(''' + ant.property(name: 'testProp2', value: 'TEST 2') + assert properties.testProp1 == project.properties.testProp1 + assert properties.testProp2 == project.properties.testProp2 + ''') + } + + /** + * Test access to AntBuilder properties + */ + void testAntBuilderProperties() { + def ant = new AntBuilder() + assertNull ant.project.properties.'myProp' + ant.property(name: 'myProp', value: 'blabla') + assert ant.project.properties.'myProp' == 'blabla' + } + + /** + * Tests that the AntBuilder can handle conditions (conditions aren't tasks) + * (test for GROOVY-824) + */ + void testCondition() { + def ant = new AntBuilder() + ant.condition(property: 'containsHi') { + contains([string: 'hi', substring: 'hi']) + } + assert ant.project.properties['containsHi'] == 'true' + ant.condition(property: 'equalsHi', else: 'false') { + Equals([arg1: 'hi', arg2: 'bye']) + } + assert ant.project.properties['equalsHi'] == 'false' + } + + /** + * Tests that using the AntBuilder within the <groovy> task doesn't cause double execution + * (test for GROOVY-1602) + */ + void testAntBuilderWithinGroovyTask() { + def antFile = new File('src/test-resources/groovy/ant/AntTest.xml') + assertTrue "Couldn't find ant test script", antFile.exists() + + def project = new Project() + project.init() + ProjectHelper.projectHelper.parse(project, antFile) + + def customListener = new SimpleListener() + project.addBuildListener customListener + + project.executeTarget('testAntBuilderWithinGroovyTask') + + def expectedSpoof = + '''started: taskdef[name:groovy, classname:org.codehaus.groovy.ant.Groovy] +finished: taskdef[name:groovy, classname:org.codehaus.groovy.ant.Groovy] +started: echo[message:before groovy task] +finished: echo[message:before groovy task] +started: groovy[:] +started: echo[message:ant builder within groovy task] +finished: echo[message:ant builder within groovy task] +finished: groovy[:] +started: echo[message:after groovy task] +finished: echo[message:after groovy task] +''' + + assertEquals expectedSpoof, customListener.spoof.toString() + } + + /** + * Test usage of import + */ + void testImport() { + def antFile = new File('src/test-resources/groovy/ant/AntTest_import.xml') + assertTrue "Couldn't find ant test script", antFile.exists() + + def ant = new AntBuilder() + def customListener = new SimpleListener() + ant.project.addBuildListener customListener + + ant.'import'(file: antFile.absolutePath) + def expectedSpoof = """\ +started: import[file:${antFile.absolutePath}] +started: echo[message:outside targets, at the top] +finished: echo[message:outside targets, at the top] +finished: import[file:${antFile.absolutePath}] +""" + assertEquals expectedSpoof, customListener.spoof.toString() + + customListener.spoof.length = 0 + ant.project.executeTarget('firstTarget') + expectedSpoof = '''\ +started: echo[message:inside firstTarget] +finished: echo[message:inside firstTarget] +''' + assertEquals expectedSpoof, customListener.spoof.toString() + + customListener.spoof.length = 0 + ant.target(name: 'myTestTarget', depends: '2ndTarget') { + echo(message: "echo from AntBuilder's target foo") + } + expectedSpoof = '''\ +started: echo[message:inside 2ndTarget] +finished: echo[message:inside 2ndTarget] +started: echo[message:echo from AntBuilder's target foo] +finished: echo[message:echo from AntBuilder's target foo] +''' + assertEquals expectedSpoof, customListener.spoof.toString() + + // test that the previously created target can be called + customListener.spoof.length = 0 + ant.project.executeTarget('myTestTarget') + assertEquals expectedSpoof, customListener.spoof.toString() + } + + void testDefineTarget_groovy2900() { + def ant = new AntBuilder() + def project = ant.project + def customListener = new SimpleListener() + project.addBuildListener customListener + ant.defineTarget(name: 'myTestTarget') { + echo(message: "myTestTarget") + } + ant.defineTarget(name: 'myTestTarget2', depends: 'myTestTarget') { + echo(message: "myTestTarget2") + } + ant.target(name: 'myTestTarget3', depends: 'myTestTarget') { + ant.defineTarget(name: 'myTestTarget4') { + echo(message: "myTestTarget4") + } + echo(message: "myTestTarget3") + } + ant.defineTarget(name: 'myTestTarget5', depends: 'myTestTarget4') { + echo(message: "myTestTarget5 should not appear") + } + project.executeTarget('myTestTarget2') + project.executeTarget('myTestTarget4') + assert customListener.spoof.toString() == '''\ +started: echo[message:myTestTarget] +finished: echo[message:myTestTarget] +started: echo[message:myTestTarget3] +finished: echo[message:myTestTarget3] +started: echo[message:myTestTarget] +finished: echo[message:myTestTarget] +started: echo[message:myTestTarget2] +finished: echo[message:myTestTarget2] +started: echo[message:myTestTarget4] +finished: echo[message:myTestTarget4] +''' + } +} + +class SimpleListener extends org.apache.tools.ant.DefaultLogger { + def spoof = new StringBuffer() + + void taskStarted(BuildEvent event) { + if (!(event.task instanceof UnknownElement)) Assert.fail("Task is already configured. Listeners won't have a chance to alter UnknownElement for additional configuration"); + spoof << "started: " + logTask(event.task) + "\n" + } + + void taskFinished(BuildEvent event) { + spoof << "finished: " + logTask(event.task) + "\n" + } + + private String logTask(task) { + task.taskName + task.wrapper.attributeMap + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTestCase.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTestCase.groovy b/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTestCase.groovy new file mode 100644 index 0000000..21d3d64 --- /dev/null +++ b/subprojects/groovy-ant/src/test/groovy/groovy/ant/AntTestCase.groovy @@ -0,0 +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. + */ +package groovy.ant + +abstract class AntTestCase extends GroovyTestCase { + protected void doInTmpDir(Closure cl) { + // tag::create_zip_builder[] + def ant = new AntBuilder() + // end::create_zip_builder[] + def baseDir = File.createTempDir() + ant.project.baseDir = baseDir + try { + cl.call(ant, new FileTreeBuilder(baseDir)) + } finally { + baseDir.deleteDir() + } + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/ant/FileNameFinderTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/ant/FileNameFinderTest.groovy b/subprojects/groovy-ant/src/test/groovy/groovy/ant/FileNameFinderTest.groovy new file mode 100644 index 0000000..c05c4b2 --- /dev/null +++ b/subprojects/groovy-ant/src/test/groovy/groovy/ant/FileNameFinderTest.groovy @@ -0,0 +1,35 @@ +/* + * 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. + */ +package groovy.ant + +/** + * Make sure FileNameFinder uses Ant filesets correctly. + */ +class FileNameFinderTest extends GroovyLogTestCase { + + void testFilesInTestDirArePickedUp() { + def finder = new FileNameFinder() + def groovyFiles = finder.getFileNames('src/test/groovy', '**/*.groovy') + assert groovyFiles, 'There should be groovy files in src/test/groovy' + // now collect all those not starting with the 'Ant' + def nonAntFiles = finder.getFileNames('src/test/groovy', '**/*.groovy', '**/Ant*') + assert nonAntFiles, 'There should be non-Ant files in src/test/groovy' + assert groovyFiles.size() > nonAntFiles.size() + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTask.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTask.java b/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTask.java new file mode 100644 index 0000000..21e0cb8 --- /dev/null +++ b/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTask.java @@ -0,0 +1,49 @@ +/* + * 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. + */ +package groovy.ant; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +import org.codehaus.groovy.runtime.InvokerHelper; + +public class SpoofTask extends Task { + private int foo; + + public SpoofTask() { + super(); + SpoofTaskContainer.spoof("SpoofTask ctor"); + } + + public void setFoo(final int i) { + foo = i; + } + + + public void execute() throws BuildException { + SpoofTaskContainer.spoof("begin SpoofTask execute"); + SpoofTaskContainer.spoof("tag name from wrapper: " + getWrapper().getElementTag()); + // don't rely on Map.toString(), behaviour is not documented + SpoofTaskContainer.spoof("attributes map from wrapper: " + + InvokerHelper.toMapString(getWrapper().getAttributeMap())); + SpoofTaskContainer.spoof("param foo: " + foo); + + SpoofTaskContainer.spoof("end SpoofTask execute"); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTaskContainer.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTaskContainer.java b/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTaskContainer.java new file mode 100644 index 0000000..6025c6c --- /dev/null +++ b/subprojects/groovy-ant/src/test/groovy/groovy/ant/SpoofTaskContainer.java @@ -0,0 +1,71 @@ +/* + * 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. + */ +package groovy.ant; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.TaskContainer; +import org.apache.tools.ant.UnknownElement; + +import java.util.ArrayList; +import java.util.List; + +public class SpoofTaskContainer extends Task implements TaskContainer { + private List<Task> tasks = new ArrayList<Task>(); + static StringBuffer spoof = new StringBuffer(); + + public SpoofTaskContainer() { + super(); + spoof("SpoofTaskContainer ctor"); + } + + static StringBuffer getSpoof() { + return spoof; + } + + static void resetSpoof() { + spoof = new StringBuffer(); + } + + static void spoof(String message) { + spoof.append(message); + spoof.append("\n"); + } + + public void addTask(Task task) { + // to work with ant 1.6 + spoof("in addTask"); + if (task instanceof UnknownElement) { + spoof("configuring UnknownElement"); + task.maybeConfigure(); + task = ((UnknownElement) task).getTask(); + } + tasks.add(task); + } + + public void execute() throws BuildException { + spoof("begin SpoofTaskContainer execute"); + for (Object task1 : tasks) { + Task task = (Task) task1; + task.perform(); + } + spoof("end SpoofTaskContainer execute"); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/util/AntTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/util/AntTest.groovy b/subprojects/groovy-ant/src/test/groovy/groovy/util/AntTest.groovy deleted file mode 100644 index 9136f6b..0000000 --- a/subprojects/groovy-ant/src/test/groovy/groovy/util/AntTest.groovy +++ /dev/null @@ -1,319 +0,0 @@ -/* - * 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. - */ -package groovy.util - -import org.apache.tools.ant.BuildEvent -import org.apache.tools.ant.Project -import org.apache.tools.ant.ProjectHelper -import groovy.xml.NamespaceBuilder -import org.apache.tools.ant.UnknownElement -import org.junit.Assert - -/** - * Tests for the <groovy> task. - * - * @author Marc Guillemot - */ -class AntTest extends GroovyTestCase { - - void testAnt() { - def ant = new AntBuilder() - // let's just call one task - ant.echo('hello') - // here's an example of a block of Ant inside GroovyMarkup - ant.sequential { - echo('inside sequential') - def myDir = 'target/AntTest/' - mkdir(dir: myDir) - copy(todir: myDir) { - fileset(dir: 'src/test/groovy') { - include(name: '**/*.groovy') - } - } - echo('done') - } - // now let's do some normal Groovy again - def file = new File('target/AntTest/groovy/util/AntTest.groovy') - assert file.exists() - } - - void testFileIteration() { - def ant = new AntBuilder() - // let's create a scanner of filesets - def scanner = ant.fileScanner { - fileset(dir: 'src/test/groovy') { - include(name: '**/Ant*.groovy') - } - } - // now let's iterate over - def found = false - for (f in scanner) { - println("Found file ${f}") - found = true - assert f instanceof File - assert f.name.endsWith('.groovy') - } - assert found - } - - void testJunitTask() { - def ant = new AntBuilder() - ant.junit { - test(name: 'groovy.util.SomethingThatDoesNotExist') - } - } - - void testPathBuilding() { - def ant = new AntBuilder() - def value = ant.path { - fileset(dir: 'xdocs') { - include(name: '*.wiki') - } - } - assert value != null - assertEquals org.apache.tools.ant.types.Path, value.getClass() - } - - void testTaskContainerExecutionSequence() { - SpoofTaskContainer.getSpoof().length = 0 - def antFile = new File('src/test-resources/groovy/util/AntTest.xml') - assertTrue "Couldn't find ant test script", antFile.exists() - // run it with ant, to be sure that our assumptions are correct - def project = new Project() - project.init() - ProjectHelper.projectHelper.parse(project, antFile) - project.executeTarget(project.defaultTarget) - - def expectedSpoof = - '''SpoofTaskContainer ctor -in addTask -configuring UnknownElement -SpoofTask ctor -begin SpoofTaskContainer execute -begin SpoofTask execute -tag name from wrapper: spoof -attributes map from wrapper: [foo:123] -param foo: 123 -end SpoofTask execute -end SpoofTaskContainer execute -''' - println SpoofTaskContainer.getSpoof().toString() - assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() - SpoofTaskContainer.spoof.length = 0 - - def ant = new AntBuilder() - def PATH = 'task.path' - - // and now run it with the AntBuilder - ant.path(id: PATH) { ant.pathelement(location: 'classes') } - ['spoofcontainer': SpoofTaskContainer, 'spoof': SpoofTask].each { pair -> - ant.taskdef(name: pair.key, classname: pair.value.name, classpathref: PATH) - } - ant.spoofcontainer { - ant.spoof(foo: 123) - } - assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() - - // now run it with AntBuilder using Namespaces (test for GROOVY-1070) - def antNS = new AntBuilder() - SpoofTaskContainer.resetSpoof() - - // and now run it with the AntBuilder - antNS.path(id: PATH) {antNS.pathelement(location: 'classes')} - ['spoofcontainer': SpoofTaskContainer, 'spoof': SpoofTask].each { pair -> - antNS.taskdef(name: pair.key, classname: pair.value.name, classpathref: PATH, - uri: 'testNS') - } - def testNS = NamespaceBuilder.newInstance(antNS, 'testNS', 'testNSprefix') - testNS.spoofcontainer { - testNS.spoof(foo: 123) - } - assertEquals expectedSpoof, SpoofTaskContainer.getSpoof().toString() - } - - /** Checks that we can access dynamically (through Ant's property task) defined properties in Groovy scriptlets */ - void testDynamicProperties() { - def antBuilder = new AntBuilder() - antBuilder.property(name: 'testProp1', value: 'TEST 1') - antBuilder.taskdef(name: 'groovy', classname: 'org.codehaus.groovy.ant.Groovy') - antBuilder.groovy(''' - ant.property(name: 'testProp2', value: 'TEST 2') - assert properties.testProp1 == project.properties.testProp1 - assert properties.testProp2 == project.properties.testProp2 - ''') - } - - /** - * Test access to AntBuilder properties - */ - void testAntBuilderProperties() { - def ant = new AntBuilder() - assertNull ant.project.properties.'myProp' - ant.property(name: 'myProp', value: 'blabla') - assert ant.project.properties.'myProp' == 'blabla' - } - - /** - * Tests that the AntBuilder can handle conditions (conditions aren't tasks) - * (test for GROOVY-824) - */ - void testCondition() { - def ant = new AntBuilder() - ant.condition(property: 'containsHi') { - contains([string: 'hi', substring: 'hi']) - } - assert ant.project.properties['containsHi'] == 'true' - ant.condition(property: 'equalsHi', else: 'false') { - Equals([arg1: 'hi', arg2: 'bye']) - } - assert ant.project.properties['equalsHi'] == 'false' - } - - /** - * Tests that using the AntBuilder within the <groovy> task doesn't cause double execution - * (test for GROOVY-1602) - */ - void testAntBuilderWithinGroovyTask() { - def antFile = new File('src/test-resources/groovy/util/AntTest.xml') - assertTrue "Couldn't find ant test script", antFile.exists() - - def project = new Project() - project.init() - ProjectHelper.projectHelper.parse(project, antFile) - - def customListener = new SimpleListener() - project.addBuildListener customListener - - project.executeTarget('testAntBuilderWithinGroovyTask') - - def expectedSpoof = - '''started: taskdef[name:groovy, classname:org.codehaus.groovy.ant.Groovy] -finished: taskdef[name:groovy, classname:org.codehaus.groovy.ant.Groovy] -started: echo[message:before groovy task] -finished: echo[message:before groovy task] -started: groovy[:] -started: echo[message:ant builder within groovy task] -finished: echo[message:ant builder within groovy task] -finished: groovy[:] -started: echo[message:after groovy task] -finished: echo[message:after groovy task] -''' - - assertEquals expectedSpoof, customListener.spoof.toString() - } - - /** - * Test usage of import - */ - void testImport() { - def antFile = new File('src/test-resources/groovy/util/AntTest_import.xml') - assertTrue "Couldn't find ant test script", antFile.exists() - - def ant = new AntBuilder() - def customListener = new SimpleListener() - ant.project.addBuildListener customListener - - ant.'import'(file: antFile.absolutePath) - def expectedSpoof = """\ -started: import[file:${antFile.absolutePath}] -started: echo[message:outside targets, at the top] -finished: echo[message:outside targets, at the top] -finished: import[file:${antFile.absolutePath}] -""" - assertEquals expectedSpoof, customListener.spoof.toString() - - customListener.spoof.length = 0 - ant.project.executeTarget('firstTarget') - expectedSpoof = '''\ -started: echo[message:inside firstTarget] -finished: echo[message:inside firstTarget] -''' - assertEquals expectedSpoof, customListener.spoof.toString() - - customListener.spoof.length = 0 - ant.target(name: 'myTestTarget', depends: '2ndTarget') { - echo(message: "echo from AntBuilder's target foo") - } - expectedSpoof = '''\ -started: echo[message:inside 2ndTarget] -finished: echo[message:inside 2ndTarget] -started: echo[message:echo from AntBuilder's target foo] -finished: echo[message:echo from AntBuilder's target foo] -''' - assertEquals expectedSpoof, customListener.spoof.toString() - - // test that the previously created target can be called - customListener.spoof.length = 0 - ant.project.executeTarget('myTestTarget') - assertEquals expectedSpoof, customListener.spoof.toString() - } - - void testDefineTarget_groovy2900() { - def ant = new AntBuilder() - def project = ant.project - def customListener = new SimpleListener() - project.addBuildListener customListener - ant.defineTarget(name: 'myTestTarget') { - echo(message: "myTestTarget") - } - ant.defineTarget(name: 'myTestTarget2', depends: 'myTestTarget') { - echo(message: "myTestTarget2") - } - ant.target(name: 'myTestTarget3', depends: 'myTestTarget') { - ant.defineTarget(name: 'myTestTarget4') { - echo(message: "myTestTarget4") - } - echo(message: "myTestTarget3") - } - ant.defineTarget(name: 'myTestTarget5', depends: 'myTestTarget4') { - echo(message: "myTestTarget5 should not appear") - } - project.executeTarget('myTestTarget2') - project.executeTarget('myTestTarget4') - assert customListener.spoof.toString() == '''\ -started: echo[message:myTestTarget] -finished: echo[message:myTestTarget] -started: echo[message:myTestTarget3] -finished: echo[message:myTestTarget3] -started: echo[message:myTestTarget] -finished: echo[message:myTestTarget] -started: echo[message:myTestTarget2] -finished: echo[message:myTestTarget2] -started: echo[message:myTestTarget4] -finished: echo[message:myTestTarget4] -''' - } -} - -class SimpleListener extends org.apache.tools.ant.DefaultLogger { - def spoof = new StringBuffer() - - void taskStarted(BuildEvent event) { - if (!(event.task instanceof UnknownElement)) Assert.fail("Task is already configured. Listeners won't have a chance to alter UnknownElement for additional configuration"); - spoof << "started: " + logTask(event.task) + "\n" - } - - void taskFinished(BuildEvent event) { - spoof << "finished: " + logTask(event.task) + "\n" - } - - private String logTask(task) { - task.taskName + task.wrapper.attributeMap - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/util/FileNameFinderTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/util/FileNameFinderTest.groovy b/subprojects/groovy-ant/src/test/groovy/groovy/util/FileNameFinderTest.groovy deleted file mode 100644 index 3720787..0000000 --- a/subprojects/groovy-ant/src/test/groovy/groovy/util/FileNameFinderTest.groovy +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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. - */ -package groovy.util - -/** - * Make sure FileNameFinder uses Ant filesets correctly. - * - * @author Dierk Koenig - * @author Paul King - */ -class FileNameFinderTest extends GroovyLogTestCase { - - void testFilesInTestDirArePickedUp() { - def finder = new FileNameFinder() - def groovyFiles = finder.getFileNames('src/test/groovy', '**/*.groovy') - assert groovyFiles, 'There should be groovy files in src/test/groovy' - // now collect all those not starting with the 'Ant' - def nonAntFiles = finder.getFileNames('src/test/groovy', '**/*.groovy', '**/Ant*') - assert nonAntFiles, 'There should be non-Ant files in src/test/groovy' - assert groovyFiles.size() > nonAntFiles.size() - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTask.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTask.java b/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTask.java deleted file mode 100644 index 0444dca..0000000 --- a/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTask.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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. - */ -package groovy.util; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Task; -import org.codehaus.groovy.runtime.InvokerHelper; - -public class SpoofTask extends Task { - private int foo; - - public SpoofTask() { - super(); - SpoofTaskContainer.spoof("SpoofTask ctor"); - } - - public void setFoo(final int i) { - foo = i; - } - - - public void execute() throws BuildException { - SpoofTaskContainer.spoof("begin SpoofTask execute"); - SpoofTaskContainer.spoof("tag name from wrapper: " + getWrapper().getElementTag()); - // don't rely on Map.toString(), behaviour is not documented - SpoofTaskContainer.spoof("attributes map from wrapper: " - + InvokerHelper.toMapString(getWrapper().getAttributeMap())); - SpoofTaskContainer.spoof("param foo: " + foo); - - SpoofTaskContainer.spoof("end SpoofTask execute"); - } - -} http://git-wip-us.apache.org/repos/asf/groovy/blob/b5a8e6ad/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTaskContainer.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTaskContainer.java b/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTaskContainer.java deleted file mode 100644 index e71e6dd..0000000 --- a/subprojects/groovy-ant/src/test/groovy/groovy/util/SpoofTaskContainer.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ -package groovy.util; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.TaskContainer; -import org.apache.tools.ant.UnknownElement; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class SpoofTaskContainer extends Task implements TaskContainer { - private List tasks = new ArrayList(); - static StringBuffer spoof = new StringBuffer(); - - public SpoofTaskContainer() { - super(); - spoof("SpoofTaskContainer ctor"); - } - - static StringBuffer getSpoof() { - return spoof; - } - - static void resetSpoof() { - spoof = new StringBuffer(); - } - - static void spoof(String message) { - spoof.append(message); - spoof.append("\n"); - } - - public void addTask(Task task) { - // to work with ant 1.6 - spoof("in addTask"); - if (task instanceof UnknownElement) { - spoof("configuring UnknownElement"); - task.maybeConfigure(); - task = ((UnknownElement) task).getTask(); - } - tasks.add(task); - } - - public void execute() throws BuildException { - spoof("begin SpoofTaskContainer execute"); - for (Iterator iter = tasks.iterator(); iter.hasNext();) { - Task task = (Task) iter.next(); - task.perform(); - } - spoof("end SpoofTaskContainer execute"); - } - -}
