1 package test.tuigwaa; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 6 import junit.framework.TestCase; 7 8 import com.isenshi.util.extlib.DiconResource; 9 10 public class DiconResourceTest extends TestCase { 11 12 public void testRef() { 13 Bean bean = new Bean("test"); 14 Component component = new Component(bean); 15 16 DiconResource resource = new DiconResource(); 17 resource.addComponent(bean); 18 resource.addComponent(component); 19 resource.addRefComponent(bean); 20 resource.setPath("hoge.dicon"); 21 resource.save(System.out); 22 resource.save(); 23 24 DiconResource inResource = new DiconResource(); 25 inResource.setPath("hoge.dicon"); 26 inResource.load(); 27 Component loadedComponent = (Component)inResource.getComponent(Component.class); 28 29 assertEquals("test",loadedComponent.getBean().getName()); 30 31 } 32 33 public void testArray(){ 34 Component component = new Component(); 35 component.setNums(new Integer[]{new Integer(2), new Integer(3)}); 36 37 DiconResource resource = new DiconResource(); 38 resource.addComponent(component); 39 resource.setPath("hoge.dicon"); 40 resource.save(System.out); 41 resource.save(); 42 43 DiconResource inResource = new DiconResource(); 44 inResource.setPath("hoge.dicon"); 45 inResource.load(); 46 Component loadedComponent = (Component)inResource.getComponent(Component.class); 47 48 assertEquals(2,loadedComponent.getNums()[0].intValue()); 49 assertEquals(3,loadedComponent.getNums()[1].intValue()); 50 } 51 52 53 54 public void testPrimitive(){ 55 PrimitiveBean bean = new PrimitiveBean(); 56 bean.setFlag(true); 57 58 59 DiconResource resource = new DiconResource(); 60 resource.addComponent(bean); 61 resource.setPath("pri.dicon"); 62 resource.save(System.out); 63 resource.save(); 64 65 DiconResource inResource = new DiconResource(); 66 inResource.setPath("pri.dicon"); 67 inResource.load(); 68 PrimitiveBean loadedBean = (PrimitiveBean)inResource.getComponent(PrimitiveBean.class); 69 70 assertTrue(loadedBean.isFlag()); 71 } 72 73 public void testDate(){ 74 Component c = new Component(); 75 76 Calendar cal = Calendar.getInstance(); 77 c.setDate(cal.getTime()); 78 79 DiconResource resource = new DiconResource(); 80 resource.addComponent(c); 81 resource.setPath("hoge.dicon"); 82 resource.save(System.out); 83 resource.save(); 84 85 DiconResource inResource = new DiconResource(); 86 inResource.setPath("hoge.dicon"); 87 inResource.load(); 88 Component loadedC = (Component)inResource.getComponent(Component.class); 89 90 Calendar cal2 = Calendar.getInstance(); 91 cal2.setTime(loadedC.getDate()); 92 93 assertEquals(cal.get(Calendar.YEAR), cal2.get(Calendar.YEAR)); 94 } 95 96 public static class PrimitiveBean{ 97 98 private boolean flag = false; 99 100 public void setFlag(boolean flag) { 101 this.flag = flag; 102 } 103 104 public boolean isFlag() { 105 return flag; 106 } 107 } 108 109 110 public static class Component{ 111 112 private Bean bean; 113 114 private Integer[] nums; 115 116 private Date date; 117 118 public Date getDate() { 119 return date; 120 } 121 122 public void setDate(Date date) { 123 this.date = date; 124 } 125 126 public Integer[] getNums() { 127 return nums; 128 } 129 130 public void setNums(Integer[] nums) { 131 this.nums = nums; 132 } 133 134 public Component(){ 135 } 136 137 public Component(Bean bean){ 138 setBean(bean); 139 } 140 141 public Bean getBean(){ 142 return bean; 143 } 144 145 public void setBean(Bean bean){ 146 this.bean = bean; 147 } 148 149 } 150 151 public static class Bean { 152 153 private String name; 154 155 public Bean(){ 156 157 } 158 159 public Bean(String name) { 160 this.name = name; 161 } 162 163 public void setName(String name) { 164 this.name = name; 165 } 166 167 public String getName() { 168 return name; 169 } 170 171 } 172 }