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 java.awt.Color;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.convert.ConverterException;
27  
28  import org.apache.myfaces.trinidad.convert.ColorConverter;
29  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
30  import org.jmock.Mock;
31  
32  import junit.framework.Test;
33  import junit.framework.TestSuite;
34  
35  /**
36   * Unit tests for ColorConverter
37   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/test/java/oracle/adf/view/faces/convert/ColorConverterTest.java#1 $) $Date: 16-aug-2005.15:12:23 $
38   */
39  public class ColorConverterTest extends ConverterTestCase
40  {
41    public ColorConverterTest(String testName)
42    {
43      super(testName);
44    }
45  
46    @Override
47    protected void setUp() throws Exception
48    {
49      super.setUp();
50    }
51    
52    @Override
53    protected void tearDown() throws Exception
54    {
55      super.tearDown();
56    }
57    
58    public static Test suite()
59    {
60      return new TestSuite(ColorConverterTest.class);
61    }
62    
63    /**
64     * Tests that null returns immediately.
65     *
66     * @throws ConverterException  when test fails
67     */
68    public void testNull() throws ConverterException
69    {
70      Mock mock = mock(UIComponent.class);
71      UIComponent component = (UIComponent) mock.proxy();
72      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
73  
74      ColorConverter converter  = new ColorConverter();
75  
76      doTestNull(facesContext, wrapper, converter);
77    }
78  
79    /**
80     * Test when context is set to null
81     */
82    public void testNullContext()
83    {
84      Mock mock = mock(UIComponent.class);
85      UIComponent component = (UIComponent) mock.proxy();
86      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
87      ColorConverter converter  = new ColorConverter();
88  
89      doTestNullContext(wrapper, converter);
90    }
91  
92    public void testNullComponent()
93    {
94      ColorConverter converter  = new ColorConverter();
95  
96      doTestNullComponent(facesContext, converter);
97    }
98  
99    public void testEmptyValueConversion()
100   {
101     super.doTestBlankValue(new ColorConverter());
102   }
103 
104   /**
105    * Test for equality of converters
106    */
107   public void testEquals()
108   {
109     ColorConverter converter  = new ColorConverter();
110     ColorConverter otherConverter = new ColorConverter();
111     doTestEquals(converter, otherConverter, true);
112 
113     String[] patterns = {"rrr,ggg,bbb", "rrr-ggg-bbb"};
114     String[] otherPatterns = {"rrr,ggg,bbb", "rrr-ggg-bbb"};
115     converter.setPatterns(patterns);
116     converter.setMessageDetailConvert("Test message detail");
117     otherConverter.setMessageDetailConvert("Test message detail");
118     otherConverter.setPatterns(otherPatterns);
119     doTestEquals(converter, otherConverter, true);
120 
121     String[] newPattern = {"#RRGGBB", "RR.GG.BB"};
122     otherConverter.setPatterns(newPattern);
123     doTestEquals(converter, otherConverter, false);
124 
125     // check by modifiying the tranisent to be differnt
126     // patterns are same
127     otherConverter.setPatterns(otherPatterns);
128     otherConverter.setTransient(true);
129     doTestEquals(converter, otherConverter, false);
130 
131 
132     // transient same, patterns same, but allowsTransparent diff
133     otherConverter.setTransient(false);
134     otherConverter.setTransparentAllowed(true);
135     doTestEquals(converter, otherConverter, false);
136   }
137 
138   public void testDefaultColorPatternWorks()
139   {
140     ColorConverter converter  = new ColorConverter();
141     Mock mock = mock(UIComponent.class);
142     UIComponent component = (UIComponent) mock.proxy();
143     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
144     String value = "#FFFFFF";
145     Color expectedColor = new Color(255,255,255);
146     doTestGetAsObject(converter, facesContext, wrapper, value, expectedColor);
147   }
148 
149   /**
150    * Tests that dates after the date range cause a ValidationException.
151    */
152   public void testStateHolderSaveRestore()
153   {
154     ColorConverter converter = new ColorConverter();
155     Mock mock = mock(UIComponent.class);
156     UIComponent component = (UIComponent) mock.proxy();
157     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
158 
159     String[] patterns = {"#RRGGBB","RR.GG.BB"};
160     converter.setPatterns(patterns);
161     converter.setMessageDetailConvert("Works fine");
162     ColorConverter restoreConverter = new  ColorConverter();
163 
164     doTestStateHolderSaveRestore(converter, restoreConverter,
165         facesContext, wrapper);
166     mock.verify();
167   }
168   /**
169    * Test ColorConverte's getAsObject(FacesContext, UIComponent, String) method
170    * works fine.
171    */
172   public void testGetAsObjectConversion()
173   {
174     ColorConverter converter = new ColorConverter();
175     Mock mock = mock(UIComponent.class);
176     UIComponent component = (UIComponent) mock.proxy();
177     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
178 
179     String[] patterns = { "#RRGGBB",
180                           "RR.GG.BB",
181                           "RRGGBB",
182                           "RR GG BB",
183                           "rrr-ggg-bbb",
184                           "rrr ggg bbb",
185                           "r-g-b",
186                           };
187 
188     String values[]   = { "#FF0204",
189                           "FF0206",
190                           "FF FF FF",
191                           "0-0-0",
192                           "105 105 105",
193                         };
194 
195    Color[] matchColors = {  new Color(255,2,4),
196                             new Color(255,2,6),
197                             new Color(255,255,255),
198                             new Color(0,0,0),
199                             new Color(105,105,105)
200                          };
201 
202     converter.setPatterns(patterns);
203     for (int i = 0; i < values.length; i++)
204     {
205       doTestGetAsObject(converter, facesContext, wrapper, values[i], matchColors[i]);
206     }
207     mock.verify();
208   }
209 
210   /**
211    * Test Color conveters getAsString(FacesContext, UIComponent, Object) method
212    * works fine
213    */
214   public void testGetAsString()
215   {
216     ColorConverter converter  = new ColorConverter();
217     Mock mock = mock(UIComponent.class);
218     UIComponent component = (UIComponent) mock.proxy();
219     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
220 
221     Color[] colors = {  new Color(255,2,4),
222                         new Color(255,2,6),
223                         new Color(255,255,255),
224                         new Color(0,0,0),
225                         new Color(105,105,105),
226                       };
227 
228     List<String[]> patternsHoloder = new ArrayList<String[]>();
229     patternsHoloder.add(new String[]{"#RRGGBB", "RRGGBB"});
230     patternsHoloder.add(new String[]{"RR.GG.BB", "#RRGGBB" });
231     patternsHoloder.add(new String[]{"RRGGBB", "r-g-b"});
232     patternsHoloder.add(new String[]{"RR GG BB", "rrr ggg bbb"});
233     patternsHoloder.add(new String[]{"rrr-ggg-bbb", "rrr ggg bbb" });
234 
235     String matchValues[]   = {  "#FF0204",
236                                 "FF.02.06",
237                                 "FFFFFF",
238                                 "00 00 00",
239                                 "105-105-105",
240                              };
241 
242     for (int i = 0; i < patternsHoloder.size(); i++)
243     {
244       String[] patterns = patternsHoloder.get(i);
245       converter.setPatterns(patterns);
246       doTestGetAsString(converter, facesContext, wrapper,
247                                     colors[i], matchValues[i] );
248     }
249     mock.verify();
250   }
251 
252   /**
253    * Test that try to set null value for patterns throw IllegalArgumentException
254    */
255   public void testNullValueForPatterns()
256   {
257     ColorConverter converter = new ColorConverter();
258     Mock mock = mock(UIComponent.class);
259     
260     try
261     {
262       converter.setPatterns(null);
263       fail("Expected IllegalArgumentException for null value of patterns");
264     }
265     catch (IllegalArgumentException ex)
266     {
267       // expected fine
268     }
269     mock.verify();
270   }
271 
272   public void testGetAsObjectIllegalValue()
273   {
274     ColorConverter converter = new ColorConverter();
275     Mock mock = mock(UIComponent.class);
276     UIComponent component = (UIComponent) mock.proxy();
277     mock.stubs().method("getId").will(returnValue("test"));
278     //component.setupGetId("test");
279     try
280     {
281       converter.getAsString(facesContext, component, new Integer(1));
282       fail("Expected a converter exception");
283     } catch (IllegalArgumentException ex)
284     {
285       // expected
286     }
287     mock.verify();
288   }
289 }