You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2006/01/19 05:24:56 UTC

svn commit: r370367 - in /struts/shale/trunk/core-library/src: java/org/apache/shale/remoting/ java/org/apache/shale/remoting/impl/ test/org/apache/shale/remoting/ test/org/apache/shale/remoting/impl/

Author: craigmcc
Date: Wed Jan 18 20:24:50 2006
New Revision: 370367

URL: http://svn.apache.org/viewcvs?rev=370367&view=rev
Log:
Add a helper class (XhtmlHelper) for use by renderers that want to leverage
the Shale remoting facilities for accessing remote resources without having
to worry about what FacesServlet is mapped to, or what mappings were configured
for the remoting package.

To support this feature, the Mapping interface was expanded to support an
additional mapping (resourceId --> URL) so that this concept could be generalized
into a Mapping implementation.

Added:
    struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java   (with props)
    struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java   (with props)
Modified:
    struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Bundle.properties
    struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mapping.java
    struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mechanism.java
    struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/impl/MappingImpl.java
    struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/impl/MappingImplTestCase.java

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Bundle.properties
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Bundle.properties?rev=370367&r1=370366&r2=370367&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Bundle.properties (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Bundle.properties Wed Jan 18 20:24:50 2006
@@ -5,3 +5,7 @@
 mimeType.exception=Exception occurred checking MIME type mapping
 resource.exception=Exception occurred retrieving URL for a resource
 resource.refuse=Refusing access to restricted resource
+xhtml.noMappings=The "Mappings" instance for this application has not yet been configured -- check your server logs to ensure there were no startup exceptions
+xhtml.noMechanism=The resource mechanism you have specified is null
+xhtml.noResourceId=The resource identifier you have specified is null
+xhtml.noServletMapping=The "Mappings" instance is not configured with the servlet mapping(s) for javax.facesl.webapp.FacesServlet -- check your web.xml file to make sure this servlet is declared and mapped

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mapping.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mapping.java?rev=370367&r1=370366&r2=370367&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mapping.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mapping.java Wed Jan 18 20:24:50 2006
@@ -91,6 +91,16 @@
 
 
      /**
+      * <p>Map the specified resource identifier to a complete URL that may
+      * be used to request this resource from the server.</p>
+      *
+      * @param context <code>FacesContext</code> for the current request
+      * @param resourceId Resource identifier to be mapped
+      */
+     public String mapResourceId(FacesContext context, String resourceId);
+
+
+     /**
       * <p>If the specified view identifier matches the pattern specified by
       * this {@link Mapping}, return the corresponding resource identifier
       * that should be passed on to our {@link Processor}.  If the specified

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mechanism.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mechanism.java?rev=370367&r1=370366&r2=370367&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mechanism.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/Mechanism.java Wed Jan 18 20:24:50 2006
@@ -32,7 +32,35 @@
     /**
      * <p>Private constructor to disable creation of new instances.</p>
      */
-    private Mechanism() { }
+    private Mechanism(String description) {
+
+        this.description = description;
+
+    }
+
+
+    // ------------------------------------------------------ Instance Variables
+
+
+    /**
+     * <p>The description of this mechanism.</p>
+     */
+    private String description = null;
+
+
+    // ---------------------------------------------------------- Public Methods
+
+
+    /**
+     * <p>Return a string representation of this mechansim.</p>
+     */
+    public String toString() {
+
+        return this.description;
+
+    }
+
+
 
 
     // -------------------------------------------------------- Create Instances
@@ -42,14 +70,14 @@
      * <p>{@link Mechanism} indicating that the processor should serve a static
      * resource from the classpath of this web application.</p>
      */
-    public static final Mechanism CLASS_RESOURCE = new Mechanism();
+    public static final Mechanism CLASS_RESOURCE = new Mechanism("CLASS_RESOURCE");
 
 
     /**
      * <p>{@link Mechanism} indicating that the processor should serve a static
      * resource from the web application archive of this web applicationl</p>
      */
-    public static final Mechanism WEBAPP_RESOURCE = new Mechanism();
+    public static final Mechanism WEBAPP_RESOURCE = new Mechanism("WEBAPP_RESOURCE");
 
 
     /**
@@ -57,14 +85,14 @@
      * calculate the content type, and contents, of the response to be
      * created.</p>
      */
-    public static final Mechanism DYNAMIC_RESOURCE = new Mechanism();
+    public static final Mechanism DYNAMIC_RESOURCE = new Mechanism("DYNAMIC_RESOURCE");
 
 
     /**
      * <p>{@link Mechanism} indicating that an unspecified mechanism will be
      * used to provide the content for the response.</p>
      */
-    public static final Mechanism OTHER_RESOURCE = new Mechanism();
+    public static final Mechanism OTHER_RESOURCE = new Mechanism("OTHER_RESOURCE");
 
 
 }

