KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.BuildFileTest;
25 import org.apache.tools.ant.Location;
26 import org.apache.tools.ant.Project;
27
28 /**
29  */

30 public class ImportTest extends BuildFileTest {
31
32     public ImportTest(String JavaDoc name) {
33         super(name);
34     }
35
36     public void setUp() {
37     }
38
39     public void tearDown() {
40     }
41
42     public void testSimpleImport() {
43         configureProject("src/etc/testcases/taskdefs/import/import.xml");
44         assertLogContaining("Before importIn imported topAfter import");
45     }
46
47     public void testUnnamedNesting() {
48         configureProject("src/etc/testcases/taskdefs/import/unnamedImport.xml",
49                          Project.MSG_WARN);
50         String JavaDoc log = getLog();
51         assertTrue("Warnings logged when not expected: " + log,
52                     log.length() == 0);
53     }
54
55     public void testSerial() {
56         configureProject("src/etc/testcases/taskdefs/import/subdir/serial.xml");
57         assertLogContaining("Unnamed2.xmlUnnamed1.xml");
58         String JavaDoc fullLog = getFullLog();
59         String JavaDoc substring = "Skipped already imported file";
60         assertTrue("expecting full log to contain \"" + substring
61             + "\" full log was \"" + fullLog + "\"",
62             fullLog.indexOf(substring) >= 0);
63     }
64
65     // allow this as imported in targets are only tested when a target is run
66
public void testImportInTargetNoEffect() {
67         configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
68         expectPropertyUnset("no-import", "foo");
69         assertTrue(null == getProject().getReference("baz"));
70     }
71
72     // deactivate this test as imports within targets are not allowed
73
public void notTestImportInTargetWithEffect() {
74         configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
75         expectPropertySet("do-import", "foo", "bar");
76         assertNotNull(getProject().getReference("baz"));
77     }
78
79     public void testImportInTargetNotAllowed() {
80         configureProject(
81             "src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
82         expectBuildExceptionContaining(
83             "do-import", "not a top level task",
84             "import only allowed as a top-level task");
85     }
86
87     public void testImportInSequential() {
88         configureProject(
89             "src/etc/testcases/taskdefs/import/subdir/importinsequential.xml");
90         expectPropertySet("within-imported", "foo", "bar");
91         assertNotNull(getProject().getReference("baz"));
92     }
93
94     public void testImportSameTargets() {
95         try {
96             configureProject(
97                 "src/etc/testcases/taskdefs/import/same_target.xml");
98         } catch (BuildException ex) {
99             String JavaDoc message = ex.getMessage();
100             if (message.indexOf("Duplicate target") == -1) {
101                 assertTrue("Did not see 'Duplicate target' in '" + message +"'", false);
102             }
103             return;
104         }
105         assertTrue(
106             "Did not see build exception",
107             false);
108     }
109
110     public void testImportError() {
111         try {
112             configureProject(
113                 "src/etc/testcases/taskdefs/import/import_bad_import.xml");
114         } catch (BuildException ex) {
115             Location lo = ex.getLocation();
116             assertTrue(
117                 "expected location of build exception to be set",
118                 (lo != null));
119             assertTrue(
120                 "expected location to contain calling file",
121                 lo.getFileName().indexOf("import_bad_import.xml") != -1);
122             assertTrue(
123                 "expected message of ex to contain called file",
124                 ex.getMessage().indexOf("bad.xml") != -1);
125             return;
126         }
127         assertTrue(
128             "Did not see build exception",
129             false);
130     }
131
132     public void testSymlinkedImports() throws Exception JavaDoc {
133         String JavaDoc ln = "/usr/bin/ln";
134         if (!new File JavaDoc(ln).exists()) {
135             ln = "/bin/ln";
136         }
137         if (!new File JavaDoc(ln).exists()) {
138             // Running on Windows or something, so skip it.
139
return;
140         }
141         String JavaDoc symlink = "src/etc/testcases/taskdefs/import/symlinks/d3b";
142         if (Runtime.getRuntime().exec(new String JavaDoc[] {ln, "-s", "d3a", symlink}).waitFor() != 0) {
143             throw new IOException JavaDoc("'" + ln + " -s d3a " + symlink + "' failed");
144         }
145         try {
146             configureProject(
147                 "src/etc/testcases/taskdefs/import/symlinks/d1/p1.xml");
148             assertPropertyEquals(
149                 "ant.file.p2",
150                 new File JavaDoc("src/etc/testcases/taskdefs/import/symlinks/d2/p2.xml")
151                 .getAbsolutePath());
152             assertPropertyEquals(
153                 "ant.file.p3",
154                 new File JavaDoc("src/etc/testcases/taskdefs/import/symlinks/d3b/p3.xml")
155                 .getAbsolutePath());
156         } finally {
157             new File JavaDoc(symlink).delete();
158         }
159     }
160
161 }
162
Popular Tags