KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > JavaBeanDataModelTest


1 /*
2  * $Id: JavaBeanDataModelTest.java,v 1.1 2004/08/05 01:29:18 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.awt.Image JavaDoc;
11
12 import java.awt.image.BufferedImage JavaDoc;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.List JavaDoc;
18
19 import junit.framework.TestCase;
20
21 public class JavaBeanDataModelTest extends TestCase {
22
23     private JavaBeanDataModel model;
24     private TestBean bean, bean2;
25     private Date JavaDoc date;
26     private Image JavaDoc image;
27
28     protected void setUp() {
29     try {
30         model = new JavaBeanDataModel(TestBean.class);
31     } catch (Exception JavaDoc ex) {
32         fail(ex.getMessage());
33     }
34     bean = new TestBean();
35     bean.setDate(date = new Date JavaDoc());
36     bean.setImage(image = new BufferedImage JavaDoc(100,100,BufferedImage.TYPE_INT_RGB));
37     }
38     protected void tearDown() {
39     model.setJavaBean(null);
40     model = null;
41     bean = null;
42     }
43
44     public void testConstruction() throws Exception JavaDoc {
45
46     DataModel model = new JavaBeanDataModel(TestBean.class);
47
48     List JavaDoc props = new ArrayList JavaDoc();
49     props.add("string");
50     props.add("integer");
51     props.add("float");
52     props.add("date");
53     props.add("image");
54     props.add("array");
55     props.add("list");
56     props.add("map");
57
58     List JavaDoc fieldNames = Arrays.asList(model.getFieldNames());
59     assertTrue("Some missing fields", fieldNames.containsAll(props));
60     }
61
62     public void testGetValue() {
63     model.setJavaBean(bean);
64
65     // Static comparison
66
assertEquals("A String", model.getValue("string"));
67     assertEquals(17, ((Integer JavaDoc)model.getValue("integer")).intValue());
68     assertEquals(0.17f, ((Float JavaDoc)model.getValue("float")).floatValue(), 0.01f);
69     assertEquals(date, model.getValue("date"));
70     assertEquals(image, model.getValue("image"));
71
72     // Instance values
73
assertEquals(bean.getString(), model.getValue("string"));
74     assertEquals(bean.getInteger(), ((Integer JavaDoc)model.getValue("integer")).intValue());
75     assertEquals(bean.getFloat(), ((Float JavaDoc)model.getValue("float")).floatValue(), 0.01f);
76     assertEquals(bean.getDate(), model.getValue("date"));
77     assertEquals(bean.getImage(), model.getValue("image"));
78     }
79
80     public void testSetValue() {
81     TestBean newBean = new TestBean();
82     model.setJavaBean(newBean);
83
84     // Set the values which are the same as an existing bean
85
model.setValue("string", bean.getString());
86     model.setValue("integer", new Integer JavaDoc(bean.getInteger()));
87     model.setValue("float", new Float JavaDoc(bean.getFloat()));
88     model.setValue("date", bean.getDate());
89     model.setValue("image", bean.getImage());
90
91     assertEquals(newBean.getString(), bean.getString());
92     assertEquals(newBean.getInteger(), bean.getInteger());
93     assertEquals(newBean.getFloat(), bean.getFloat(), 0.01f);
94     assertEquals(newBean.getDate(), bean.getDate());
95     assertEquals(newBean.getImage(), bean.getImage());
96     }
97
98 }
99
Popular Tags