1
2
3
4
5
6
7 package test.tuigwaa;
8
9 import java.util.Calendar;
10 import java.util.Date;
11
12 import junit.framework.TestCase;
13
14 import com.isenshi.util.converter.ConverterResource;
15 import com.isenshi.util.converter.ConverterResourceImpl;
16 import com.isenshi.util.converter.function.ConverterFunction;
17 import com.isenshi.util.converter.function.DateConverterFunction;
18 import com.isenshi.util.converter.function.Id2ObjConverterFunction;
19 import com.isenshi.util.functor.CompositeUnaryFunction;
20 import com.isenshi.util.functor.UnaryFunction;
21
22 /***
23 * @author nishioka
24 */
25 public class ConverterTest extends TestCase {
26
27 private void setDate(IdDateBean bean) {
28 bean.setDateArray(new Integer[] { new Integer(1977), new Integer(8),
29 new Integer(19) });
30 }
31
32 private ConverterFunction getDateFunction() {
33 ConverterFunction function = new DateConverterFunction();
34 function.setSourceField("dateArray");
35 function.setTargetField("date");
36 return function;
37 }
38
39 private void assertDate(IdDateBean bean) {
40 Calendar cal = Calendar.getInstance();
41 cal.setTime(bean.getDate());
42 assertEquals(1977, cal.get(Calendar.YEAR));
43 assertEquals(7, cal.get(Calendar.MONTH));
44 assertEquals(19, cal.get(Calendar.DATE));
45 }
46
47 public void testDateConverter() {
48 IdDateBean bean = new IdDateBean();
49 setDate(bean);
50 bean = (IdDateBean) getDateFunction().evaluate(bean);
51 assertDate(bean);
52 }
53
54 private void setId(IdDateBean bean){
55 bean.setId(new Long(2));
56 }
57
58 private ConverterFunction getIdFunction(){
59 ConverterFunction function = new Id2ObjConverterFunction();
60 function.setSourceField("id");
61 function.setTargetField("bean");
62 return function;
63 }
64
65 private void assertId(IdDateBean bean){
66 IdBean idBean = (IdBean) bean.getBean();
67 assertEquals(2, idBean.getId().longValue());
68 }
69
70 public void testIdConverter() {
71 IdDateBean bean = new IdDateBean();
72 setId(bean);
73 bean = (IdDateBean)getIdFunction().evaluate(bean);
74 assertId(bean);
75 }
76
77 public void testComposite(){
78 IdDateBean bean = new IdDateBean();
79 setId(bean);
80 setDate(bean);
81 UnaryFunction function = new CompositeUnaryFunction(getIdFunction(), getDateFunction());
82 bean = (IdDateBean)function.evaluate(bean);
83 assertDate(bean);
84 assertId(bean);
85 }
86
87 public void testResource(){
88 IdDateBean bean = new IdDateBean();
89 setId(bean);
90 setDate(bean);
91 ConverterResource resource = new ConverterResourceImpl();
92 ConverterFunction cfunction = resource.createConverterFunction("id2ObjConverter", "id", "bean");
93 resource.resiter("", "key", cfunction);
94 cfunction = resource.createConverterFunction("dateConverter", "dateArray", "date");
95 resource.resiter("", "key", cfunction);
96 UnaryFunction function = resource.getFunction("", "key");
97 bean = (IdDateBean)function.evaluate(bean);
98 assertDate(bean);
99 assertId(bean);
100 }
101
102
103 public static class IdDateBean {
104
105 private Integer[] dateArray;
106
107 private Date date;
108
109 public Date getDate() {
110 return date;
111 }
112
113 public void setDate(Date date) {
114 this.date = date;
115 }
116
117 public Integer[] getDateArray() {
118 return dateArray;
119 }
120
121 public void setDateArray(Integer[] dateArray) {
122 this.dateArray = dateArray;
123 }
124
125 private IdBean bean_;
126
127 private Long id_;
128
129 public void setId(Long id) {
130 this.id_ = id;
131 }
132
133 public Long getId() {
134 return id_;
135 }
136
137 public void setBean(IdBean bean) {
138 this.bean_ = bean;
139 }
140
141 public IdBean getBean() {
142 return bean_;
143 }
144 }
145
146 public static class IdBean {
147
148 private Long id;
149
150 public Long getId() {
151 return id;
152 }
153
154 public void setId(Long id) {
155 this.id = id;
156 }
157 }
158 }