Loading...

torque-dev@db.apache.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

svn commit: r915916 - in /db/torque/torque4/trunk/torque-generator/src: main/java/org/apache/torque/gf/file/ main/java/org/apache/torque/gf/source/ test/java/org/apache/torque/gf/file/ test/java/org/apache/torque/gf/source/ tfischer Wed Feb 24 11:00:34 2010

Author: tfischer
Date: Wed Feb 24 18:39:27 2010
New Revision: 915916

URL: http://svn.apache.org/viewvc?rev=915916&view=rev
Log:
Refactored the path matcher from FileSources in extra classes in the new file 
package (step 1)

Added:
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/Fileset.java
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/WildcardFilter.java
    
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/
    
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/FilesetTest.java
      - copied, changed from r910632, 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/source/FileSourcesImplTest.java
Removed:
    
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/source/FileSourcesImplTest.java
Modified:
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java

Added: 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/Fileset.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/Fileset.java?rev=915916&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/Fileset.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/Fileset.java
 Wed Feb 24 18:39:27 2010
@@ -0,0 +1,408 @@
+package org.apache.torque.gf.file;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOCase;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Selects Files in a directory and the subdirectories of the directory.
+ * From these files, all that match an include pattern and do not
+ * match an exclude pattern are selected.
+ * 
+ * @Version $Id: $
+ */
+public class Fileset 
+{
+       /** The base directory of the fileset. */
+       private File basedir;
+
+       /** 
+        * The patterns for the files to include.
+        * If null or empty, all Files are included. 
+        */
+    private Set<String> includes;
+    
+    /**
+     * The patterns for the files to exclude.
+     * If null or empty, no Files are excluded. 
+     */
+    private Set<String> excludes;
+
+    /**
+     * Default constructor.
+     */
+    public Fileset()
+    {
+    }
+    
+    /**
+     * All-Args constructor.
+     * 
+     * @param basedir
+     * @param includes The patterns for the files to include.
+        *        If null or empty, all Files are included. 
+     * @param excludes The patterns for the files to exclude.
+     *        If null or empty, no Files are excluded. 
+     */
+    public Fileset(File basedir, Set<String> includes, Set<String> excludes)
+    {
+       this.basedir = basedir;
+       this.includes = includes;
+       this.excludes = excludes;
+    }
+    
+    /**
+     * Returns the base directory of the fileset.
+     * 
+     * @return the base directory, or null if no basedir is specified.
+     */
+       public File getBasedir()
+       {
+               return basedir;
+       }
+
+    /**
+     * Sets the base directory of the fileset.
+     * 
+     * @param basedir the base directory, or null.
+     */
+       public void setBasedir(File basedir) 
+       {
+               this.basedir = basedir;
+       }
+
+       /**
+        * Returns the include patterns for the fileset.
+        *  
+        * @return the include patterns, or null if all files should be 
included.
+        */
+       public Set<String> getIncludes()
+       {
+               return includes;
+       }
+
+       /**
+        * Sets the include patterns for the fileset.
+        *  
+        * @return the include patterns, or null if all files should be 
included.
+        */
+       public void setIncludes(Set<String> includes)
+       {
+               this.includes = includes;
+       }
+
+       /**
+        * Returns the exclude patterns for the fileset.
+        *  
+        * @return the exclude patterns, or null if all files should be 
excluded.
+        */
+       public Set<String> getExcludes()
+       {
+               return excludes;
+       }
+
+       /**
+        * Sets the exclude patterns for the fileset.
+        *  
+        * @return the exclude patterns, or null if all files should be 
excluded.
+        */
+       public void setExcludes(Set<String> excludes)
+       {
+               this.excludes = excludes;
+       }
+       
+       public List<File> getFiles() throws IOException
+       {
+               List<File> result = new ArrayList<File>();
+               if (includes == null) 
+               {
+                       getAllFiles(basedir, result);
+                       return result;
+               }
+               // process includes
+               for (String includePattern : includes)
+               {
+               int wildcardFreeSeparatorPos 
+                               = getWildcardFreeSeparatorPos(includePattern);
+               File wildcardFreeBaseDir = new File(
+                               basedir,
+                               getPathPartBefore(
+                                               includePattern, 
+                                               wildcardFreeSeparatorPos));
+               String wildcardPattern 
+                   = getPathPartAfter(includePattern, 
wildcardFreeSeparatorPos);
+               String[] wildcardParts = StringUtils.split(wildcardPattern, 
"\\/");
+               List<String> wildcardPartList = Arrays.asList(wildcardParts);
+
+               List<File> includes = getFiles(
+                               wildcardFreeBaseDir,
+                               wildcardPartList);
+               result.addAll(includes);
+               }
+               // process excludes
+               if (excludes == null)
+               {
+                       return result;
+               }
+               Iterator<File> fileIt = result.iterator();
+               while (fileIt.hasNext())
+               {
+                       File file = fileIt.next();
+                       boolean excluded = false;
+                       for (String excludePattern : excludes)
+                       {
+                               File excludePatternFile = new File(basedir, 
excludePattern);
+                               if (matchesPattern(file, 
excludePatternFile.getPath()))
+                               {
+                                       excluded = true;
+                                       break;
+                               }
+                       }
+                       if (excluded)
+                       {
+                               fileIt.remove();
+                       }
+               }
+               return result;
+       }
+       
+       static void getAllFiles(File currentBaseDir, List<File> toAddTo)
+                       throws IOException
+       {
+               File[] filesInDir = currentBaseDir.listFiles(
+                               new WildcardFilter("*", false, true));
+           if (filesInDir == null)
+           {
+               throw new IOException(
+                       "Could not list files in the following Directory "
+                           + "while reading the sources: "
+                           + currentBaseDir.getAbsolutePath());
+           }
+               toAddTo.addAll(Arrays.asList(filesInDir));
+               
+               File[] dirsInDir = currentBaseDir.listFiles(
+                               new WildcardFilter("*", true, false));
+               
+               for (File dir : dirsInDir)
+               {
+                       getAllFiles(dir, toAddTo);
+               }
+       }
+       
+    static List<File> getFiles(
+                   File currentBaseDir, List<String> pathPartList)
+               throws IOException
+       {
+               List<String> partsCopy = new ArrayList<String>(pathPartList);
+               String includeToProcess = partsCopy.remove(0);
+               if (partsCopy.size() == 0)
+               {
+                   File[] result = currentBaseDir.listFiles(
+                           new WildcardFilter(includeToProcess, false, true));
+                   if (result == null)
+                   {
+                       throw new IOException(
+                               "Could not list files in the following 
Directory "
+                                   + "while reading the sources: "
+                                   + currentBaseDir.getAbsolutePath());
+                   }
+                   return Arrays.asList(result);
+               }
+               if ("..".equals(includeToProcess))
+               {
+                   return getFiles(currentBaseDir.getParentFile(), partsCopy);
+               }
+               File[] matchingDirs = currentBaseDir.listFiles(
+                       new WildcardFilter(includeToProcess, true, false));
+               List<File> result = new ArrayList<File>();
+               for (File dir : matchingDirs)
+               {
+                   result.addAll(getFiles(dir, partsCopy));
+               }
+               return result;
+       }
+
+    /**
+     * Returns the position of the separator which separates the base part
+     * of the path which does not contain any wildcards from the rest.
+     * Example:
+     * <ul>
+     *   <li>*.txt returns -1(no separator)</li>
+     *   <li>schema*.xml returns -1(no separator)</li>
+     *   <li>xml\schema*.xml returns 3 (backslash position)</li>
+     *   <li>/xml/???/schema*.xml returns 4 (middle slash position)</li>
+     * </ul>
+     * @param path the path to compute the position from, not null.
+     * @return the separator position, -1 if no base part exists.
+     */
+    static int getWildcardFreeSeparatorPos(String path)
+    {
+        int asteriskIndex = path.indexOf("*");
+        int questionMarkIndex = path.indexOf("?");
+        if (asteriskIndex != -1)
+        {
+            if (questionMarkIndex != -1)
+            {
+                int min = Math.min(asteriskIndex, questionMarkIndex);
+                return getLargestSeparatorPos(path, min);
+            }
+            else
+            {
+                return getLargestSeparatorPos(path, asteriskIndex);
+            }
+        }
+        return getLargestSeparatorPos(path, questionMarkIndex);
+    }
+
+    /**
+     * Returns the largest position of a path separator within the path
+     * which is smaller than endIndex. An endIndex of -1 means that
+     * the largest separator pos should be given. -1 is returned if no
+     * separator is present in the region.
+     * <ul>
+     *   <li>getBaseDir("/xml/xxxx", 5) returns 4</li>
+     *   <li>getBaseDir("/xml/x/y", -1) returns 6</li>
+     *   <li>getBaseDir("/xml/x/y/", -1) returns 8</li>
+     * </ul>
+     */
+    static int getLargestSeparatorPos(String path, int maxIndex)
+    {
+        String baseString;
+        if (maxIndex == -1)
+        {
+            baseString = path;
+        }
+        else
+        {
+            baseString = path.substring(0, maxIndex);
+        }
+        return FilenameUtils.indexOfLastSeparator(baseString);
+    }
+
+    /**
+     * Returns the part of the path before the cutPosition.
+     * If this part is empty or separatorPos is -1, "." is returned.
+     * The character at cutPosition is not included in the result. 
+     *  
+     * @param path the path to get the part from.
+     * @param cutPosition the position where to cut.
+     * 
+     * @return the part of the path before cutPosition, or "." if this part
+     *         does not exist.  
+     */
+    static String getPathPartBefore(String path, int cutPosition)
+    {
+        if (cutPosition == -1)
+        {
+            return ".";
+        }
+        else
+        {
+            String resultString = path.substring(0, cutPosition);
+            if (StringUtils.EMPTY.equals(resultString))
+            {
+                resultString = ".";
+            }
+            return resultString;
+        }
+    }
+    
+
+    /**
+     * Returns the part of the path after the separatorPos.
+     * The character at cutPosition is not included in the result. 
+     *
+     * @param path the path to get the part from.
+     * @param cutPosition the position where to cut.
+     * 
+     * @return the part of the path before cutPosition, or "." if this part
+     *         does not exist.  
+     */
+    static String getPathPartAfter(String path, int cutPosition)
+    {
+        String resultString = path.substring(cutPosition + 1);
+        return resultString;
+    }
+    
+    static boolean matchesPattern(File file, String pattern)
+    {
+       String filePath = file.getPath();
+       List<String> fileParts = splitAndNormalize(filePath);
+       List<String> patternParts = splitAndNormalize(pattern);
+       if (fileParts.size() != patternParts.size())
+               {
+               return false;
+               }
+       Iterator<String> patternPartIt = patternParts.iterator();
+       for (String filePart : fileParts)
+       {
+               String patternPart = patternPartIt.next();
+               if (!FilenameUtils.wildcardMatch(
+                               filePart,
+                               patternPart,
+                    IOCase.SENSITIVE))
+               {
+                       return false;
+               }       
+       }
+       return true;
+    }
+
+    /**
+     * Splits a path in its parts and normalizes the path 
+     * (i.e. removes . and ..), if possible.
+     * 
+     * @param path the path to normalize
+     * 
+     * @return the normalized path in its parts.
+     */
+    static List<String> splitAndNormalize(String path) 
+    {
+       String[] parts = StringUtils.split(path, "\\/");
+       List<String> normalizedParts = new ArrayList<String>();
+       for (String part : parts) 
+       {
+               if (".".equals(part))
+               {
+                       continue;
+               }
+               if ("..".equals(part))
+               {
+                       if (!normalizedParts.isEmpty())
+                       {
+                               normalizedParts.remove(normalizedParts.size() - 
1);
+                               continue;
+                       }
+               }
+               normalizedParts.add(part);
+       }
+       return normalizedParts;
+    }
+}

