KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > TestLabeledPropertySelectionModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import org.apache.tapestry.form.IPropertySelectionModel;
18 import org.apache.tapestry.form.LabeledPropertySelectionModel;
19 import org.apache.tapestry.junit.TapestryTestCase;
20
21 /**
22  * Test case for {@link org.apache.tapestry.form.LabeledPropertySelectionModel}.
23  *
24  * @author Paul Ferraro
25  * @since 4.0
26  */

27 public class TestLabeledPropertySelectionModel extends TapestryTestCase
28 {
29     public void testEmptyModel()
30     {
31         LabeledPropertySelectionModel model = new LabeledPropertySelectionModel();
32         
33         validateLabel(model, "", null, "");
34         
35         assertEquals(model.getOptionCount(), 1);
36     }
37     
38     public void testDefaultLabeledModel()
39     {
40         LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel());
41         
42         validateLabel(model, "", null, "");
43         
44         validateModel(model);
45     }
46
47     public void testLabeledModel()
48     {
49         String JavaDoc label = "Select a value";
50         Object JavaDoc option = null;
51         String JavaDoc value = "-1";
52         
53         LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option, value);
54         
55         assertEquals(label, model.getLabel());
56         assertEquals(option, model.getOption());
57         assertEquals(value, model.getValue());
58         
59         validateLabel(model, label, option, value);
60         
61         validateModel(model);
62     }
63     
64     private void validateLabel(IPropertySelectionModel model, String JavaDoc label, Object JavaDoc option, String JavaDoc value)
65     {
66         assertTrue(model.getOptionCount() > 0);
67         
68         assertEquals(model.getLabel(0), label);
69         assertEquals(model.getOption(0), option);
70         assertEquals(model.getValue(0), value);
71         assertEquals(model.translateValue(value), option);
72     }
73     
74     private void validateModel(IPropertySelectionModel model)
75     {
76         assertEquals(model.getOptionCount(), 3);
77         
78         assertEquals(model.getLabel(1), "true");
79         assertEquals(model.getOption(1), Boolean.TRUE);
80         assertEquals(model.getValue(1), "0");
81         assertEquals(model.translateValue("0"), Boolean.TRUE);
82
83         assertEquals(model.getLabel(2), "false");
84         assertEquals(model.getOption(2), Boolean.FALSE);
85         assertEquals(model.getValue(2), "1");
86         assertEquals(model.translateValue("1"), Boolean.FALSE);
87     }
88     
89     private IPropertySelectionModel createInnerModel()
90     {
91         return new IPropertySelectionModel()
92         {
93             private boolean[] values = new boolean[] { true, false };
94             
95             public int getOptionCount()
96             {
97                 return values.length;
98             }
99
100             public Object JavaDoc getOption(int index)
101             {
102                 return Boolean.valueOf(values[index]);
103             }
104
105             public String JavaDoc getLabel(int index)
106             {
107                 return Boolean.toString(values[index]);
108             }
109
110             public String JavaDoc getValue(int index)
111             {
112                 return Integer.toString(index);
113             }
114
115             public Object JavaDoc translateValue(String JavaDoc value)
116             {
117                 return getOption(Integer.parseInt(value));
118             }
119         };
120     }
121 }
122
Popular Tags