001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.BuildFileTest;
026: import org.apache.tools.ant.Location;
027: import org.apache.tools.ant.Project;
028:
029: /**
030: */
031: public class ImportTest extends BuildFileTest {
032:
033: public ImportTest(String name) {
034: super (name);
035: }
036:
037: public void setUp() {
038: }
039:
040: public void tearDown() {
041: }
042:
043: public void testSimpleImport() {
044: configureProject("src/etc/testcases/taskdefs/import/import.xml");
045: assertLogContaining("Before importIn imported topAfter import");
046: }
047:
048: public void testUnnamedNesting() {
049: configureProject(
050: "src/etc/testcases/taskdefs/import/unnamedImport.xml",
051: Project.MSG_WARN);
052: String log = getLog();
053: assertTrue("Warnings logged when not expected: " + log, log
054: .length() == 0);
055: }
056:
057: public void testSerial() {
058: configureProject("src/etc/testcases/taskdefs/import/subdir/serial.xml");
059: assertLogContaining("Unnamed2.xmlUnnamed1.xml");
060: String fullLog = getFullLog();
061: String substring = "Skipped already imported file";
062: assertTrue("expecting full log to contain \"" + substring
063: + "\" full log was \"" + fullLog + "\"", fullLog
064: .indexOf(substring) >= 0);
065: }
066:
067: // allow this as imported in targets are only tested when a target is run
068: public void testImportInTargetNoEffect() {
069: configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
070: expectPropertyUnset("no-import", "foo");
071: assertTrue(null == getProject().getReference("baz"));
072: }
073:
074: // deactivate this test as imports within targets are not allowed
075: public void notTestImportInTargetWithEffect() {
076: configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
077: expectPropertySet("do-import", "foo", "bar");
078: assertNotNull(getProject().getReference("baz"));
079: }
080:
081: public void testImportInTargetNotAllowed() {
082: configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml");
083: expectBuildExceptionContaining("do-import",
084: "not a top level task",
085: "import only allowed as a top-level task");
086: }
087:
088: public void testImportInSequential() {
089: configureProject("src/etc/testcases/taskdefs/import/subdir/importinsequential.xml");
090: expectPropertySet("within-imported", "foo", "bar");
091: assertNotNull(getProject().getReference("baz"));
092: }
093:
094: public void testImportSameTargets() {
095: try {
096: configureProject("src/etc/testcases/taskdefs/import/same_target.xml");
097: } catch (BuildException ex) {
098: String message = ex.getMessage();
099: if (message.indexOf("Duplicate target") == -1) {
100: assertTrue("Did not see 'Duplicate target' in '"
101: + message + "'", false);
102: }
103: return;
104: }
105: assertTrue("Did not see build exception", false);
106: }
107:
108: public void testImportError() {
109: try {
110: configureProject("src/etc/testcases/taskdefs/import/import_bad_import.xml");
111: } catch (BuildException ex) {
112: Location lo = ex.getLocation();
113: assertTrue(
114: "expected location of build exception to be set",
115: (lo != null));
116: assertTrue(
117: "expected location to contain calling file",
118: lo.getFileName().indexOf("import_bad_import.xml") != -1);
119: assertTrue("expected message of ex to contain called file",
120: ex.getMessage().indexOf("bad.xml") != -1);
121: return;
122: }
123: assertTrue("Did not see build exception", false);
124: }
125:
126: public void testSymlinkedImports() throws Exception {
127: String ln = "/usr/bin/ln";
128: if (!new File(ln).exists()) {
129: ln = "/bin/ln";
130: }
131: if (!new File(ln).exists()) {
132: // Running on Windows or something, so skip it.
133: return;
134: }
135: String symlink = "src/etc/testcases/taskdefs/import/symlinks/d3b";
136: File symlinkFile = new File(System.getProperty("root"), symlink);
137: if (Runtime.getRuntime().exec(
138: new String[] { ln, "-s", "d3a",
139: symlinkFile.getAbsolutePath() }).waitFor() != 0) {
140: throw new IOException("'" + ln + " -s d3a " + symlink
141: + "' failed");
142: }
143: try {
144: configureProject("src/etc/testcases/taskdefs/import/symlinks/d1/p1.xml");
145: assertPropertyEquals(
146: "ant.file.p2",
147: new File(System.getProperty("root"),
148: "src/etc/testcases/taskdefs/import/symlinks/d2/p2.xml")
149: .getAbsolutePath());
150: assertPropertyEquals(
151: "ant.file.p3",
152: new File(System.getProperty("root"),
153: "src/etc/testcases/taskdefs/import/symlinks/d3b/p3.xml")
154: .getAbsolutePath());
155: } finally {
156: symlinkFile.delete();
157: }
158: }
159:
160: public void testTargetFirst() {
161: configureProject("src/etc/testcases/taskdefs/import/importtargetfirst.xml");
162: assertLogContaining("Importing targetfirstAfter target firstAfter importing");
163: }
164:
165: public void testTargetName() {
166: configureProject("src/etc/testcases/taskdefs/import/c.xml");
167: }
168:
169: }
|