Added: 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/WildcardFilter.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/WildcardFilter.java?rev=915916&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/WildcardFilter.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/file/WildcardFilter.java
 Wed Feb 24 18:39:27 2010
@@ -0,0 +1,79 @@
+package org.apache.torque.gf.file;
+
+import java.io.File;
+import java.io.FileFilter;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOCase;
+
+/**
+ * A filter evaluating a file name against a wildcard expression.
+ */
+public class WildcardFilter implements FileFilter
+{
+    /**
+     * The wildcard expression against which the file names are
+     * checked.
+     */
+    private String expression;
+
+    /**
+     * Whether directories are accepted at all.
+     */
+    private boolean acceptDir;
+
+    /**
+     * Whether files are accepted at all.
+     */
+    private boolean acceptFile;
+
+    /**
+     * Constructor.
+     *
+     * @param expression The wildcard expression against which
+     *        the file names are checked.
+     * @param acceptDir Whether directories are accepted at all.
+     * @param acceptFile Whether files are accepted at all.
+     */
+    public WildcardFilter(
+            String expression,
+            boolean acceptDir,
+            boolean acceptFile)
+    {
+        this.expression = expression;
+        this.acceptDir = acceptDir;
+        this.acceptFile = acceptFile;
+    }
+
+    /**
+     * Returns whether a file matches the criteria of this filter.
+     * If the file is a directory and <code>acceptDir</code> is false,
+     * the file is rejected.
+     * If the file is regular file and <code>acceptFile</code> is false,
+     * the file is rejected.
+     * If the filename does not match the wildcard filter, the file is
+     * rejected.
+     * If none of the above applies, the file is accepted.
+     *
+     * @return false if the file is rejected, true if it is accepted.
+     */
+    public boolean accept(File file)
+    {
+        if (!acceptDir && file.isDirectory())
+        {
+            return false;
+        }
+        if (!acceptFile && file.isFile())
+        {
+            return false;
+        }
+        if (FilenameUtils.wildcardMatch(
+                file.getName(),
+                expression,
+                IOCase.SENSITIVE))
+        {
+            return true;
+        }
+        return false;
+    }
+}
\ No newline at end of file

