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: */
025: /*
026: * $Header: /home/projects/mule/scm/mule/mule/src/test/java/org/mule/test/config/MessagesTestCase.java,v 1.4 2005/06/23 08:01:27 gnt Exp $
027: * $Revision: 1.4 $
028: * $Date: 2005/06/23 08:01:27 $
029: * ------------------------------------------------------------------------------------------------------
030: *
031: * Copyright (c) SymphonySoft Limited. All rights reserved.
032: * http://www.symphonysoft.com
033: *
034: * The software in this package is published under the terms of the BSD
035: * style license a copy of which has been included with this distribution in
036: * the LICENSE.txt file.
037: */
038: package com.bostechcorp.cbesb.common.trn.test;
039:
040: import java.io.File;
041: import java.io.IOException;
042: import java.util.ArrayList;
043:
044: import junit.framework.TestCase;
045:
046: import org.apache.xmlbeans.XmlError;
047: import org.apache.xmlbeans.XmlException;
048: import org.apache.xmlbeans.XmlObject;
049: import org.apache.xmlbeans.XmlOptions;
050:
051: import com.bostechcorp.cbesb.trn.x10.*;
052: import com.bostechcorp.cbesb.trn.x10.TransformerControlDocument;
053:
054: public class TrnParserTest extends TestCase {
055:
056: TransformerControlDocument doc;
057: String controlFileName = "target/test-data/config/test.trn";
058:
059: public TrnParserTest() {
060: super ();
061:
062: }
063:
064: public TransformerControlDocument parseXml(String file) {
065: // Get the XML instance into a file using the path provided.
066: File xmlfile = new File(file);
067:
068: try {
069: // Parse the instance into the type generated from the schema.
070: doc = TransformerControlDocument.Factory.parse(xmlfile);
071: } catch (XmlException e) {
072: e.printStackTrace();
073: } catch (IOException e) {
074: e.printStackTrace();
075: }
076: return doc;
077: }
078:
079: public boolean validateXml(XmlObject xml) {
080: boolean isXmlValid = false;
081:
082: // A collection instance to hold validation error messages.
083: ArrayList validationMessages = new ArrayList();
084:
085: // Validate the XML, collecting messages.
086: isXmlValid = xml.validate(new XmlOptions()
087: .setErrorListener(validationMessages));
088:
089: if (!isXmlValid) {
090: System.out.println("Invalid XML: ");
091: for (int i = 0; i < validationMessages.size(); i++) {
092: XmlError error = (XmlError) validationMessages.get(i);
093: System.out.println(error.getMessage());
094: System.out.println(error.getObjectLocation());
095: }
096: }
097: return isXmlValid;
098: }
099:
100: public void testTransferControllDocumentParser() throws Exception {
101: try {
102:
103: // TransformerControlDocument doc= TransformerControlDocument.Factory.parse(new File(controlFileName));
104:
105: doc = parseXml("");
106: assertTrue(validateXml(doc));
107:
108: // assertTrue(doc.getTransformerControl().getFormats().getInput().getFormat().equals("xsd"));
109:
110: TransformerControlDef tc = doc.getTransformerControl();
111:
112: FormatsDef f = tc.getFormats();
113:
114: System.out.println(tc.toString());
115:
116: System.err.println(tc.getOperationList());
117: System.err.println(f);
118: System.err.flush();
119: // System.err.println (tc.getFormats().toString());
120:
121: // assertTrue(doc.getTransformerControl().getFormats().getInput().getName().equals("target/test-data/config/CWInvoices.xsd"));
122:
123: } catch (Exception e) {
124: System.err.println("TranslateCompiler exception: " + e);
125: e.printStackTrace();
126: fail("unexpected expection: " + e.getMessage());
127: }
128: }
129:
130: public void testFilterParser() throws Exception {
131: try {
132:
133: // TransformerControlDocument doc= TransformerControlDocument.Factory.parse(new File(controlFileName));
134:
135: doc = parseXml("target/test-data/config/testFilter.trn");
136: // assertTrue (validateXml(doc));
137:
138: // assertTrue(doc.getTransformerControl().getFormats().getInput().getFormat().equals("xsd"));
139:
140: TransformerControlDef tc = doc.getTransformerControl();
141:
142: OperationListDef operList = tc.getOperationList();
143: OperationDef[] op = operList.getOperationArray();
144:
145: for (int i = 0; i < operList.sizeOfOperationArray(); i++) {
146: cycle(op[i]);
147: }
148: FormatsDef f = tc.getFormats();
149:
150: System.out.println(tc.toString());
151:
152: System.err.println(tc.getOperationList());
153: System.err.println(f);
154: System.err.flush();
155: // System.err.println (tc.getFormats().toString());
156:
157: // assertTrue(doc.getTransformerControl().getFormats().getInput().getName().equals("target/test-data/config/CWInvoices.xsd"));
158:
159: } catch (Exception e) {
160: System.err.println("TranslateCompiler exception: " + e);
161: e.printStackTrace();
162: fail("unexpected expection: " + e.getMessage());
163: }
164: }
165:
166: private void cycle(OperationDef op) {
167: if (op.getName().equals("copy")) {
168: ParamDef param = op.getParameters();
169: PropertyListDef[] prop = param.getPropertyListArray();
170: if (prop.length > 0) {
171: SettingDef opl = prop[0].getSettingArray(0);
172: System.out.println(opl.getValue());
173: }
174: }
175: OperationListDef operList = op.getOperationList();
176: if (operList != null && operList.sizeOfOperationArray() > 0) {
177: for (int i = 0; i < operList.sizeOfOperationArray(); i++) {
178: OperationDef oper = operList.getOperationArray(i);
179: cycle(oper);
180: }
181: }
182: }
183: }
|