Author: hlship
Date: Tue Apr 28 21:39:40 2009
New Revision: 769554

URL: http://svn.apache.org/viewvc?rev=769554&view=rev
Log:
TAP5-223: Allow properties files (on classpath or in the context) to be used as 
SymbolProviders

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties
   (with props)

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,33 @@
+// Copyright 2009 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.tapestry5.internal;
+
+import org.apache.tapestry5.internal.services.ContextResource;
+import org.apache.tapestry5.ioc.internal.services.ResourceSymbolProvider;
+import org.apache.tapestry5.services.Context;
+
+/**
+ * Makes a {...@link org.apache.tapestry5.ioc.Resource} in the {...@link 
org.apache.tapestry5.services.Context} available as a
+ * {...@link org.apache.tapestry5.ioc.services.SymbolProvider}
+ *
+ * @since 5.1.0.5
+ */
+public class ContextResourceSymbolProvider extends ResourceSymbolProvider
+{
+    public ContextResourceSymbolProvider(Context context, String path)
+    {
+        super(new ContextResource(context, path));
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/ContextResourceSymbolProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,70 @@
+// Copyright 2009 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.tapestry5.internal;
+
+import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.services.Context;
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+public class ContextResourceSymbolProviderTest extends InternalBaseTestCase
+{
+    private static final String CONTENT = "homer=simpson\r\nmonty=burns";
+
+    private static final String PATH = "bar/foo.properties";
+
+    @Test
+    public void access() throws Exception
+    {
+        File f = File.createTempFile("foo", ".properties");
+
+        setupFile(f);
+
+        Context context = mockContext();
+
+        expect(context.getRealFile("/" + PATH)).andReturn(f);
+
+        replay();
+
+        ContextResourceSymbolProvider provider = new 
ContextResourceSymbolProvider(context, PATH);
+
+        /* test general access */
+        assertEquals(provider.valueForSymbol("homer"), "simpson");
+        assertEquals(provider.valueForSymbol("monty"), "burns");
+
+        /* check for case-insensitivity */
+        assertEquals(provider.valueForSymbol("HOMER"), "simpson");
+
+        /* non-existent keys should return null */
+        assertNull(provider.valueForSymbol("marge"));
+
+        verify();
+
+        f.delete();
+    }
+
+    private void setupFile(File f) throws Exception
+    {
+        FileOutputStream fos = new FileOutputStream(f);
+
+        fos.write(CONTENT.getBytes());
+
+        fos.close();
+
+        fos = null;
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/ContextResourceSymbolProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,31 @@
+// Copyright 2009 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.tapestry5.ioc.internal.services;
+
+import org.apache.tapestry5.ioc.internal.util.ClasspathResource;
+
+/**
+ * Makes a {...@link org.apache.tapestry5.ioc.Resource} on the classpath 
available as a {...@link
+ * org.apache.tapestry5.ioc.services.SymbolProvider}
+ *
+ * @since 5.1.0.5
+ */
+public class ClasspathResourceSymbolProvider extends ResourceSymbolProvider
+{
+    public ClasspathResourceSymbolProvider(String path)
+    {
+        super(new ClasspathResource(path));
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,82 @@
+// Copyright 2009 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.tapestry5.ioc.internal.services;
+
+import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
+import org.apache.tapestry5.ioc.services.SymbolProvider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Makes a {...@link org.apache.tapestry5.ioc.Resource} available as a 
{...@link org.apache.tapestry5.ioc.services.SymbolProvider}
+ *
+ * @since 5.1.0.5
+ */
+public class ResourceSymbolProvider implements SymbolProvider
+{
+    private final Resource resource;
+
+    private final Map<String, String> properties = 
CollectionFactory.newCaseInsensitiveMap();
+
+    public ResourceSymbolProvider(final Resource resource)
+    {
+        this.resource = resource;
+
+        readProperties();
+    }
+
+    private void readProperties()
+    {
+        Properties p = new Properties();
+
+        InputStream is = null;
+
+        try
+        {
+            is = resource.openStream();
+
+            p.load(is);
+
+            is.close();
+
+            is = null;
+
+            for (Map.Entry<Object, Object> entry : p.entrySet())
+            {
+                String key = entry.getKey().toString();
+
+                properties.put(key, p.getProperty(key));
+            }
+        }
+        catch (IOException ex)
+        {
+            throw new RuntimeException(ex);
+        }
+        finally
+        {
+            InternalUtils.close(is);
+        }
+    }
+
+    public String valueForSymbol(String symbolName)
+    {
+        return properties.get(symbolName);
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,39 @@
+// Copyright 2009 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.tapestry5.ioc.internal.services;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class ClasspathResourceSymbolProviderTest extends Assert
+{
+    private static final String PATH = 
"org/apache/tapestry5/ioc/internal/services/foo.properties";
+
+    @Test
+    public void access()
+    {
+        ClasspathResourceSymbolProvider provider = new 
ClasspathResourceSymbolProvider(PATH);
+
+        /* test general access */
+        assertEquals(provider.valueForSymbol("homer"), "simpson");
+        assertEquals(provider.valueForSymbol("monty"), "burns");
+
+        /* check for case-insensitivity */
+        assertEquals(provider.valueForSymbol("HOMER"), "simpson");
+
+        /* non-existent keys should return null */
+        assertNull(provider.valueForSymbol("marge"));
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ClasspathResourceSymbolProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,53 @@
+// Copyright 2009 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.tapestry5.ioc.internal.services;
+
+import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.ioc.test.IOCTestCase;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+public class ResourceSymbolProviderTest extends IOCTestCase
+{
+    private static final String CONTENT = "homer=simpson\r\nmonty=burns";
+
+    @Test
+    public void access() throws Exception
+    {
+        Resource resource = mockResource();
+
+        InputStream is = new ByteArrayInputStream(CONTENT.getBytes());
+
+        expect(resource.openStream()).andReturn(is);
+
+        replay();
+
+        ResourceSymbolProvider provider = new ResourceSymbolProvider(resource);
+
+        /* test general access */
+        assertEquals(provider.valueForSymbol("homer"), "simpson");
+        assertEquals(provider.valueForSymbol("monty"), "burns");
+
+        /* check for case-insensitivity */
+        assertEquals(provider.valueForSymbol("HOMER"), "simpson");
+
+        /* non-existent keys should return null */
+        assertNull(provider.valueForSymbol("marge"));
+
+        verify();
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ResourceSymbolProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties?rev=769554&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties
 Tue Apr 28 21:39:40 2009
@@ -0,0 +1,2 @@
+homer=simpson
+monty=burns

Propchange: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/resources/org/apache/tapestry5/ioc/internal/services/foo.properties
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to