Added: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java?rev=370367&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java (added)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java Wed Jan 18 20:24:50 2006
@@ -0,0 +1,277 @@
+/*
+ * Copyright 2005-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.shale.remoting;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ResourceBundle;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+
+/**
+ * <p>Helper bean for rendering links to download resources commonly used
+ * in HTML and XHTML pages.  The specified resource identifier is automatically
+ * mapped based upon the Shale Remoting configuration that this application
+ * is using, as well as adapting to the servlet mapping for the JavaServer
+ * Faces controller servlet.  A given resource identifier will only be linked
+ * once for a given request.</p>
+ *
+ * <p>Instances of this class are stateless and have no side effects.</p>
+ */
+public class XhtmlHelper {
+    
+
+    // -------------------------------------------------------------- Manifest Constants
+
+
+    /**
+     * <p>The prefix to the request attributes that we will use to keep track
+     * of whether a particular resource has been linked already.</p>
+     */
+    protected static final String PREFIX = "org.apache.shale.remoting.LINKED";
+
+
+    // ---------------------------------------------------------- Public Methods
+
+
+    /**
+     * <p>Render a link to a JavaScript resource at the specified resource
+     * identifier.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param component <code>UIComponent</code> being rendered
+     * @param writer <code>ResponseWriter</code> to render output to
+     * @param mechanism Mechanism used to retrieve the specified resource
+     *  (used to select the appropriate {@link Processor}
+     * @param resourceId Resource identifier used to retrieve the requested
+     *  JavaScript resource
+     *
+     * @exception IllegalArgumentException if <code>mechanism</code> or
+     *  <code>resourceId</code> is <code>null</code>
+     * @exception IllegalStateException if a configuration error prevents
+     *  the mapping of this resource identifier to a corresponding URI
+     * @exception IOException if an input/output error occurs
+     */
+    public void linkJavascript(FacesContext context, UIComponent component,
+                               ResponseWriter writer,
+                               Mechanism mechanism, String resourceId)
+        throws IOException {
+
+        linkJavascript(context, component, writer,
+                       mechanism, resourceId, "text/javascript");
+
+    }
+
+
+    /**
+     * <p>Render a link to a JavaScript resource at the specified resource
+     * identifier.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param component <code>UIComponent</code> being rendered
+     * @param writer <code>ResponseWriter</code> to render output to
+     * @param mechanism Mechanism used to retrieve the specified resource
+     *  (used to select the appropriate {@link Processor}
+     * @param resourceId Resource identifier used to retrieve the requested
+     *  JavaScript resource
+     * @param contentType Content type to specify (for pulling specific
+     *  versions of JavaScript resources)
+     *
+     * @exception IllegalArgumentException if <code>mechanism</code> or
+     *  <code>resourceId</code> is <code>null</code>
+     * @exception IllegalStateException if a configuration error prevents
+     *  the mapping of this resource identifier to a corresponding URI
+     * @exception IOException if an input/output error occurs
+     */
+    public void linkJavascript(FacesContext context, UIComponent component,
+                               ResponseWriter writer,
+                               Mechanism mechanism, String resourceId,
+                               String contentType) throws IOException {
+
+        if (linked(context, resourceId)) {
+            return;
+        }
+
+        writer.startElement("script", component);
+        writer.writeAttribute("type", contentType, null);
+        writer.writeURIAttribute("src", mapResourceId(context, mechanism, resourceId), null);
+        writer.endElement("script");
+        writer.write("\n");
+
+        link(context, resourceId);
+
+    }
+
+
+    /**
+     * <p>Render a link to a CSS stylesheet at the specified resource
+     * identifier.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param component <code>UIComponent</code> being rendered
+     * @param writer <code>ResponseWriter</code> to render output to
+     * @param mechanism Mechanism used to retrieve the specified resource
+     *  (used to select the appropriate {@link Processor}
+     * @param resourceId Resource identifier used to retrieve the requested
+     *  stylesheet resource
+     *
+     * @exception IllegalArgumentException if <code>mechanism</code> or
+     *  <code>resourceId</code> is <code>null</code>
+     * @exception IllegalStateException if a configuration error prevents
+     *  the mapping of this resource identifier to a corresponding URI
+     * @exception IOException if an input/output error occurs
+     */
+    public void linkStylesheet(FacesContext context, UIComponent component,
+                               ResponseWriter writer,
+                               Mechanism mechanism, String resourceId)
+        throws IOException {
+
+        if (linked(context, resourceId)) {
+            return;
+        }
+
+        writer.startElement("link", component);
+        writer.writeAttribute("type", "text/css", null);
+        writer.writeAttribute("rel", "stylesheet", null);
+        writer.writeURIAttribute("href", mapResourceId(context, mechanism, resourceId), null);
+        writer.endElement("link");
+        writer.write("\n");
+
+        link(context, resourceId);
+
+    }
+    // ------------------------------------------------------- Protected Methods
+
+
+    /**
+     * <p>Mark the specified resource identifier as having already been
+     * linked in the current request.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param resourceId Resource identifier to mark as having been linked
+     */
+    protected void link(FacesContext context, String resourceId) {
+
+        context.getExternalContext().getRequestMap().
+                put(PREFIX + resourceId, Boolean.TRUE);
+
+    }
+
+
+    /**
+     * <p>Return <code>true</code> if the specified resource identifier has
+     * already been linked in the current request, and should therefore not
+     * be linked again.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param resourceId Resource identifier to check for prior linking
+     */
+    protected boolean linked(FacesContext context, String resourceId) {
+
+        return context.getExternalContext().getRequestMap().
+                containsKey(PREFIX + resourceId);
+
+    }
+
+
+    /**
+     * <p>Map the specified resource identifier to a request URL, taking into
+     * account the mappings for the specified mechanism and the servlet mapping
+     * for the JavaServer Faces controller servlet.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     * @param mechanism Requested mechanism
+     * @param resourceId Resource identifier to be mapped
+     *
+     * @exception IllegalArgumentException if <code>mechanism</code> or
+     *  <code>resourceId</code> is <code>null</code>
+     * @exception IllegalStateException if a configuration error prevents
+     *  the mapping of this resource identifier to a corresponding URI
+     */
+    protected String mapResourceId(FacesContext context, Mechanism mechanism,
+                                   String resourceId) {
+
+        // Validate our incoming parameters
+        if (resourceId == null) {
+            throw new IllegalArgumentException
+                    (resourceBundle(context).getString("xhtml.noResourceId"));
+        }
+        if (mechanism == null) {
+            throw new IllegalArgumentException
+                    (resourceBundle(context).getString("xhtml.noMechanism"));
+        }
+
+        // Acquire a reference to the Mappings instance for this application
+        Mappings mappings = (Mappings)
+          context.getExternalContext().getApplicationMap().
+          get(Constants.MAPPINGS_ATTR);
+        if (mappings == null) {
+            throw new IllegalStateException
+                    (resourceBundle(context).getString("xhtml.noMappings"));
+        }
+
+        // Acquire the first mapping to be used for FacesServlet
+        String patterns[] = mappings.getPatterns();
+        if ((patterns == null) || (patterns.length < 1)) {
+            throw new IllegalArgumentException
+                    (resourceBundle(context).getString("xhtml.noServletMapping"));
+        }
+        String pattern = patterns[0]; // Arbitrarily choose the first one
+
+        // Acquire the Mapping instance to be used for the requesed mechanism
+        List list = mappings.getMappings();
+        if (list == null) {
+            list = new ArrayList();
+        }
+        Iterator instances = list.iterator();
+        Mapping mapping = null;
+        while (instances.hasNext()) {
+            Mapping instance = (Mapping) instances.next();
+            if (mechanism == instance.getMechanism()) {
+                mapping = instance;
+                break;
+            }
+        }
+        if (mapping == null) {
+            throw new IllegalArgumentException(mechanism.toString());
+        }
+
+        // Ask this Mapping to map the resource identifier appropriately
+        return mapping.mapResourceId(context, resourceId);
+
+    }
+
+
+    /**
+     * <p>Return the localized resource bundle we should use to generate
+     * exception or log messages for this request.</p>
+     *
+     * @param context <code>FacesContext</code> for this request
+     */
+    protected ResourceBundle resourceBundle(FacesContext context) {
+
+        return ResourceBundle.getBundle("org.apache.shale.remoting.Bundle",
+                                        context.getViewRoot().getLocale(),
+                                        Thread.currentThread().getContextClassLoader());
+
+    }
+
+
+}

Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/XhtmlHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/impl/MappingImpl.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/impl/MappingImpl.java?rev=370367&r1=370366&r2=370367&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/impl/MappingImpl.java (original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/remoting/impl/MappingImpl.java Wed Jan 18 20:24:50 2006
@@ -149,6 +149,35 @@
 
 
     /** {@inheritDoc} */
+    public String mapResourceId(FacesContext context, String resourceId) {
+
+        // Acquire the servlet mapping to be used for FacesServlet
+        String patterns[] = getMappings().getPatterns();
+        String pattern = patterns[0]; // Arbitrarily choose the first one
+
+        // Configure the entire URL we will return
+        StringBuffer sb = new StringBuffer(context.getExternalContext().getRequestContextPath());
+        if (pattern.endsWith("/*")) { // FacesServlet is prefix mapped
+            sb.append(pattern.substring(0, pattern.length() - 2));
+        }
+        if (getPattern().endsWith("*")) { // Processor is prefix mapped
+            sb.append(getPattern().substring(0, getPattern().length() - 2));
+        }
+        sb.append(resourceId);
+        if (getPattern().startsWith("*.")) { // Processor is extension mapped
+            sb.append(getPattern().substring(1));
+        }
+        if (pattern.startsWith("*.")) { // FacesServlet is extension mapped
+            sb.append(pattern.substring(1));
+        }
+
+        // Return the completed URL
+        return sb.toString();
+
+    }
+
+
+    /** {@inheritDoc} */
     public String mapViewId(FacesContext context) {
 
         // Extract the view identifier we will be using to match against

Added: struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java?rev=370367&view=auto
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java (added)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java Wed Jan 18 20:24:50 2006
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2005-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.shale.remoting;
+
+import java.util.Locale;
+import javax.faces.component.UIOutput;
+import javax.faces.component.UIViewRoot;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.shale.remoting.faces.ResponseFactory;
+import org.apache.shale.remoting.impl.MappingImpl;
+import org.apache.shale.remoting.impl.MappingsImpl;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+import org.apache.shale.test.mock.MockPrintWriter;
+
+/**
+ * <p>Unit tests for <code>org.apache.shale.remoting.XhtmlHelper</code>.</p>
+ */
+public class XhtmlHelperTestCase extends AbstractJsfTestCase {
+    
+    // ------------------------------------------------------------ Constructors
+
+
+    // Construct a new instance of this test case.
+    public XhtmlHelperTestCase(String name) {
+        super(name);
+    }
+
+
+    // ----------------------------------------------------------- Setup Methods
+
+
+    // Set up instance variables for this test case.
+    public void setUp() {
+
+        super.setUp();
+        helper = new XhtmlHelper();
+        facesContext.setViewRoot(new UIViewRoot());
+        mappings = new MappingsImpl();
+        mappings.setExtension(".jsp");
+        mappings.setPatterns(new String[] { "*.faces" });
+        mapping = new MappingImpl();
+        mapping.setMappings(mappings);
+        mapping.setMechanism(Mechanism.CLASS_RESOURCE);
+        mappings.addMapping(mapping);
+        facesContext.getExternalContext().getApplicationMap().
+                put(Constants.MAPPINGS_ATTR, mappings);
+        facesContext.getViewRoot().setLocale(Locale.getDefault());
+        request.setPathElements("/webapp", "/foo", null, null);
+
+    }
+
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(XhtmlHelperTestCase.class));
+
+    }
+
+
+    // Tear down instance variables for this test case.
+    public void tearDown() {
+
+        helper = null;
+        mapping = null;
+        mappings = null;
+        super.tearDown();
+
+    }
+
+
+    // ------------------------------------------------------ Instance Variables
+
+
+    // The helper instance to be tested
+    private XhtmlHelper helper = null;
+
+
+    // The mapping instance to be tested
+    private MappingImpl mapping = null;
+
+
+    // The mappings instance to be tested
+    private MappingsImpl mappings = null;
+
+
+    // -------------------------------------------------------- Static Variables
+
+
+    // Prefix to the rendered JavaScript test string
+    private static final String JAVASCRIPT_CONTENT =
+      "<script type=\"text/javascript\" src=\"/webapp/dynamic/foo/bar.js.faces\"/>";
+
+
+    // Prefix to the rendered Stylesheet test string
+    private static final String STYLESHEET_CONTENT =
+      "<link type=\"text/css\" rel=\"stylesheet\" href=\"/webapp/faces/foo/bar.css.dyn\"/>";
+
+
+    // ------------------------------------------------------------ Test Methods
+
+
+    // Test linking to a JavaScript resource
+    public void testLinkJavascript() throws Exception {
+
+        // Use extension mapping for FacesServlet and prefix mapping for the resource
+        mappings.setPatterns(new String[] { "*.faces" });
+        mapping.setPattern("/dynamic/*");
+
+        // Perform the link request and check the results
+        facesContext.setResponseWriter
+          ((new ResponseFactory()).getResponseWriter(facesContext, "text/javascript"));
+        helper.linkJavascript(facesContext, new UIOutput(),
+                              facesContext.getResponseWriter(),
+                              Mechanism.CLASS_RESOURCE, "/foo/bar.js");
+
+        // Evaluate the results
+        assertEquals("text/javascript", response.getContentType());
+        MockPrintWriter writer = (MockPrintWriter) response.getWriter();
+        char content[] = writer.content();
+        assertNotNull(content);
+        assertTrue(content.length > JAVASCRIPT_CONTENT.length());
+        for (int i = 0; i < JAVASCRIPT_CONTENT.length(); i++) {
+            assertEquals("Character at position " + i, JAVASCRIPT_CONTENT.charAt(i), content[i]);
+        }
+
+
+    }
+
+    // Test a pristine instance
+    public void testPristine() {
+
+        ;
+
+    }
+
+
+    // Test linking to a stylesheet resource
+    public void testLinkStylesheet() throws Exception {
+
+        // Use prefix mapping for FacesServlet and extension mapping for the resource
+        mappings.setPatterns(new String[] { "/faces/*" });
+        mapping.setPattern("*.dyn");
+
+        // Perform the link request and check the results
+        facesContext.setResponseWriter
+          ((new ResponseFactory()).getResponseWriter(facesContext, "text/css"));
+        helper.linkStylesheet(facesContext, new UIOutput(),
+                              facesContext.getResponseWriter(),
+                              Mechanism.CLASS_RESOURCE, "/foo/bar.css");
+
+        // Evaluate the results
+        assertEquals("text/css", response.getContentType());
+        MockPrintWriter writer = (MockPrintWriter) response.getWriter();
+        char content[] = writer.content();
+        assertNotNull(content);
+        assertTrue(content.length > STYLESHEET_CONTENT.length());
+        for (int i = 0; i < STYLESHEET_CONTENT.length(); i++) {
+            assertEquals("Character at position " + i, STYLESHEET_CONTENT.charAt(i), content[i]);
+        }
+
+    }
+
+
+}

