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 2005/05/01 06:26:39 UTC

svn commit: r165454 - in /struts/shale/trunk/test-framework/src/java/org/apache/shale/test: base/AbstractJsfTestCase.java mock/MockApplication.java mock/MockApplicationFactory.java mock/MockNavigationHandler.java mock/MockServletContext.java mock/MockViewHandler.java

Author: craigmcc
Date: Sat Apr 30 21:26:36 2005
New Revision: 165454

URL: http://svn.apache.org/viewcvs?rev=165454&view=rev
Log:
Flesh out mock object behaviors needed to build unit tests for
DialogNavigationHandler in the core library.

Modified:
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/base/AbstractJsfTestCase.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplication.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplicationFactory.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockNavigationHandler.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockViewHandler.java

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/base/AbstractJsfTestCase.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/base/AbstractJsfTestCase.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/base/AbstractJsfTestCase.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/base/AbstractJsfTestCase.java Sat Apr 30 21:26:36 2005
@@ -114,11 +114,12 @@
         facesContext.setViewRoot(root);
         ApplicationFactory applicationFactory = (ApplicationFactory)
             FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
-        application = (MockApplication) applicationFactory.getApplication();
+        application = new MockApplication();
+        applicationFactory.setApplication(application);
         facesContext.setApplication(application);
         RenderKitFactory renderKitFactory = (RenderKitFactory)
             FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
-        RenderKit renderKit = new MockRenderKit();
+        renderKit = new MockRenderKit();
         try {
             renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT,
                                           renderKit);
@@ -145,6 +146,7 @@
         externalContext = null;
         facesContext = null;
         lifecycle = null;
+        renderKit = null;
         request = null;
         response = null;
         servletContext = null;
@@ -162,6 +164,7 @@
     protected MockExternalContext     externalContext = null;
     protected MockFacesContext        facesContext = null;
     protected MockLifecycle           lifecycle = null;
+    protected MockRenderKit           renderKit = null;
     protected MockHttpServletRequest  request = null;
     protected MockHttpServletResponse response = null;
     protected MockServletContext      servletContext = null;

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplication.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplication.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplication.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplication.java Sat Apr 30 21:26:36 2005
@@ -44,6 +44,7 @@
 import javax.faces.event.ActionListener;
 import javax.faces.event.ActionEvent;
 import javax.faces.event.PhaseId;
+import javax.faces.render.RenderKitFactory;
 import javax.faces.validator.Validator;
 
 /**
@@ -66,6 +67,7 @@
         components = new HashMap();
         converters = new HashMap();
         setDefaultLocale(Locale.getDefault());
+        setDefaultRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
         setNavigationHandler(new MockNavigationHandler());
         setPropertyResolver(new MockPropertyResolver());
         setStateManager(new MockStateManager());

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplicationFactory.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplicationFactory.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplicationFactory.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockApplicationFactory.java Sat Apr 30 21:26:36 2005
@@ -37,7 +37,7 @@
      */
     public MockApplicationFactory() {
 
-        this.application = new MockApplication();
+        ;
 
     }
 
@@ -56,6 +56,9 @@
 
     public Application getApplication() {
 
+        if (this.application == null) {
+            this.application = new MockApplication();
+        }
         return this.application;
 
     }

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockNavigationHandler.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockNavigationHandler.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockNavigationHandler.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockNavigationHandler.java Sat Apr 30 21:26:36 2005
@@ -16,7 +16,11 @@
 
 package org.apache.shale.test.mock;
 
+import java.util.HashMap;
+import java.util.Map;
 import javax.faces.application.NavigationHandler;
+import javax.faces.application.ViewHandler;
+import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
 
 /**
@@ -40,9 +44,26 @@
     // ----------------------------------------------------- Mock Object Methods
 
 
+    /**
+     * <p>Add a outcome-viewId pair to the destinations map.</p>
+     */
+    public void addDestination(String outcome, String viewId) {
+
+        destinations.put(outcome, viewId);
+
+    }
+
+
     // ------------------------------------------------------ Instance Variables
 
 
