001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $ID$
023: *
024: */
025: package com.bostechcorp.cbesb.common.mdl;
026:
027: import java.io.File;
028:
029: import junit.framework.TestCase;
030:
031: import com.bostechcorp.cbesb.common.mdl.IContentElement;
032: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
033: import com.bostechcorp.cbesb.common.mdl.IContentNode;
034: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
035: import com.bostechcorp.cbesb.common.mdl.IFormatDefinition;
036: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
037: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
038: import com.bostechcorp.cbesb.common.mdl.IVariableFormatDefinition;
039: import com.bostechcorp.cbesb.common.mdl.MDLParser;
040: import com.bostechcorp.cbesb.common.mdl.impl.ContentElementImpl;
041: import com.bostechcorp.cbesb.common.mdl.impl.ContentGroupImpl;
042: import com.bostechcorp.cbesb.common.mdl.impl.ElementDefinitionImpl;
043: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocumentImpl;
044: import com.bostechcorp.cbesb.common.mdl.impl.VariableFormatDefinitionImpl;
045:
046: /**
047: * test elementDefinition
048: */
049: public class TestElementDefinition extends TestCase {
050: File mdlFile = new File("target/test-data/in/testSerialize.mdl");
051:
052: IMDLDocument mdlDoc = new MDLDocumentImpl();
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056:
057: mdlDoc = MDLParser.load(mdlFile);
058: }
059:
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: }
063:
064: /**
065: * test get element's name
066: * The definition of an element must contain a name and either a format or
067: * datatype.
068: *
069: */
070: public void testGetName() {
071: IElementDefinition elementDef[] = mdlDoc
072: .getAllElementDefinitions();
073: for (int i = 0; i < elementDef.length; i++) {
074: String elementName = elementDef[i].getName();
075: // all the elements must have a name
076: assertNotNull(elementName);
077: }
078: String name = "OO";
079: assertEquals(name, elementDef[0].getName());
080:
081: IContentNode cNode[] = mdlDoc.getAllMessageDefinitions()[0]
082: .getChildren();
083: for (int j = 0; j < cNode.length; j++) {
084: if (cNode[j] instanceof IContentElement) {
085: IElementDefinition cElementDef = ((IContentElement) cNode[j])
086: .getElementDefinition();
087: assertNotNull(cElementDef.getName());
088: // if the name is "" ,it must has a ref tag
089: if (cElementDef.getName() == "") {
090: assertNotNull(cNode[j].getRef());
091: }
092: }
093: }
094: }
095:
096: /**
097: * test get namespaceURI
098: * if a element is global then its namespaceURI is the same as message if a
099: * element is locally ,its namespace is empty
100: */
101: public void testGetNamespaceURI() {
102: IElementDefinition elementDef[] = mdlDoc
103: .getAllElementDefinitions();
104: for (int i = 0; i < elementDef.length; i++) {
105: assertEquals("http://www.bostechcorp.com/serialize",
106: elementDef[i].getNamespaceURI());
107: }
108: IContentNode cNode[] = mdlDoc.getAllMessageDefinitions()[0]
109: .getChildren();
110: for (int j = 0; j < cNode.length; j++) {
111: if (cNode[j] instanceof IContentElement) {
112: IElementDefinition cElementDef = ((IContentElement) cNode[j])
113: .getElementDefinition();
114:
115: assertEquals("", cElementDef.getNamespaceURI());
116:
117: }
118: }
119: }
120:
121: /**
122: * test get datatype
123: * if a element has no children, then datatype is required, if a element has
124: * children, then it must not has a datatype.
125: */
126: public void testGetDatatype() {
127: IElementDefinition elementDef[] = mdlDoc
128: .getAllElementDefinitions();
129: for (int i = 0; i < elementDef.length; i++) {
130: if (elementDef[i].getChildCount() == 0) {
131: assertNotNull(elementDef[i].getDatatype());
132: } else
133: assertEquals("", elementDef[i].getDatatype());
134: }
135: IContentNode cNode[] = mdlDoc.getAllMessageDefinitions()[0]
136: .getChildren();
137: for (int j = 0; j < cNode.length; j++) {
138: if (cNode[j] instanceof IContentElement) {
139: IElementDefinition cElementDef = ((IContentElement) cNode[j])
140: .getElementDefinition();
141: if (cElementDef.getName() != "") {
142: if (cElementDef.getChildCount() == 0) {
143: assertNotNull(cElementDef.getDatatype());
144: } else
145: assertEquals("", cElementDef.getDatatype());
146: }
147: }
148: }
149: String datatype = elementDef[1].getDatatype();
150: assertEquals("string", datatype);
151: }
152:
153: public void testGetFormatDefinition() {
154: IElementDefinition elementDef = mdlDoc.getElementDefinition(
155: "http://www.bostechcorp.com/serialize", "OO");
156: IFormatDefinition formatDef = elementDef.getFormatDefinition();
157: assertEquals("variable", formatDef.getName());
158: IVariableFormatDefinition vFormatDef = (IVariableFormatDefinition) formatDef;
159: assertEquals(".", vFormatDef.getDelimiter());
160: }
161:
162: /**
163: * test get childCount.
164: * message,element and group can has children
165: */
166: public void testGetChildCount() {
167: IMessageDefinition messageDef = mdlDoc
168: .getAllMessageDefinitions()[0];
169: int childCount = messageDef.getChildCount();
170: assertEquals(6, childCount);
171:
172: IContentNode cNode[] = messageDef.getChildren();
173: for (int i = 0; i < cNode.length; i++) {
174: if (cNode[i] instanceof IContentElement) {
175: IContentElement cElement = (IContentElement) cNode[i];
176: if (cElement.getElementDefinition().getName() == "serialize") {
177: int childCount2 = cElement.getElementDefinition()
178: .getChildCount();
179: assertEquals(2, childCount2);
180: }
181: }
182: }
183: }
184:
185: public void testGetChildAtIndex() {
186: IMessageDefinition messageDef = mdlDoc
187: .getAllMessageDefinitions()[0];
188: IContentNode cNode = messageDef.getChildAtIndex(3);
189: IElementDefinition cElementDef = ((IContentElement) cNode)
190: .getElementDefinition();
191: assertEquals("Quantity", cElementDef.getName());
192: assertEquals("integer", cElementDef.getDatatype());
193: }
194:
195: /**
196: * test get children
197: * child of an element can be a element or a group
198: */
199: public void testgetChildren() {
200: IMessageDefinition messageDef = mdlDoc
201: .getAllMessageDefinitions()[0];
202: IContentNode cNode[] = messageDef.getChildren();
203: for (int i = 0; i < cNode.length; i++) {
204: if (cNode[i] instanceof IContentGroup) {
205: IContentGroup cGroup = (IContentGroup) cNode[i];
206: assertEquals("group1", cGroup.getName());
207: assertEquals(1, cGroup.getMinOccurs());
208: // -1 meand MaxOccurs is set to "unbounded"
209: assertEquals(-1, cGroup.getMaxOccurs());
210:
211: // get the children of the group
212: IContentNode ccNode[] = cGroup.getChildren();
213: assertEquals(2, ccNode.length);
214: IElementDefinition elementDef = ((IContentElement) ccNode[1])
215: .getElementDefinition();
216: assertEquals(1, ccNode[1].getMinOccurs());
217: assertEquals(5, ccNode[1].getMaxOccurs());
218: //the element has a "ref" tag
219: assertEquals("", elementDef.getName());
220: }
221: }
222: }
223:
224: /**
225: * test get description
226: * description is not child of element
227: */
228: public void testGetDescription() {
229: IElementDefinition elementDef = mdlDoc.getElementDefinition(
230: "http://www.bostechcorp.com/serialize", "XX");
231: assertEquals(0, elementDef.getChildCount());
232: assertNotNull(elementDef.getDescription());
233: assertEquals(" for test ", elementDef.getDescription());
234: }
235:
236: /**
237: * test add and remove children
238: * the child we added into a element can be a element or a group
239: *
240: */
241: public void testADDAndRemoveChildren() {
242: IElementDefinition elementDef = new ElementDefinitionImpl();
243: elementDef.setName("append");
244: elementDef.setNamespaceURI("com.append");
245: IContentElement cElement = new ContentElementImpl();
246: cElement.setElementDefinition(elementDef);
247: IMessageDefinition messageDef = mdlDoc
248: .getAllMessageDefinitions()[0];
249: int count = messageDef.getChildCount();
250: //add child
251: messageDef.appendChild(cElement);
252: //childCount+1
253: assertEquals(count + 1, messageDef.getChildCount());
254:
255: IElementDefinition elementDef2 = new ElementDefinitionImpl();
256: elementDef2.setName("insert");
257: elementDef2.setNamespaceURI("com.insert");
258: IContentElement cElement2 = new ContentElementImpl();
259: cElement2.setElementDefinition(elementDef2);
260:
261: //insert child at index
262: messageDef.insertChildAtIndex(cElement2, messageDef
263: .getChildren().length - 1);
264: assertEquals(count + 2, messageDef.getChildCount());
265:
266: //remove child at index
267: messageDef
268: .removeChildAtIndex(messageDef.getChildren().length - 1);
269: assertEquals(count + 1, messageDef.getChildCount());
270:
271: //the child is a group
272: IContentGroup cGroup = new ContentGroupImpl();
273: cGroup.setName("group");
274: cGroup.setNamespaceURI("com.group");
275: cGroup.setMaxOccurs(2);
276: cGroup.setMinOccurs(1);
277: IContentNode cNode = messageDef.getChildren()[0];
278: IElementDefinition elementDef3 = ((IContentElement) cNode)
279: .getElementDefinition();
280: assertEquals(0, elementDef.getChildCount());
281: elementDef3.appendChild(cGroup);
282: assertEquals(1, elementDef3.getChildCount());
283: IContentNode eNode[] = elementDef3.getChildren();
284: for (int i = 0; i < eNode.length; i++) {
285: if (eNode[i] instanceof IContentGroup) {
286: IContentGroup eGroup = (IContentGroup) eNode[i];
287: assertEquals("group", eGroup.getName());
288: assertEquals("com.group", eGroup.getNamespaceURI());
289: assertEquals(1, eGroup.getMinOccurs());
290: assertEquals(2, eGroup.getMaxOccurs());
291: }
292: }
293: }
294:
295: public void testInsertAndGetChildAtIndex() {
296: IMessageDefinition messageDef = mdlDoc
297: .getAllMessageDefinitions()[0];
298: IElementDefinition elementDef3 = new ElementDefinitionImpl();
299: elementDef3.setName("insert");
300: elementDef3.setNamespaceURI("com.insert");
301: elementDef3.setGlobal(true);
302: IVariableFormatDefinition vFormatDef = new VariableFormatDefinitionImpl();
303: vFormatDef.setDelimiter("|");
304: vFormatDef.setIDMethod((byte) 1);
305: elementDef3.setFormatDefinition(vFormatDef);
306: IContentElement cElement2 = new ContentElementImpl();
307: cElement2.setElementDefinition(elementDef3);
308: //insert at index 0
309: messageDef.insertChildAtIndex(cElement2, 0);
310: assertEquals(7, messageDef.getChildCount());
311:
312: //get the element
313: IContentNode cNode[] = messageDef.getChildren();
314: if (cNode[0] instanceof IContentElement) {
315: IContentElement cElement = (IContentElement) cNode[0];
316: IElementDefinition elementDef = cElement
317: .getElementDefinition();
318: assertEquals("insert", elementDef.getName());
319: assertEquals("com.insert", elementDef.getNamespaceURI());
320: assertTrue(elementDef.isGlobal());
321: IFormatDefinition formatDef = elementDef
322: .getFormatDefinition();
323: IVariableFormatDefinition varFormatDef = (IVariableFormatDefinition) formatDef;
324: assertEquals("|", varFormatDef.getDelimiter());
325: assertEquals(1, varFormatDef.getIDMethod());
326: }
327: }
328: }
|