1 7 8 package org.jdesktop.swing.data; 9 10 import java.awt.Image ; 11 12 import java.awt.image.BufferedImage ; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Date ; 17 import java.util.List ; 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 date; 26 private Image image; 27 28 protected void setUp() { 29 try { 30 model = new JavaBeanDataModel(TestBean.class); 31 } catch (Exception ex) { 32 fail(ex.getMessage()); 33 } 34 bean = new TestBean(); 35 bean.setDate(date = new Date ()); 36 bean.setImage(image = new BufferedImage (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 { 45 46 DataModel model = new JavaBeanDataModel(TestBean.class); 47 48 List props = new ArrayList (); 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 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 assertEquals("A String", model.getValue("string")); 67 assertEquals(17, ((Integer )model.getValue("integer")).intValue()); 68 assertEquals(0.17f, ((Float )model.getValue("float")).floatValue(), 0.01f); 69 assertEquals(date, model.getValue("date")); 70 assertEquals(image, model.getValue("image")); 71 72 assertEquals(bean.getString(), model.getValue("string")); 74 assertEquals(bean.getInteger(), ((Integer )model.getValue("integer")).intValue()); 75 assertEquals(bean.getFloat(), ((Float )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 model.setValue("string", bean.getString()); 86 model.setValue("integer", new Integer (bean.getInteger())); 87 model.setValue("float", new Float (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 |