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: * $ID$
024: */
025: package com.bostechcorp.cbesb.common.mdl;
026:
027: import java.io.File;
028:
029: import com.bostechcorp.cbesb.common.mdl.IElementDefinition;
030: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
031: import com.bostechcorp.cbesb.common.mdl.MDLParser;
032: import com.bostechcorp.cbesb.common.mdl.impl.ElementDefinitionImpl;
033: import com.bostechcorp.cbesb.common.mdl.impl.MDLDocumentImpl;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * test get/add/remove elementDefinition.
039: * Element definitions can be defined globally so they can be used by
040: * multiple message or element definitions, or locally.
041: * The definition of an elementmust contain a name and either a format or
042: * datatype.
043: * name required.
044: */
045: public class TestProcessElementDefinition extends TestCase {
046:
047: File mdlFile = new File("target/test-data/in/variable2a.mdl");
048: File mdlFile3 = new File("target/test-data/in/group1.mdl");
049: File mdlFile5 = new File("target/test-data/in/fixed1.mdl");
050:
051: IMDLDocument mdlDoc = new MDLDocumentImpl();
052: IMDLDocument mdlDoc3 = new MDLDocumentImpl();
053: IMDLDocument mdlDoc5 = new MDLDocumentImpl();
054:
055: protected void setUp() throws Exception {
056: super .setUp();
057: mdlDoc = MDLParser.load(mdlFile);
058: mdlDoc3 = MDLParser.load(mdlFile3);
059: mdlDoc5 = MDLParser.load(mdlFile5);
060: }
061:
062: protected void tearDown() throws Exception {
063: super .tearDown();
064: }
065:
066: public void testGetAllElementDefinition() {
067: //All the elements in mdlDoc5 are locally,so if we get allElementDefinition,
068: //we can not get them.
069: IElementDefinition elementDef[] = mdlDoc5
070: .getAllElementDefinitions();
071: int length = elementDef.length;
072: assertEquals(0, length);
073:
074: //mdlDoc3 has glovally elements,so we can get them with getAllElementDefinition
075: //the global element count is 6
076: IElementDefinition elementDef2[] = mdlDoc3
077: .getAllElementDefinitions();
078: assertEquals(6, elementDef2.length);
079:
080: //all elements must has a name
081: for (int i = 0; i < elementDef2.length; i++) {
082: assertNotNull(elementDef2[i].getName());
083: }
084: }
085:
086: public void testGetAllVisibleElementDefinition() {
087: //getAllVisibleElementdefinition can get global element
088: IElementDefinition elementDef[] = mdlDoc3
089: .getAllVisibleElementDefinitions();
090: assertEquals(6, elementDef.length);
091:
092: //getAllVisibleElementdefinition can get element in other mdl file that referenced.
093: IElementDefinition elementDef2[] = mdlDoc
094: .getAllVisibleElementDefinitions();
095: assertEquals(6, elementDef2.length);
096: }
097:
098: public void testGetElementDefinition() {
099: IElementDefinition elementDef[] = mdlDoc3
100: .getAllElementDefinitions();
101: IElementDefinition element = elementDef[0];
102: String elementName = element.getName();
103: String namespaceURI = mdlDoc3.getTargetNamespace();
104:
105: //get elementDefinition with name and namespace
106: //can only get global element
107: IElementDefinition gElement = mdlDoc3.getElementDefinition(
108: namespaceURI, elementName);
109: assertEquals(elementDef[0], gElement);
110: }
111:
112: public void testAddElementDefinition() {
113: IElementDefinition addElementDef = new ElementDefinitionImpl();
114: addElementDef.setName("add");
115: addElementDef.setNamespaceURI("com.add");
116: //add element
117: mdlDoc3.addElementDefinition(addElementDef);
118: assertEquals(7, mdlDoc3.getAllElementDefinitions().length);
119: IElementDefinition gElementDef = mdlDoc3.getElementDefinition(
120: "com.add", "add");
121: assertEquals(addElementDef, gElementDef);
122:
123: }
124:
125: public void testRemoveElementDefinition() {
126: IElementDefinition elementDef[] = mdlDoc3
127: .getAllElementDefinitions();
128: assertEquals(6, elementDef.length);
129: IElementDefinition rElementDef = elementDef[4];
130: String rName = rElementDef.getName();
131: String rNamespace = rElementDef.getNamespaceURI();
132: //remove element
133: mdlDoc3.removeElementDefinition(rNamespace, rName);
134: assertEquals(5, mdlDoc3.getAllElementDefinitions().length);
135: }
136: }
|