KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > context > ContextTest


1 package org.columba.core.context;
2
3 import junit.framework.TestCase;
4
5 import org.columba.core.context.base.ContextFactory;
6 import org.columba.core.context.base.api.IAttributeType;
7 import org.columba.core.context.base.api.IStructureType;
8 import org.columba.core.context.base.api.IStructureValue;
9 import org.columba.core.context.base.api.MULTIPLICITY;
10 import org.columba.core.context.base.api.IAttributeType.BASETYPE;
11 import org.columba.core.main.MainInterface;
12
13 public class ContextTest extends TestCase {
14
15     public ContextTest() {
16         super();
17     }
18
19     /**
20      *
21      * attribute test <code>
22      * <userlist name="STRING" description="STRING">
23      * </userlist>
24      * </code>
25      */

26     public void test() throws Exception JavaDoc {
27         ContextFactory factory = new ContextFactory();
28
29         // top-level structure
30
IStructureType type = factory.createStructure("userlist", "ns");
31         type.setCardinality(MULTIPLICITY.ONE_TO_ONE);
32
33         // mandatory String-based attribute
34
IAttributeType attr = type.addAttribute("name", "ns");
35
36         // optional String-based attribute
37
IAttributeType attr2 = type.addAttribute("description", "ns");
38
39         IAttributeType result1 = type.getAttribute("name", "ns");
40         assertEquals(attr, result1);
41     }
42
43     public void testWithValue() throws Exception JavaDoc {
44         ContextFactory factory = new ContextFactory();
45
46         // top-level structure
47
IStructureType type = factory.createStructure("userlist", "ns");
48         type.setCardinality(MULTIPLICITY.ONE_TO_ONE);
49
50         // mandatory String-based attribute
51
IAttributeType attr = type.addAttribute("name", "ns");
52         // optional String-based attribute
53
IAttributeType attr2 = type.addAttribute("description", "ns");
54
55         IAttributeType result1 = type.getAttribute("name", "ns");
56         assertEquals(attr, result1);
57
58         IStructureValue value = factory.createValue("userlist", "ns", type);
59         value.setString("name", "ns", "Name");
60         value.setString("description", "ns", "Description");
61
62         // should throw exception as attribute type "name2" is not defined in
63
// type structure
64
try {
65             value.setString("name2", "ns", "Description");
66             fail();
67         } catch (RuntimeException JavaDoc e) {
68         }
69
70         try {
71             value.addChild("test", "ns");
72             fail();
73         } catch (RuntimeException JavaDoc e) {
74         }
75
76     }
77
78     /**
79      *
80      * structure test
81      *
82      * <pre>
83      * &lt;userlist name=&quot;STRING&quot; description=&quot;STRING&quot;&gt;
84      * &lt;user firstname=&quot;STRING&quot; lastname=&quot;STRING&quot;&gt;
85      * &lt;/user&gt;
86      * &lt;user firstname=&quot;STRING&quot; lastname=&quot;STRING&quot;&gt;
87      * &lt;/user&gt;
88      * &lt;/userlist&gt;
89      * </pre>
90      */

91     public void test2() throws Exception JavaDoc {
92         ContextFactory factory = new ContextFactory();
93
94         // top-level structure
95
IStructureType userList = factory.createStructure("userlist", "ns");
96         userList.setCardinality(MULTIPLICITY.ONE_TO_ONE);
97
98         // mandatory String-based attribute
99
IAttributeType attr = userList.addAttribute("name", "ns");
100
101         // optional String-based attribute
102
IAttributeType attr2 = userList.addAttribute("description", "ns");
103         attr2.setBaseType(BASETYPE.STRING);
104
105         // user struct
106
IStructureType user = userList.addChild("user", "ns");
107         user.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
108
109         IAttributeType attr1_1 = user.addAttribute("firstname", "ns");
110         IAttributeType attr1_2 = user.addAttribute("lastname", "ns");
111     }
112
113     public void test2WithValue() throws Exception JavaDoc {
114         ContextFactory factory = new ContextFactory();
115
116         // top-level structure
117
IStructureType userList = factory.createStructure("userlist", "ns");
118         userList.setCardinality(MULTIPLICITY.ONE_TO_ONE);
119
120         // mandatory String-based attribute
121
IAttributeType attr = userList.addAttribute("name", "ns");
122
123         // optional String-based attribute
124
IAttributeType attr2 = userList.addAttribute("description", "ns");
125         attr2.setBaseType(BASETYPE.STRING);
126
127         // user struct
128
IStructureType user = userList.addChild("user", "ns");
129         user.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
130
131         IAttributeType attr1_1 = user.addAttribute("firstname", "ns");
132         IAttributeType attr1_2 = user.addAttribute("lastname", "ns");
133
134         // create test userlist data
135
IStructureValue value = factory.createValue("userlist", "ns", userList);
136         value.setString("name", "ns", "Name");
137         value.setString("description", "ns", "Description");
138
139         IStructureValue value1 = value.addChild("user", "ns");
140         value1.setString("firstname", "ns", "FirstName");
141         value1.setString("lastname", "ns", "LastName");
142
143         IStructureValue value2 = value.addChild("user", "ns");
144         value2.setString("firstname", "ns", "FirstName");
145         value2.setString("lastname", "ns", "LastName");
146     }
147
148     public void test3() throws Exception JavaDoc {
149         ContextFactory factory = new ContextFactory();
150
151         IStructureType type = factory.createStructure("context",
152                 "org.columba.core");
153
154         // MULTIPLICITY.ONE is default
155
IStructureType core = type.addChild("core", "org.columba.core");
156
157         // identity definition
158
IStructureType identity = core.addChild("identity", "org.columba.core");
159         // MULTIPLICITY.ZERO_TO_ONE is default
160
IAttributeType emailAddress = identity.addAttribute("emailAddress",
161                 "org.columba.core");
162         identity.addAttribute("displayName", "org.columba.core");
163         identity.addAttribute("firstName", "org.columba.core");
164         identity.addAttribute("lastName", "org.columba.core");
165         identity.addAttribute("website", "org.columba.core");
166
167         // date time timezone definition
168
IStructureType dateTime = core.addChild("dateTime", "org.columba.core");
169         IAttributeType date = dateTime.addAttribute("date", "org.columba.core");
170         IAttributeType timeZone = dateTime.addAttribute("timeZone",
171                 "org.columba.core");
172         date.setBaseType(BASETYPE.DATE);
173
174         // date range (start time, end time) definition
175
IStructureType dateRange = core.addChild("dateRange",
176                 "org.columba.core");
177         IAttributeType startStart = dateRange.addAttribute("startDate",
178                 "org.columba.core");
179         startStart.setBaseType(BASETYPE.DATE);
180         IAttributeType endDate = dateRange.addAttribute("endDate",
181                 "org.columba.core");
182         endDate.setBaseType(BASETYPE.DATE);
183
184         // document definition
185
IStructureType document = core.addChild("document", "org.columba.core");
186         document.addAttribute("author", "org.columba.core");
187         document.addAttribute("title", "org.columba.core");
188         document.addAttribute("summary", "org.columba.core");
189         document.addAttribute("body", "org.columba.core");
190
191         // locale definition
192
IStructureType locale = core.addChild("locale", "org.columba.core");
193         locale.addAttribute("language", "org.columba.core");
194         locale.addAttribute("country", "org.columba.core");
195         locale.addAttribute("variant", "org.columba.core");
196
197         // list of attachments
198
IStructureType attachmentList = core.addChild("attachmentList",
199                 "org.columba.core");
200         IStructureType attachment = attachmentList.addChild("attachment",
201                 "org.columba.core");
202         attachment.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
203         // single attachment
204
attachment.addAttribute("name", "org.columba.core");
205         IAttributeType contentType = attachment.addAttribute("content",
206                 "org.columba.core");
207         contentType.setBaseType(BASETYPE.BINARY);
208
209         // message
210
IStructureType message = core.addChild("message", "org.columba.core");
211         message.addAttribute("subject", "org.columba.core");
212         // single sender - re-use identity type
213
IStructureType sender = message.addChild("sender", "org.columba.core");
214         sender.addChild(identity);
215         sender.setCardinality(MULTIPLICITY.ONE_TO_ONE);
216         // re-use identity type for recipient list
217
IStructureType recipients = message.addChild("recipients",
218                 "org.columba.core");
219         recipients.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
220         recipients.addChild(identity);
221         // message body
222
message.addAttribute("bodyText", "org.columba.core");
223         message.addAttribute("selectedBodytext", "org.columba.core");
224         // message contains list of attachments
225
message.addChild(attachmentList);
226     }
227 }
228
Popular Tags