| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package builder |
| 20 |
/** |
| 21 |
* Test cases for the Ant builder documentation. |
| 22 |
*/ |
| 23 |
class AntBuilderSpecTest extends GroovyTestCase { |
| 24 |
|
| 25 |
void doInTmpDir(Closure cl) { |
| 26 |
// tag::create_zip_builder[] |
| 27 |
def ant = new AntBuilder() |
| 28 |
// end::create_zip_builder[] |
| 29 |
def baseDir = File.createTempDir() |
| 30 |
ant.project.baseDir = baseDir |
| 31 |
try { |
| 32 |
cl.call(ant, new FileTreeBuilder(baseDir)) |
| 33 |
} finally { |
| 34 |
baseDir.deleteDir() |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
void testEcho() { |
| 39 |
// tag::example_echo[] |
| 40 |
def ant = new AntBuilder() // <1> |
| 41 |
ant.echo('hello from Ant!') // <2> |
| 42 |
// end::example_echo[] |
| 43 |
} |
| 44 |
|
| 45 |
void testCreateZip() { |
| 46 |
doInTmpDir { ant, baseDir -> |
| 47 |
baseDir.src { |
| 48 |
'test.groovy'("println 'Hello'") |
| 49 |
} |
| 50 |
// tag::create_zip_zip[] |
| 51 |
ant.zip(destfile: 'sources.zip', basedir: 'src') |
| 52 |
// end::create_zip_zip[] |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
void testCopyFiles() { |
| 57 |
doInTmpDir {ant, baseDir -> |
| 58 |
baseDir.src { |
| 59 |
test { |
| 60 |
groovy { |
| 61 |
'test1.groovy'('assert 1+1==2') |
| 62 |
'test2.groovy'('assert 1+1==2') |
| 63 |
util { |
| 64 |
'AntTest.groovy'('assert 1+1==2') |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
// tag::copy_files[] |
| 70 |
// lets just call one task |
| 71 |
ant.echo("hello") |
| 72 |
|
| 73 |
// here is an example of a block of Ant inside GroovyMarkup |
| 74 |
ant.sequential { |
| 75 |
echo("inside sequential") |
| 76 |
def myDir = "target/AntTest/" |
| 77 |
mkdir(dir: myDir) |
| 78 |
copy(todir: myDir) { |
| 79 |
fileset(dir: "src/test") { |
| 80 |
include(name: "**/*.groovy") |
| 81 |
} |
| 82 |
} |
| 83 |
echo("done") |
| 84 |
} |
| 85 |
|
| 86 |
// now lets do some normal Groovy again |
| 87 |
def file = new File(ant.project.baseDir,"target/AntTest/groovy/util/AntTest.groovy") |
| 88 |
assert file.exists() |
| 89 |
// end::copy_files[] |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void testFileScanner() { |
| 94 |
doInTmpDir {ant, baseDir -> |
| 95 |
baseDir.src { |
| 96 |
test { |
| 97 |
groovy { |
| 98 |
'test1.groovy'('assert 1+1==2') |
| 99 |
'test2.groovy'('assert 1+1==2') |
| 100 |
util { |
| 101 |
'AntTest.groovy'('assert 1+1==2') |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
// tag::filescanner[] |
| 107 |
// lets create a scanner of filesets |
| 108 |
def scanner = ant.fileScanner { |
| 109 |
fileset(dir:"src/test") { |
| 110 |
include(name:"**/Ant*.groovy") |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// now lets iterate over |
| 115 |
def found = false |
| 116 |
for (f in scanner) { |
| 117 |
println("Found file $f") |
| 118 |
found = true |
| 119 |
assert f instanceof File |
| 120 |
assert f.name.endsWith(".groovy") |
| 121 |
} |
| 122 |
assert found |
| 123 |
// end::filescanner[] |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void testExecuteJUnit() { |
| 128 |
doInTmpDir {ant, baseDir -> |
| 129 |
// tag::run_junit[] |
| 130 |
// lets create a scanner of filesets |
| 131 |
ant.junit { |
| 132 |
test(name:'groovy.util.SomethingThatDoesNotExist') |
| 133 |
} |
| 134 |
// end::run_junit[] |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void testCompileRunJava() { |
| 139 |
doInTmpDir {ant, baseDir -> |
| 140 |
// tag::compile_java[] |
| 141 |
ant.echo(file:'Temp.java', ''' |
| 142 |
class Temp { |
| 143 |
public static void main(String[] args) { |
| 144 |
System.out.println("Hello"); |
| 145 |
} |
| 146 |
} |
| 147 |
''') |
| 148 |
ant.javac(srcdir:'.', includes:'Temp.java', fork:'true') |
| 149 |
ant.java(classpath:'.', classname:'Temp', fork:'true') |
| 150 |
ant.echo('Done') |
| 151 |
// end::compile_java[] |
| 152 |
} |
| 153 |
} |
| 154 |
} |