1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
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
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
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 }