Propchange: struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/XhtmlHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/impl/MappingImplTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/impl/MappingImplTestCase.java?rev=370367&r1=370366&r2=370367&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/impl/MappingImplTestCase.java (original)
+++ struts/shale/trunk/core-library/src/test/org/apache/shale/remoting/impl/MappingImplTestCase.java Wed Jan 18 20:24:50 2006
@@ -16,9 +16,11 @@
 
 package org.apache.shale.remoting.impl;
 
+import java.util.Locale;
 import javax.faces.component.UIViewRoot;
 import junit.framework.Test;
 import junit.framework.TestSuite;
+import org.apache.shale.remoting.Constants;
 import org.apache.shale.test.base.AbstractJsfTestCase;
 
 /**
@@ -49,6 +51,11 @@
         mappings.setPatterns(new String[] { "*.faces" });
         mapping = new MappingImpl();
         mapping.setMappings(mappings);
+        mappings.addMapping(mapping);
+        facesContext.getExternalContext().getApplicationMap().
+                put(Constants.MAPPINGS_ATTR, mappings);
+        facesContext.getViewRoot().setLocale(Locale.getDefault());
+        request.setPathElements("/webapp", "/foo", null, null);
 
     }
 
@@ -65,6 +72,7 @@
     public void tearDown() {
 
         mapping = null;
+        mappings = null;
         super.tearDown();
 
     }
@@ -202,6 +210,38 @@
         assertNull(mapping.getMechanism());
         assertNull(mapping.getPattern());
         assertNull(mapping.getProcessor());
+
+    }
+
+
+    // Test mapping resource identifiers to view identifiers
+    public void testResourceIdentifiers() throws Exception {
+
+        String resourceId = "/foo/bar.baz";
+
+        // FacesServlet=prefix Mapping=prefix
+        mappings.setPatterns(new String[] { "/faces/*" });
+        mapping.setPattern("/bop/*");
+        assertEquals("/webapp/faces/bop/foo/bar.baz",
+                     mapping.mapResourceId(facesContext, resourceId));
+
+        // FacesServlet=prefix Mapping=extension
+        mappings.setPatterns(new String[] { "/faces/*" });
+        mapping.setPattern("*.bop");
+        assertEquals("/webapp/faces/foo/bar.baz.bop",
+                     mapping.mapResourceId(facesContext, resourceId));
+
+        // FacesServlet=extension Mapping=prefix
+        mappings.setPatterns(new String[] { "*.faces" });
+        mapping.setPattern("/bop/*");
+        assertEquals("/webapp/bop/foo/bar.baz.faces",
+                     mapping.mapResourceId(facesContext, resourceId));
+
+        // FacesServlet=extension Mapping=prefix
+        mappings.setPatterns(new String[] { "*.faces" });
+        mapping.setPattern("*.bop");
+        assertEquals("/webapp/foo/bar.baz.bop.faces",
+                     mapping.mapResourceId(facesContext, resourceId));
 
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org