1 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 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 label = "Select a value"; 50 Object option = null; 51 String 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 label, Object option, String 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 getOption(int index) 101 { 102 return Boolean.valueOf(values[index]); 103 } 104 105 public String getLabel(int index) 106 { 107 return Boolean.toString(values[index]); 108 } 109 110 public String getValue(int index) 111 { 112 return Integer.toString(index); 113 } 114 115 public Object translateValue(String value) 116 { 117 return getOption(Integer.parseInt(value)); 118 } 119 }; 120 } 121 } 122 | Popular Tags |