1   package org.codehaus.xfire.aegis.type;
2   
3   import java.util.Date;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import junit.framework.TestCase;
8   
9   import org.dom4j.Document;
10  import org.dom4j.DocumentHelper;
11  import org.dom4j.Element;
12  import org.dom4j.Namespace;
13  import org.dom4j.QName;
14  import org.dom4j.io.SAXReader;
15  
16  /***
17   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
18   * @since Aug 19, 2004
19   */
20  public class TypeTest
21      extends TestCase
22  {
23      public void testSimpleType()
24          throws Exception
25      {
26          SAXReader reader = new SAXReader();
27          Document doc = DocumentHelper.createDocument();
28          
29          Namespace ns = DocumentHelper.createNamespace("test", "urn:test");
30          QName qname = DocumentHelper.createQName("bleh", ns);
31          QName rootQname = DocumentHelper.createQName("root", ns);
32          
33          Element root = doc.addElement(rootQname);
34          
35          Map map = new HashMap();
36          SimpleBean bean = new SimpleBean();
37          map.put("bean", bean);
38          
39          bean.setBleh("bleh");
40          
41          Type type = new SimpleType();
42          type.setOgnl("#bean.bleh");
43          type.setQName(qname);
44          
45          type.write( root, map );
46          
47          Element typeEl = root.element( qname );
48          assertNotNull(typeEl);
49          assertEquals("bleh", typeEl.getText());
50          
51          map = new HashMap();
52          bean = new SimpleBean();
53          map.put("bean", bean);
54          
55          type.read( root, map );
56          
57          assertEquals( "bleh", bean.getBleh() );
58      }
59      
60      public void testDateType()
61          throws Exception
62      {
63          SAXReader reader = new SAXReader();
64          Document doc = DocumentHelper.createDocument();
65          
66          Namespace ns = DocumentHelper.createNamespace("test", "urn:test");
67          QName qname = DocumentHelper.createQName("bleh", ns);
68          QName rootQname = DocumentHelper.createQName("root", ns);
69          
70          Element root = doc.addElement(rootQname);
71          
72          Map map = new HashMap();
73          SimpleBean bean = new SimpleBean();
74          map.put("bean", bean);
75          
76          bean.setDate( new Date() );
77          
78          Type type = new DateType();
79          type.setOgnl("#bean.date");
80          type.setQName(qname);
81          
82          type.write( root, map );
83          
84          Element typeEl = root.element( qname );
85          assertNotNull(typeEl);
86          assertNotNull(typeEl.getText());
87          System.out.println(typeEl.getText());
88          
89          map = new HashMap();
90          bean = new SimpleBean();
91          map.put("bean", bean);
92          
93          type.read( root, map );
94          
95          assertNotNull( bean.getDate() );
96      }
97      
98      public void testIntegerType()
99          throws Exception
100     {
101         SAXReader reader = new SAXReader();
102         Document doc = DocumentHelper.createDocument();
103         
104         Namespace ns = DocumentHelper.createNamespace("test", "urn:test");
105         QName qname = DocumentHelper.createQName("bleh", ns);
106         QName rootQname = DocumentHelper.createQName("root", ns);
107         
108         Element root = doc.addElement(rootQname);
109         
110         Map map = new HashMap();
111         SimpleBean bean = new SimpleBean();
112         map.put("bean", bean);
113         
114         bean.setInteger(10);
115         
116         Type type = new IntegerType();
117         type.setOgnl("#bean.integer");
118         type.setQName(qname);
119         
120         type.write( root, map );
121         
122         Element typeEl = root.element( qname );
123         assertNotNull(typeEl);
124         assertEquals("10", typeEl.getText());
125         
126         map = new HashMap();
127         bean = new SimpleBean();
128         map.put("bean", bean);
129         
130         type.read( root, map );
131         
132         assertEquals( 10, bean.getInteger() );
133     }
134     
135     public void testBeanType()
136         throws Exception
137     {
138         SAXReader reader = new SAXReader();
139         Document doc = DocumentHelper.createDocument();
140         
141         Namespace ns = DocumentHelper.createNamespace("test", "urn:test");
142         QName beanQ = DocumentHelper.createQName("bean", ns);
143         QName blehQ = DocumentHelper.createQName("bleh", ns);
144         QName rootQ = DocumentHelper.createQName("root", ns);
145         
146         Element root = doc.addElement(rootQ);
147         
148         Map map = new HashMap();
149         SimpleBean bean = new SimpleBean();
150         map.put("bean", bean);
151         
152         bean.setBleh("bleh");
153         
154         BeanType type = new BeanType();
155         type.setQName(beanQ);
156         type.setOgnl("new org.codehaus.xfire.aegis.type.SimpleBean()");
157         type.setKey("bean");
158         
159         SimpleType child = new SimpleType();
160         child.setOgnl("#bean.bleh");
161         child.setQName(blehQ);
162         type.addChild(child);
163         
164         type.write( root, map );
165         
166         Element beanEl = root.element( beanQ );
167         assertNotNull(beanEl);
168         assertEquals("", beanEl.getText());
169         
170         Element blehEl = beanEl.element( blehQ );
171         assertNotNull(blehEl);
172         assertEquals("bleh", blehEl.getText());
173         
174         map = new HashMap();
175 
176         type.read( root, map );
177         
178         assertEquals( "bleh", bean.getBleh() );
179     }
180 }