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    package org.apache.tapestry.form;
015    
016    import org.apache.tapestry.BaseComponentTestCase;
017    import org.testng.annotations.Test;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    
023    /**
024     * Tests the functionality of {@link BeanPropertySelectionModel}.
025     *
026     * @author jkuhnert
027     */
028    @Test
029    public class BeanPropertySelectionModelTest extends BaseComponentTestCase
030    {
031        
032        public void test_Null_Model()
033        {
034            BeanPropertySelectionModel model = new BeanPropertySelectionModel();
035            assertEquals(model.getOptionCount(), 0);
036        }
037        
038        public void test_Basic_Model()
039        {
040            List<SimpleBean> list = new ArrayList();
041            list.add(new SimpleBean(1, "Name 1", "Description 1"));
042            list.add(new SimpleBean(2, "Name 2", "Description 2"));
043            list.add(new SimpleBean(3, "Name 3", "Description 3"));
044            
045            BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name");
046            
047            assertEquals(model.getOptionCount(), 3);
048            
049            SimpleBean b2 = (SimpleBean)model.getOption(1);
050            assertEquals(b2.getId(), 2);
051            assertEquals(model.getLabel(2), "Name 3");
052            
053            assertEquals(model.translateValue("1"), b2);
054        }
055    
056        public void test_Invalid_Option_Index()
057        {
058            BeanPropertySelectionModel model = new BeanPropertySelectionModel();
059    
060            assertEquals(model.getOptionCount(), 0);
061            assertEquals(model.getOption(3), null);
062        }
063    }