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.validator;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.faces.component.StateHolder;
25  import javax.faces.component.UIComponent;
26  import javax.faces.validator.Validator;
27  import javax.faces.validator.ValidatorException;
28  
29  import org.apache.myfaces.trinidadbuild.test.AbstractBaseTestCase;
30  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
31  import org.apache.shale.test.mock.MockFacesContext;
32  import org.jmock.Mock;
33  
34  /**
35   * Base class for unit tests for Validators
36   *
37   */
38  public abstract class ValidatorTestCase extends AbstractBaseTestCase
39  {
40    public ValidatorTestCase(String testName)
41    {
42      super(testName);
43    }
44  
45    protected void setMockLabelForComponent(MockUIComponentWrapper wrapper)
46    {
47      Map<String, Object> attributes = new HashMap<String, Object>();
48      attributes.put("label", "label");
49      wrapper.getMock().stubs().method("getAttributes").will(returnValue(attributes));
50    }
51  
52    /**
53     * Tests that null returns immediately.
54     *
55     * @throws ValidatorException  when test fails
56     */
57    protected void doTestNull(
58      MockFacesContext context,
59      MockUIComponentWrapper wrapper,
60      Validator validator
61      ) throws ValidatorException
62    {
63      validator.validate(context, wrapper.getUIComponent(), null);
64  
65      wrapper.getMock().verify();
66    }
67  
68    /**
69     * if contex or component = null then should throw NullPointerException
70     */
71    protected void doTestNullContext(
72      MockUIComponentWrapper wrapper,
73      Validator validator) throws NullPointerException
74    {
75      try
76      {
77        validator.validate(null, wrapper.getUIComponent(), "dummy");
78        fail("Expected NullpointerException - if context or component is null");
79      }
80      catch (NullPointerException npe)
81      {
82        // this is expected
83      }
84    }
85  
86    /**
87     * if contex or component = null then should throw NullPointerException
88     */
89    protected void doTestNullComponent(MockFacesContext context,
90      Validator validator ) throws NullPointerException
91    {
92      try
93      {
94        validator.validate(context, null, "dummy");
95        fail("Expected NullpointerException - if context or component is null");
96      }
97      catch (NullPointerException npe)
98      {
99        // this is expected
100     }
101   }
102 
103   /**
104    * Test the validate action on validators.
105    * @param validator validator on which validate action is to be tested
106    * @param context MockFaces context
107    * @param component MockFaces component
108    * @throws javax.faces.validator.ValidatorException
109    */
110   protected void doTestValidate(
111     Validator validator,
112     MockFacesContext context,
113     MockUIComponentWrapper wrapper,
114     Object value
115     )  throws ValidatorException
116   {
117     validator.validate(context, wrapper.getUIComponent(), value );
118     wrapper.getMock().verify();
119   }
120 
121   /**
122    * Test for equality or mismatch of Validators
123    * @param thisValidator
124    * @param otherValidator
125    * @param isEqual - Identifies whether the comparison for equality of validators
126    *        or mismatch of valiadtors
127    */
128   protected void doTestEquals(
129     Validator thisValidator,
130     Validator otherValidator,
131     boolean isEqual)
132   {
133     assertEquals(isEqual, thisValidator.equals(otherValidator));
134   }
135 
136   /**
137    * Test to check for Validators which implements the StateHolder interface
138    * @param thisValidator  Source validator
139    * @param otherValidator The validator in which the state will be restored to
140    * @param context MockFaces context
141    * @param component MockFaces Component
142    */
143   protected void doTestStateHolderSaveRestore(
144     Validator thisValidator,
145     Validator otherValidator,
146     MockFacesContext context,
147     MockUIComponentWrapper wrapper)
148   {
149     Object state = ((StateHolder)thisValidator).saveState(context);
150 
151     ((StateHolder)otherValidator).restoreState(context, state);
152     // do all actions of save and restore
153     doTestEquals(thisValidator, otherValidator, true);
154     wrapper.getMock().verify();
155   }
156 
157   public void doTestIsNotString(Validator validator)
158   {
159     Mock mock = mock(UIComponent.class);
160     UIComponent component = (UIComponent) mock.proxy();
161     mock.stubs().method("getId").will(returnValue("test"));
162     try
163     {
164       validator.validate(facesContext, component, new Integer(1));
165       // if exception is not thrown - mark it as an failure
166       fail("Expected Validator Exception");
167     }
168     catch (IllegalArgumentException iae)
169     {
170       // if exception then fine.
171     }
172   }
173 }
174 
175  //////////////////////////////////////////////////////////////////////////////
176   //                             MOCK OBJECTS
177   // 1. Get a MockControl for the interface we would like to simulate
178   // 2. get the MockObject from MockControl
179   // 3. specify the behaviour of the Mock Object (record state)
180   // 4. activate the MockObject via the control  (replay state)
181   //
182   //////////////////////////////////////////////////////////////////////////////