1
2 package net.sf.voruta;
3
4 import junit.framework.*;
5 import net.sf.cglib.*;
6 import java.sql.*;
7 import java.util.*;
8 import java.lang.reflect.*;
9 import java.lang.reflect.Array;
10
11 /***
12 *
13 * @author baliuka
14 */
15 public class DbUtilsTest extends TestCase {
16
17
18 public DbUtilsTest(java.lang.String testName) {
19 super(testName);
20 }
21
22 public static Test suite() {
23 TestSuite suite = new TestSuite(DbUtilsTest.class);
24 return suite;
25 }
26
27 public static void main(java.lang.String[] args) throws Exception{
28
29 junit.textui.TestRunner.run(suite());
30
31
32 }
33
34
35 /*** Test of resultSetToMap method, of class net.sf.voruta.DbUtils. */
36 public void testResultSetToMap() throws Exception{
37
38 String names[] = {"id","name","value"};
39 Object objects[][] = {{"1","2","3"}};
40
41 Map map = new HashMap();
42 for(int i=0; i< names.length; i++ ){
43 map.put(names[i],objects[0][i]);
44 }
45 Map result = new HashMap();
46 MockDbResultSet rs = new MockDbResultSet(names,objects);
47 rs.next();
48
49 DbUtils.resultSetToMap(rs,result);
50
51 assertEquals(map,result);
52
53 MapHandler h = new MapHandler();
54 rs.reset();
55 assertEquals(map,h.handle(rs, Map.class, null ));
56
57 rs.reset();
58 assertEquals(map,h.handle(rs, HashMap.class, null ));
59
60 rs.reset();
61 assertEquals(map,h.handle(rs, Properties.class, null ));
62
63
64
65
66 }
67
68
69 public void testResultSetToBeanMap() throws Exception{
70
71 String names[] = {"id","name","value"};
72 Object objects[][] = {
73 {new Long(1),"2", new Float(3)},
74 {new Long(4),"5", new Float(6)}
75 };
76
77 Map map = new HashMap();
78
79 Bean bean = new Bean();
80 map.put( new Long(1), bean );
81
82 bean.setId( 1 );
83 bean.setName( "2" );
84 bean.setValue( 3 );
85
86 bean = new Bean();
87
88 map.put( new Long(4), bean );
89
90 bean.setId( 4 );
91 bean.setName( "5" );
92 bean.setValue( 6 );
93
94
95
96 Map result = new HashMap();
97 MockDbResultSet rs = new MockDbResultSet(names,objects);
98
99
100 DbUtils.resultSetToBeanMap(rs,Bean.class, result,"id");
101
102 assertEquals(map,result);
103
104 BeanMapHandler h = new BeanMapHandler(Bean.class, "id" );
105
106 rs.reset();
107 assertEquals(map,h.handle(rs,Map.class,null));
108
109 rs.reset();
110 assertEquals(map,h.handle(rs,Hashtable.class,null));
111
112
113
114 }
115
116
117
118 public void testPairHandler() throws Exception{
119
120 String names[] = {"key","value"};
121 Object objects[][] = {
122 {"1","2"},
123 {"2","3"}
124 };
125
126 Map map = new HashMap();
127 map.put( objects[0][0], objects[0][1] );
128 map.put( objects[1][0], objects[1][1] );
129
130 MockDbResultSet rs = new MockDbResultSet( names, objects );
131 ResultSetHandler handler = new PairHandler();
132 assertEquals(map, handler.handle(rs,HashMap.class,null) );
133 rs.reset();
134 assertEquals(map, handler.handle(rs,Map.class,null) );
135
136 rs.reset();
137 assertEquals(map, handler.handle(rs,Properties.class,null) );
138
139
140 }
141
142 /*** Test of resultSetToBean method, of class net.sf.voruta.DbUtils. */
143 public void testResultSetToBean() throws Exception{
144
145 String names[] = {"id","name","value"};
146 Object objects[][] = {{new Long(1),"2", new Float(3)}};
147
148 Bean bean = new Bean();
149 bean.setId(1);
150 bean.setName("2");
151 bean.setValue(3);
152
153 Bean result = new Bean();
154 MockDbResultSet rs = new MockDbResultSet(names,objects);
155
156 rs.next();
157 DbUtils.resultSetToBean(rs,result);
158
159 assertEquals(bean,result);
160
161 rs.reset();
162
163 BeanHandler h = new BeanHandler();
164
165 assertEquals(bean,h.handle(rs,Bean.class,null));
166
167
168 }
169
170 /*** Test of resultSetToBeanCollection method, of class net.sf.voruta.DbUtils. */
171 public void testResultSetToBeanCollection() throws Exception{
172
173 String names[] = {"id","name","value"};
174 Object objects[][] = {
175 {new Long(1),"2", new Float(3)},
176 {new Long(4),"5", new Float(6)},
177 };
178
179 Bean bean1 = new Bean();
180 bean1.setId(1);
181 bean1.setName("2");
182 bean1.setValue(3);
183
184 Bean bean2 = new Bean();
185 bean2.setId(4);
186 bean2.setName("5");
187 bean2.setValue(6);
188
189 List list = new ArrayList();
190 list.add(bean1);
191 list.add(bean2);
192
193 List results = new ArrayList();
194
195 MockDbResultSet rs = new MockDbResultSet(names,objects);
196
197
198 DbUtils.resultSetToBeanCollection(rs, Bean.class, results);
199
200 assertEquals(list, results);
201
202 BeanCollectionHandler h = new BeanCollectionHandler(Bean.class);
203 rs.reset();
204 assertEquals(new HashSet(list), h.handle(rs,Set.class,null) );
205
206 rs.reset();
207
208 assertEquals(list, h.handle(rs, Collection.class,null) );
209
210 rs.reset();
211
212 assertEquals(list, h.handle(rs, List.class,null) );
213
214 rs.reset();
215
216
217
218 assertEquals(list, Arrays.asList( (Object [])h.handle(rs, Object[].class, null)) );
219
220 }
221
222 public void testColumn() throws Exception{
223
224 String names[] = {"id","name","value"};
225 Object objects[][] = {{"1"},{"2"},{"3"}};
226 List result = Arrays.asList(new String[]{"1","2","3"});
227
228
229 MockDbResultSet rs = new MockDbResultSet(names,objects);
230
231 ColumnHandler h = new ColumnHandler();
232
233 assertEquals(result,h.handle(rs,List.class,null) );
234 rs.reset();
235
236 assertEquals(result, Arrays.asList( (Object[])h.handle(rs,String[].class,null) ));
237
238
239 }
240
241 /*** Test of resultSetToArray method, of class net.sf.voruta.DbUtils. */
242 public void testResultSetToArray() throws Exception{
243
244 String names[] = {"id","name","value"};
245 Object objects[][] = {{"1","2","3"}};
246
247 MockDbResultSet rs = new MockDbResultSet(names,objects);
248 rs.next();
249
250 String [] result = (String [])DbUtils.resultSetToArray(rs,String[].class);
251
252 assertEquals(result.length,names.length);
253
254 for(int i = 0; i < objects[0].length; i++){
255 assertEquals(result[i],objects[0][i]);
256 }
257
258 VectorHandler h = new VectorHandler();
259
260 rs.reset();
261
262 assertEquals(Arrays.asList(result), Arrays.asList((Object[])h.handle(rs,String[].class,null) ) );
263
264 rs.reset();
265
266 assertEquals(Arrays.asList(result), h.handle(rs, List.class,null) );
267
268 rs.reset();
269
270 assertEquals(Arrays.asList(result), h.handle(rs, ArrayList.class,null) );
271
272
273
274 }
275
276 public static class Bean{
277
278 /*** Holds value of property id. */
279 private long id;
280
281 /*** Holds value of property name. */
282 private String name;
283
284 /*** Holds value of property value. */
285 private float value;
286
287 /*** Getter for property id.
288 * @return Value of property id.
289 *
290 */
291 public long getId() {
292 return this.id;
293 }
294
295 /*** Setter for property id.
296 * @param id New value of property id.
297 *
298 */
299 public void setId(long id) {
300 this.id = id;
301 }
302
303 /*** Getter for property name.
304 * @return Value of property name.
305 *
306 */
307 public String getName() {
308 return this.name;
309 }
310
311 /*** Setter for property name.
312 * @param name New value of property name.
313 *
314 */
315 public void setName(String name) {
316 this.name = name;
317 }
318
319 /*** Getter for property value.
320 * @return Value of property value.
321 *
322 */
323 public float getValue() {
324 return this.value;
325 }
326
327 /*** Setter for property value.
328 * @param value New value of property value.
329 *
330 */
331 public void setValue(float value) {
332 this.value = value;
333 }
334
335 public int hashCode() {
336
337 return (int)id;
338 }
339
340 public boolean equals(Object obj) {
341 if(obj != null && obj instanceof Bean ){
342 Bean bean = (Bean)obj;
343 return bean.id == id && bean.value == value && (
344 bean.name == null ? name == null : bean.name.equals(name)
345 ) ;
346 }
347 return false;
348 }
349
350 }
351
352
353 }
This page was automatically generated by Maven