ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1375136 - in /ant/core/trunk: WHATSNEW src/main/org/apache/tools/ant/types/resources/FileResource.java src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
Date Mon, 20 Aug 2012 17:48:34 GMT
Author: mbenson
Date: Mon Aug 20 17:48:34 2012
New Revision: 1375136

URL: http://svn.apache.org/viewvc?rev=1375136&view=rev
Log:
Any FileResource whose represented File has a parent also has a basedir.

Added:
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
  (with props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/src/main/org/apache/tools/ant/types/resources/FileResource.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=1375136&r1=1375135&r2=1375136&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Mon Aug 20 17:48:34 2012
@@ -14,6 +14,8 @@ Changes that could break older environme
    classes of Execute has been extracted to the
    org.apache.tools.ant.taskdefs.launcher package.
 
+ * Any FileResource whose represented File has a parent also has a basedir.
+
 Fixed bugs:
 -----------
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/FileResource.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/FileResource.java?rev=1375136&r1=1375135&r2=1375136&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/FileResource.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/FileResource.java Mon Aug
20 17:48:34 2012
@@ -57,8 +57,8 @@ public class FileResource extends Resour
      * @param name   the relative filename.
      */
     public FileResource(File b, String name) {
-        setFile(FILE_UTILS.resolveFile(b, name));
-        setBaseDir(b);
+        this.baseDir = b;
+        this.file = FILE_UTILS.resolveFile(b, name);
     }
 
     /**
@@ -76,8 +76,8 @@ public class FileResource extends Resour
      * @since Ant 1.8
      */
     public FileResource(Project p, File f) {
+        this(f);
         setProject(p);
-        setFile(f);
     }
 
     /**
@@ -97,6 +97,9 @@ public class FileResource extends Resour
     public void setFile(File f) {
         checkAttributesAllowed();
         file = f;
+        if (f != null && (getBaseDir() == null || !FILE_UTILS.isLeadingPath(getBaseDir(),
f))) {
+            setBaseDir(f.getParentFile());
+        }
     }
 
     /**
@@ -372,7 +375,7 @@ public class FileResource extends Resour
 
     /**
      * Create a new resource that matches a relative or absolute path.
-     * If the current instance has a baseDir attribute, it is copied.
+     * If the current instance has a compatible baseDir attribute, it is copied.
      * @param path relative/absolute path to a resource
      * @return a new resource of type FileResource
      * @throws BuildException if desired
@@ -381,7 +384,9 @@ public class FileResource extends Resour
     public Resource getResource(String path) {
         File newfile = FILE_UTILS.resolveFile(getFile(), path);
         FileResource fileResource = new FileResource(newfile);
-        fileResource.setBaseDir(getBaseDir());
+        if (FILE_UTILS.isLeadingPath(getBaseDir(), newfile)) {
+            fileResource.setBaseDir(getBaseDir());
+        }
         return fileResource;
     }
 }

Added: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java?rev=1375136&view=auto
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
(added)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
Mon Aug 20 17:48:34 2012
@@ -0,0 +1,109 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types.resources;
+
+import java.io.File;
+
+import org.apache.tools.ant.Project;
+
+import junit.framework.TestCase;
+
+/**
+ * Test Java API of {@link FileResource}.
+ */
+public class FileResourceTest extends TestCase {
+
+    private File root;
+
+    public void setUp() {
+        root = new File(System.getProperty("root"));
+    }
+
+    public void testAttributes() {
+        FileResource f = new FileResource();
+        f.setBaseDir(root);
+        f.setName("foo");
+        assertEquals(new File(root, "foo"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo", f.getName());
+    }
+
+    public void testNonImmediateBasedir() {
+        FileResource f = new FileResource();
+        f.setBaseDir(root);
+        f.setName("foo/bar");
+        assertEquals(new File(root, "foo/bar"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo/bar", f.getName());
+    }
+
+    public void testFile() {
+        FileResource f = new FileResource(new File(root, "foo"));
+        assertEquals(new File(root, "foo"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo", f.getName());
+    }
+
+    public void testBasedirAndName() {
+        FileResource f = new FileResource(root, "foo");
+        assertEquals(new File(root, "foo"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo", f.getName());
+    }
+
+    public void testNonImmediateBasedirAndName() {
+        FileResource f = new FileResource(root, "foo/bar");
+        assertEquals(new File(root, "foo/bar"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo/bar", f.getName());
+    }
+
+    public void testProjectAndFilename() {
+        Project p = new Project();
+        p.setBaseDir(root);
+        FileResource f = new FileResource(p, "foo");
+        assertEquals(new File(root, "foo"), f.getFile());
+        assertEquals(root, f.getBaseDir());
+        assertEquals("foo", f.getName());
+    }
+
+    public void testRelativeFactoryResource() {
+        FileResource f = new FileResource(root, "foo");
+        FileResource relative = f.getResource("bar").as(FileResource.class);
+        assertEquals(new File(root, "foo/bar"), relative.getFile());
+        assertEquals("foo/bar", relative.getName());
+        assertEquals(root, relative.getBaseDir());
+    }
+
+    public void testAbsoluteFactoryResource() {
+        FileResource f = new FileResource(new File(root, "foo/a"));
+        assertEquals(new File(root, "foo"), f.getBaseDir());
+        File bar = new File(root, "bar");
+        FileResource fromFactory = f.getResource(bar.getAbsolutePath()).as(FileResource.class);
+        assertEquals(bar, fromFactory.getFile());
+        assertEquals(root, fromFactory.getBaseDir());
+    }
+
+    public void testParentSiblingFactoryResource() {
+        FileResource f = new FileResource(new File(root, "foo/a"));
+        assertEquals(new File(root, "foo"), f.getBaseDir());
+        FileResource parentSibling = f.getResource("../../bar").as(FileResource.class);
+        assertEquals(root, parentSibling.getBaseDir());
+        assertEquals("bar", parentSibling.getName());
+    }
+}

Propchange: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/FileResourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message