View Javadoc
1 /* 2 * Angkor Web Framework 3 * 4 * Distributable under LGPL license. 5 * See terms of license at gnu.org. 6 */ 7 8 package com.tirsen.angkor.test.unit.beans; 9 10 import junit.framework.TestCase; 11 import com.tirsen.angkor.*; 12 import com.tirsen.angkor.table.Table; 13 import com.tirsen.angkor.table.TableModel; 14 import com.tirsen.angkor.beans.*; 15 import com.tirsen.angkor.widget.*; 16 17 import java.beans.PropertyEditor; 18 import java.util.*; 19 20 /*** 21 * <!-- $Id: BeanModelsTest.java,v 1.1 2002/10/07 19:49:23 tirsen Exp $ --> 22 * 23 * @author $Author: tirsen $ 24 * @version $Revision: 1.1 $ 25 */ 26 public class BeanModelsTest extends TestCase 27 { 28 private int number; 29 private JavaBean bean; 30 private JavaBean nested; 31 32 public static class JavaBean 33 { 34 private int number; 35 private String text; 36 private JavaBean nested; 37 private JavaBean[] nestedArray; 38 39 public int getNumber() 40 { 41 return number; 42 } 43 44 public void setNumber(int number) 45 { 46 this.number = number; 47 } 48 49 public String getText() 50 { 51 return text; 52 } 53 54 public void setText(String text) 55 { 56 this.text = text; 57 } 58 59 public JavaBean getNested() 60 { 61 return nested; 62 } 63 64 public void setNested(JavaBean nested) 65 { 66 this.nested = nested; 67 } 68 69 public JavaBean[] getNestedArray() 70 { 71 return nestedArray; 72 } 73 74 public void setNestedArray(JavaBean[] nestedArray) 75 { 76 this.nestedArray = nestedArray; 77 } 78 79 public List getNestedList() 80 { 81 return Arrays.asList(getNestedArray()); 82 } 83 84 public Collection getNestedCollection() 85 { 86 return getNestedList(); 87 } 88 89 public Iterator getNestedIterator() 90 { 91 return getNestedList().iterator(); 92 } 93 94 public JavaBean getIndexed(int i) 95 { 96 return (JavaBean) getNestedArray()[i]; 97 } 98 } 99 100 public BeanModelsTest(String s) 101 { 102 super(s); 103 } 104 105 public void setUp() 106 { 107 number = 42; 108 bean = new JavaBean(); 109 bean.setNumber(number); 110 nested = new JavaBean(); 111 nested.setNumber(number); 112 bean.setNested(nested); 113 } 114 115 /*** 116 * Tests the following functionality: 117 * <li> ability to extract a property value. 118 * <li> change the value via a property editor. 119 */ 120 public void testPropertyValueModel() 121 { 122 ValueModel model = new BeanModel(bean).getValue("number"); 123 124 assertTrue("model did not extract correct value", ((Integer) model.getValue()).intValue() == number); 125 126 // change value 127 int newnumber = number * 4711; 128 PropertyEditor editor = model.getPropertyEditor(); 129 editor.setAsText("" + newnumber); 130 model.setValue(editor.getValue()); 131 132 assertTrue("model did not change value correctly", ((Integer) model.getValue()).intValue() == newnumber); 133 } 134 135 public void testNested() 136 { 137 ValueModel value = new BeanModel(bean).getValue("nested.number"); 138 bean.setNested(new JavaBean()); 139 bean.getNested().setNumber(4711); 140 assertTrue("model did not extract correct value", ((Integer) value.getValue()).intValue() == 4711); 141 142 BeanTableModel table = new BeanModel(bean).getTable("nested.nestedArray"); 143 table.addColumn("Number", "number"); 144 bean.getNested().setNestedArray(new JavaBean[] { new JavaBean() }); 145 value = table.getValueAt(0, 0); 146 value.setValue(new Integer(4711)); 147 assertTrue("model did not extract correct value", ((Integer) value.getValue()).intValue() == 4711); 148 } 149 150 /*** 151 * Tests the following functionality: 152 * <li> build the models to beans which does not yet exists, create the beans and use the models. 153 */ 154 public void testModelProxy() 155 { 156 // create some models with no object backing them up 157 BeanModel proxy = new BeanModel(JavaBean.class); 158 BeanModel propertyProxy = proxy.getBean("nested"); 159 ValueModel property = propertyProxy.getValue("number"); 160 161 // create the objects and bind the proxy 162 proxy.setBean(bean); 163 164 // check that the property is correct 165 assertTrue("model did not fetch value correctly", number == ((Integer) property.getValue()).intValue()); 166 } 167 168 public void testArrayTable() throws Exception 169 { 170 checkTableModel(null, "nestedArray"); 171 } 172 173 public void testListTable() throws Exception 174 { 175 checkTableModel(JavaBean.class, "nestedList"); 176 } 177 178 public void testIteratorTable() throws Exception 179 { 180 checkTableModel(JavaBean.class, "nestedIterator"); 181 } 182 183 public void testCollectionTable() throws Exception 184 { 185 checkTableModel(JavaBean.class, "nestedCollection"); 186 } 187 188 public void testIndexedTable() throws Exception 189 { 190 checkTableModel(JavaBean.class, "indexed"); 191 } 192 193 private void checkTableModel(Class propertyClass, String property) 194 { 195 BeanModel model = new BeanModel(JavaBean.class); 196 BeanTableModel tableModel = model.getTable(property, propertyClass); 197 tableModel.addColumn("Number", "number"); 198 tableModel.addColumn("Text", "text"); 199 200 bean.setNestedArray(new JavaBean[0]); 201 model.setBean(bean); 202 203 assertTrue("doesn't handle empty rows", tableModel.getRowCount() == 0); 204 205 JavaBean bean1 = new JavaBean(); 206 bean1.setNumber(1); 207 JavaBean bean2 = new JavaBean(); 208 bean2.setNumber(2); 209 bean.setNestedArray(new JavaBean[] { bean1, bean2 }); 210 211 System.out.println("tableModel.getRowCount() = " + tableModel.getRowCount()); 212 assertTrue("incorrect number of rows", tableModel.getRowCount() == 2); 213 assertTrue("didn't access column correctly", ((Number) tableModel.getValueAt(0, 0).getValue()).intValue() == 1); 214 215 tableModel.getValueAt(0, 0).setValue(new Integer(4711)); 216 assertTrue("didn't modify column correctly", ((Number) tableModel.getValueAt(0, 0).getValue()).intValue() == 4711); 217 } 218 }

This page was automatically generated by Maven