[jboss-svn-commits] JBoss Common SVN: r3466 - in declarchive/trunk/impl-base/src: test/java/org/jboss/declarchive/impl/base/path and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Aug 21 05:29:05 EDT 2009


Author: ALRubinger
Date: 2009-08-21 05:29:04 -0400 (Fri, 21 Aug 2009)
New Revision: 3466

Added:
   declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/BasicPathTestCase.java
Modified:
   declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/BasicPath.java
   declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/PathUtil.java
   declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/PathUtilTestCase.java
Log:
[TMPARCH-12] Add tests for BasicPath impl

Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/BasicPath.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/BasicPath.java	2009-08-21 06:37:32 UTC (rev 3465)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/BasicPath.java	2009-08-21 09:29:04 UTC (rev 3466)
@@ -16,10 +16,10 @@
  */
 package org.jboss.declarchive.impl.base.path;
 
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.jboss.declarchive.api.Path;
-import org.jboss.declarchive.impl.base.Validate;
 
 /**
  * BasicPath
@@ -28,6 +28,7 @@
  * namespace context at construction time.  Thread-safe.
  *
  * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
  * @version $Revision: $
  */
 public class BasicPath implements Path, Comparable<Path>
@@ -59,15 +60,18 @@
    /**
     * Creates a new Path with the specified context
     * 
-    * @param prefix The prefix to prepend to every context returned
-    * in {@link BasicPath#get()}.  May be null or blank
     * @param context The context which this path represents.  Null or 
-    * blank represents the root.
+    * blank represents the root.  Relative paths will be adjusted
+    * to absolute form.
     */
    public BasicPath(final String context)
    {
-      Validate.notNull(context, "Context must be specified");
-      this.context = context;
+      final String resolvedContext = PathUtil.optionallyPrependSlash(context);
+      if (log.isLoggable(Level.FINER))
+      {
+         log.finer("Resolved \"" + context + "\" to absolute form: " + resolvedContext);
+      }
+      this.context = resolvedContext;
    }
 
    /**

Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/PathUtil.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/PathUtil.java	2009-08-21 06:37:32 UTC (rev 3465)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/path/PathUtil.java	2009-08-21 09:29:04 UTC (rev 3466)
@@ -40,6 +40,11 @@
     */
    static final char SLASH = '/';
 
+   /**
+    * Empty String
+    */
+   static final String EMPTY = "";
+
    //-------------------------------------------------------------------------------------||
    // Constructor ------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -70,7 +75,7 @@
 
       // Compose
       final String relative = PathUtil.adjustToAbsoluteDirectoryContext(base);
-      final String reformedContext = PathUtil.removePrecedingSlash(context);
+      final String reformedContext = PathUtil.optionallyRemovePrecedingSlash(context);
       final String actual = relative + reformedContext;
 
       // Return
@@ -96,7 +101,7 @@
       }
 
       // Strip absolute form
-      final String removedPrefix = removePrecedingSlash(path);
+      final String removedPrefix = optionallyRemovePrecedingSlash(path);
       // Add end of context slash
       final String addedPostfix = optionallyAppendSlash(removedPrefix);
 
@@ -133,12 +138,12 @@
 
    /**
     * Removes, if present, the absolute slash preceding
-    * the specified path, and returns the adjusted result
+    * the specified path, and returns the adjusted result. 
     * 
     * @param path
     * @return
     */
