Browse Source

Fix for serial import relative files bug

The import task now uses getLocation().getFileName() to
get the file that the <import/> task is located and not
the importstack.

Also the canonical form of the imported file is stored
in the importstack.

Changed message if imported file is imported more than
once.

Include unit test for above.
PR: 21662


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274842 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 22 years ago
parent
commit
d15bd65fa5
4 changed files with 33 additions and 13 deletions
  1. +5
    -0
      src/etc/testcases/taskdefs/import/subdir/serial.xml
  2. +9
    -4
      src/main/org/apache/tools/ant/taskdefs/ImportTask.java
  3. +12
    -4
      src/testcases/org/apache/tools/ant/BuildFileTest.java
  4. +7
    -5
      src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java

+ 5
- 0
src/etc/testcases/taskdefs/import/subdir/serial.xml View File

@@ -0,0 +1,5 @@
<project name="serial">
<import file="../unnamed1.xml"/>
<import file="../unnamed2.xml"/>
</project>


+ 9
- 4
src/main/org/apache/tools/ant/taskdefs/ImportTask.java View File

@@ -125,14 +125,17 @@ public class ImportTask extends Task {
// helper that doesn't set the import. // helper that doesn't set the import.
throw new BuildException("import requires support in ProjectHelper"); throw new BuildException("import requires support in ProjectHelper");
} }
Object currentSource = importStack.elementAt(importStack.size() - 1);

// ProjectHelper2.AntXmlContext context; // ProjectHelper2.AntXmlContext context;
// context=(ProjectHelper2.AntXmlContext)project.getReference("ant.parsing.context"); // context=(ProjectHelper2.AntXmlContext)project.getReference("ant.parsing.context");


// File buildFile=context.buildFile; // File buildFile=context.buildFile;
// File buildFileParent=context.buildFileParent; // File buildFileParent=context.buildFileParent;
File buildFile = (File) currentSource;

if (getLocation() == null || getLocation().getFileName() == null) {
throw new BuildException("Unable to get location of import task");
}

File buildFile = new File(getLocation().getFileName());
buildFile = new File(buildFile.getAbsolutePath()); buildFile = new File(buildFile.getAbsolutePath());
//System.out.println("Importing from " + currentSource); //System.out.println("Importing from " + currentSource);
File buildFileParent = new File(buildFile.getParent()); File buildFileParent = new File(buildFile.getParent());
@@ -153,9 +156,11 @@ public class ImportTask extends Task {
+ buildFile.getAbsolutePath()); + buildFile.getAbsolutePath());
} }


importedFile = new File(getPath(importedFile));

if (importStack.contains(importedFile)) { if (importStack.contains(importedFile)) {
getProject().log( getProject().log(
"\nSkipped already imported file to avoid loop:\n "
"Skipped already imported file:\n "
+ importedFile + "\n", Project.MSG_WARN); + importedFile + "\n", Project.MSG_WARN);
return; return;
} }


+ 12
- 4
src/testcases/org/apache/tools/ant/BuildFileTest.java View File

@@ -108,17 +108,25 @@ public abstract class BuildFileTest extends TestCase {
String realLog = getLog(); String realLog = getLog();
assertEquals(log, realLog); assertEquals(log, realLog);
} }
/**
* Assert that the given substring is in the log messages
*/


protected void assertLogContaining(String substring) {
String realLog = getLog();
assertTrue("expecting log to contain \"" + substring + "\" log was \""
+ realLog + "\"",
realLog.indexOf(substring) >= 0);
}
/** /**
* Assert that the given message has been logged with a priority * Assert that the given message has been logged with a priority
* &gt;= INFO when running the given target. * &gt;= INFO when running the given target.
*/ */
protected void expectLogContaining(String target, String log) { protected void expectLogContaining(String target, String log) {
executeTarget(target); executeTarget(target);
String realLog = getLog();
assertTrue("expecting log to contain \""+log+"\" log was \""
+ realLog + "\"",
realLog.indexOf(log) >= 0);
assertLogContaining(log);
} }


/** /**


+ 7
- 5
src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java View File

@@ -74,11 +74,7 @@ public class ImportTest extends BuildFileTest {


public void testSimpleImport() { public void testSimpleImport() {
configureProject("src/etc/testcases/taskdefs/import/import.xml"); configureProject("src/etc/testcases/taskdefs/import/import.xml");
String logMessage = getLog();
String expect = "Before importIn imported topAfter import";
assertTrue("expecting log to contain \"" + expect + "\" log was \""
+ logMessage + "\"",
logMessage.indexOf(expect) >= 0);
assertLogContaining("Before importIn imported topAfter import");
} }


public void testUnnamedNesting() { public void testUnnamedNesting() {
@@ -88,5 +84,11 @@ public class ImportTest extends BuildFileTest {
assertTrue("Warnings logged when not expected: " + log, assertTrue("Warnings logged when not expected: " + log,
log.length() == 0); log.length() == 0);
} }

public void testSerial() {
configureProject("src/etc/testcases/taskdefs/import/subdir/serial.xml");
assertLogContaining(
"Unnamed2.xmlUnnamed1.xmlSkipped already imported file");
}
} }



Loading…
Cancel
Save