001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import org.apache.tapestry.BaseComponentTestCase;
018    import org.testng.annotations.Test;
019    
020    /**
021     * Test case for {@link org.apache.tapestry.form.LabeledPropertySelectionModel}.
022     * 
023     * @author Paul Ferraro
024     * @since 4.0
025     */
026    @Test
027    public class LabeledPropertySelectionModelTest extends BaseComponentTestCase
028    {
029        public void test_Null_Value()
030        {
031            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel();
032            
033            assertEquals(null, model.translateValue(null));
034        }
035    
036        public void test_Empty_Model()
037        {
038            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel();
039    
040            validateLabel(model, "", null, "");
041    
042            assertEquals(model.getOptionCount(), 1);
043        }
044    
045        public void test_Default_Labeled_Model()
046        {
047            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel());
048    
049            validateLabel(model, "", null, "");
050    
051            validateModel(model);
052        }
053    
054        public void test_Labeled_Model()
055        {
056            String label = "Select a value";
057            Object option = null;
058            String value = "-1";
059    
060            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option, value);
061    
062            assertEquals(label, model.getLabel());
063            assertEquals(option, model.getOption());
064            assertEquals(value, model.getValue());
065    
066            validateLabel(model, label, option, value);
067    
068            validateModel(model);
069        }
070    
071        public void test_Disabled()
072        {
073            String label = "Choose...";
074    
075            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label);
076    
077            assertEquals(model.getLabel(0), label);
078            assertEquals(model.getLabel(1), String.valueOf(Boolean.TRUE));
079            assert model.isDisabled(0);
080            assert !model.isDisabled(1);
081            assert model.isDisabled(2);
082        }
083    
084        public void test_Label_Never_Disabled()
085        {
086            String label = "Choose...";
087    
088            LabeledPropertySelectionModel model =
089                    new LabeledPropertySelectionModel(createInnerModel(), label, null, "", true);
090    
091            assertEquals(model.getLabel(0), label);
092            assertEquals(model.getLabel(1), String.valueOf(Boolean.TRUE));
093            assert !model.isDisabled(0);
094            assert !model.isDisabled(1);
095            assert model.isDisabled(2);
096        }
097    
098        public void test_Label_Option_Disabled()
099        {
100            String label = "Choose...";
101            String option = "-1";
102    
103            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option);
104    
105            assertEquals(model.getLabel(0), label);
106            assert !model.isDisabled(0);
107            assert !model.isDisabled(1);
108            assert model.isDisabled(2);
109        }
110    
111        public void test_Label_Value_With_Option_Disabled()
112        {
113            String label = "Choose...";
114            String value = "-1";
115            Object option = Boolean.FALSE;
116    
117            LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option, value);
118    
119            assertEquals(model.getLabel(0), label);
120            assertEquals(model.getOption(0), option);
121            assertEquals(model.getOptionCount(), 3);
122            assertEquals(model.getValue(0), value);
123            
124            assert !model.isDisabled(0);
125            assert !model.isDisabled(1);
126            assert model.isDisabled(2);
127        }
128    
129        private void validateLabel(IPropertySelectionModel model, String label, Object option,
130                String value)
131        {
132            assertTrue(model.getOptionCount() > 0);
133    
134            assertEquals(model.getLabel(0), label);
135            assertEquals(model.getOption(0), option);
136            assertEquals(model.getValue(0), value);
137            assertEquals(model.translateValue(value), option);
138        }
139    
140        private void validateModel(IPropertySelectionModel model)
141        {
142            assertEquals(model.getOptionCount(), 3);
143    
144            assertEquals(model.getLabel(1), "true");
145            assertEquals(model.getOption(1), Boolean.TRUE);
146            assertEquals(model.getValue(1), "0");
147            assertEquals(model.translateValue("0"), Boolean.TRUE);
148    
149            assertEquals(model.getLabel(2), "false");
150            assertEquals(model.getOption(2), Boolean.FALSE);
151            assertEquals(model.getValue(2), "1");
152            assertEquals(model.translateValue("1"), Boolean.FALSE);
153        }
154    
155        private IPropertySelectionModel createInnerModel()
156        {
157            return new IPropertySelectionModel()
158            {
159                private boolean[] values = new boolean[] { true, false };
160    
161                public int getOptionCount()
162                {
163                    return values.length;
164                }
165    
166                public Object getOption(int index)
167                {
168                    return Boolean.valueOf(values[index]);
169                }
170    
171                public String getLabel(int index)
172                {
173                    return Boolean.toString(values[index]);
174                }
175    
176                public String getValue(int index)
177                {
178                    return Integer.toString(index);
179                }
180    
181                public boolean isDisabled(int index)
182                {
183                    return !values[index];
184                }
185                
186                public Object translateValue(String value)
187                {
188                    return getOption(Integer.parseInt(value));
189                }
190            };
191        }
192    }