Modified: 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java?rev=915916&r1=915915&r2=915916&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java
 (original)
+++ 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java
 Wed Feb 24 18:39:27 2010
@@ -20,19 +20,16 @@
  */
 
 import java.io.File;
-import java.io.FileFilter;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.io.IOException;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOCase;
-import org.apache.commons.lang.StringUtils;
 import org.apache.torque.gf.configuration.ConfigurationException;
 import org.apache.torque.gf.configuration.ConfigurationHandlers;
+import org.apache.torque.gf.file.Fileset;
 
 /**
  * A collection of Sources, which all have the same elements,
@@ -112,129 +109,21 @@
         this.path = path;
         this.configurationHandlers = configurationHandlers;
 
-        int baseDirSeparatorPos = getBaseDirSeparatorPos(path);
-        File baseDir = new File(
-                sourcesDir,
-                getBaseDir(path, baseDirSeparatorPos));
-        String wildcardPattern = getWildcardPattern(path, baseDirSeparatorPos);
-        String[] wildcardParts = StringUtils.split(wildcardPattern, "\\/");
-        List<String> wildcardList = Arrays.asList(wildcardParts);
-
-        paths = getFiles(baseDir, wildcardList);
-        pathIt = paths.iterator();
-    }
-
-    List<File> getFiles(File baseDir, List<String> pathParts)
-            throws ConfigurationException
-    {
-        List<String> pathPartsCopy = new ArrayList<String>(pathParts);
-        String partToProcess = pathPartsCopy.remove(0);
-        if (pathPartsCopy.size() == 0)
-        {
-            File[] result = baseDir.listFiles(
-                    new WildcardFilter(partToProcess, false, true));
-            if (result == null)
-            {
-                throw new ConfigurationException(
-                        "Could not list files in the following Directory "
-                            + "while reading the sources: "
-                            + baseDir.getAbsolutePath());
-            }
-            return Arrays.asList(result);
-        }
-        if ("..".equals(partToProcess))
-        {
-            return getFiles(baseDir.getParentFile(), pathPartsCopy);
-        }
-        File[] matchingDirs = baseDir.listFiles(
-                new WildcardFilter(partToProcess, true, false));
-        List<File> result = new ArrayList<File>();
-        for (File dir : matchingDirs)
-        {
-            result.addAll(getFiles(dir, pathPartsCopy));
-        }
-        return result;
-    }
-
-    /**
-     * Returns the position of the separator which separates the base part
-     * of the path which does not contain any wildcards from the rest.
-     * Example:
-     * <ul>
-     *   <li>*.txt returns -1(no separator)</li>
-     *   <li>schema*.xml returns -1(no separator)</li>
-     *   <li>xml\schema*.xml returns 3 (backslash position)</li>
-     *   <li>/xml/???/schema*.xml returns 4 (middle slash position)</li>
-     * </ul>
-     * @param path the path to compute the position from, not null.
-     * @return the separator position, -1 if no base part exists.
-     */
-    static int getBaseDirSeparatorPos(String path)
-    {
-        int asteriskIndex = path.indexOf("*");
-        int questionMarkIndex = path.indexOf("?");
-        if (asteriskIndex != -1)
-        {
-            if (questionMarkIndex != -1)
-            {
-                int min = Math.min(asteriskIndex, questionMarkIndex);
-                return getLargestSeparatorPos(path, min);
-            }
-            else
-            {
-                return getLargestSeparatorPos(path, asteriskIndex);
-            }
-        }
-        return getLargestSeparatorPos(path, questionMarkIndex);
-    }
-
-    /**
-     * Returns the largest position of a path separator within the path
-     * which is smaller than endIndex. An endIndex of -1 means that
-     * the largest separator pos should be given. -1 is returned if no
-     * separator is present in the region.
-     * <ul>
-     *   <li>getBaseDir("/xml/xxxx", 5) returns 4</li>
-     *   <li>getBaseDir("/xml/x/y", -1) returns 6</li>
-     *   <li>getBaseDir("/xml/x/y/", -1) returns 8</li>
-     * </ul>
-     */
-    private static int getLargestSeparatorPos(String path, int maxIndex)
-    {
-        String baseString;
-        if (maxIndex == -1)
-        {
-            baseString = path;
-        }
-        else
+        Fileset fileset = new Fileset();
+        fileset.setBasedir(sourcesDir);
+        Set<String> includes = new HashSet<String>();
+        includes.add(path);
+        fileset.setIncludes(includes);
+        
+        try
         {
-            baseString = path.substring(0, maxIndex);
+               paths = fileset.getFiles();
         }
-        return FilenameUtils.indexOfLastSeparator(baseString);
-    }
-
-    static String getBaseDir(String path, int baseDirSeparatorPos)
-    {
-        if (baseDirSeparatorPos == -1)
+        catch (IOException e)
         {
-            return ".";
+               throw new ConfigurationException(e);
         }
-        else
-        {
-            String resultString = path.substring(0, baseDirSeparatorPos);
-            if (StringUtils.EMPTY.equals(resultString))
-            {
-                resultString = ".";
-            }
-            return resultString;
-        }
-
-    }
-
-    static String getWildcardPattern(String path, int baseDirSeparatorPos)
-    {
-        String resultString = path.substring(baseDirSeparatorPos + 1);
-        return resultString;
+        pathIt = paths.iterator();
     }
 
     public boolean hasNext()
