Author: hlship
Date: Tue Nov 28 17:05:13 2006
New Revision: 480310
URL: http://svn.apache.org/viewvc?view=rev&rev=480310
Log:
Add method withExtension() to Resource, and add a number of related tests.
Added:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.ext
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.txt
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource_fr.txt
Modified:
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java
Modified:
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java?view=diff&rev=480310&r1=480309&r2=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
(original)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/Resource.java
Tue Nov 28 17:05:13 2006
@@ -12,26 +12,35 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package org.apache.tapestry.ioc;
-
-import java.net.URL;
-import java.util.Locale;
-
-/**
- * Represents a resource on the server that may be used for server side
processing, or may be
- * exposed to the client side. Generally, this represents an abstraction on
top of files on the
- * class path and files stored in the web application context.
- * <p>
- * Resources are often used as map keys; they should be immutable and should
implement hashCode()
- * and equals().
- *
- *
- */
-public interface Resource
-{
- /** Returns the URL for the resource, or null if it does not exist. */
- URL getResourceURL();
-
- /** Returns a localized version of the resource. */
- Resource getLocalization(Locale locale);
-}
+package org.apache.tapestry.ioc;
+
+import java.net.URL;
+import java.util.Locale;
+
+/**
+ * Represents a resource on the server that may be used for server side
processing, or may be
+ * exposed to the client side. Generally, this represents an abstraction on
top of files on the
+ * class path and files stored in the web application context.
+ * <p>
+ * Resources are often used as map keys; they should be immutable and should
implement hashCode()
+ * and equals().
+ */
+public interface Resource
+{
+ /** Returns the URL for the resource, or null if it does not exist. */
+ URL getResourceURL();
+
+ /** Returns a localized version of the resource. */
+ Resource getLocalization(Locale locale);
+
+ /**
+ * Returns a new Resource with the extension changed (or, if the resource
does not have an
+ * extension, the extension is added). The new Resource may not exist
(that is,
+ * [EMAIL PROTECTED] #getResourceURL()} may return null.
+ *
+ * @param extension
+ * to apply to the resource
+ * @return the new resource
+ */
+ Resource withExtension(String extension);
+}
Modified:
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java?view=diff&rev=480310&r1=480309&r2=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
(original)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
Tue Nov 28 17:05:13 2006
@@ -49,8 +49,7 @@
public synchronized URL getResourceURL()
{
- if (_url == null)
- _url = _classLoader.getResource(_path);
+ if (_url == null) _url = _classLoader.getResource(_path);
return _url;
}
@@ -58,14 +57,11 @@
@Override
public boolean equals(Object obj)
{
- if (obj == null)
- return false;
+ if (obj == null) return false;
- if (obj == this)
- return true;
+ if (obj == this) return true;
- if (obj.getClass() != getClass())
- return false;
+ if (obj.getClass() != getClass()) return false;
ClasspathResource other = (ClasspathResource) obj;
@@ -84,16 +80,27 @@
{
URL url = _classLoader.getResource(path);
- if (url == null)
- continue;
+ if (url == null) continue;
- if (_path.equals(path))
- return this;
+ if (_path.equals(path)) return this;
return new ClasspathResource(_classLoader, path, url);
}
return null;
+ }
+
+ public Resource withExtension(String extension)
+ {
+ int dotx = _path.lastIndexOf(".");
+
+ if (dotx < 0) return new ClasspathResource(_classLoader, _path + "." +
extension);
+
+ String existing = _path.substring(dotx + 1);
+
+ if (existing.equals(extension)) return this;
+
+ return new ClasspathResource(_classLoader, _path.substring(0, dotx +
1) + extension);
}
@Override
Added:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java?view=auto&rev=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
(added)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/ClasspathResourceTest.java
Tue Nov 28 17:05:13 2006
@@ -0,0 +1,155 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// 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.apache.tapestry.ioc.internal.util;
+
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import java.util.Locale;
+
+import org.apache.tapestry.ioc.Resource;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class ClasspathResourceTest extends Assert
+{
+ private static final String PATH =
"org/apache/tapestry/ioc/internal/util/";
+
+ @Test
+ public void get_resource_URL() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ assertEquals(content(r), "content from resource.txt");
+ }
+
+ @Test
+ public void to_string() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ assertEquals(r.toString(), "classpath:" + PATH + "resource.txt");
+ }
+
+ @Test
+ public void get_URL_for_missing_resource() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "missing-resource.txt");
+
+ assertNull(r.getResourceURL());
+ }
+
+ @Test
+ public void localization_of_resource() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ Resource l = r.getLocalization(Locale.FRENCH);
+
+ assertEquals(content(l), "french content");
+ }
+
+ @Test
+ public void localization_to_closest_match() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ Resource l = r.getLocalization(Locale.CANADA_FRENCH);
+
+ assertEquals(content(l), "french content");
+ }
+
+ @Test
+ public void localization_to_base_resource() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ Resource l = r.getLocalization(Locale.JAPANESE);
+
+ assertSame(l, r);
+ }
+
+ @Test
+ public void with_extension_same_extension()
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+
+ assertSame(r.withExtension("txt"), r);
+ }
+
+ @Test
+ public void with_extension() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+ Resource e = r.withExtension("ext");
+
+ assertEquals(content(e), "ext content");
+ }
+
+ @Test
+ public void with_extension_adds_extension() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "resource");
+ Resource e = r.withExtension("ext");
+
+ assertEquals(content(e), "ext content");
+ }
+
+ @Test
+ public void with_extension_missing_resource_is_null()
+ {
+ Resource r = new ClasspathResource(PATH + "resource.txt");
+ Resource e = r.withExtension("does-not-exist");
+
+ assertNull(e.getResourceURL());
+ }
+
+ @Test
+ public void localization_of_missing_resource() throws Exception
+ {
+ Resource r = new ClasspathResource(PATH + "missing-resource.txt");
+
+ assertNull(r.getLocalization(Locale.FRENCH));
+ }
+
+ private String content(Resource resource) throws Exception
+ {
+ return content(resource.getResourceURL());
+ }
+
+ private String content(URL url) throws Exception
+ {
+ InputStream is = new BufferedInputStream(url.openStream());
+ Reader r = new InputStreamReader(is);
+
+ StringBuilder builder = new StringBuilder();
+ char[] buffer = new char[2000];
+
+ while (true)
+ {
+ int length = r.read(buffer, 0, buffer.length);
+
+ if (length < 0) break;
+
+ builder.append(buffer, 0, length);
+ }
+
+ r.close();
+
+ return builder.toString().trim();
+ }
+}
Modified:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java?view=diff&rev=480310&r1=480309&r2=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java
(original)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/LocationImplTest.java
Tue Nov 28 17:05:13 2006
@@ -44,6 +44,12 @@
{
return "<Resource>";
}
+
+ public Resource withExtension(String extension)
+ {
+ throw new UnsupportedOperationException("withExtension");
+ }
+
}
@Test
Added:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.ext
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.ext?view=auto&rev=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.ext
(added)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.ext
Tue Nov 28 17:05:13 2006
@@ -0,0 +1 @@
+ext content
Added:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.txt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.txt?view=auto&rev=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.txt
(added)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource.txt
Tue Nov 28 17:05:13 2006
@@ -0,0 +1 @@
+content from resource.txt
Added:
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource_fr.txt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource_fr.txt?view=auto&rev=480310
==============================================================================
---
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource_fr.txt
(added)
+++
tapestry/tapestry5/tapestry-ioc/trunk/src/test/resources/org/apache/tapestry/ioc/internal/util/resource_fr.txt
Tue Nov 28 17:05:13 2006
@@ -0,0 +1 @@
+french content