-   static String removePrecedingSlash(final String path)
+   static String optionallyRemovePrecedingSlash(final String path)
    {
       // Precondition check
       assertSpecified(path);
@@ -156,7 +161,7 @@
 
    /**
     * Adds, if not already present, the absolute slash following
-    * the specified path, and returns the adjusted result
+    * the specified path, and returns the adjusted result.  
     * 
     * @param path
     * @return
@@ -179,25 +184,31 @@
 
    /**
     * Adds, if not already present, the absolute slash preceding
-    * the specified path, and returns the adjusted result
+    * the specified path, and returns the adjusted result.  If 
+    * the argument is null, adjusts to an empty String 
+    * before processing.
     * 
     * @param path
     * @return
     */
    static String optionallyPrependSlash(final String path)
    {
-      // Precondition check
-      assertSpecified(path);
+      // Adjust null
+      String resolved = path;
+      if (resolved == null)
+      {
+         resolved = EMPTY;
+      }
 
       // If the first character is not a slash
-      if (!isFirstCharSlash(path))
+      if (!isFirstCharSlash(resolved))
       {
          // Prepend the slash
-         return SLASH + path;
+         return SLASH + resolved;
       }
 
       // Return as-is
-      return path;
+      return resolved;
    }
 
    //-------------------------------------------------------------------------------------||
@@ -211,6 +222,10 @@
    private static boolean isFirstCharSlash(final String path)
    {
       assertSpecified(path);
+      if (path.length() == 0)
+      {
+         return false;
+      }
       return path.charAt(0) == SLASH;
    }
 
@@ -221,7 +236,11 @@
    private static boolean isLastCharSlash(final String path)
    {
       assertSpecified(path);
-      return path.charAt(path.length() - 1) == '/';
+      if (path.length() == 0)
+      {
+         return false;
+      }
+      return path.charAt(path.length() - 1) == SLASH;
    }
 
    /**

Added: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/BasicPathTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/BasicPathTestCase.java	                        (rev 0)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/BasicPathTestCase.java	2009-08-21 09:29:04 UTC (rev 3466)
@@ -0,0 +1,252 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.declarchive.impl.base.path;
+
+import java.util.logging.Logger;
+
+import org.jboss.declarchive.api.Path;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * BasicPathTestCase
+ *
+ * Tests to ensure that the {@link BasicPath}
+ * implementation creates Paths as expected 
+ * from various specified contexts 
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class BasicPathTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(BasicPathTestCase.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that a null context results in 
+    * a root Path
+    */
+   @Test
+   public void testNullDefaultsToRoot()
+   {
+      // Log
+      log.info("testNullDefaultsToRoot");
+
+      // Create a path with null context
+      final Path path = new BasicPath(null);
+
+      // Ensure expected
+      final String resolved = path.get();
+      Assert.assertEquals("Null context should resolve to root path", String.valueOf(PathUtil.SLASH), resolved);
+      log.info("null argument resolves to: " + path);
+   }
+
+   /**
+    * Ensures that a relative path resolves 
+    * to absoulte form in the single-arg ctor
+    */
+   @Test
+   public void testRelativeResolvedToAbsolute()
+   {
+      // Log
+      log.info("testRelativeResolvedToAbsolute");
+
+      // Create a relative path
+      final String relative = "relative";
+      final Path path = new BasicPath(relative);
+
+      // Ensure expected
+      final String resolved = path.get();
+      final String expected = PathUtil.SLASH + relative;
+      Assert.assertEquals("Relative paths should resolve to absolute", expected, resolved);
+      log.info("\"" + relative + "\" resolves to: " + path);
+   }
+
+   /**
+    * Ensures that an absolute directory path
+    * is preserved as-is
+    */
+   @Test
+   public void testAbsoluteDirectoryContextPreserved()
+   {
+      // Log
+      log.info("testAbsoluteDirectoryContextPreserved");
+
+      // Create an absolute dir path
+      final String absoluteDir = "/absoluteDir/";
+      final Path path = new BasicPath(absoluteDir);
+
+      // Ensure expected
+      final String resolved = path.get();
+      final String expected = absoluteDir;
+      Assert.assertEquals("Absolute directory contexts should be preserved", expected, resolved);
+      log.info("\"" + absoluteDir + "\" resolves to: " + path);
+   }
+
+   /**
+    * Ensures that a new path may be created from a 
+    * context under a specified base path
+    */
+   @Test
+   public void testBasePathAndRelativeContext()
+   {
+      // Log
+      log.info("testBasePathAndRelativeContext");
+
+      // Create a base path
+      final String base = "base";
+      final Path basePath = new BasicPath(base);
+
+      // Create a new path using a relative context to the base
+      final String context = "context";
+      final Path contextPath = new BasicPath(context);
+      final Path path = new BasicPath(basePath, contextPath);
+
+      // Ensure expected
+      final String resolved = path.get();
+      final String expected = PathUtil.SLASH + base + PathUtil.SLASH + context;
+      Assert.assertEquals("Context under base should resolve to relative", expected, resolved);
+      log.info("\"" + context + "\" under base " + basePath + " resolves to: " + path);
+   }
+
+   /**
+    * Ensures that a new path may be created from a 
+    * context (as String) under a specified base path
+    */
+   @Test
+   public void testBasePathAndRelativeContextAsString()
+   {
+      // Log
+      log.info("testBasePathAndRelativeContextAsString");
+
+      // Create a base path
+      final String base = "base";
+      final Path basePath = new BasicPath(base);
+
+      // Create a new path using a relative context to the base
+      final String context = "context";
+      final Path path = new BasicPath(basePath, context);
+
+      // Ensure expected
+      final String resolved = path.get();
+      final String expected = PathUtil.SLASH + base + PathUtil.SLASH + context;
+      Assert.assertEquals("Context under base should resolve to relative", expected, resolved);
+      log.info("\"" + context + "\" under base " + basePath + " resolves to: " + path);
+   }
+
+   /**
+    * Ensures that a new path may be created from a 
+    * context (as String) under a specified base path (which is represented
+    * as a String)
+    */
+   @Test
+   public void testBasePathAsStringAndRelativeContextAsString()
+   {
+      // Log
+      log.info("testBasePathAsStringAndRelativeContextAsString");
+
+      // Create a base path
+      final String base = "base";
+
+      // Create a new path using a relative context to the base
+      final String context = "context";
+      final Path path = new BasicPath(base, context);
+
+      // Ensure expected
+      final String resolved = path.get();
+      final String expected = PathUtil.SLASH + base + PathUtil.SLASH + context;
+      Assert.assertEquals("Context under base should resolve to relative", expected, resolved);
+      log.info("\"" + context + "\" under base \"" + base + "\" resolves to: " + path);
+   }
+
+   /**
+    * Ensures that Paths with equal
+    * contexts have equal hash codes
+    */
+   @Test
+   public void testHashCode()
+   {
+      // Log
+      log.info("testHashCode");
+
+      // Create new paths
+      final String context = "context";
+      final Path path1 = new BasicPath(context);
+      final Path path2 = new BasicPath(context);
+
+      // Obtain hash 
+      final int hash1 = path1.hashCode();
+      final int hash2 = path2.hashCode();
+
+      // Ensure expected
+      Assert.assertEquals("Paths with the same context should have equal hash codes", hash1, hash2);
+      log.info("Both " + path1 + " and " + path2 + " have hashCode: " + hash1);
+   }
+
+   /**
+    * Ensures that Paths with equal contexts 
+    * are equal by value
+    */
+   @Test
+   public void testEquals()
+   {
+      // Log
+      log.info("testEquals");
+
+      // Create new paths
+      final String context = "context";
+      final Path path1 = new BasicPath(context);
+      final Path path2 = new BasicPath(context);
+
+      // Ensure expected
+      Assert.assertEquals("Paths with same context should be equal by value", path1, path2);
+      log.info(path1 + " equal by value to " + path2);
+   }
+
+   /**
+    * Ensures that Paths with inequal contexts 
+    * are equal by value
+    */
+   @Test
+   public void testNotEqual()
+   {
+      // Log
+      log.info("testEquals");
+
+      // Create new paths
+      final String context1 = "context1";
+      final String context2 = "context2";
+      final Path path1 = new BasicPath(context1);
+      final Path path2 = new BasicPath(context2);
+
+      // Ensure expected
+      Assert.assertTrue("Paths with different contexts should not be equal by value", !path1.equals(path2));
+      log.info(path1 + " not equal by value to " + path2);
+   }
+}

Modified: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/PathUtilTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/PathUtilTestCase.java	2009-08-21 06:37:32 UTC (rev 3465)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/path/PathUtilTestCase.java	2009-08-21 09:29:04 UTC (rev 3466)
@@ -55,7 +55,7 @@
       log.info("testRemovePrecedingSlash");
       final String precedingSlash = "/test/something";
       final String expected = precedingSlash.substring(1);
-      final String result = PathUtil.removePrecedingSlash(precedingSlash);
+      final String result = PathUtil.optionallyRemovePrecedingSlash(precedingSlash);
       Assert.assertEquals("Call to remove preceding slash should return everything in input except the first slash",
             expected, result);
    }
@@ -68,7 +68,7 @@
    {
       log.info("testRemovePrecedingSlash");
       final String noPrecedingSlash = "test/something";
-      final String result = PathUtil.removePrecedingSlash(noPrecedingSlash);
+      final String result = PathUtil.optionallyRemovePrecedingSlash(noPrecedingSlash);
       Assert.assertEquals(
             "Call to remove preceding slash on input with no preceding slash should return equal by value to input",
             noPrecedingSlash, result);



More information about the jboss-svn-commits mailing list