|
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
|
15
|
public int getNumber()
|
|
40
|
|
{
|
|
41
|
15
|
return number;
|
|
42
|
|
}
|
|
43
|
|
|
|
44
|
34
|
public void setNumber(int number)
|
|
45
|
|
{
|
|
46
|
34
|
this.number = number;
|
|
47
|
|
}
|
|
48
|
|
|
|
49
|
0
|
public String getText()
|
|
50
|
|
{
|
|
51
|
0
|
return text;
|
|
52
|
|
}
|
|
53
|
|
|
|
54
|
0
|
public void setText(String text)
|
|
55
|
|
{
|
|
56
|
0
|
this.text = text;
|
|
57
|
|
}
|
|
58
|
|
|
|
59
|
8
|
public JavaBean getNested()
|
|
60
|
|
{
|
|
61
|
8
|
return nested;
|
|
62
|
|
}
|
|
63
|
|
|
|
64
|
9
|
public void setNested(JavaBean nested)
|
|
65
|
|
{
|
|
66
|
9
|
this.nested = nested;
|
|
67
|
|
}
|
|
68
|
|
|
|
69
|
101
|
public JavaBean[] getNestedArray()
|
|
70
|
|
{
|
|
71
|
101
|
return nestedArray;
|
|
72
|
|
}
|
|
73
|
|
|
|
74
|
11
|
public void setNestedArray(JavaBean[] nestedArray)
|
|
75
|
|
{
|
|
76
|
11
|
this.nestedArray = nestedArray;
|
|
77
|
|
}
|
|
78
|
|
|
|
79
|
45
|
public List getNestedList()
|
|
80
|
|
{
|
|
81
|
45
|
return Arrays.asList(getNestedArray());
|
|
82
|
|
}
|
|
83
|
|
|
|
84
|
15
|
public Collection getNestedCollection()
|
|
85
|
|
{
|
|
86
|
15
|
return getNestedList();
|
|
87
|
|
}
|
|
88
|
|
|
|
89
|
15
|
public Iterator getNestedIterator()
|
|
90
|
|
{
|
|
91
|
15
|
return getNestedList().iterator();
|
|
92
|
|
}
|
|
93
|
|
|
|
94
|
37
|
public JavaBean getIndexed(int i)
|
|
95
|
|
{
|
|
96
|
37
|
return (JavaBean) getNestedArray()[i];
|
|
97
|
|
}
|
|
98
|
|
}
|
|
99
|
|
|
|
100
|
8
|
public BeanModelsTest(String s)
|
|
101
|
|
{
|
|
102
|
8
|
super(s);
|
|
103
|
|
}
|
|
104
|
|
|
|
105
|
8
|
public void setUp()
|
|
106
|
|
{
|
|
107
|
8
|
number = 42;
|
|
108
|
8
|
bean = new JavaBean();
|
|
109
|
8
|
bean.setNumber(number);
|
|
110
|
8
|
nested = new JavaBean();
|
|
111
|
8
|
nested.setNumber(number);
|
|
112
|
8
|
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
|
1
|
public void testPropertyValueModel()
|
|
121
|
|
{
|
|
122
|
1
|
ValueModel model = new BeanModel(bean).getValue("number");
|
|
123
|
|
|
|
124
|
1
|
assertTrue("model did not extract correct value", ((Integer) model.getValue()).intValue() == number);
|
|
125
|
|
|
|
126
|
|
// change value
|
|
127
|
1
|
int newnumber = number * 4711;
|
|
128
|
1
|
PropertyEditor editor = model.getPropertyEditor();
|
|
129
|
1
|
editor.setAsText("" + newnumber);
|
|
130
|
1
|
model.setValue(editor.getValue());
|
|
131
|
|
|
|
132
|
1
|
assertTrue("model did not change value correctly", ((Integer) model.getValue()).intValue() == newnumber);
|
|
133
|
|
}
|
|
134
|
|
|
|
135
|
1
|
public void testNested()
|
|
136
|
|
{
|
|
137
|
1
|
ValueModel value = new BeanModel(bean).getValue("nested.number");
|
|
138
|
1
|
bean.setNested(new JavaBean());
|
|
139
|
1
|
bean.getNested().setNumber(4711);
|
|
140
|
1
|
assertTrue("model did not extract correct value", ((Integer) value.getValue()).intValue() == 4711);
|
|
141
|
|
|
|
142
|
1
|
BeanTableModel table = new BeanModel(bean).getTable("nested.nestedArray");
|
|
143
|
1
|
table.addColumn("Number", "number");
|
|
144
|
1
|
bean.getNested().setNestedArray(new JavaBean[] { new JavaBean() });
|
|
145
|
1
|
value = table.getValueAt(0, 0);
|
|
146
|
1
|
value.setValue(new Integer(4711));
|
|
147
|
1
|
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
|
1
|
public void testModelProxy()
|
|
155
|
|
{
|
|
156
|
|
// create some models with no object backing them up
|
|
157
|
1
|
BeanModel proxy = new BeanModel(JavaBean.class);
|
|
158
|
1
|
BeanModel propertyProxy = proxy.getBean("nested");
|
|
159
|
1
|
ValueModel property = propertyProxy.getValue("number");
|
|
160
|
|
|
|
161
|
|
// create the objects and bind the proxy
|
|
162
|
1
|
proxy.setBean(bean);
|
|
163
|
|
|
|
164
|
|
// check that the property is correct
|
|
165
|
1
|
assertTrue("model did not fetch value correctly", number == ((Integer) property.getValue()).intValue());
|
|
166
|
|
}
|
|
167
|
|
|
|
168
|
1
|
public void testArrayTable() throws Exception
|
|
169
|
|
{
|
|
170
|
1
|
checkTableModel(null, "nestedArray");
|
|
171
|
|
}
|
|
172
|
|
|
|
173
|
1
|
public void testListTable() throws Exception
|
|
174
|
|
{
|
|
175
|
1
|
checkTableModel(JavaBean.class, "nestedList");
|
|
176
|
|
}
|
|
177
|
|
|
|
178
|
1
|
public void testIteratorTable() throws Exception
|
|
179
|
|
{
|
|
180
|
1
|
checkTableModel(JavaBean.class, "nestedIterator");
|
|
181
|
|
}
|
|
182
|
|
|
|
183
|
1
|
public void testCollectionTable() throws Exception
|
|
184
|
|
{
|
|
185
|
1
|
checkTableModel(JavaBean.class, "nestedCollection");
|
|
186
|
|
}
|
|
187
|
|
|
|
188
|
1
|
public void testIndexedTable() throws Exception
|
|
189
|
|
{
|
|
190
|
1
|
checkTableModel(JavaBean.class, "indexed");
|
|
191
|
|
}
|
|
192
|
|
|
|
193
|
5
|
private void checkTableModel(Class propertyClass, String property)
|
|
194
|
|
{
|
|
195
|
5
|
BeanModel model = new BeanModel(JavaBean.class);
|
|
196
|
5
|
BeanTableModel tableModel = model.getTable(property, propertyClass);
|
|
197
|
5
|
tableModel.addColumn("Number", "number");
|
|
198
|
5
|
tableModel.addColumn("Text", "text");
|
|
199
|
|
|
|
200
|
5
|
bean.setNestedArray(new JavaBean[0]);
|
|
201
|
5
|
model.setBean(bean);
|
|
202
|
|
|
|
203
|
5
|
assertTrue("doesn't handle empty rows", tableModel.getRowCount() == 0);
|
|
204
|
|
|
|
205
|
5
|
JavaBean bean1 = new JavaBean();
|
|
206
|
5
|
bean1.setNumber(1);
|
|
207
|
5
|
JavaBean bean2 = new JavaBean();
|
|
208
|
5
|
bean2.setNumber(2);
|
|
209
|
5
|
bean.setNestedArray(new JavaBean[] { bean1, bean2 });
|
|
210
|
|
|
|
211
|
5
|
System.out.println("tableModel.getRowCount() = " + tableModel.getRowCount());
|
|
212
|
5
|
assertTrue("incorrect number of rows", tableModel.getRowCount() == 2);
|
|
213
|
5
|
assertTrue("didn't access column correctly", ((Number) tableModel.getValueAt(0, 0).getValue()).intValue() == 1);
|
|
214
|
|
|
|
215
|
5
|
tableModel.getValueAt(0, 0).setValue(new Integer(4711));
|
|
216
|
5
|
assertTrue("didn't modify column correctly", ((Number) tableModel.getValueAt(0, 0).getValue()).intValue() == 4711);
|
|
217
|
|
}
|
|
218
|
|
}
|
|
219
|
|
|