2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.faces.application.NavigationHandler;
24  import javax.faces.application.ViewHandler;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  
28  /***
29   * <p>Mock implementation of <code>NavigationHandler</code>.</p>
30   *
31   * $Id$
32   */
33  
34  public class MockNavigationHandler extends NavigationHandler {
35  
36  
37      // ------------------------------------------------------------ Constructors
38  
39      /***
40       * <p>Construct a default instance.</p>
41       */
42      public MockNavigationHandler() {
43      }
44  
45  
46      // ----------------------------------------------------- Mock Object Methods
47  
48  
49      /***
50       * <p>Add a outcome-viewId pair to the destinations map.</p>
51       *
52       * @param outcome Logical outcome string
53       * @param viewId Destination view identifier
54       */
55      public void addDestination(String outcome, String viewId) {
56  
57          destinations.put(outcome, viewId);
58  
59      }
60  
61  
62      // ------------------------------------------------------ Instance Variables
63  
64  
65      /***
66       * <p>Set of destination view ids, keyed by logical outcome String
67       * that will cause navigation to that view id.</p>
68       */
69      private Map destinations = new HashMap();
70  
71  
72      // ----------------------------------------------- NavigationHandler Methods
73  
74  
75      /***
76       * <p>Process the specified navigation request.</p>
77       *
78       * @param context <code>FacesContext</code> for the current request
79       * @param action Action method being executed
80       * @param outcome Logical outcome from this action method
81       */
82      public void handleNavigation(FacesContext context,
83                                   String action, String outcome) {
84  
85          // Navigate solely based on outcome, if we get a match
86          String viewId = (String) destinations.get(outcome);
87          if (viewId != null) {
88              UIViewRoot view = getViewHandler(context).createView(context, viewId);
89              context.setViewRoot(view);
90          }
91  
92      }
93  
94  
95      // --------------------------------------------------------- Private Methods
96  
97  
98      /***
99       * <p>Return the <code>ViewHandler</code> instance for this application.</p>
100      *
101      * @param context <code>FacesContext</code> for the current request
102      */
103     private ViewHandler getViewHandler(FacesContext context) {
104 
105         return context.getApplication().getViewHandler();
106 
107     }
108 
109 
110 }