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 javax.faces.component.UIComponent;
22  import javax.faces.validator.ValidatorException;
23  
24  import org.apache.myfaces.trinidad.validator.ByteLengthValidator;
25  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
26  import org.jmock.Mock;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  /**
32   * Unit tests for ByteLengthValidator
33   *
34   */
35  public class ByteLengthValidatorTest extends ValidatorTestCase
36  {
37    public ByteLengthValidatorTest(String testName)
38    {
39      super(testName);
40    }
41  
42    @Override
43    protected void setUp() throws Exception
44    {
45      super.setUp();
46    }
47    
48    @Override
49    protected void tearDown() throws Exception
50    {
51      super.tearDown();
52    }
53    
54    public static Test suite()
55    {
56      return new TestSuite(ByteLengthValidatorTest.class);
57    }
58    
59    /**
60     * Tests that null returns immediately.
61     *
62     * @throws ValidatorException  when test fails
63     */
64    public void testNull() throws ValidatorException
65    {
66      Mock mock = buildMockUIComponent();
67      UIComponent component = (UIComponent) mock.proxy();
68      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
69      ByteLengthValidator validator = new ByteLengthValidator();
70      doTestNull(facesContext, wrapper, validator);
71    }
72  
73    /**
74     * Test when context is set to null
75     */
76    public void testNullContext()
77    {
78  
79      Mock mock = buildMockUIComponent();
80      UIComponent component = (UIComponent) mock.proxy();
81      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
82      ByteLengthValidator validator = new ByteLengthValidator();
83      doTestNullContext(wrapper, validator);
84    }
85   /**
86    * Check when component whose value is null is passed to the validator.
87    * Should result in exception.
88    */
89    public void testNullComponent()
90    {
91      ByteLengthValidator validator = new ByteLengthValidator();
92      doTestNullComponent(facesContext, validator);
93    }
94  
95    /**
96     * Tests that non String objects throw a ValidationException.
97     */
98    public void testNotString()
99    {
100     doTestIsNotString(new ByteLengthValidator());
101   }
102 
103   /**
104    * Test that basic test passes
105    */
106   public void testSanitySuccess()
107   {
108 
109     ByteLengthValidator validator = new ByteLengthValidator();
110     //some very basic sanity test
111     String values[]    = {"four"};
112     String encodings[] = {"ISO-8859-1"};
113     int maxBytes[]     = {4};
114 
115     Mock mock = buildMockUIComponent();
116     UIComponent component = (UIComponent) mock.proxy();
117     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
118 
119     for (int i = 0; i < values.length ; i++)
120     {
121       validator.setEncoding(encodings[i]);
122       validator.setMaximum(maxBytes[i]);
123       doTestValidate(validator, facesContext, wrapper, values[i]);
124     }
125   }
126 
127   public void testDefaultEncodingWorks()
128   {
129     
130     Mock mock = buildMockUIComponent();
131     UIComponent component = (UIComponent) mock.proxy();
132     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
133     ByteLengthValidator validator = new ByteLengthValidator();
134     String value = "four";
135     validator.setMaximum(4);
136     doTestValidate(validator, facesContext, wrapper, value);
137   }
138 
139   /**
140    * Test that StateHolder implementation for the validator works fine.
141    */
142   public void testStateHolderSaveRestore()
143   {
144     Mock mock = buildMockUIComponent();
145     UIComponent component = (UIComponent) mock.proxy();
146     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
147     ByteLengthValidator validator = new ByteLengthValidator();
148     validator.setEncoding("ISO-8859-1");
149     validator.setMaximum(4);
150     validator.setMessageDetailMaximum("Testing state holder?");
151     ByteLengthValidator restoreValidator = new  ByteLengthValidator();
152 
153     doTestStateHolderSaveRestore(validator, restoreValidator,
154                                  facesContext, wrapper);
155   }
156 
157   /**
158    * Test for equals function of Validator
159    */
160   public void testEquals()
161   {
162     ByteLengthValidator validator = new ByteLengthValidator();
163     //1
164     validator.setEncoding("ISO-8859-1");
165     validator.setMaximum(100);
166     validator.setMessageDetailMaximum("MaxMessage");
167 
168     ByteLengthValidator otherValidator = new ByteLengthValidator();
169     otherValidator.setEncoding("ISO-8859-1");
170     otherValidator.setMaximum(100);
171     otherValidator.setMessageDetailMaximum("MaxMessage");
172     doTestEquals(validator, otherValidator, true);
173     assertEquals(validator.hashCode(), otherValidator.hashCode());
174 
175     //2
176     otherValidator.setMaximum(150);
177     validator.setMaximum(150);
178     validator.setMessageDetailMaximum("MaxMessage1");
179     otherValidator.setMessageDetailMaximum("MaxMessage1");
180     doTestEquals(validator, otherValidator, true);
181     assertEquals(validator.hashCode(), otherValidator.hashCode());
182 
183     //3
184     otherValidator.setMaximum(200);
185     otherValidator.setMessageDetailMaximum("MaxMessage");
186     doTestEquals(validator, otherValidator, false);
187     assertEquals(false, (validator.hashCode() == otherValidator.hashCode()));
188   }
189 
190   public void testCustomMessageIsSet()
191   {
192     Mock mock = buildMockUIComponent();
193     UIComponent component = (UIComponent) mock.proxy();
194     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
195     setMockLabelForComponent(wrapper);
196     ByteLengthValidator validator = new ByteLengthValidator();
197     int maxBytes[]     = {3};
198     validator.setMessageDetailMaximum("\"{1}\"" + _IS_GREATER
199                                       + maxBytes[0]);
200 
201     //some very basic sanity test
202     String values[]    = {"four"};
203     String encodings[] = {"ISO-8859-1"};
204     String expected = "\"" + values[0] + "\"" + _IS_GREATER + maxBytes[0];
205     try
206     {
207       validator.setMaximum(maxBytes[0]);
208       validator.setEncoding(encodings[0]);
209       validator.validate(facesContext, component, values[0]);
210     }
211     catch (ValidatorException ve)
212     {
213       String msg = ve.getFacesMessage().getDetail();
214       assertEquals(msg, expected);
215     }
216   }
217   private static final String _IS_GREATER
218     = " exceeds maximum allowed length of ";
219 }