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.trinidad.convert;
20  
21  import javax.faces.component.StateHolder;
22  import javax.faces.component.UIComponent;
23  import javax.faces.convert.Converter;
24  import javax.faces.convert.ConverterException;
25  
26  import org.apache.myfaces.trinidadbuild.test.AbstractBaseTestCase;
27  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
28  import org.apache.shale.test.mock.MockFacesContext;
29  import org.jmock.Mock;
30  
31  /**
32   * Base class for unit tests of Converters
33   *
34   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/test/java/oracle/adf/view/faces/convert/ConverterTestCase.java#0 $) $Date: 17-oct-2005.16:28:54 $
35   *
36   */
37  public abstract class ConverterTestCase extends AbstractBaseTestCase
38  {
39    public ConverterTestCase(String testName)
40    {
41      super(testName);
42    }
43  
44    /**
45     * This test performs action on the method
46     * javax.faces.convert.Converter.getAsObject(FacesContext, UIComponent, String)
47     * and
48     * javax.faces.convert.Converter.getAsString(FacesContext, UIComponent, Object)
49     * for method getAsObject() should return a value of null while getAsString()
50     * should return a empty string.
51     * @throws ValidatorException  when test fails
52     */
53    protected void doTestNull(
54      MockFacesContext context,
55      MockUIComponentWrapper wrapper,
56      Converter converter
57      ) throws ConverterException
58    {
59      Object obj = converter.getAsObject(context, wrapper.getUIComponent(), null);
60      assertEquals(null, obj);
61      String str = converter.getAsString(context, wrapper.getUIComponent(), null);
62      assertEquals("",str);
63      wrapper.getMock().verify();
64    }
65  
66    /**
67     * If contex or component = null then should throw NullPointerException
68     */
69    protected void doTestNullContext(
70      MockUIComponentWrapper wrapper,
71      Converter converter) throws NullPointerException
72    {
73      try
74      {
75        converter.getAsObject(null, wrapper.getUIComponent(), "dummy");
76        fail("Expected NullpointerException - if context or component is null");
77      }
78      catch (NullPointerException npe)
79      {
80        // this is expected
81      }
82      try
83      {
84        converter.getAsString(null, wrapper.getUIComponent(), "dummy");
85        fail("Expected NullpointerException - if context or component is null");
86      }
87      catch (NullPointerException npe)
88      {
89        // this is expected
90      }
91    }
92  
93    /**
94     * If contex or component = null then should throw NullPointerException
95     */
96    protected void doTestNullComponent(MockFacesContext context,
97      Converter converter ) throws NullPointerException
98    {
99      try
100     {
101       converter.getAsObject(context, null, "dummy");
102       fail("Expected NullpointerException - if context or component is null");
103     }
104     catch (NullPointerException npe)
105     {
106       // this is expected
107     }
108     try
109     {
110       converter.getAsString(context, null, "dummy");
111       fail("Expected NullpointerException - if context or component is null");
112     }
113     catch (NullPointerException npe)
114     {
115       // this is expected
116     }
117   }
118 
119   protected void doTestBlankValue(Converter converter)
120   {
121     MockFacesContext context = new MockFacesContext();
122     Mock mock = mock(UIComponent.class);
123     UIComponent component = (UIComponent) mock.proxy();
124     Object value = converter.getAsObject(context, component,"");
125     assertEquals(null, value);
126   }
127 
128   /**
129    * Test the validity of method
130    * javax.faces.convert.Converter.getAsObject(FacesContext, UIComponent, String)
131    *
132    * @param converter converter which is to be tested
133    * @param context MockFaces context
134    * @param component MockFaces component
135    * @throws javax.faces.convert.ConvertException
136    */
137   protected void doTestGetAsObject(
138     Converter converter,
139     MockFacesContext context,
140     MockUIComponentWrapper wrapper,
141     String value,
142     Object expectedValue
143     )  throws ConverterException
144   {
145     Object conv = converter.getAsObject(context, wrapper.getUIComponent(), value);
146     assertEquals(expectedValue, conv);
147 
148     wrapper.getMock().verify();
149   }
150 
151 
152   /**
153    * Test the validity of call on the method
154    * javax.faces.convert.Converter.getAsString(FacesContext, UIComponent, Object)
155    * @param converter converter which is to be tested
156    * @param context MockFaces context
157    * @param component MockFaces component
158    * @throws javax.faces.convert.ConvertException
159    */
160   protected void doTestGetAsString(
161     Converter converter,
162     MockFacesContext context,
163     MockUIComponentWrapper wrapper,
164     Object value,
165     String expectedValue
166     )  throws ConverterException
167   {
168     Object conv = converter.getAsString(context, wrapper.getUIComponent(), value);
169     assertEquals(conv, expectedValue);
170     wrapper.getMock().verify();
171   }
172 
173 
174   /**
175    * Test for equality or mismatch of converters
176    * Converter's that do not  implement equals should override to check
177    * for it equality.
178    * @param thisConverter
179    * @param otherConverter
180    * @param isEqual - Identifies whether the comparison for equality of
181    *        converters or mismatch of converters
182    */
183   protected void doTestEquals(
184     Converter thisConverter,
185     Converter otherConverter,
186     boolean isEqual)
187   {
188     assertEquals(isEqual, thisConverter.equals(otherConverter));
189     assertEquals(isEqual, (thisConverter.hashCode() == otherConverter.hashCode()));
190   }
191 
192   /**
193    * Test to check for Validators which implements the StateHolder interface
194    * @param thisValidator  Source converter
195    * @param otherValidator The converter in which the state will be restored to
196    * @param context MockFaces context
197    * @param component MockFaces Component
198    */
199   protected void doTestStateHolderSaveRestore(
200     Converter thisConverter,
201     Converter otherConverter,
202     MockFacesContext context,
203     MockUIComponentWrapper wrapper
204     )
205   {
206     Object state = ((StateHolder)thisConverter).saveState(context);
207 
208     ((StateHolder)otherConverter).restoreState(context, state);
209     // do all actions of save and restore
210     doTestEquals(thisConverter, otherConverter, true);
211     wrapper.getMock().verify();
212   }
213 
214 
215   /**
216    * Compares two object relying on its equality.
217    * If two objects are null returns true
218    * @param o1
219    * @param o2
220    * @return
221    */
222   protected boolean equals(
223     Object o1,
224     Object o2
225     )
226   {
227     return ( o1 == o2 || (o1 != null && o1.equals(o2)));
228   }
229 }