001: package org.columba.core.context;
002:
003: import junit.framework.TestCase;
004:
005: import org.columba.core.context.base.ContextFactory;
006: import org.columba.core.context.base.api.IAttributeType;
007: import org.columba.core.context.base.api.IStructureType;
008: import org.columba.core.context.base.api.IStructureValue;
009: import org.columba.core.context.base.api.MULTIPLICITY;
010: import org.columba.core.context.base.api.IAttributeType.BASETYPE;
011: import org.columba.core.main.MainInterface;
012:
013: public class ContextTest extends TestCase {
014:
015: public ContextTest() {
016: super ();
017: }
018:
019: /**
020: *
021: * attribute test <code>
022: * <userlist name="STRING" description="STRING">
023: * </userlist>
024: * </code>
025: */
026: public void test() throws Exception {
027: ContextFactory factory = new ContextFactory();
028:
029: // top-level structure
030: IStructureType type = factory.createStructure("userlist", "ns");
031: type.setCardinality(MULTIPLICITY.ONE_TO_ONE);
032:
033: // mandatory String-based attribute
034: IAttributeType attr = type.addAttribute("name", "ns");
035:
036: // optional String-based attribute
037: IAttributeType attr2 = type.addAttribute("description", "ns");
038:
039: IAttributeType result1 = type.getAttribute("name", "ns");
040: assertEquals(attr, result1);
041: }
042:
043: public void testWithValue() throws Exception {
044: ContextFactory factory = new ContextFactory();
045:
046: // top-level structure
047: IStructureType type = factory.createStructure("userlist", "ns");
048: type.setCardinality(MULTIPLICITY.ONE_TO_ONE);
049:
050: // mandatory String-based attribute
051: IAttributeType attr = type.addAttribute("name", "ns");
052: // optional String-based attribute
053: IAttributeType attr2 = type.addAttribute("description", "ns");
054:
055: IAttributeType result1 = type.getAttribute("name", "ns");
056: assertEquals(attr, result1);
057:
058: IStructureValue value = factory.createValue("userlist", "ns",
059: type);
060: value.setString("name", "ns", "Name");
061: value.setString("description", "ns", "Description");
062:
063: // should throw exception as attribute type "name2" is not defined in
064: // type structure
065: try {
066: value.setString("name2", "ns", "Description");
067: fail();
068: } catch (RuntimeException e) {
069: }
070:
071: try {
072: value.addChild("test", "ns");
073: fail();
074: } catch (RuntimeException e) {
075: }
076:
077: }
078:
079: /**
080: *
081: * structure test
082: *
083: * <pre>
084: * <userlist name="STRING" description="STRING">
085: * <user firstname="STRING" lastname="STRING">
086: * </user>
087: * <user firstname="STRING" lastname="STRING">
088: * </user>
089: * </userlist>
090: * </pre>
091: */
092: public void test2() throws Exception {
093: ContextFactory factory = new ContextFactory();
094:
095: // top-level structure
096: IStructureType userList = factory.createStructure("userlist",
097: "ns");
098: userList.setCardinality(MULTIPLICITY.ONE_TO_ONE);
099:
100: // mandatory String-based attribute
101: IAttributeType attr = userList.addAttribute("name", "ns");
102:
103: // optional String-based attribute
104: IAttributeType attr2 = userList.addAttribute("description",
105: "ns");
106: attr2.setBaseType(BASETYPE.STRING);
107:
108: // user struct
109: IStructureType user = userList.addChild("user", "ns");
110: user.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
111:
112: IAttributeType attr1_1 = user.addAttribute("firstname", "ns");
113: IAttributeType attr1_2 = user.addAttribute("lastname", "ns");
114: }
115:
116: public void test2WithValue() throws Exception {
117: ContextFactory factory = new ContextFactory();
118:
119: // top-level structure
120: IStructureType userList = factory.createStructure("userlist",
121: "ns");
122: userList.setCardinality(MULTIPLICITY.ONE_TO_ONE);
123:
124: // mandatory String-based attribute
125: IAttributeType attr = userList.addAttribute("name", "ns");
126:
127: // optional String-based attribute
128: IAttributeType attr2 = userList.addAttribute("description",
129: "ns");
130: attr2.setBaseType(BASETYPE.STRING);
131:
132: // user struct
133: IStructureType user = userList.addChild("user", "ns");
134: user.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
135:
136: IAttributeType attr1_1 = user.addAttribute("firstname", "ns");
137: IAttributeType attr1_2 = user.addAttribute("lastname", "ns");
138:
139: // create test userlist data
140: IStructureValue value = factory.createValue("userlist", "ns",
141: userList);
142: value.setString("name", "ns", "Name");
143: value.setString("description", "ns", "Description");
144:
145: IStructureValue value1 = value.addChild("user", "ns");
146: value1.setString("firstname", "ns", "FirstName");
147: value1.setString("lastname", "ns", "LastName");
148:
149: IStructureValue value2 = value.addChild("user", "ns");
150: value2.setString("firstname", "ns", "FirstName");
151: value2.setString("lastname", "ns", "LastName");
152: }
153:
154: public void test3() throws Exception {
155: ContextFactory factory = new ContextFactory();
156:
157: IStructureType type = factory.createStructure("context",
158: "org.columba.core");
159:
160: // MULTIPLICITY.ONE is default
161: IStructureType core = type.addChild("core", "org.columba.core");
162:
163: // identity definition
164: IStructureType identity = core.addChild("identity",
165: "org.columba.core");
166: // MULTIPLICITY.ZERO_TO_ONE is default
167: IAttributeType emailAddress = identity.addAttribute(
168: "emailAddress", "org.columba.core");
169: identity.addAttribute("displayName", "org.columba.core");
170: identity.addAttribute("firstName", "org.columba.core");
171: identity.addAttribute("lastName", "org.columba.core");
172: identity.addAttribute("website", "org.columba.core");
173:
174: // date time timezone definition
175: IStructureType dateTime = core.addChild("dateTime",
176: "org.columba.core");
177: IAttributeType date = dateTime.addAttribute("date",
178: "org.columba.core");
179: IAttributeType timeZone = dateTime.addAttribute("timeZone",
180: "org.columba.core");
181: date.setBaseType(BASETYPE.DATE);
182:
183: // date range (start time, end time) definition
184: IStructureType dateRange = core.addChild("dateRange",
185: "org.columba.core");
186: IAttributeType startStart = dateRange.addAttribute("startDate",
187: "org.columba.core");
188: startStart.setBaseType(BASETYPE.DATE);
189: IAttributeType endDate = dateRange.addAttribute("endDate",
190: "org.columba.core");
191: endDate.setBaseType(BASETYPE.DATE);
192:
193: // document definition
194: IStructureType document = core.addChild("document",
195: "org.columba.core");
196: document.addAttribute("author", "org.columba.core");
197: document.addAttribute("title", "org.columba.core");
198: document.addAttribute("summary", "org.columba.core");
199: document.addAttribute("body", "org.columba.core");
200:
201: // locale definition
202: IStructureType locale = core.addChild("locale",
203: "org.columba.core");
204: locale.addAttribute("language", "org.columba.core");
205: locale.addAttribute("country", "org.columba.core");
206: locale.addAttribute("variant", "org.columba.core");
207:
208: // list of attachments
209: IStructureType attachmentList = core.addChild("attachmentList",
210: "org.columba.core");
211: IStructureType attachment = attachmentList.addChild(
212: "attachment", "org.columba.core");
213: attachment.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
214: // single attachment
215: attachment.addAttribute("name", "org.columba.core");
216: IAttributeType contentType = attachment.addAttribute("content",
217: "org.columba.core");
218: contentType.setBaseType(BASETYPE.BINARY);
219:
220: // message
221: IStructureType message = core.addChild("message",
222: "org.columba.core");
223: message.addAttribute("subject", "org.columba.core");
224: // single sender - re-use identity type
225: IStructureType sender = message.addChild("sender",
226: "org.columba.core");
227: sender.addChild(identity);
228: sender.setCardinality(MULTIPLICITY.ONE_TO_ONE);
229: // re-use identity type for recipient list
230: IStructureType recipients = message.addChild("recipients",
231: "org.columba.core");
232: recipients.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
233: recipients.addChild(identity);
234: // message body
235: message.addAttribute("bodyText", "org.columba.core");
236: message.addAttribute("selectedBodytext", "org.columba.core");
237: // message contains list of attachments
238: message.addChild(attachmentList);
239: }
240: }
|