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  
20  package javax.faces.convert;
21  
22  import javax.faces.component.UIInput;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.shale.test.base.AbstractJsfTestCase;
26  
27  /**
28   * This testcase test <code>javax.faces.convert.EnumConverter</code>.
29   * 
30   * @author Michael Kurz (latest modification by $Author: jakobk $)
31   * @version $Revision: 925224 $ $Date: 2010-03-19 09:09:35 -0500 (Fri, 19 Mar 2010) $
32   */
33  public class EnumConverterTest extends AbstractJsfTestCase {
34      private enum testEnum {ITEM1, ITEM2};
35      private EnumConverter converter;
36      
37      public EnumConverterTest(String name) {
38          super(name);
39      }
40      
41      protected void setUp() throws Exception {
42          super.setUp();
43          converter = new EnumConverter(testEnum.class);
44      }
45  
46      protected void tearDown() throws Exception {
47          super.tearDown();
48          converter = null;
49      }
50  
51      /**
52       * Test method for
53       * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
54       */
55      public void testGetAsObject() {
56          UIInput input = new UIInput();
57          Object convertedObj = converter.getAsObject(FacesContext.getCurrentInstance(), input, "ITEM2");
58          assertEquals(convertedObj, testEnum.ITEM2);
59      }
60  
61      /**
62       * Test method for
63       * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
64       */
65      public void testGetAsObjectNull() {
66          UIInput input = new UIInput();
67             Object convertedObj = converter.getAsObject(FacesContext.getCurrentInstance(), input, null);
68             assertNull(convertedObj);
69      }
70      
71      /**
72       * Test method for
73       * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
74       */
75      public void testGetAsObjectNoEnum() {
76          UIInput input = new UIInput();
77          try {
78              converter.getAsObject(FacesContext.getCurrentInstance(), input, "NO_ENUM_CONST");
79              fail("Converter exception should be thrown");
80          } catch (ConverterException e) {
81              // should be thrown
82          }
83      }
84  
85      /**
86       * Test method for
87       * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
88       */
89      public void testGetAsObjectNoClassSet() {
90          Converter testConverter = new EnumConverter();
91          UIInput input = new UIInput();
92          try {
93              testConverter.getAsObject(FacesContext.getCurrentInstance(), input, "ITEM2");
94              fail("Converter exception should be thrown");
95          } catch (ConverterException e) {
96              // should be thrown
97          }
98      }
99      
100     /**
101      * Test method for
102      * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
103      */
104     public void testGetAsString() {
105         UIInput input = new UIInput();
106         String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, testEnum.ITEM1);
107         assertEquals(convertedStr, testEnum.ITEM1.toString());
108     }
109 
110     /**
111      * Test method for
112      * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
113      */
114     public void testGetAsStringNull() {
115         UIInput input = new UIInput();
116         String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, null);
117         assertEquals(convertedStr, "");
118     }
119 
120     /**
121      * Test method for
122      * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
123      */
124     public void testGetAsStringNoEnum() {
125         UIInput input = new UIInput();
126         String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, "HALLO");
127         assertEquals(convertedStr, "HALLO");
128     }
129 
130     /**
131      * Test method for
132      * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
133      */
134     public void testGetAsStringNoClassSet() {
135         Converter testConverter = new EnumConverter();
136         UIInput input = new UIInput();
137         try {
138             testConverter.getAsString(FacesContext.getCurrentInstance(), input, testEnum.ITEM1);
139             fail("Converter exception should be thrown");
140         } catch (ConverterException e) {
141             // should be thrown
142         }
143     }
144 }