diff --git a/src/etc/testcases/taskdefs/import/subdir/importinsequential-inner.xml b/src/etc/testcases/taskdefs/import/subdir/importinsequential-inner.xml new file mode 100644 index 000000000..a361ffe2c --- /dev/null +++ b/src/etc/testcases/taskdefs/import/subdir/importinsequential-inner.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/etc/testcases/taskdefs/import/subdir/importinsequential.xml b/src/etc/testcases/taskdefs/import/subdir/importinsequential.xml new file mode 100644 index 000000000..36b725fe7 --- /dev/null +++ b/src/etc/testcases/taskdefs/import/subdir/importinsequential.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java index a8ded92de..d569e5538 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java @@ -116,20 +116,20 @@ public class ImportTask extends Task { if (file == null) { throw new BuildException("import requires file attribute"); } - + if (getOwningTarget() == null + || !"".equals(getOwningTarget().getName())) { + throw new BuildException("import only allowed as a top-level task"); + } + ProjectHelper helper = (ProjectHelper) getProject().getReference("ant.projectHelper"); Vector importStack = helper.getImportStack(); + if (importStack.size() == 0) { // this happens if ant is used with a project // helper that doesn't set the import. throw new BuildException("import requires support in ProjectHelper"); } -// ProjectHelper2.AntXmlContext context; -// context=(ProjectHelper2.AntXmlContext)project.getReference("ant.parsing.context"); - -// File buildFile=context.buildFile; -// File buildFileParent=context.buildFileParent; if (getLocation() == null || getLocation().getFileName() == null) { throw new BuildException("Unable to get location of import task"); @@ -137,7 +137,7 @@ public class ImportTask extends Task { File buildFile = new File(getLocation().getFileName()); buildFile = new File(buildFile.getAbsolutePath()); - //System.out.println("Importing from " + currentSource); + File buildFileParent = new File(buildFile.getParent()); getProject().log("Importing file " + file + " from " @@ -165,26 +165,6 @@ public class ImportTask extends Task { return; } -// // Add parent build file to the map to avoid cycles... -// String parentFilename = getPath(buildFile); -// if (!context.importedFiles.containsKey(parentFilename)) { -// context.importedFiles.put(parentFilename, buildFile); -// } -// -// // Make sure we import the file only once -// String importedFilename = getPath(importedFile); -// if (context.importedFiles.containsKey(importedFilename)) { -// project.log("\nSkipped already imported file:\n "+ -// importedFilename+"\n",Project.MSG_WARN); -// return; -// } else { -// context.importedFiles.put(importedFilename, importedFile); -// } - -// context.ignoreProjectTag=true; -// context.helper.parse(project, importedFile, -// new ProjectHelper2.RootHandler(context)); - helper.parse(getProject(), importedFile); } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java b/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java index 9179c55cb..d161e2e67 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java @@ -91,16 +91,33 @@ public class ImportTest extends BuildFileTest { "Unnamed2.xmlUnnamed1.xmlSkipped already imported file"); } + // allow this as imported in targets are only tested when a target is run public void testImportInTargetNoEffect() { configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml"); expectPropertyUnset("no-import", "foo"); assertTrue(null == getProject().getReference("baz")); } - public void testImportInTargetWithEffect() { + // deactivate this test as imports within targets are not allowed + public void notTestImportInTargetWithEffect() { configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml"); expectPropertySet("do-import", "foo", "bar"); assertNotNull(getProject().getReference("baz")); } + + public void testImportInTargetNotAllowed() { + configureProject( + "src/etc/testcases/taskdefs/import/subdir/importintarget.xml"); + expectBuildExceptionContaining( + "do-import", "not a top level task", + "import only allowed as a top-level task"); + } + + public void testImportInSequential() { + configureProject( + "src/etc/testcases/taskdefs/import/subdir/importinsequential.xml"); + expectPropertySet("within-imported", "foo", "bar"); + assertNotNull(getProject().getReference("baz")); + } }