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.commons.converter;
20  
21  import javax.faces.application.FacesMessage;
22  import javax.faces.component.UIInput;
23  import javax.faces.convert.ConverterException;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.test.base.AbstractJsfTestCase;
29  
30  /**
31   * @author lu4242 (latest modification by $Author: lu4242 $)
32   * @version $Revision: 1031895 $ $Date: 2010-11-05 19:46:39 -0500 (Fri, 05 Nov 2010) $
33   */
34  public class EnumConverterTest extends AbstractJsfTestCase
35  {
36  
37      private EnumConverter converter;
38  
39      public EnumConverterTest(String testName)
40      {
41          super(testName);
42      }
43  
44      public void setUp() throws Exception
45      {
46          super.setUp();
47          converter = new EnumConverter(EnumTest.class);
48      }
49      
50      public enum EnumTest
51      {
52          OPTION1,
53          OPTION2,
54          OPTION3
55      }
56  
57      public void tearDown() throws Exception
58      {
59          super.tearDown();
60          converter = null;
61      }
62  
63      public static Test suite()
64      {
65          return new TestSuite(EnumConverterTest.class);
66      }
67  
68      public void testEnumItemFound()
69      {
70          UIInput input = new UIInput();
71          input.setId("txt_test");
72  
73          try
74          {
75              //Any different to true is false.
76              Object value = converter.getAsObject(facesContext, input, "OPTION2");
77              assertEquals(EnumTest.OPTION2, value);
78              assertEquals("OPTION2", converter.getAsString(facesContext, input, value));
79          }
80          catch (ConverterException exception)
81          {
82              fail(exception.getMessage());
83          }
84      }
85      
86      public void testEnumItemNotFound()
87      {
88          UIInput input = new UIInput();
89          input.setId("txt_test");
90  
91          try
92          {
93              //Any different to true is false.
94              Object value = converter.getAsObject(facesContext, input, "OPTION100");
95              fail();
96          }
97          catch (ConverterException exception)
98          {
99              FacesMessage facesMessage = exception.getFacesMessage();
100             assertEquals(FacesMessage.SEVERITY_ERROR, facesMessage.getSeverity());
101         }
102     }    
103     
104 }