View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.orchestra.lib.jsf;
    20
    21  import javax.faces.application.Application;
    22  import javax.faces.component.UIComponent;
    23  import javax.faces.context.FacesContext;
    24  import javax.faces.el.VariableResolver;
    25
    26  import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
    27  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
    28  import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
    29  import org.apache.shale.test.base.AbstractJsfTestCase;
    30  import org.springframework.beans.factory.BeanFactory;
    31  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    32  import org.springframework.web.jsf.DelegatingVariableResolver;
    33
    34  public class TestSerializableConverter extends AbstractDependencyInjectionSpringContextTests
    35  {
    36      static class ShaleHandler extends AbstractJsfTestCase
    37      {
    38          public ShaleHandler()
    39          {
    40              super("none");
    41          }
    42
    43          public void doSetUp() throws Exception
    44          {
    45              this.setUp();
    46          }
    47
    48          public void doTearDown() throws Exception
    49          {
    50              this.tearDown();
    51          }
    52      }
    53
    54      ShaleHandler shale = new ShaleHandler();
    55
    56      protected String[] getConfigLocations()
    57      {
    58          return new String[]
    59              {
    60                  "classpath:org/apache/myfaces/orchestra/lib/jsf/TestSerializableConverter.xml"
    61              };
    62      }
    63
    64      protected void onSetUp() throws Exception
    65      {
    66          super.onSetUp();
    67          shale.doSetUp();
    68
    69          LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
    70          frameworkAdapter.setApplicationContext(applicationContext);
    71          frameworkAdapter.setConversationMessager(new LogConversationMessager());
    72          FrameworkAdapter.setCurrentInstance(frameworkAdapter);
    73
    74      }
    75
    76      protected void onTearDown() throws Exception
    77      {
    78          shale.doTearDown();
    79          super.onTearDown();
    80      }
    81
    82      static class SpringVariableResolver extends DelegatingVariableResolver
    83      {
    84          BeanFactory factory;
    85          SpringVariableResolver(BeanFactory factory, VariableResolver orig)
    86          {
    87              super(orig);
    88              this.factory = factory;
    89          }
    90
    91          protected BeanFactory getBeanFactory(FacesContext facesContext)
    92          {
    93              return factory;
    94          }
    95      }
    96
    97      /**
    98       * Test saving and restoring of a SerializableConverter that references
    99       * a Converter object that does not implement StateHolder.
    100      */
    101     public void testStateless() throws Exception
    102     {
    103         Object result;
    104
    105         // as for testStateful, but converter does not implement StateHolder
    106         // define a stateful converter
    107         FacesContext facesContext = FacesContext.getCurrentInstance();
    108         Application app = facesContext.getApplication();
    109
    110         // DelegatingResolver requires a WebApplicationContext object...
    111         SpringVariableResolver springResolver = new SpringVariableResolver(applicationContext, app.getVariableResolver());
    112         app.setVariableResolver(springResolver);
    113
    114         SerializableConverter conv = new SerializableConverter("statelessConverter");
    115         UIComponent component = null;
    116         result = conv.getAsObject(facesContext, component, "aabb");
    117         assertEquals("AABB", result);
    118
    119         Object state = conv.saveState(facesContext);
    120
    121         SerializableConverter conv2 = new SerializableConverter();
    122         conv2.restoreState(facesContext, state);
    123         result = conv.getAsObject(facesContext, component, "ccdd");
    124         assertEquals("CCDD", result);
    125     }
    126
    127     public void testStateful() throws Exception
    128     {
    129         Object result;
    130
    131         // as for testStateful, but converter does not implement StateHolder
    132         // define a stateful converter
    133         FacesContext facesContext = FacesContext.getCurrentInstance();
    134         Application app = facesContext.getApplication();
    135
    136         // DelegatingResolver requires a WebApplicationContext object...
    137         SpringVariableResolver springResolver = new SpringVariableResolver(applicationContext, app.getVariableResolver());
    138         app.setVariableResolver(springResolver);
    139
    140         SerializableConverter conv = new SerializableConverter("statefulConverter");
    141         UIComponent component = null;
    142         result = conv.getAsObject(facesContext, component, "aabb");
    143         assertEquals("AABB:18", result);
    144
    145         Object state = conv.saveState(facesContext);
    146
    147         SerializableConverter conv2 = new SerializableConverter();
    148         conv2.restoreState(facesContext, state);
    149         result = conv.getAsObject(facesContext, component, "ccdd");
    150         assertEquals("CCDD:19", result);
    151     }
    152 }