@@ -316,76 +205,4 @@
     {
         return Collections.unmodifiableList(paths);
     }
-
-    /**
-     * A filter evaluating a file name against a wildcard expression.
-     */
-    private static class WildcardFilter implements FileFilter
-    {
-        /**
-         * The wildcard expression against which the file names are
-         * checked.
-         */
-        private String expression;
-
-        /**
-         * Whether directories are accepted at all.
-         */
-        private boolean acceptDir;
-
-        /**
-         * Whether files are accepted at all.
-         */
-        private boolean acceptFile;
-
-        /**
-         * Constructor.
-         *
-         * @param expression The wildcard expression against which
-         *        the file names are checked.
-         * @param acceptDir Whether directories are accepted at all.
-         * @param acceptFile Whether files are accepted at all.
-         */
-        public WildcardFilter(
-                String expression,
-                boolean acceptDir,
-                boolean acceptFile)
-        {
-            this.expression = expression;
-            this.acceptDir = acceptDir;
-            this.acceptFile = acceptFile;
-        }
-
-        /**
-         * Returns whether a file matches the criteria of this filter.
-         * If the file is a directory and <code>acceptDir</code> is false,
-         * the file is rejected.
-         * If the file is regular file and <code>acceptFile</code> is false,
-         * the file is rejected.
-         * If the filename does not match the wildcard filter, the file is
-         * rejected.
-         * If none of the above applies, the file is accepted.
-         *
-         * @return false if the file is rejected, true if it is accepted.
-         */
-        public boolean accept(File file)
-        {
-            if (!acceptDir && file.isDirectory())
-            {
-                return false;
-            }
-            if (!acceptFile && file.isFile())
-            {
-                return false;
-            }
-            if (FilenameUtils.wildcardMatch(
-                    file.getName(),
-                    expression,
-                    IOCase.SENSITIVE))
-            {
-                return true;
-            }
-            return false;
-        }
-    }
 }

