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 } 082 083 public void test_Label_Option_Disabled() 084 { 085 String label = "Choose..."; 086 String option = "-1"; 087 088 LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option); 089 090 assertEquals(model.getLabel(0), label); 091 assert !model.isDisabled(0); 092 assert !model.isDisabled(1); 093 } 094 095 public void test_Label_Value_With_Option_Disabled() 096 { 097 String label = "Choose..."; 098 String value = "-1"; 099 Object option = Boolean.FALSE; 100 101 LabeledPropertySelectionModel model = new LabeledPropertySelectionModel(createInnerModel(), label, option, value); 102 103 assertEquals(model.getLabel(0), label); 104 assertEquals(model.getOption(0), option); 105 assertEquals(model.getOptionCount(), 3); 106 assertEquals(model.getValue(0), value); 107 108 assert !model.isDisabled(0); 109 assert !model.isDisabled(1); 110 } 111 112 private void validateLabel(IPropertySelectionModel model, String label, Object option, 113 String value) 114 { 115 assertTrue(model.getOptionCount() > 0); 116 117 assertEquals(model.getLabel(0), label); 118 assertEquals(model.getOption(0), option); 119 assertEquals(model.getValue(0), value); 120 assertEquals(model.translateValue(value), option); 121 } 122 123 private void validateModel(IPropertySelectionModel model) 124 { 125 assertEquals(model.getOptionCount(), 3); 126 127 assertEquals(model.getLabel(1), "true"); 128 assertEquals(model.getOption(1), Boolean.TRUE); 129 assertEquals(model.getValue(1), "0"); 130 assertEquals(model.translateValue("0"), Boolean.TRUE); 131 132 assertEquals(model.getLabel(2), "false"); 133 assertEquals(model.getOption(2), Boolean.FALSE); 134 assertEquals(model.getValue(2), "1"); 135 assertEquals(model.translateValue("1"), Boolean.FALSE); 136 } 137 138 private IPropertySelectionModel createInnerModel() 139 { 140 return new IPropertySelectionModel() 141 { 142 private boolean[] values = new boolean[] { true, false }; 143 144 public int getOptionCount() 145 { 146 return values.length; 147 } 148 149 public Object getOption(int index) 150 { 151 return Boolean.valueOf(values[index]); 152 } 153 154 public String getLabel(int index) 155 { 156 return Boolean.toString(values[index]); 157 } 158 159 public String getValue(int index) 160 { 161 return Integer.toString(index); 162 } 163 164 public boolean isDisabled(int index) 165 { 166 return false; 167 } 168 169 public Object translateValue(String value) 170 { 171 return getOption(Integer.parseInt(value)); 172 } 173 }; 174 } 175 }