+    /**
+     * <p>Set of destination view ids, keyed by logical outcome String
+     * that will cause navigation to that view id.</p>
+     */
+    private Map destinations = new HashMap();
+
+
     // ----------------------------------------------- NavigationHandler Methods
     
 
@@ -56,7 +77,27 @@
     public void handleNavigation(FacesContext context,
                                  String action, String outcome) {
 
-        ; // FIXME - provide default implementation
+        // Navigate solely based on outcome, if we get a match
+        String viewId = (String) destinations.get(outcome);
+        if (viewId != null) {
+            UIViewRoot view = getViewHandler(context).createView(context, viewId);
+            context.setViewRoot(view);
+        }
+
+    }
+
+
+    // --------------------------------------------------------- Private Methods
+
+
+    /**
+     * <p>Return the <code>ViewHandler</code> instance for this application.</p>
+     *
+     * @param context <code>FacesContext</code> for the current request
+     */
+    private ViewHandler getViewHandler(FacesContext context) {
+
+        return context.getApplication().getViewHandler();
 
     }
 

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java Sat Apr 30 21:26:36 2005
@@ -136,14 +136,23 @@
 
     public URL getResource(String path) throws MalformedURLException {
 
-        throw new UnsupportedOperationException();
+        // Return a corresponding class loader resource
+        return this.getClass().getResource(path);
 
     }
 
 
     public InputStream getResourceAsStream(String path) {
 
-        throw new UnsupportedOperationException();
+        try {
+            URL url = getResource(path);
+            if (url != null) {
+                return url.openStream();
+            }
+        } catch (Exception e) {
+            ;
+        }
+        return null;
 
     }
 

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockViewHandler.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockViewHandler.java?rev=165454&r1=165453&r2=165454&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockViewHandler.java (original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockViewHandler.java Sat Apr 30 21:26:36 2005
@@ -20,6 +20,7 @@
 import javax.faces.application.ViewHandler;
 import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
+import javax.faces.render.RenderKitFactory;
 
 /**
  * <p>Mock implementation of <code>ViewHandler</code>.</p>
@@ -51,21 +52,54 @@
 
     public Locale calculateLocale(FacesContext context) {
 
-        return context.getViewRoot().getLocale();
+        Locale locale = context.getApplication().getDefaultLocale();
+        if (locale == null) {
+            locale = Locale.getDefault();
+        }
+        return locale;
 
     }
 
 
     public String calculateRenderKitId(FacesContext context) {
 
-        return context.getViewRoot().getRenderKitId();
+        String renderKitId = context.getApplication().getDefaultRenderKitId();
+        if (renderKitId == null) {
+            renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
+        }
+        return renderKitId;
 
     }
 
 
     public UIViewRoot createView(FacesContext context, String viewId) {
 
-        throw new UnsupportedOperationException();
+        // Save locale and renderKitId from previous view (if any), per spec
+        Locale locale = null;
+        String renderKitId = null;
+        if (context.getViewRoot() != null) {
+            locale = context.getViewRoot().getLocale();
+            renderKitId = context.getViewRoot().getRenderKitId();
+        }
+
+        // Configure a new UIViewRoot instance
+        UIViewRoot view = new UIViewRoot();
+        view.setViewId(viewId);
+        if (locale != null) {
+            view.setLocale(locale);
+        } else {
+            view.setLocale
+              (context.getApplication().getViewHandler().calculateLocale(context));
+        }
+        if (renderKitId != null) {
+            view.setRenderKitId(renderKitId);
+        } else {
+            view.setRenderKitId
+              (context.getApplication().getViewHandler().calculateRenderKitId(context));
+        }
+
+        // Return the configured instance
+        return view;
 
     }
 



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