2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.rosetta.test;
24 import java.io.Reader;
25 import java.io.StringReader;
27 import org.junit.Test;
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.LogTarget;
30 import org.onap.aaf.misc.env.TimeTaken;
31 import org.onap.aaf.misc.env.Trans;
32 import org.onap.aaf.misc.env.Trans.Metric;
33 import org.onap.aaf.misc.env.impl.EnvFactory;
34 import org.onap.aaf.misc.env.jaxb.JAXBmar;
35 import org.onap.aaf.misc.env.jaxb.JAXBumar;
36 import org.onap.aaf.misc.env.util.StringBuilderWriter;
37 import org.onap.aaf.misc.rosetta.InXML;
38 import org.onap.aaf.misc.rosetta.Out;
39 import org.onap.aaf.misc.rosetta.OutJson;
40 import org.onap.aaf.misc.rosetta.OutRaw;
41 import org.onap.aaf.misc.rosetta.OutXML;
43 import s.xsd.LargerData;
45 public class JU_FromXML {
46 private static int ITERATIONS = 1;
49 private final static String xml =
50 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
51 "<LargerData xmlns=\"urn:s:xsd\">\n" +
53 " <id>sd object 1</id>\n" +
54 " <date>1346765355134</date>\n" +
55 " <item>Item 1.1</item>\n" +
56 " <item>Item 1.2</item>\n" +
59 " <id>sd object 2</id>\n" +
60 " <date>1346765355134</date>\n" +
61 " <item>Item 2.1</item>\n" +
62 " <item>Item 2.2</item>\n" +
64 " <fluff>MyFluff</fluff>\n" +
69 public void test() throws Exception {
70 InXML inXML = new InXML(LargerData.class);
72 System.out.println(xml);
73 StringBuilderWriter sbw = new StringBuilderWriter(1024);
75 Reader rdr = new StringReader(xml);
77 new OutRaw().extract(rdr, sbw, inXML);
78 System.out.println(sbw.getBuffer());
83 public void xml2JSON() throws Exception {
84 System.out.println("*** XML -> JSON (No Warm up) ***");
85 Out jout = new OutJson();
86 InXML inXML = new InXML(LargerData.class);
88 StringBuilderWriter sbw = new StringBuilderWriter(1024);
91 Report report = new Report(ITERATIONS,"XML");
94 trans = EnvFactory.newTrans();
95 Reader sr = new StringReader(xml);
96 TimeTaken tt = trans.start("Parse XML", Env.XML);
98 jout.extract(sr, sbw, inXML);
102 report.glean(trans,Env.XML);
103 } while (report.go());
106 System.out.println(sbw.toString());
110 public void xml2XML() throws Exception {
111 System.out.println("*** XML -> (Event Queue) -> XML (No Warm up) ***");
112 Out xout = new OutXML("LargerData");
113 InXML inXML = new InXML(LargerData.class);
115 StringBuilderWriter sbw = new StringBuilderWriter(1024);
118 Report report = new Report(ITERATIONS,"XML");
121 trans = EnvFactory.newTrans();
122 Reader sr = new StringReader(xml);
123 TimeTaken tt = trans.start("Parse XML", Env.XML);
125 xout.extract(sr, sbw, inXML);
129 report.glean(trans,Env.XML);
130 } while (report.go());
133 System.out.println(sbw.toString());
138 public void warmup() throws Exception {
140 System.out.println("*** Warmup JAXB ***");
142 JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
143 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
144 //jaxBmar.asFragment(true);
145 //jaxBmar.pretty(true);
146 StringBuilderWriter sbw = new StringBuilderWriter(1024);
151 Report report = new Report(ITERATIONS,"XML");
154 trans = EnvFactory.newTrans();
155 TimeTaken all = trans.start("Combo", Env.SUB);
157 TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
159 ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
163 tt = trans.start("JAXB marshal", Env.XML);
165 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
172 report.glean(trans,Env.XML);
173 } while (report.go());
176 System.out.println(sbw.toString());
180 public void xml2jaxb2xml() throws Exception {
181 System.out.println("*** XML -> JAXB Object -> XML ***");
182 JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
183 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
184 //jaxBmar.asFragment(true);
185 //jaxBmar.pretty(true);
186 StringBuilderWriter sbw = new StringBuilderWriter(1024);
190 Report report = new Report(ITERATIONS,"XML");
193 trans = EnvFactory.newTrans();
194 TimeTaken all = trans.start("Combo", Env.SUB);
196 TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
198 ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
202 tt = trans.start("JAXB marshal", Env.XML);
204 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
211 report.glean(trans,Env.XML);
212 } while (report.go());
215 System.out.println(sbw.toString()); }
218 public void xml2jaxb2PrettyXml() throws Exception {
219 System.out.println("*** XML -> JAXB Object -> Pretty XML ***");
220 JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
221 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
222 //jaxBmar.asFragment(true);
223 jaxBmar.pretty(true);
224 StringBuilderWriter sbw = new StringBuilderWriter(1024);
226 Trans trans = EnvFactory.newTrans();
228 for (int i=0;i<ITERATIONS;++i) {
230 TimeTaken all = trans.start("Combo", Env.SUB);
232 TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
234 ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
238 tt = trans.start("JAXB marshal", Env.XML);
240 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
251 m = trans.auditTrail(0,null);
253 m = trans.auditTrail(0, sbw.getBuffer());
254 System.out.println(sbw.getBuffer());
256 System.out.println(ITERATIONS + " entries, Total Time: " + m.total + "ms, Avg Time: " + m.total/ITERATIONS + "ms");