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: *
023: *
024: *$Id: TestContentGroup.java 3531 2006-12-08 09:28:11Z sji $
025: */
026: package com.bostechcorp.cbesb.common.mdl;
027:
028: import java.io.File;
029:
030: import junit.framework.TestCase;
031:
032: import com.bostechcorp.cbesb.common.mdl.IContentElement;
033: import com.bostechcorp.cbesb.common.mdl.IContentGroup;
034: import com.bostechcorp.cbesb.common.mdl.IContentNode;
035: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
036: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
037: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
038: import com.bostechcorp.cbesb.common.mdl.MDLParser;
039: import com.bostechcorp.cbesb.common.mdl.impl.ContentElementImpl;
040: import com.bostechcorp.cbesb.common.mdl.impl.ContentGroupImpl;
041: import com.bostechcorp.cbesb.common.mdl.impl.ElementDefinitionImpl;
042: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocumentImpl;
043:
044: /**
045: * test contentGroup
046: *
047: */
048: public class TestContentGroup extends TestCase {
049: File mdlFile = new File(
050: "target/test-data/in/contentNodeAndGroup.mdl");
051:
052: IMDLDocument mdlDoc = new MDLDocumentImpl();
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: mdlDoc = MDLParser.load(mdlFile);
057:
058: }
059:
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: }
063:
064: /**
065: * Groups don't have their own format definitions,
066: * they represent a logical organization of the content.
067: * name required
068: * minOccurs optional
069: * maxOccurs optional
070: */
071:
072: public void testGetContentGroup() {
073: IMessageDefinition msgDef = mdlDoc.getAllMessageDefinitions()[0];
074: IContentNode cNode[] = msgDef.getChildren();
075: for (int i = 0; i < cNode.length; i++) {
076: if (cNode[i] instanceof IContentGroup) {
077: IContentGroup cGroup = (IContentGroup) cNode[i];
078: assertEquals("group1", cGroup.getName());
079: assertEquals(-1, cNode[i].getMaxOccurs());
080: assertEquals(1, cNode[i].getMinOccurs());
081: }
082: }
083: }
084:
085: /**
086: *test get children
087: *child of a group can be a element or a group
088: *
089: */
090: public void testGetChildren() {
091: IMessageDefinition msgDef = mdlDoc.getAllMessageDefinitions()[0];
092: IContentGroup gNode = (IContentGroup) msgDef.getChildren()[3];
093: IContentNode cNode[] = gNode.getChildren();
094: for (int i = 0; i < cNode.length; i++) {
095: // the child is an element
096: if (cNode[i] instanceof IContentElement) {
097: IContentElement cElement = (IContentElement) cNode[i];
098: assertEquals(1, cNode[i].getMinOccurs());
099: assertEquals(1, cNode[i].getMaxOccurs());
100:
101: IElementDefinition elementDef = cElement
102: .getElementDefinition();
103: assertEquals("UnitPrice", elementDef.getName());
104: }
105: //the child is a group
106: if (cNode[i] instanceof IContentGroup) {
107: IContentGroup cGroup = (IContentGroup) cNode[i];
108: assertEquals("group2", cGroup.getName());
109: assertEquals(5, cGroup.getMaxOccurs());
110: assertEquals(3, cGroup.getMinOccurs());
111: }
112: }
113: }
114:
115: public void testGetChildAtIndex() {
116: IMessageDefinition msgDef = mdlDoc.getAllMessageDefinitions()[0];
117: IContentGroup gNode = (IContentGroup) msgDef.getChildren()[3];
118:
119: IContentNode cElement = gNode.getChildAtIndex(0);
120: assertTrue(cElement instanceof IContentElement);
121: assertEquals("UnitPrice", ((IContentElement) cElement)
122: .getElementDefinition().getName());
123:
124: IContentNode cGroup = gNode.getChildAtIndex(1);
125: assertTrue(cGroup instanceof IContentGroup);
126: assertEquals("group2", ((IContentGroup) cGroup).getName());
127: }
128:
129: public void testAppendChildren() {
130: IMessageDefinition msgDef = mdlDoc.getAllMessageDefinitions()[0];
131: IContentGroup gNode = (IContentGroup) msgDef.getChildren()[3];
132:
133: // append an element
134: IElementDefinition aElementDef = new ElementDefinitionImpl();
135: aElementDef.setName("append");
136: aElementDef.setNamespaceURI("com.append");
137: IContentElement cElement = new ContentElementImpl();
138: cElement.setElementDefinition(aElementDef);
139: gNode.appendChild(cElement);
140: assertEquals(3, gNode.getChildCount());
141:
142: //append a group
143: IContentGroup cGroup = new ContentGroupImpl();
144: cGroup.setName("appendG");
145: cGroup.setNamespaceURI("com.appendG");
146: gNode.appendChild(cGroup);
147: assertEquals(4, gNode.getChildCount());
148:
149: // get the element we appended
150: IContentNode cNode[] = gNode.getChildren();
151: for (int i = 0; i < cNode.length; i++) {
152: if (cNode[i] instanceof IContentElement) {
153: IContentElement gcElement = (IContentElement) cNode[i];
154: IElementDefinition cElementDef = gcElement
155: .getElementDefinition();
156: if ((cElementDef.getName()).equals("append")) {
157: assertEquals("com.append", cElementDef
158: .getNamespaceURI());
159: }
160: }
161: }
162:
163: //get the group we appended
164: for (int j = 0; j < cNode.length; j++) {
165: if (cNode[j] instanceof IContentGroup) {
166: IContentGroup gcGroup = (IContentGroup) cNode[j];
167: if (gcGroup.getName().equals("appendG")) {
168: assertEquals("com.appendG", gcGroup
169: .getNamespaceURI());
170: }
171: }
172: }
173: }
174:
175: public void testInsertAndRemoveChildAtIndex() {
176: IMessageDefinition msgDef = mdlDoc.getAllMessageDefinitions()[0];
177: IContentGroup gNode = (IContentGroup) msgDef.getChildren()[3];
178: IElementDefinition elementDef = new ElementDefinitionImpl();
179: elementDef.setName("index");
180: elementDef.setNamespaceURI("com.index");
181: IContentElement cElement = new ContentElementImpl();
182: cElement.setElementDefinition(elementDef);
183: //insert child at index 0
184: gNode.insertChildAtIndex(cElement, 0);
185: assertEquals(3, gNode.getChildCount());
186: //get the element at index 0
187: IElementDefinition gElement = ((IContentElement) gNode
188: .getChildren()[0]).getElementDefinition();
189: assertEquals("index", gElement.getName());
190: assertEquals("com.index", gElement.getNamespaceURI());
191:
192: //remove child at index 0
193: gNode.removeChildAtIndex(0);
194: assertEquals(2, gNode.getChildCount());
195:
196: }
197: }
|