Copied: 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/FilesetTest.java
 (from r910632, 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/source/FileSourcesImplTest.java)
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/FilesetTest.java?p2=db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/FilesetTest.java&p1=db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/source/FileSourcesImplTest.java&r1=910632&r2=915916&rev=915916&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/source/FileSourcesImplTest.java
 (original)
+++ 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/gf/file/FilesetTest.java
 Wed Feb 24 18:39:27 2010
@@ -1,4 +1,4 @@
-package org.apache.torque.gf.source;
+package org.apache.torque.gf.file;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -22,293 +22,379 @@
 import static org.junit.Assert.assertEquals;
 
 import java.io.File;
-import java.util.ArrayList;
+import java.io.IOException;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
-import org.apache.torque.gf.configuration.ConfigurationException;
-import org.apache.torque.gf.configuration.ConfigurationHandlers;
-import org.apache.torque.gf.source.properties.PropertiesSourceType;
-import org.apache.torque.gf.source.xml.XmlSourceType;
 import org.junit.Test;
 
-public class FileSourcesImplTest
+public class FilesetTest
 {
-    private ConfigurationHandlers configurationHandler 
-        = new ConfigurationHandlers();
+    private static File TEST_BASE_DIR = new File("src/test/fileSourcesImpl");
 
     @Test
-    public void testGetDirsNoWildcards()
+    public void testAnalyzePathNoWildcards()
     {
         String path = "/tmp/schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(4, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals("/tmp", baseDir);
+        String wildcardFree 
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals("/tmp", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetBaseDirOnlyFilename()
+    public void testAnalyzePathOnlyFilename()
     {
         String path = "schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(-1, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals(".", baseDir);
+        String wildcardFree 
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals(".", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetBaseDirQuestionMark()
+    public void testAnalyzePathQuestionMark()
     {
         String path = "C:\\schema\\?\\schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(9, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals("C:\\schema", baseDir);
+        String wildcardFree 
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals("C:\\schema", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("?\\schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetBaseDirAsterisk()
+    public void testAnalyzePathAsterisk()
     {
         String path = "C:\\schema\\*\\schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(9, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals("C:\\schema", baseDir);
+        String wildcardFree
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals("C:\\schema", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("*\\schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetBaseDirMultipleWildcards()
+    public void testAnalyzePathMultipleWildcards()
     {
         String path = "/tmp/*/???/schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(4, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals("/tmp", baseDir);
+        String wildcardFree
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals("/tmp", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("*/???/schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetBaseDirWildcardFirst()
+    public void testAnalyzePathWildcardFirst()
     {
         String path = "*/???/schema.xml";
-        int baseDirSeparatorPos = FileSourcesImpl.getBaseDirSeparatorPos(path);
+        int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
         assertEquals(-1, baseDirSeparatorPos);
-        String baseDir = FileSourcesImpl.getBaseDir(path, baseDirSeparatorPos);
-        assertEquals(".", baseDir);
+        String wildcardFree 
+                = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
+        assertEquals(".", wildcardFree);
         String wildcardPattern
-                = FileSourcesImpl.getWildcardPattern(path, 
baseDirSeparatorPos);
+                = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
         assertEquals("*/???/schema.xml", wildcardPattern);
     }
 
     @Test
-    public void testGetSourcesFixedNameBasedir() throws ConfigurationException
+    public void testFilelistFixedNameBasedir() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "1.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
-        {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
-        }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(new File("src/test/fileSourcesImpl/./1.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("1.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./1.properties"));
     }
 
     @Test
-    public void testGetSourcesFixedNameSubdir() throws ConfigurationException
+    public void testFilelistFixedNameSubdir() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "subfolder/2.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
-        {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
-        }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new File("src/test/fileSourcesImpl/subfolder/2.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/2.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "subfolder/2.properties"));
     }
 
     @Test
-    public void testGetSourcesMixedSlashBackslash() throws 
ConfigurationException
+    public void testFilelistMixedSlashBackslash() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "subfolder/subsubfolder\\3.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
-        {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
-        }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new 
File("src/test/fileSourcesImpl/subfolder/subsubfolder/3.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/subsubfolder\\3.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, 
"subfolder/subsubfolder/3.properties"));
     }
 
     @Test
-    public void testGetSourcesWildcardDir() throws ConfigurationException
+    public void testFilelistWildcardDir() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "*//2.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
-        {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
-        }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new File("src/test/fileSourcesImpl/./subfolder/2.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("*//2.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./subfolder/2.properties"));
     }
 
     @Test
-    public void testGetSourcesQuestionmarkFilename()
-            throws ConfigurationException
+    public void testFilelistQuestionmarkFilename() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "?.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
-        {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
-        }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new File("src/test/fileSourcesImpl/./1.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("?.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./1.properties"));
     }
 
     @Test
-    public void testGetSourcesWildcardFilename()
-            throws ConfigurationException
+    public void testFilelistWildcardFilename() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "*.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("*.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./1.properties"),
+                new File(TEST_BASE_DIR, "./11.properties"));
+    }
+
+    @Test
+    public void testFilelistDoubleDotsStayingInBaseDir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/../1.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "subfolder/../1.properties"));
+    }
+
+    @Test
+    public void testFilelistDoubleDotsLeavingBaseDir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                new File(TEST_BASE_DIR, "subfolder"),
+                getSetWith("../1.properties"),
+                null);
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "subfolder/../1.properties"));
+    }
+    
+    /**
+     * Creates as set containing the Strings in content.
+     * 
+     * @param content The Strings which should be in the set.
+     * 
+     * @return the Set containing all the strings.
+     */
+    private static Set<String> getSetWith(String... content)
+    {
+        Set<String> result = new HashSet<String>();
+        for (String part : content)
         {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
+            result.add(part);
         }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new File("src/test/fileSourcesImpl/./1.properties"));
-        expectedFiles.add(
-                new File("src/test/fileSourcesImpl/./11.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        return result;
     }
-
+    
     @Test
-    public void testGetSourcesDoubleDotsStayingInSourceDir()
-            throws ConfigurationException
+    public void testFilelistAllNull() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "subfolder/../1.properties",
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
                 null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
+                null);
+        List<File> fileList = fileset.getFiles();
+        Iterator<File> fileIt = fileList.iterator();
+        while (fileIt.hasNext())
         {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
+            File file = fileIt.next();
+            if (file.getPath().indexOf(".svn") != -1)
+            {
+                fileIt.remove();
+            }
         }
-        Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new 
File("src/test/fileSourcesImpl/subfolder/../1.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        assertFileListEquals(
+                fileList,
+                new File(TEST_BASE_DIR, "1.properties"),
+                new File(TEST_BASE_DIR, "11.properties"),
+                new File(TEST_BASE_DIR, "package.html"),
+                new File(TEST_BASE_DIR, "subfolder/2.properties"),
+                new File(TEST_BASE_DIR, 
"subfolder/subsubfolder/3.properties"));
     }
 
     @Test
-    public void testGetSourcesDoubleDotsLeavingSourceDir()
-            throws ConfigurationException
+    public void testFilelistExcludeInBasedir() throws IOException
     {
-        FileSourcesImpl fileSourcesImpl = new FileSourcesImpl(
-                null,
-                "../1.properties",
-                null,
-                null,
-                new ArrayList<TransformerDefinition>(),
-                configurationHandler,
-                new File("src/test/fileSourcesImpl/subfolder"));
-        Set<File> paths = new HashSet<File>();
-        while (fileSourcesImpl.hasNext())
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("*"),
+                getSetWith("11.properties"));
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./package.html"),
+                new File(TEST_BASE_DIR, "./1.properties"));
+    }
+
+    @Test
+    public void testFilelistExcludeInSubdir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/*"),
+                getSetWith("subfolder/2.properties"));
+        assertFileListEquals(
+                fileset,
+                new File[] {});
+    }
+
+    @Test
+    public void testFilelistExcludeWithBackslashes() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/*"),
+                getSetWith("subfolder\\2.properties"));
+        assertFileListEquals(
+                fileset,
+                new File[] {});
+    }
+
+    @Test
+    public void testFilelistExcludeAsteriskSubdir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/*"),
+                getSetWith("*/2.properties"));
+        assertFileListEquals(
+                fileset,
+                new File[] {});
+    }
+
+    @Test
+    public void testFilelistExcludeQuestionMarkSubdir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/*"),
+                getSetWith("su??old?r/2.properties"));
+        assertFileListEquals(
+                fileset,
+        new File[] {});
+    }
+
+    @Test
+    public void testFilelistExcludeDot() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("subfolder/*"),
+                getSetWith("su??old?r/./2.properties"));
+        assertFileListEquals(
+                fileset,
+                new File[] {});
+    }
+
+    @Test
+    public void testFilelistExcludeDoubleDot() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                TEST_BASE_DIR,
+                getSetWith("*"),
+                getSetWith("subfolder/../11.properties"));
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "./package.html"),
+                new File(TEST_BASE_DIR, "./1.properties"));
+    }
+    
+    @Test
+    public void testFilelistExcludeDoubleDotLeavingBaseDir() throws IOException
+    {
+        Fileset fileset = new Fileset(
+                new File(TEST_BASE_DIR, "subfolder"),
+                getSetWith("../*"),
+                getSetWith("../1.properties", ".././package.html"));
+        assertFileListEquals(
+                fileset,
+                new File(TEST_BASE_DIR, "subfolder/../11.properties"));
+    }
+    
+    /**
+     * Checks that the file list of the Fileset contains all the 
+     * expected paths and no other paths. 
+     * 
+     * @param fileset The Fileset which file list should be checked.
+     * @param expectedPaths the paths which should be in the fileset.
+     * 
+     * @throws IOException if Fileset#getFiles() throws it.
+     */
+    private static void assertFileListEquals(
+                Fileset fileset,
+                File... expected)
+            throws IOException
+    {
+        List<File> fileList = fileset.getFiles();
+        assertFileListEquals(fileList, expected);
+    }
+
+    /**
+     * Checks that a file list contains all the 
+     * expected paths and no other paths. 
+     * 
+     * @param fileList The list of files which should be checked.
+     * @param expectedPaths the paths which should be in the file list.
+     */
+    private static void assertFileListEquals(
+                List<File> fileList,
+                File... expected)
+    {
+        Set<File> actualFiles = new HashSet<File>();
+        for (File file : fileList)
         {
-            FileSource fileSource = (FileSource) fileSourcesImpl.next();
-            paths.add(fileSource.getPath());
+            actualFiles.add(file);
         }
         Set<File> expectedFiles = new HashSet<File>();
-        expectedFiles.add(
-                new 
File("src/test/fileSourcesImpl/subfolder/../1.properties"));
-        assertEquals(expectedFiles.size(), paths.size());
-        assertEquals(expectedFiles, paths);
+        for (File file : expected)
+        {
+            expectedFiles.add(file);
+        }
+        assertEquals(expectedFiles, actualFiles);
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]