KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > SubAntTest


1 /*
2  * Copyright 2003-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21
22 import junit.framework.AssertionFailedError;
23
24 import org.apache.tools.ant.BuildEvent;
25 import org.apache.tools.ant.BuildFileTest;
26 import org.apache.tools.ant.BuildListener;
27 import org.apache.tools.ant.input.InputHandler;
28 import org.apache.tools.ant.types.Path;
29
30 /**
31  */

32 public class SubAntTest extends BuildFileTest {
33
34     public SubAntTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public void setUp() {
39         configureProject("src/etc/testcases/taskdefs/subant.xml");
40     }
41
42     public void tearDown() {
43         executeTarget("cleanup");
44     }
45
46     public void testnodirs() {
47         project.executeTarget("testnodirs");
48         expectLog("testnodirs", "No sub-builds to iterate on");
49     }
50
51     // target must be specified
52
public void testgenericantfile() {
53         File JavaDoc dir1 = project.resolveFile(".");
54         File JavaDoc dir2 = project.resolveFile("subant/subant-test1");
55         File JavaDoc dir3 = project.resolveFile("subant/subant-test2");
56
57         testBaseDirs("testgenericantfile",
58                      new String JavaDoc[] { dir1.getAbsolutePath(),
59                          dir2.getAbsolutePath(),
60                          dir3.getAbsolutePath()
61
62                      });
63     }
64
65     public void testantfile() {
66         File JavaDoc dir1 = project.resolveFile(".");
67         // basedir of subant/subant-test1/subant.xml is ..
68
// therefore we expect here the subant/subant-test1 subdirectory
69
File JavaDoc dir2 = project.resolveFile("subant/subant-test1");
70         // basedir of subant/subant-test2/subant.xml is ..
71
// therefore we expect here the subant subdirectory
72
File JavaDoc dir3 = project.resolveFile("subant");
73
74         testBaseDirs("testantfile",
75                      new String JavaDoc[] { dir1.getAbsolutePath(),
76                          dir2.getAbsolutePath(),
77                          dir3.getAbsolutePath()
78
79                      });
80
81     }
82
83     protected void testBaseDirs(String JavaDoc target, String JavaDoc[] dirs) {
84         SubAntTest.BasedirChecker bc = new SubAntTest.BasedirChecker(dirs);
85         project.addBuildListener(bc);
86         executeTarget(target);
87         AssertionFailedError ae = bc.getError();
88         if (ae != null) {
89             throw ae;
90         }
91         project.removeBuildListener(bc);
92     }
93
94     private class BasedirChecker implements BuildListener {
95         private String JavaDoc[] expectedBasedirs;
96         private int calls = 0;
97         private AssertionFailedError error;
98
99         BasedirChecker(String JavaDoc[] dirs) {
100             expectedBasedirs = dirs;
101         }
102
103         public void buildStarted(BuildEvent event) {}
104         public void buildFinished(BuildEvent event) {}
105         public void targetFinished(BuildEvent event){}
106         public void taskStarted(BuildEvent event) {}
107         public void taskFinished(BuildEvent event) {}
108         public void messageLogged(BuildEvent event) {}
109
110         public void targetStarted(BuildEvent event) {
111             if (event.getTarget().getName().equals("")) {
112                 return;
113             }
114             if (error == null) {
115                 try {
116                     assertEquals(expectedBasedirs[calls++],
117                             event.getProject().getBaseDir().getAbsolutePath());
118                 } catch (AssertionFailedError e) {
119                     error = e;
120                 }
121             }
122         }
123
124         AssertionFailedError getError() {
125             return error;
126         }
127
128     }
129
130
131 }
132
Popular Tags