1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
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
53
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
63
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
73
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
82 }
83 }
84
85
86
87
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
97 }
98 }
99
100
101
102
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
112
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
122
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
132
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
142 }
143 }
144 }