01: package com.bostechcorp.cbesb.runtime.parser;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.FileOutputStream;
06: import java.io.InputStream;
07:
08: import javax.xml.transform.OutputKeys;
09: import javax.xml.transform.Transformer;
10: import javax.xml.transform.TransformerException;
11: import javax.xml.transform.TransformerFactory;
12: import javax.xml.transform.dom.DOMSource;
13: import javax.xml.transform.stream.StreamResult;
14:
15: import junit.framework.TestCase;
16:
17: import org.w3c.dom.Document;
18:
19: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
20: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
21: import com.bostechcorp.cbesb.common.mdl.util.FormatDefUtil;
22: import com.bostechcorp.cbesb.common.util.FileUtil;
23: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataParser;
24: import com.bostechcorp.cbesb.runtime.parser.impl.LegacyDataSerializer;
25:
26: public class TestVariable2 extends TestCase {
27:
28: public void testVariable2() throws Throwable {
29: File mdlFile = new File("target/test-data/in/Variable2a.mdl");
30: File dataFile = new File("target/test-data/in/Variable1b.dat");
31: File outputFile = new File(
32: "target/test-data/out/Variable2b.txt");
33:
34: IMDLDocument mdlDoc = FormatDefUtil.getMdlDocFromPath(mdlFile
35: .getAbsolutePath());
36:
37: IMessageDefinition msgDef = mdlDoc.getMessageDefinition(mdlDoc
38: .getTargetNamespace(), mdlDoc
39: .getAllMessageDefinitions()[0].getName());
40:
41: Document domDoc = null;
42: LegacyDataParser parser = new LegacyDataParser();
43: try {
44: InputStream is = new FileInputStream(dataFile);
45: domDoc = parser.parse(is, msgDef);
46: } catch (Exception pe) {
47: System.err.println("Error Parsing:");
48: System.err.println(pe.getMessage());
49: System.exit(0);
50: }
51:
52: try {
53: TransformerFactory tFactory = TransformerFactory
54: .newInstance();
55: Transformer transformer = tFactory.newTransformer();
56: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
57: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
58: DOMSource source = new DOMSource(domDoc);
59: StreamResult result = new StreamResult(System.out);
60: transformer.transform(source, result);
61: } catch (TransformerException te) {
62: System.err.println("Error Writing Output");
63: System.err.println(te.getMessage());
64: System.exit(0);
65: }
66:
67: try {
68: LegacyDataSerializer serializer = new LegacyDataSerializer();
69: FileOutputStream fio = new FileOutputStream(outputFile);
70: serializer.serialize(domDoc, msgDef, System.out);
71: serializer.serialize(domDoc, msgDef, fio);
72: String rsuleString = FileUtil
73: .readStringFromFile("target/test-data/out/Variable2b.txt");
74: String beginString = FileUtil
75: .readStringFromFile("target/test-data/in/Variable1b.dat");
76: assertTrue(rsuleString.equals(beginString));
77: } catch (Exception e) {
78: System.err.println("Error Serializer:");
79: System.err.println(e.getMessage());
80: System.exit(0);
81: }
82: }
83: }
|