LENS-513 - add jar should be able to take regex path and should be able to add 
multiple jars


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/4dcff2c2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/4dcff2c2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/4dcff2c2

Branch: refs/heads/current-release-line
Commit: 4dcff2c2658c2ba9c25839e67f6faf1fb0f451b3
Parents: 1a06e77
Author: Yash Sharma <[email protected]>
Authored: Mon Jun 22 19:25:41 2015 +0530
Committer: Rajat Khandelwal <[email protected]>
Committed: Mon Jun 22 19:25:41 2015 +0530

----------------------------------------------------------------------
 .../lens/server/session/SessionResource.java    |  28 +-
 .../apache/lens/server/util/ScannedPaths.java   | 209 ++++++-------
 .../lens/server/util/TestScannedPaths.java      | 302 ++++++++++++++++++-
 3 files changed, 405 insertions(+), 134 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4dcff2c2/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
----------------------------------------------------------------------
diff --git 
a/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java 
b/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
index 4d82a06..a5033fe 100644
--- 
a/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
+++ 
b/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
@@ -19,7 +19,6 @@
 package org.apache.lens.server.session;
 
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -143,17 +142,14 @@ public class SessionResource {
   @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, 
MediaType.TEXT_PLAIN})
   public APIResult addResource(@FormDataParam("sessionid") LensSessionHandle 
sessionid,
     @FormDataParam("type") String type, @FormDataParam("path") String path) {
-    Iterator<String> foundFiles = new ScannedPaths(path, type).iterator();
-    String matchedPath = null;
+    ScannedPaths scannedPaths = new ScannedPaths(path, type);
     int matchedPathsCount = 0;
 
-    if (foundFiles == null) {
-      return new APIResult(Status.FAILED, "No matching resources found for 
provided path.");
-    }
-
     int numAdded = 0;
-    while (foundFiles.hasNext()) {
-      matchedPath = foundFiles.next();
+    for (String matchedPath : scannedPaths) {
+      if (matchedPath.startsWith("file:") && 
!matchedPath.startsWith("file://")) {
+        matchedPath = "file://" + matchedPath.substring("file:".length());
+      }
       numAdded += sessionService.addResourceToAllServices(sessionid, type, 
matchedPath);
       matchedPathsCount++;
     }
@@ -203,20 +199,16 @@ public class SessionResource {
   @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, 
MediaType.TEXT_PLAIN})
   public APIResult deleteResource(@FormDataParam("sessionid") 
LensSessionHandle sessionid,
     @FormDataParam("type") String type, @FormDataParam("path") String path) {
-    Iterator<String> foundFiles = new ScannedPaths(path, type).iterator();
-    String matchedPath = null;
-
-    if (foundFiles == null) {
-      return new APIResult(Status.PARTIAL, "No matching resources found for 
provided path.");
-    }
+    ScannedPaths scannedPaths = new ScannedPaths(path, type);
 
     int numDeleted = 0;
 
-    while(foundFiles.hasNext()) {
-      matchedPath = foundFiles.next();
-
+    for(String matchedPath : scannedPaths) {
       for (LensService service : LensServices.get().getLensServices()) {
         try {
+          if (matchedPath.startsWith("file:") && 
!matchedPath.startsWith("file://")) {
+            matchedPath = "file://" + matchedPath.substring("file:".length());
+          }
           service.deleteResource(sessionid, type, matchedPath);
           numDeleted++;
         } catch (LensException e) {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4dcff2c2/lens-server/src/main/java/org/apache/lens/server/util/ScannedPaths.java
----------------------------------------------------------------------
diff --git 
a/lens-server/src/main/java/org/apache/lens/server/util/ScannedPaths.java 
b/lens-server/src/main/java/org/apache/lens/server/util/ScannedPaths.java
index ac773fe..92687c3 100644
--- a/lens-server/src/main/java/org/apache/lens/server/util/ScannedPaths.java
+++ b/lens-server/src/main/java/org/apache/lens/server/util/ScannedPaths.java
@@ -26,36 +26,26 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
-
 import org.apache.hadoop.fs.Path;
 
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 
+
 @Slf4j
 public class ScannedPaths implements Iterable<String> {
   private String path = null;
   private String type = null;
 
-  private Path fsPath;
-
-  /** Is the provided expression parsed **/
-  private boolean isScanned;
-
-  /* Keep all matched paths so we don't have to query filesystem multiple 
times */
-  private Map<String, String> matchedPaths = null;
-
   /* The Chosen Ones */
-  @Getter(lazy=true) private final List<String> finalPaths = getMatchedPaths();
+  @Getter(lazy=true) private final List<String> finalPaths = 
getMatchedPaths(path, type);
 
   public ScannedPaths(String path, String type) {
     this.path = path;
@@ -64,8 +54,10 @@ public class ScannedPaths implements Iterable<String> {
 
   @Override
   public Iterator<String> iterator() {
-    /** Does all the pattern matching and returns the iterator to finalPaths 
collection **/
-    return (getFinalPaths() == null) ? null : getFinalPaths().iterator();
+    /** Does all the pattern matching and returns the iterator to finalPaths 
collection.
+     *  finalPaths should never be null.
+     **/
+    return getFinalPaths().iterator();
   }
 
   /**
@@ -75,123 +67,114 @@ public class ScannedPaths implements Iterable<String> {
    *
    * Updates finalPaths List with matched paths and returns an iterator for 
matched paths.
    */
-  private List<String> getMatchedPaths() {
-    List<String> finalPaths = null;
+  private List<String> getMatchedPaths(String path, String type) {
+    List<String> finalPaths = new ArrayList<>();
+    FileSystem fs;
+
     try {
-      FileSystem fs = FileSystem.get(new URI(path), new Configuration());
-      fsPath = new Path(new URI(path).getPath());
-
-      if (fs.isDirectory(fsPath)) {
-        findAllMatchedPaths(true);
-        filterByJarType();
-      /* Updates finalPaths List with restrictions imposed
-         by jar_order/glob_order file */
-        finalPaths = getMatchedPathsFilteredByOrder();
+      fs = FileSystem.get(new URI(path), new Configuration());
+      Path pt = new Path(new URI(path));
+
+      if (fs.exists(pt) && fs.isFile(pt)) {
+        /**
+         * CASE 1 : Direct FILE provided in path
+         **/
+        finalPaths.add(pt.toUri().toString());
+      } else if (fs.exists(pt) && fs.isDirectory(pt)) {
+        /**
+         * CASE 2 : DIR provided in path
+         **/
+        Path resourceOrderFile = null;
+        InputStream resourceOrderIStream = null;
+        FileStatus[] statuses;
+        List<String> newMatches;
+        List<String> resources;
+        boolean resourceFileFound = false;
+
+        fs = pt.getFileSystem(new Configuration());
+        resourceOrderFile = new Path(pt, "jar_order");
+        /** Add everything in dir if no jar_order or glob_order is present **/
+        if (!fs.exists(resourceOrderFile)) {
+          resourceOrderFile = new Path(pt, "glob_order");
+          if (!fs.exists(resourceOrderFile)) {
+            /** Get matched resources recursively for all files **/
+            statuses = fs.globStatus(new Path(pt, "*"));
+            for (FileStatus st : statuses) {
+              finalPaths.add(st.getPath().toUri().toString());
+            }
+          } else {
+            resourceFileFound = true;
+          }
+        } else {
+          resourceFileFound = true;
+        }
+
+        if (resourceFileFound) {
+          /** Else get jars as per order specified in jar_order/glob_order **/
+          resourceOrderIStream = fs.open(resourceOrderFile);
+          resources = IOUtils.readLines(resourceOrderIStream, 
Charset.forName("UTF-8"));
+          for (String resource : resources) {
+            if (resource == null || resource.isEmpty()) {
+              continue;
+            }
+
+            /** Get matched resources recursively for provided path/pattern **/
+            if (resource.startsWith("/") || resource.contains(":/")) {
+              newMatches = getMatchedPaths(new Path(resource).toString(), 
type);
+            } else {
+              newMatches = getMatchedPaths(new Path(pt, resource).toString(), 
type);
+            }
+
+            if (newMatches != null) {
+              finalPaths.addAll(newMatches);
+            }
+          }
+        }
       } else {
-        findAllMatchedPaths(false);
-        filterByJarType();
-        if (matchedPaths != null) {
-          finalPaths = new ArrayList<String>(matchedPaths.values());
+        /**
+         * CASE 3 : REGEX provided in path
+         * */
+        FileStatus[] statuses = fs.globStatus(pt);
+        for (FileStatus st : statuses) {
+          List<String> newMatches = getMatchedPaths(st.getPath().toString(), 
type);
+          if (newMatches != null) {
+            finalPaths.addAll(newMatches);
+          }
         }
       }
+
+      filterDirsAndJarType(fs, finalPaths);
+
     } catch (FileNotFoundException fex) {
       log.error("File not found while scanning path.", fex);
     } catch (URISyntaxException | IOException ex) {
       log.error("Exception while initializing PathScanner.", ex);
+    } catch (Exception e) {
+      log.error("Exception while initializing PathScanner.", e);
     }
+
     return finalPaths;
   }
 
-  /**
-   * Populates the matchedPaths[] with all paths matching the pattern.
-   */
-  private void findAllMatchedPaths(boolean isDir) {
-    try {
-      Path path = isDir ? new Path(fsPath, "*") : fsPath;
-      FileStatus[] statuses = path.getFileSystem(new 
Configuration()).globStatus(path);
-
-      if (statuses == null || statuses.length == 0) {
-        log.info("No matched paths found for expression " + path);
-        return;
-      }
-      matchedPaths = new HashMap<String, String>();
-      for (int count = 0; count < statuses.length; count++) {
-        matchedPaths.put(statuses[count].getPath().getName(), 
statuses[count].getPath().toString());
-      }
-    } catch (FileNotFoundException fex) {
-      log.error("File not found while scanning path.", fex);
-      return;
-    } catch (IOException ioex) {
-      log.error("IOException while scanning path.", ioex);
-      return;
-    }
-  }
 
   /**
-   * Filters the matchedPaths by "jar" type.
-   * Removes non-jar resources if type is specified as "jar".
-   */
-  private void filterByJarType() {
-    if (matchedPaths == null) {
-      return;
-    } else if (type.equalsIgnoreCase("jar")) {
-      Iterator<Map.Entry<String, String>> iter = 
matchedPaths.entrySet().iterator();
-
+   * Skip Dirs from matched regex.
+   * We are interested only in file resources.
+   **/
+  private void filterDirsAndJarType(FileSystem fs, List<String> matches) {
+    try {
+      Iterator<String> iter = matches.iterator();
+      String path;
       while (iter.hasNext()) {
-        Map.Entry<String, String> entry = iter.next();
-        if (!entry.getKey().endsWith(".jar")) {
+        path = iter.next();
+        if (fs.isDirectory(new Path(path))) {
+          iter.remove();
+        } else if (type.equalsIgnoreCase("jar") && !path.endsWith(".jar")) {
           iter.remove();
         }
       }
+    } catch (IOException e) {
+      log.error("Exception while initializing filtering dirs.", e);
     }
   }
-
-  /**
-   * Filters the matchedPath[] to remove unwanted resources
-   * and apply ordering to the resources as specified in jar_order or 
glob_order file.
-   * Bypasses filtering if none of the files is present in the directory.
-   */
-  private List<String> getMatchedPathsFilteredByOrder() {
-    if (matchedPaths == null) {
-      return  null;
-    }
-
-    List<String> finalPaths = null;
-    InputStream resourceOrderIStream = null;
-    List<String> resources;
-    try {
-      FileSystem fs = fsPath.getFileSystem(new Configuration());
-      Path resourceOrderFile = new Path(fsPath.toUri().getPath(), "jar_order");
-
-      if (!fs.exists(resourceOrderFile)) {
-        resourceOrderFile = new Path(fsPath.toUri().getPath(), "glob_order");
-        if (!fs.exists(resourceOrderFile)) {
-          /* No order file present. Bypass filtering and Add all resource 
matching pattern */
-          return new ArrayList<>(matchedPaths.values());
-        }
-      }
-
-      resourceOrderIStream = fs.open(resourceOrderFile);
-      resources = IOUtils.readLines(resourceOrderIStream, 
Charset.forName("UTF-8"));
-      finalPaths = new ArrayList<>();
-      for(String resource : resources) {
-        if (resource == null || resource.isEmpty()) {
-          continue;
-        }
-
-        if (matchedPaths.containsKey(resource)) {
-          finalPaths.add(matchedPaths.get(resource));
-        }
-      }
-    } catch (FileNotFoundException fex) {
-      log.error("File not found while scanning path.", fex);
-      finalPaths = null;
-    } catch (IOException ioex) {
-      log.error("IOException while scanning path.", ioex);
-      finalPaths = null;
-    } finally {
-      IOUtils.closeQuietly(resourceOrderIStream);
-    }
-    return finalPaths;
-  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4dcff2c2/lens-server/src/test/java/org/apache/lens/server/util/TestScannedPaths.java
----------------------------------------------------------------------
diff --git 
a/lens-server/src/test/java/org/apache/lens/server/util/TestScannedPaths.java 
b/lens-server/src/test/java/org/apache/lens/server/util/TestScannedPaths.java
index 5073634..ac9faf2 100644
--- 
a/lens-server/src/test/java/org/apache/lens/server/util/TestScannedPaths.java
+++ 
b/lens-server/src/test/java/org/apache/lens/server/util/TestScannedPaths.java
@@ -29,6 +29,7 @@ import org.apache.commons.io.FileUtils;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.Iterators;
 
 import lombok.extern.slf4j.Slf4j;
@@ -64,7 +65,7 @@ public class TestScannedPaths {
       fileRegex = tempPath + "tempdata_unknown_*";
       sc = new ScannedPaths(fileRegex, "file");
 
-      Assert.assertNull(sc.iterator(), "Iterator should be null for unmatched 
path patterns.");
+      Assert.assertFalse(sc.iterator().hasNext(), "Iterator should be empty 
for unmatched path patterns.");
     } catch (Exception e) {
       Assert.fail("Exception while testing ScannedPaths : " + e.getMessage());
     } finally {
@@ -126,7 +127,303 @@ public class TestScannedPaths {
     }
   }
 
-  private File createNewPath(String fileName) {
+  /**
+   * Private method used by testScannedPathsMultipleJarGlobOrder.
+   * Creates temp dir structure with files.
+   *
+   * Dir structure created:
+   * sourceDirPath/tempfiles/tempdata_a.jar
+   * sourceDirPath/tempfiles/tempdata_c.jar
+   * sourceDirPath/tempfiles/dir1/tempdata_a.jar
+   * sourceDirPath/tempfiles/dir1/tempdata_b.jar
+   * sourceDirPath/tempfiles/dir1/tempdata_c.data
+   * sourceDirPath/tempfiles/dir2/tempdata_a.data
+   * sourceDirPath/tempfiles/dir2/tempdata_b.data
+   * sourceDirPath/tempfiles/dir2/tempdata_c.jar
+   * sourceDirPath/tempfiles/dir3/tempdata_a.jar
+   * sourceDirPath/tempfiles/dir3/tempdata_b.data
+   * sourceDirPath/tempfiles/dir3/tempdata_c-duplicate.jar
+   * sourceDirPath/tempfiles/dir3/tempdata_c.jar
+   *
+   * // Additional regex checks for regex in jar_order/glob_order
+   * sourceDirPath/tempfiles/dir3/innerDirA/inceptionLevel2/tempdata_c.jar
+   * 
sourceDirPath/tempfiles/dir3/innerDirA/inceptionLevel2/tempdata_c-duplicate.jar
+   * 
sourceDirPath/tempfiles/dir3/innerDirB/inceptionLevel2/tempdata_c-duplicate-2.jar
+   * @param sourceDirPath
+   */
+  private void createTempDirStructure1(String sourceDirPath) {
+    File jarFile = null, globFile = null;
+    String filenameA, filenameB, filenameC;
+    String tempPath = sourceDirPath + "/tempfiles/";
+    FileUtils.deleteQuietly(new File(tempPath));
+
+    String jarExtension = ".jar";
+    String dataExtension = ".data";
+    String dir1 = "dir1/", dir2 = "dir2/", dir3 = "dir3/";
+
+    filenameA = "tempdata_a";
+    filenameB = "tempdata_b";
+    filenameC = "tempdata_c";
+
+    PrintWriter writer;
+
+    try {
+      createNewPath(tempPath, filenameC, jarExtension);
+      createNewPath(tempPath, filenameA, jarExtension);
+      createNewPath(tempPath, dir1, filenameA, jarExtension);
+      createNewPath(tempPath, dir1, filenameB, jarExtension);
+      createNewPath(tempPath, dir1, filenameC, dataExtension);
+      createNewPath(tempPath, dir2, filenameA, dataExtension);
+      createNewPath(tempPath, dir2, filenameB, dataExtension);
+      createNewPath(tempPath, dir2, filenameC, jarExtension);
+      createNewPath(tempPath, dir3, filenameA, jarExtension);
+      createNewPath(tempPath, dir3, filenameB, dataExtension);
+      createNewPath(tempPath, dir3, filenameC, jarExtension);
+      createNewPath(tempPath, dir3, filenameC, "-duplicate", jarExtension);
+
+      /** Additional paths for inner dirs **/
+      createNewPath(tempPath, dir3, "innerDirA/inceptionLevel2/", filenameC, 
jarExtension);
+      createNewPath(tempPath, dir3, "innerDirA/inceptionLevel2/", filenameC, 
"-duplicate", jarExtension);
+      createNewPath(tempPath, dir3, "innerDirB/inceptionLevel2/", filenameC, 
"-duplicate-2", jarExtension);
+
+      /** Create jar_order **/
+      jarFile = createNewPath(tempPath, dir1, "jar_order");
+      writer = new PrintWriter(jarFile, "UTF-8");
+      writer.println(filenameB + jarExtension);
+      writer.println(filenameA + jarExtension);
+      writer.close();
+      jarFile = createNewPath(tempPath, dir2, "jar_order");
+      writer = new PrintWriter(jarFile, "UTF-8");
+      writer.println(filenameC + jarExtension);
+      writer.close();
+      jarFile = createNewPath(tempPath, dir3, "jar_order");
+      writer = new PrintWriter(jarFile, "UTF-8");
+      writer.println(filenameC + "-duplicate" + jarExtension);
+      writer.println(filenameA + jarExtension);
+      writer.close();
+
+      /** Create glob_order **/
+      globFile = createNewPath(tempPath, dir1, "glob_order");
+      writer = new PrintWriter(globFile, "UTF-8");
+      writer.println(filenameC + dataExtension);
+      writer.println(filenameB + jarExtension);
+      writer.println(filenameA + jarExtension);
+      writer.close();
+
+      globFile = createNewPath(tempPath, dir3, "glob_order");
+      writer = new PrintWriter(globFile, "UTF-8");
+      writer.println(filenameC + jarExtension);
+      writer.println(filenameC + "-duplicate" + jarExtension);
+      /** Check regex compatibility in order files **/
+      writer.println("inner*/*Level*/" + filenameC + "-duplicate*" + 
jarExtension);
+      writer.close();
+
+
+
+    } catch (Exception ex) {
+      Assert.fail("Exception while creating temp paths : " + ex.getMessage());
+    }
+  }
+
+
+  /**
+   * Private method used by testScannedPathsRecursive.
+   * Creates temp dir structure with files.
+   *
+   * Dir structure created:
+   * sourceDirPath/tempfiles/a_dir/jar_1
+   * sourceDirPath/tempfiles/a_dir/jar_2
+   * sourceDirPath/tempfiles/a_dir/jar_order (*1\n*2)
+   *
+   * sourceDirPath/tempfiles/b_dir/jar_3
+   * sourceDirPath/tempfiles/b_dir/jar_4
+   * sourceDirPath/tempfiles/b_dir/glob_order (*4\n*3)
+   *
+   * sourceDirPath/tempfiles/c_dir/jar_5
+   * sourceDirPath/tempfiles/c_dir/jar_6
+   *
+   * sourceDirPath/tempfiles/jar_order (a*\nb*\nc*)
+   *
+   * @param sourceDirPath
+   */
+  private void createTempDirStructure2(String sourceDirPath) {
+    File orderFile = null;
+    String filenameA, filenameB, filenameC;
+    String tempPath = sourceDirPath + "/parent_dir/";
+    FileUtils.deleteQuietly(new File(tempPath));
+
+    PrintWriter writer;
+
+    try {
+      createNewPath(tempPath, "a_dir/jar_1");
+      createNewPath(tempPath, "a_dir/jar_2");
+      createNewPath(tempPath, "b_dir/jar_3");
+      createNewPath(tempPath, "b_dir/jar_4");
+      createNewPath(tempPath, "c_dir/jar_5");
+      createNewPath(tempPath, "c_dir/jar_6");
+
+
+      /** Create jar_order **/
+      orderFile = createNewPath(tempPath, "a_dir/jar_order");
+      writer = new PrintWriter(orderFile, "UTF-8");
+      writer.println("*1");
+      writer.println("*2");
+      writer.close();
+
+      orderFile = createNewPath(tempPath, "b_dir/glob_order");
+      writer = new PrintWriter(orderFile, "UTF-8");
+      writer.println("*4");
+      writer.println("*3");
+      writer.close();
+
+      orderFile = createNewPath(tempPath, "jar_order");
+      writer = new PrintWriter(orderFile, "UTF-8");
+      writer.println("a*");
+      writer.println("b*");
+      writer.println("c*");
+      writer.close();
+
+    } catch (Exception ex) {
+      Assert.fail("Exception while creating temp paths : " + ex.getMessage());
+    }
+  }
+
+  public void testScannedPathsMultipleJarGlobOrder() throws Exception {
+    ScannedPaths sc;
+    Iterator<String> iter = null;
+    String tempPath = "target/test/";
+
+    String filenameA = "tempdata_a";
+    String filenameB = "tempdata_b";
+    String filenameC = "tempdata_c";
+
+    String fileRegex1 = tempPath + "*";
+    String fileRegex2 = tempPath + "*/*";
+
+    try {
+      createTempDirStructure1(tempPath);
+
+      sc = new ScannedPaths(fileRegex1, "jar");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 2, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameA));
+
+      sc = new ScannedPaths(fileRegex2, "jar");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 7, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains(filenameB));
+      Assert.assertTrue(iter.next().contains(filenameA));
+      Assert.assertTrue(iter.next().contains(filenameC)); // Direct matched 
tempdata_c.jar
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameC + "-duplicate"));
+      Assert.assertTrue(iter.next().contains(filenameA));
+      Assert.assertTrue(iter.next().contains(filenameA)); // Direct matched 
tempdata_a.jar
+
+      /** Remove jar_order files from temp dir **/
+      FileUtils.deleteQuietly(new File(tempPath + "tempfiles/dir1/jar_order"));
+      FileUtils.deleteQuietly(new File(tempPath + "tempfiles/dir2/jar_order"));
+      FileUtils.deleteQuietly(new File(tempPath + "tempfiles/dir3/jar_order"));
+
+      sc = new ScannedPaths(fileRegex1, "file");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 2, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameA));
+
+      sc = new ScannedPaths(fileRegex2, "file");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 12, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameB));
+      Assert.assertTrue(iter.next().contains(filenameA));
+      Assert.assertTrue(iter.next().contains(filenameC)); // Direct matched 
tempdata_c.jar
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameB));
+      Assert.assertTrue(iter.next().contains(filenameA));
+      Assert.assertTrue(iter.next().contains(filenameC));
+      Assert.assertTrue(iter.next().contains(filenameC + "-duplicate"));
+      Assert.assertTrue(iter.next().contains(filenameC + "-duplicate-2")); // 
Inner dir regex match
+      Assert.assertTrue(iter.next().contains(filenameC + "-duplicate")); // 
Inner dir regex match
+      Assert.assertTrue(iter.next().contains(filenameA)); // Direct matched 
tempdata_a.jar
+
+    } catch (Exception e) {
+      Assert.fail("Exception while testing ScannedPaths : " + e.getMessage());
+    } finally {
+      FileUtils.deleteQuietly(new File(tempPath));
+    }
+  }
+
+
+  public void testScannedPathsRecursive() throws Exception {
+    ScannedPaths sc;
+    Iterator<String> iter = null;
+    String tempPath = "target/test/";
+
+    String fileRegex = tempPath + "parent_*";
+
+    try {
+      createTempDirStructure2(tempPath);
+
+      sc = new ScannedPaths(fileRegex, "file");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 6, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains("jar_1"));
+      Assert.assertTrue(iter.next().contains("jar_2"));
+      Assert.assertTrue(iter.next().contains("jar_4"));
+      Assert.assertTrue(iter.next().contains("jar_3"));
+      Assert.assertTrue(iter.next().contains("jar_6"));
+      Assert.assertTrue(iter.next().contains("jar_5"));
+
+      /** Now also enforce order for c_dir **/
+      File orderFile = createNewPath(tempPath, "parent_dir/c_dir/glob_order");
+      PrintWriter writer = new PrintWriter(orderFile, "UTF-8");
+      writer.println("*5");
+      writer.println("*6");
+      writer.close();
+
+      sc = new ScannedPaths(fileRegex, "file");
+      Assert.assertEquals(Iterators.size(sc.iterator()), 6, "Incorrect number 
of matches found");
+      iter = sc.iterator();
+      Assert.assertTrue(iter.next().contains("jar_1"));
+      Assert.assertTrue(iter.next().contains("jar_2"));
+      Assert.assertTrue(iter.next().contains("jar_4"));
+      Assert.assertTrue(iter.next().contains("jar_3"));
+      Assert.assertTrue(iter.next().contains("jar_5"));
+      Assert.assertTrue(iter.next().contains("jar_6"));
+
+    } catch (Exception e) {
+      Assert.fail("Exception while testing ScannedPaths : " + e.getMessage());
+    } finally {
+      FileUtils.deleteQuietly(new File(tempPath));
+    }
+  }
+
+
+  public void testScannedPathsNonExistent() throws Exception {
+    ScannedPaths sc;
+    Iterator<String> iter = null;
+    String tempPath = "target/test/";
+
+    String fileRegex = tempPath + "paradox/nopath";
+
+    try {
+      sc = new ScannedPaths(fileRegex, "file");
+      Assert.assertFalse(sc.iterator().hasNext(), "Iterator should be empty 
for unmatched path patterns.");
+
+      sc = new ScannedPaths(fileRegex, "jar");
+      Assert.assertFalse(sc.iterator().hasNext(), "Iterator should be empty 
for unmatched path patterns.");
+
+    } catch (Exception e) {
+      Assert.fail("Exception while testing ScannedPaths : " + e.getMessage());
+    } finally {
+      FileUtils.deleteQuietly(new File(tempPath));
+    }
+  }
+
+  private File createNewPath(String ... params) {
+    String fileName = Joiner.on("").join(params);
+
     File f = new File(fileName);
     try {
       if (!f.getParentFile().exists()) {
@@ -140,5 +437,4 @@ public class TestScannedPaths {
     }
     return f;
   }
-
 }

Reply via email to