001 // Copyright 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.coerce; 016 017 import org.apache.tapestry.BaseComponentTestCase; 018 import org.apache.tapestry.form.IPropertySelectionModel; 019 import org.testng.annotations.Test; 020 021 /** 022 * Tests for {@link org.apache.tapestry.coerce.StringToPropertySelectionModelConverter} and 023 * {@link org.apache.tapestry.coerce.StringConvertedPropertySelectionModel}. 024 * 025 * @author Howard M. Lewis Ship 026 * @since 4.0 027 */ 028 @Test 029 public class StringToPropertySelectionModelConverterTest extends BaseComponentTestCase 030 { 031 private IPropertySelectionModel newModel(String value) 032 { 033 return (IPropertySelectionModel) new StringToPropertySelectionModelConverter() 034 .convertValue(value); 035 } 036 037 private void assertValues(IPropertySelectionModel model, String... values) 038 { 039 assertEquals(values.length, model.getOptionCount()); 040 041 for (int i = 0; i < values.length; i++) 042 { 043 assertEquals(values[i], model.getValue(i)); 044 assertEquals(values[i], model.getOption(i)); 045 } 046 } 047 048 private void assertLabels(IPropertySelectionModel model, String... labels) 049 { 050 assertEquals(labels.length, model.getOptionCount()); 051 052 for (int i = 0; i < labels.length; i++) 053 assertEquals(labels[i], model.getLabel(i)); 054 } 055 056 public void testJustLabels() 057 { 058 IPropertySelectionModel model = newModel("Green,Red,Blue"); 059 060 assertValues(model, "Green", "Red", "Blue"); 061 assertLabels(model, "Green", "Red", "Blue"); 062 } 063 064 public void testLabelsAndValues() 065 { 066 IPropertySelectionModel model = newModel("Red=RED,Green=GREEN,Blue=BLUE"); 067 068 assertValues(model, "RED", "GREEN", "BLUE"); 069 assertLabels(model, "Red", "Green", "Blue"); 070 } 071 072 public void testBlankValue() 073 { 074 IPropertySelectionModel model = newModel("--Colors--=,Red=RED,Green=GREEN,Blue=BLUE"); 075 076 assertValues(model, "", "RED", "GREEN", "BLUE"); 077 assertLabels(model, "--Colors--", "Red", "Green", "Blue"); 078 } 079 080 public void testWhiteSpaceTrimmed() 081 { 082 IPropertySelectionModel model = newModel("--Colors--=\t,\n\tRed\t=\tRED\t,\n\tGreen\t=\tGREEN\t,\n\tBlue\t=\tBLUE\n"); 083 084 assertValues(model, "", "RED", "GREEN", "BLUE"); 085 assertLabels(model, "--Colors--", "Red", "Green", "Blue"); 086 } 087 }