You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@locus.apache.org on 2000/08/02 11:24:25 UTC

cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/types CommandlineJavaTest.java PathTest.java

bodewig     00/08/02 02:24:24

  Modified:    src/main/org/apache/tools/ant/taskdefs Javac.java Rmic.java
               src/main/org/apache/tools/ant/taskdefs/optional/junit
                        JUnitTask.java
               src/main/org/apache/tools/ant/taskdefs/optional/metamata
                        MParse.java
               src/main/org/apache/tools/ant/types Path.java
               src/testcases/org/apache/tools/ant/types
                        CommandlineJavaTest.java PathTest.java
  Log:
  Add nested fileset and filesetref attributes to Path.
  
  This means you can now add somedir/*.jar to the CLASSPATH by
  <classpath>
    <fileset dir="somedir">
      <include name="**/*.jar" />
    </fileset>
  </classpath>
  
  and probably adjust the pattern if you don't want to recurse the tree.
  
  Revision  Changes    Path
  1.28      +3 -3      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java
  
  Index: Javac.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- Javac.java	2000/07/31 08:24:47	1.27
  +++ Javac.java	2000/08/02 09:24:22	1.28
  @@ -357,7 +357,7 @@
           // add dest dir to classpath so that previously compiled and
           // untouched classes are on classpath
   
  -        classpath.setLocation(destDir.getAbsolutePath());
  +        classpath.setLocation(destDir);
   
           // add our classpath to the mix
   
  @@ -415,7 +415,7 @@
               File f = project.resolveFile(list[i]);
   
               if (f.exists()) {
  -                target.setLocation(f.getAbsolutePath());
  +                target.setLocation(f);
              } else {
                  log("Dropping from classpath: "+
                      f.getAbsolutePath(), Project.MSG_VERBOSE);
  @@ -782,7 +782,7 @@
                  for (int i=0 ; i < files.length ; i++) {
                      File f = new File(dir,files[i]);
                      if (f.exists() && f.isFile()) {
  -                       classpath.setLocation(f.getAbsolutePath());
  +                       classpath.setLocation(f);
                      }
                  }
              }
  
  
  
  1.11      +1 -1      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rmic.java
  
  Index: Rmic.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rmic.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Rmic.java	2000/07/24 15:52:58	1.10
  +++ Rmic.java	2000/08/02 09:24:22	1.11
  @@ -410,7 +410,7 @@
               File f = project.resolveFile(list[i]);
   
               if (f.exists()) {
  -                target.setLocation(f.getAbsolutePath());
  +                target.setLocation(f);
              } else {
                  log("Dropping from classpath: "+
                      f.getAbsolutePath(), Project.MSG_VERBOSE);
  
  
  
  1.3       +1 -1      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  
  Index: JUnitTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JUnitTask.java	2000/07/24 15:53:04	1.2
  +++ JUnitTask.java	2000/08/02 09:24:23	1.3
  @@ -103,7 +103,7 @@
        * Set the path to junit classes.
        * @param junit path to junit classes.
        */
  -    public void setJunit(String junit) {
  +    public void setJunit(File junit) {
           commandline.createClasspath(project).createPathElement().setLocation(junit);
       }
   
  
  
  
  1.2       +3 -3      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java
  
  Index: MParse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MParse.java	2000/07/31 08:04:06	1.1
  +++ MParse.java	2000/08/02 09:24:23	1.2
  @@ -134,9 +134,9 @@
           }
   
           final Path classpath = cmdl.createClasspath(project);
  -        classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/metamatadebug.jar");
  -        classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/metamata.jar");
  -        classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/JavaCC.zip");
  +        classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/metamatadebug.jar"));
  +        classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/metamata.jar"));
  +        classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/JavaCC.zip"));
   
           final Commandline.Argument arg = cmdl.createVmArgument();
           arg.setValue("-mx140M");
  
  
  
  1.2       +123 -41   jakarta-ant/src/main/org/apache/tools/ant/types/Path.java
  
  Index: Path.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/Path.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Path.java	2000/07/24 16:05:29	1.1
  +++ Path.java	2000/08/02 09:24:23	1.2
  @@ -54,6 +54,8 @@
   
   package org.apache.tools.ant.types;
   
  +import org.apache.tools.ant.BuildException;
  +import org.apache.tools.ant.DirectoryScanner;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.PathTokenizer;
   
  @@ -93,20 +95,44 @@
   
   public class Path {
   
  -    private Vector definition;
  +    private Vector elements;
       private Project project;
   
       public static Path systemClasspath = 
           new Path(null, System.getProperty("java.class.path"));
   
  +
  +    /**
  +     * Helper class, holds the nested <pathelement> values.
  +     */
  +    public class PathElement {
  +        private String[] parts;
  +
  +        public void setLocation(File loc) {
  +            parts = new String[] {translateFile(loc.getAbsolutePath())};
  +        }
  +
  +        public void setPath(String path) {
  +            parts = Path.translatePath(project, path);
  +        }
  +
  +        public String[] getParts() {
  +            return parts;
  +        }
  +    }
  +
  +    /**
  +     * Invoked by IntrospectionHelper for <code>setXXX(Path p)</code>
  +     * attribute setters.  
  +     */
       public Path(Project p, String path) {
           this(p);
  -        setPath(path);
  +        createPathElement().setPath(path);
       }
   
       public Path(Project p) {
           this.project = project;
  -        definition = new Vector();
  +        elements = new Vector();
       }
   
       /**
  @@ -114,56 +140,98 @@
        * @param location the location of the element to add (must not be
        * <code>null</code> nor empty.
        */
  -    public void setLocation(String location) {
  -        if (location != null && location.length() > 0) {
  -            String element = translateFile(resolveFile(project, location));
  -            if (definition.indexOf(element) == -1) {
  -                definition.addElement(element);
  -            }
  -        }
  +    public void setLocation(File location) {
  +        createPathElement().setLocation(location);
       }
   
   
       /**
  -     * Append the contents of the other Path instance to this.
  +     * Parses a path definition and creates single PathElements.
  +     * @param path the path definition.
        */
  -    public void append(Path other) {
  -        String[] l = other.list();
  -        for (int i=0; i<l.length; i++) {
  -            if (definition.indexOf(l[i]) == -1) {
  -                definition.addElement(l[i]);
  -            }
  -        }
  +    public void setPath(String path) {
  +        createPathElement().setPath(path);
       }
   
  +
       /**
  -     * Parses a path definition and creates single PathElements.
  -     * @param path the path definition.
  +     * Created the nested <pathelement> element.
        */
  -    public void setPath(String path) {
  -        final Vector elements = translatePath(project, path);
  -        for (int i=0; i < elements.size(); i++) {
  -            String element = (String) elements.elementAt(i);
  -            if (definition.indexOf(element) == -1) {
  -                definition.addElement(element);
  -            }
  -        }
  +    public PathElement createPathElement() {
  +        PathElement pe = new PathElement();
  +        elements.addElement(pe);
  +        return pe;
       }
   
  +    /**
  +     * Adds a nested <fileset> element.
  +     */
  +    public void addFileset(FileSet fs) {
  +        elements.addElement(fs);
  +    }
   
  -    public Path createPathElement() {
  -        return this;
  +    /**
  +     * Adds a nested <filesetref> element.
  +     */
  +    public void addFilesetRef(Reference r) {
  +        elements.addElement(r);
       }
   
  +    /**
  +     * Append the contents of the other Path instance to this.
  +     */
  +    public void append(Path other) {
  +        String[] l = other.list();
  +        for (int i=0; i<l.length; i++) {
  +            if (elements.indexOf(l[i]) == -1) {
  +                elements.addElement(l[i]);
  +            }
  +        }
  +    }
   
       /**
        * Returns all path elements defined by this and netsed path objects.
        * @return list of path elements.
        */
       public String[] list() {
  -        final String[] result = new String[definition.size()];
  -        definition.copyInto(result);
  -        return result;
  +        Vector result = new Vector(2*elements.size());
  +        for (int i=0; i<elements.size(); i++) {
  +            Object o = elements.elementAt(i);
  +            if (o instanceof Reference) {
  +                Reference r = (Reference) o;
  +                o = r.getReferencedObject(project);
  +                // we only support references to filesets right now
  +                if (o == null || !(o instanceof FileSet)) {
  +                    String msg = r.getRefId()+" doesn\'t denote a fileset";
  +                    throw new BuildException(msg);
  +                }
  +            }
  +            
  +            if (o instanceof String) {
  +                // obtained via append
  +                addUnlessPresent(result, (String) o);
  +            } else if (o instanceof PathElement) {
  +                String[] parts = ((PathElement) o).getParts();
  +                if (parts == null) {
  +                    throw new BuildException("You must either set location or path on <pathelement>");
  +                }
  +                for (int j=0; j<parts.length; j++) {
  +                    addUnlessPresent(result, parts[j]);
  +                }
  +            } else if (o instanceof FileSet) {
  +                FileSet fs = (FileSet) o;
  +                DirectoryScanner ds = fs.getDirectoryScanner(project);
  +                String[] s = ds.getIncludedFiles();
  +                File dir = fs.getDir();
  +                for (int j=0; j<s.length; j++) {
  +                    addUnlessPresent(result, 
  +                                     translateFile((new File(dir, s[j])).getAbsolutePath()));
  +                }
  +            }
  +        }
  +        String[] res = new String[result.size()];
  +        result.copyInto(res);
  +        return res;
       }
   
   
  @@ -187,12 +255,13 @@
   
           return result.toString();
       }
  -
  -
   
  -    public static Vector translatePath(Project project, String source) {
  +    /**
  +     * Splits a PATH (with : or ; as separators) into its parts.
  +     */
  +    public static String[] translatePath(Project project, String source) {
           final Vector result = new Vector();
  -        if (source == null) return result;
  +        if (source == null) return new String[0];
   
           PathTokenizer tok = new PathTokenizer(source);
           StringBuffer element = new StringBuffer();
  @@ -204,10 +273,15 @@
               }
               result.addElement(element.toString());
           }
  -        return result;
  +        String[] res = new String[result.size()];
  +        result.copyInto(res);
  +        return res;
       }
   
  -
  +    /**
  +     * Returns its argument with all file separator characters
  +     * replaced so that they match the local OS conventions.  
  +     */
       public static String translateFile(String source) {
           if (source == null) return "";
   
  @@ -219,7 +293,6 @@
           return result.toString();
       }
   
  -
       protected static boolean translateFileSep(StringBuffer buffer, int pos) {
           if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') {
               buffer.setCharAt(pos, File.separatorChar);
  @@ -228,8 +301,11 @@
           return false;
       }
   
  +    /**
  +     * How many parts does this Path instance consist of.
  +     */
       public int size() {
  -        return definition.size();
  +        return list().length;
       }
   
       private static String resolveFile(Project project, String relativeName) {
  @@ -237,6 +313,12 @@
               return project.resolveFile(relativeName).getAbsolutePath();
           }
           return relativeName;
  +    }
  +
  +    private static void addUnlessPresent(Vector v, String s) {
  +        if (v.indexOf(s) == -1) {
  +            v.addElement(s);
  +        }
       }
   
   }
  
  
  
  1.3       +5 -4      jakarta-ant/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java
  
  Index: CommandlineJavaTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CommandlineJavaTest.java	2000/07/25 08:44:12	1.2
  +++ CommandlineJavaTest.java	2000/08/02 09:24:24	1.3
  @@ -92,15 +92,16 @@
           assertEquals("no classpath", 
                        "org.apache.tools.ant.CommandlineJavaTest", s[3]);
   
  -        c.createClasspath(project).setLocation("junit.jar");
  -        c.createClasspath(project).setLocation("ant.jar");
  +        c.createClasspath(project).setLocation(new File("junit.jar"));
  +        c.createClasspath(project).setLocation(new File("ant.jar"));
           s = c.getCommandline();
           assertEquals("with classpath", 6, s.length);
           assertEquals("with classpath", "java", s[0]);
           assertEquals("with classpath", "-Djava.compiler=NONE", s[1]);
           assertEquals("with classpath", "-classpath", s[2]);
  -        assertEquals("with classpath", 
  -                     "junit.jar"+java.io.File.pathSeparator+"ant.jar", s[3]);
  +        assert("junit.jar contained", 
  +               s[3].indexOf("junit.jar"+java.io.File.pathSeparator) >= 0);
  +        assert("ant.jar contained", s[3].endsWith("ant.jar"));
           assertEquals("with classpath", "junit.textui.TestRunner", s[4]);
           assertEquals("with classpath", 
                        "org.apache.tools.ant.CommandlineJavaTest", s[5]);
  
  
  
  1.2       +3 -17     jakarta-ant/src/testcases/org/apache/tools/ant/types/PathTest.java
  
  Index: PathTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/types/PathTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PathTest.java	2000/07/25 08:44:13	1.1
  +++ PathTest.java	2000/08/02 09:24:24	1.2
  @@ -145,7 +145,7 @@
   
       public void testSetLocation() {
           Path p = new Path(project);
  -        p.setLocation("/a");
  +        p.setLocation(new File(File.separatorChar+"a"));
           String[] l = p.list();
           if (isUnixStyle) {
               assertEquals(1, l.length);
  @@ -154,24 +154,13 @@
               assertEquals(1, l.length);
               assertEquals("\\a", l[0]);
           }
  -
  -        p = new Path(project);
  -        p.setLocation("\\a");
  -        l = p.list();
  -        if (isUnixStyle) {
  -            assertEquals(1, l.length);
  -            assertEquals("/a", l[0]);
  -        } else {
  -            assertEquals(1, l.length);
  -            assertEquals("\\a", l[0]);
  -        }
       }
   
       public void testAppending() {
           Path p = new Path(project, "/a:/b");
           String[] l = p.list();
           assertEquals("2 after construction", 2, l.length);
  -        p.setLocation("/c");
  +        p.setLocation(new File("/c"));
           l = p.list();
           assertEquals("3 after setLocation", 3, l.length);
           p.setPath("\\d;\\e");
  @@ -186,9 +175,6 @@
           Path p = new Path(project, "");
           String[] l = p.list();
           assertEquals("0 after construction", 0, l.length);
  -        p.setLocation("");
  -        l = p.list();
  -        assertEquals("0 after setLocation", 0, l.length);
           p.setPath("");
           l = p.list();
           assertEquals("0 after setPath", 0, l.length);
  @@ -201,7 +187,7 @@
           Path p = new Path(project, "/a:/a");
           String[] l = p.list();
           assertEquals("1 after construction", 1, l.length);
  -        p.setLocation("\\a");
  +        p.setLocation(new File(File.separatorChar+"a"));
           l = p.list();
           assertEquals("1 after setLocation", 1, l.length);
           p.setPath("\\a;/a");