[jboss-svn-commits] JBoss Common SVN: r3826 - in shrinkwrap/trunk: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Dec 4 00:58:55 EST 2009
Author: ALRubinger
Date: 2009-12-04 00:58:55 -0500 (Fri, 04 Dec 2009)
New Revision: 3826
Added:
shrinkwrap/trunk/spi/src/main/java/org/jboss/shrinkwrap/spi/PathProvider.java
Removed:
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java
Modified:
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Path.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/BasicPath.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/PathUtil.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/BasicPathTestCase.java
Log:
[SHRINKWRAP-95] Add an SPI method for PathProvider.parent()
Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Path.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Path.java 2009-12-04 03:42:05 UTC (rev 3825)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Path.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -17,8 +17,6 @@
package org.jboss.shrinkwrap.api;
/**
- * Path
- *
* Represents a target context within an {@link Archive} under
* which an {@link Asset} may be found.
*
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/BasicPath.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/BasicPath.java 2009-12-04 03:42:05 UTC (rev 3825)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/BasicPath.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -20,6 +20,7 @@
import java.util.logging.Logger;
import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.spi.PathProvider;
/**
* BasicPath
@@ -31,7 +32,7 @@
* @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
* @version $Revision: $
*/
-public class BasicPath implements Path, Comparable<Path>
+public class BasicPath implements PathProvider, Comparable<Path>
{
//-------------------------------------------------------------------------------------||
@@ -78,7 +79,7 @@
if (log.isLoggable(Level.FINER))
{
log.finer("Resolved \"" + context + "\" to absolute form: " + resolvedContext);
- }
+ }
this.context = resolvedContext;
}
@@ -149,6 +150,28 @@
return path.get().compareTo(this.get());
}
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.shrinkwrap.spi.PathProvider#parent()
+ */
+ @Override
+ public Path parent()
+ {
+ // Get the last index of "/"
+ final String resolvedContext = PathUtil.optionallyRemoveFollowingSlash(this.context);
+ final int lastIndex = resolvedContext.lastIndexOf(PathUtil.SLASH);
+ // If it either doesn't occur or is the root
+ if (lastIndex == -1 || (lastIndex == 0 && resolvedContext.length() == 1))
+ {
+ // No parent present, return null
+ return null;
+ }
+ // Get the parent context
+ final String sub = resolvedContext.substring(0, lastIndex);
+ // Return
+ return new BasicPath(sub);
+ }
+
//-------------------------------------------------------------------------------------||
// Overridden Implementations ---------------------------------------------------------||
//-------------------------------------------------------------------------------------||
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/PathUtil.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/PathUtil.java 2009-12-04 03:42:05 UTC (rev 3825)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/path/PathUtil.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -28,7 +28,7 @@
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
*/
-final class PathUtil
+public final class PathUtil
{
//-------------------------------------------------------------------------------------||
@@ -143,7 +143,7 @@
* @param path
* @return
*/
- static String optionallyRemovePrecedingSlash(final String path)
+ public static String optionallyRemovePrecedingSlash(final String path)
{
// Precondition check
assertSpecified(path);
@@ -160,13 +160,36 @@
}
/**
+ * Removes, if present, the absolute slash following
+ * the specified path, and returns the adjusted result.
+ *
+ * @param path
+ * @return
+ */
+ static String optionallyRemoveFollowingSlash(final String path)
+ {
+ // Precondition check
+ assertSpecified(path);
+
+ // Is there's a last character of slash
+ if (isLastCharSlash(path))
+ {
+ // Return everything but last char
+ return path.substring(0, path.length() - 1);
+ }
+
+ // Return as-is
+ return path;
+ }
+
+ /**
* Adds, if not already present, the absolute slash following
* the specified path, and returns the adjusted result.
*
* @param path
* @return
*/
- static String optionallyAppendSlash(final String path)
+ public static String optionallyAppendSlash(final String path)
{
// Precondition check
assertSpecified(path);
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/BasicPathTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/BasicPathTestCase.java 2009-12-04 03:42:05 UTC (rev 3825)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/BasicPathTestCase.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -18,7 +18,10 @@
import java.util.logging.Logger;
+import junit.framework.TestCase;
+
import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.spi.PathProvider;
import org.junit.Assert;
import org.junit.Test;
@@ -249,4 +252,35 @@
Assert.assertTrue("Paths with different contexts should not be equal by value", !path1.equals(path2));
log.info(path1 + " not equal by value to " + path2);
}
+
+ /**
+ * Ensures the contract of {@link PathProvider#parent()}
+ * is intact
+ */
+ @Test
+ public void testParent()
+ {
+ // Log
+ log.info("testParent");
+
+ // Create new paths
+ final String rootString = "/";
+ final String subpathString = "subpath";
+ final String contextString = "context";
+ final String context2String = "context/";
+ final BasicPath root= new BasicPath(rootString);
+ final BasicPath subpath = new BasicPath(subpathString);
+ final BasicPath context = new BasicPath(subpath, contextString);
+ final BasicPath contextWithFollowingSlash = new BasicPath(subpath, context2String);
+
+ // Test
+ TestCase.assertEquals("The parent of the context path should be equal to the initial subpath", subpath, context
+ .parent());
+ TestCase.assertEquals(
+ "The parent of the context path with a following slash should be equal to the initial subpath", subpath,
+ contextWithFollowingSlash.parent());
+ TestCase.assertEquals("The parent of the subpath should be the root", root, subpath.parent());
+ TestCase.assertNull("The parent of the root should be null", root.parent());
+
+ }
}
Deleted: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java 2009-12-04 03:42:05 UTC (rev 3825)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -1,254 +0,0 @@
-/*
- * 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.shrinkwrap.impl.base.path;
-
-import java.util.logging.Logger;
-
-
-import org.jboss.shrinkwrap.api.Path;
-import org.jboss.shrinkwrap.api.Paths;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * PathsTestCase
- *
- * Tests to ensure that the {@link Paths}
- * implementation creates Paths as expected
- * from various specified contexts
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public class PathsTestCase
-{
-
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(PathsTestCase.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 = Paths.create(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 = Paths.create(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 = Paths.create(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 = Paths.create(base);
-
- // Create a new path using a relative context to the base
- final String context = "context";
- final Path contextPath = Paths.create(context);
- final Path path = Paths.create(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 = Paths.create(base);
-
- // Create a new path using a relative context to the base
- final String context = "context";
- final Path path = Paths.create(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 = Paths.create(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 = Paths.create(context);
- final Path path2 = Paths.create(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 = Paths.create(context);
- final Path path2 = Paths.create(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 = Paths.create(context1);
- final Path path2 = Paths.create(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);
- }
-}
Added: shrinkwrap/trunk/spi/src/main/java/org/jboss/shrinkwrap/spi/PathProvider.java
===================================================================
--- shrinkwrap/trunk/spi/src/main/java/org/jboss/shrinkwrap/spi/PathProvider.java (rev 0)
+++ shrinkwrap/trunk/spi/src/main/java/org/jboss/shrinkwrap/spi/PathProvider.java 2009-12-04 05:58:55 UTC (rev 3826)
@@ -0,0 +1,41 @@
+/*
+ * 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.shrinkwrap.spi;
+
+import org.jboss.shrinkwrap.api.Path;
+
+/**
+ * Service Provider Interface of implementations of a
+ * {@link Path}; exposes support that need not be visible to \
+ * end users.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface PathProvider extends Path
+{
+
+ /**
+ * Obtains the parent of this Path, if exists, else null.
+ * For instance if the Path is "/my/path", the parent
+ * will be "/my". Each call will result in a new object reference,
+ * though subsequent calls upon the same Path will be equal by value.
+ * @return
+ */
+ Path parent();
+
+}
More information about the jboss-svn-commits
mailing list