[jboss-svn-commits] JBoss Common SVN: r3680 - in shrinkwrap/trunk: impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Nov 12 05:09:55 EST 2009
Author: aslak
Date: 2009-11-12 05:09:55 -0500 (Thu, 12 Nov 2009)
New Revision: 3680
Added:
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Paths.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java
Log:
SHRINKWRAP-57 Created API Path factory for creating new paths.
Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Paths.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Paths.java (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Paths.java 2009-11-12 10:09:55 UTC (rev 3680)
@@ -0,0 +1,114 @@
+/*
+ * 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.api;
+
+
+/**
+ * A Factory for Path creation.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public final class Paths
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private static final String PATH_IMPL = "org.jboss.shrinkwrap.impl.base.path.BasicPath";
+
+ /**
+ * Creates a new Path representing the root path (/).
+ *
+ * @return a new root Path
+ */
+ public static Path root()
+ {
+ return create(null);
+ }
+
+ /**
+ * Creates a new Path with the specified context
+ *
+ * @param context The context which this path represents. Null or
+ * blank represents the root. Relative paths will be adjusted
+ * to absolute form.
+ * @return a new Path
+ */
+ public static Path create(String context)
+ {
+ return createInstance(new Class<?>[] {String.class}, new Object[]{context});
+ }
+
+ /**
+ * Creates a new Path using the specified base
+ * and specified relative context.
+ *
+ * @param basePath A absolute path
+ * @param context A relative path to basePath
+ * @return a new Path
+ */
+ public static Path create(String basePath, String context)
+ {
+ return createInstance(new Class<?>[] {String.class, String.class}, new Object[]{basePath, context});
+ }
+
+ /**
+ * Creates a new Path using the specified base
+ * and specified relative context.
+ *
+ * @param basePath A absolute path
+ * @param context A relative path to basePath
+ * @return a new Path
+ */
+ public static Path create(Path basePath, String context)
+ {
+ return createInstance(new Class<?>[] {Path.class, String.class}, new Object[]{basePath, context});
+ }
+
+ /**
+ * Creates a new Path using the specified base
+ * and specified relative context.
+ *
+ * @param basePath A absolute path
+ * @param context A relative path to basePath
+ * @return a new Path
+ */
+ public static Path create(Path basePath, Path context)
+ {
+ return createInstance(new Class<?>[] {Path.class, Path.class}, new Object[]{basePath, context});
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members - Internal Helpers ---------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private static Path createInstance(Class<?>[] argumentTypes, Object[] arguments)
+ {
+ return (Path) ReflectionUtil.createInstance(PATH_IMPL, argumentTypes, arguments);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * No instantiation
+ */
+ private Paths() {}
+
+}
Added: 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 (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/path/PathsTestCase.java 2009-11-12 10:09:55 UTC (rev 3680)
@@ -0,0 +1,254 @@
+/*
+ * 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);
+ }
+}
More information about the jboss-svn-commits
mailing list