How to use marshall method of com.qaprosoft.carina.core.foundation.utils.marshaller.MarshallerHelper class

Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.marshaller.MarshallerHelper.marshall

Source:XmlUtilsTest.java Github

copy

Full Screen

...14 * limitations under the License.15 *******************************************************************************/16package com.qaprosoft.carina.core.utils;17import com.qaprosoft.carina.core.foundation.utils.XmlFormatter;18import com.qaprosoft.carina.core.foundation.utils.marshaller.MarshallerHelper;19import com.qaprosoft.carina.core.foundation.utils.marshaller.exception.ParserException;20import org.testng.Assert;21import org.testng.annotations.Test;22import javax.xml.bind.annotation.*;23import javax.xml.transform.Source;24import javax.xml.transform.stream.StreamSource;25import java.io.*;26import java.nio.charset.StandardCharsets;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.Arrays;31import java.util.List;32import java.util.Objects;33public class XmlUtilsTest {34 private static final String XML_PATH = "src/test/resources/xml/testXml.xml";35 private static final String XML_FORMATTER_PATH = "src/test/resources/xml/testFormatterXml.xml";36 private static final Member MEMBER1 = new Member("Molecule Man", 29);37 private static final Member MEMBER2 = new Member("Madame Uppercut", 39);38 private static final Member MEMBER3 = new Member("Eternal Flame", 25);39 // Person is class without @XmlRootElement40 private static final Person PERSON = new Person("Jhon Eniston");41 private static final City CITY = new City("Metro City", 2016, true, new Members(Arrays.asList(MEMBER1, MEMBER2, MEMBER3)));42 private static final String WRONG_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><city><name>Metro City</name>";43 @Test44 public void testParserExceptionWithText() {45 try {46 throw new ParserException("Can't parse");47 } catch (ParserException e) {48 Assert.assertEquals(e.getMessage(), "Can't parse", "Message wasn't overridden in " + e.getClass().getName());49 }50 }51 @Test52 public void testParserExceptionWithTextAndThrowable() {53 try {54 throw new ParserException("Can't parse", new RuntimeException());55 } catch (ParserException e) {56 Assert.assertEquals(e.getMessage(), "Can't parse", "Message wasn't overridden in " + e.getClass().getName());57 }58 }59 @Test60 public void testXmlFormatter() {61 String xmlStr = MarshallerHelper.marshall(CITY);62 String actualFormatterXmlStr = XmlFormatter.prettyPrint(xmlStr);63 String expectedFormatterXmlStr = readFile(XML_FORMATTER_PATH);64 Assert.assertEquals(actualFormatterXmlStr, expectedFormatterXmlStr, "Xml string wasn't formatted properly");65 }66 @Test67 public void testXmlFormatterWithEmptyXml() {68 String actualFormatterXmlStr = XmlFormatter.prettyPrint("");69 Assert.assertEquals(actualFormatterXmlStr, "", "Xml string isn't empty");70 }71 @Test72 public void testXmlFormatterWithWrongXml() {73 String actualFormatterXmlStr = XmlFormatter.prettyPrint(WRONG_XML);74 Assert.assertEquals(actualFormatterXmlStr, WRONG_XML, "Wrong xml string was formatted");75 }76 private String readFile(String pathStr) {77 Path path = Paths.get(pathStr);78 byte[] bytes = null;79 try {80 bytes = Files.readAllBytes(path);81 } catch (IOException ex) {82 // Handle exception83 }84 return new String(bytes, StandardCharsets.UTF_16);85 }86 @Test87 public void testMarshallUnmarshall() {88 String cityXmlStr = MarshallerHelper.marshall(CITY);89 City actualCity = MarshallerHelper.unmarshall(cityXmlStr, City.class);90 Assert.assertEquals(actualCity, CITY, actualCity.getName() + " is different than " + CITY.getName());91 }92 @Test(expectedExceptions = RuntimeException.class)93 public void testUnmarshallThrowRuntimeException() {94 String cityXmlStr = MarshallerHelper.marshall(CITY);95 MarshallerHelper.unmarshall(cityXmlStr, Member.class);96 }97 @Test(expectedExceptions = RuntimeException.class)98 public void testMarshallThrowRuntimeException() {99 MarshallerHelper.marshall(PERSON);100 }101 102 @Test103 public void testMarshallUnmarshallFile() {104 File xmlFile = new File(XML_PATH);105 MarshallerHelper.marshall(CITY, xmlFile);106 City actualCity = MarshallerHelper.unmarshall(xmlFile, City.class);107 Assert.assertEquals(actualCity, CITY, actualCity.getName() + " is different than " + CITY.getName());108 }109 @Test(expectedExceptions = RuntimeException.class)110 public void testUnmarshallFileThrowRuntimeException() {111 File xmlFile = new File(XML_PATH);112 MarshallerHelper.marshall(CITY, xmlFile);113 MarshallerHelper.unmarshall(xmlFile, Member.class);114 }115 @Test(expectedExceptions = RuntimeException.class)116 public void testMarshallFileThrowRuntimeException() {117 File xmlFile = new File(XML_PATH);118 MarshallerHelper.marshall(PERSON, xmlFile);119 }120 @Test121 public void testMarshallUnmarshallInputStream() {122 File xmlFile = new File(XML_PATH);123 try {124 OutputStream fos = new FileOutputStream(xmlFile);125 MarshallerHelper.marshall(CITY, fos);126 InputStream fis = new FileInputStream(xmlFile);127 City actualCity = MarshallerHelper.unmarshall(fis, City.class);128 Assert.assertEquals(actualCity, CITY, actualCity.getName() + " is different than " + CITY.getName());129 } catch (FileNotFoundException e) {130 Assert.fail(e.getMessage(), e);131 }132 }133 @Test(expectedExceptions = RuntimeException.class)134 public void testUnmarshallInputStreamThrowRuntimeException() {135 File xmlFile = new File(XML_PATH);136 try {137 OutputStream fos = new FileOutputStream(xmlFile);138 MarshallerHelper.marshall(CITY, fos);139 InputStream fis = new FileInputStream(xmlFile);140 MarshallerHelper.unmarshall(fis, Member.class);141 } catch (FileNotFoundException e) {142 Assert.fail(e.getMessage(), e);143 }144 }145 @Test(expectedExceptions = RuntimeException.class)146 public void testMarshallOutputStreamThrowRuntimeException() {147 File xmlFile = new File(XML_PATH);148 try {149 OutputStream fos = new FileOutputStream(xmlFile);150 MarshallerHelper.marshall(PERSON, fos);151 } catch (FileNotFoundException e) {152 Assert.fail(e.getMessage(), e);153 }154 }155 @Test156 public void testMarshallUnmarshallSource() {157 File xmlFile = new File(XML_PATH);158 MarshallerHelper.marshall(CITY, xmlFile);159 Source source = new StreamSource(xmlFile);160 City actualCity = MarshallerHelper.unmarshall(source, City.class);161 Assert.assertEquals(actualCity, CITY, actualCity.getName() + " is different than " + CITY.getName());162 }163 @Test(expectedExceptions = RuntimeException.class)164 public void testUnmarshallSourceThrowRuntimeException() {165 File xmlFile = new File(XML_PATH);166 MarshallerHelper.marshall(CITY, xmlFile);167 Source source = new StreamSource(xmlFile);168 MarshallerHelper.unmarshall(source, Member.class);169 }170 @Test171 public void testMarshallUnmarshallWriter() {172 File xmlFile = new File(XML_PATH);173 try {174 Writer writer = new FileWriter(xmlFile);175 MarshallerHelper.marshall(CITY, writer);176 City actualCity = MarshallerHelper.unmarshall(xmlFile, City.class);177 Assert.assertEquals(actualCity, CITY, actualCity.getName() + " is different than " + CITY.getName());178 } catch (IOException e) {179 Assert.fail(e.getMessage(), e);180 }181 }182 @Test(expectedExceptions = RuntimeException.class)183 public void testMarshallWriterThrowRuntimeException() {184 File xmlFile = new File(XML_PATH);185 try {186 Writer writer = new FileWriter(xmlFile);187 MarshallerHelper.marshall(PERSON, writer);188 } catch (IOException e) {189 Assert.fail(e.getMessage(), e);190 }191 }192 @XmlRootElement(name = "city")193 private static class City implements Serializable {194 private String name;195 private int formed;196 private boolean active;197 private Members members;198 public City(String name, int formed, boolean active, Members members) {199 this.name = name;200 this.formed = formed;201 this.active = active;...

Full Screen

Full Screen

Source:MarshallerHelper.java Github

copy

Full Screen

...12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package com.qaprosoft.carina.core.foundation.utils.marshaller;17import java.io.File;18import java.io.InputStream;19import java.io.OutputStream;20import java.io.Writer;21import javax.xml.transform.Result;22import javax.xml.transform.Source;23import javax.xml.transform.stream.StreamResult;24public class MarshallerHelper {25 /**26 * Marshaller/Unmarshaller implementation27 */28 private static final Marshaller marshaller = Marshaller.getInstance();29 public static void marshall(Object jaxbElement, Result paramResult) {30 marshaller.marshall(jaxbElement, paramResult);31 }32 /**33 * Serializes JAXBElement into File34 * 35 * @param jaxbElement36 * - JAXBElement37 * @param resultFile38 * - File39 */40 public static void marshall(Object jaxbElement, File resultFile) {41 marshall(jaxbElement, new StreamResult(resultFile));42 }43 /**44 * Serializes JAXBElement to OutputStream45 * 46 * @param jaxbElement47 * - JAXBElement48 * @param os49 * - OutputStream50 */51 public static void marshall(Object jaxbElement, OutputStream os) {52 marshall(jaxbElement, new StreamResult(os));53 }54 /**55 * Serializes JAXBElement to String56 * 57 * @param jaxbElement58 * - JAXBElement59 * @return String60 */ 61 public static String marshall(Object jaxbElement){62 return marshaller.marshall(jaxbElement);63 }64 65 /**66 * Create JAXBElement from Source67 * 68 * @param <T> Generic69 * @param source Source70 * @param resultClazz expected class71 * 72 * @return T &lt;T&gt;73 */74 public static <T> T unmarshall(Source source, Class<T> resultClazz) {75 return marshaller.unmarshall(source, resultClazz);76 }77 /**78 * Create JAXBElement from File79 * 80 * @param <T> Generic81 * @param file File82 * @param resultClazz expected class83 * 84 * @return T &lt;T&gt;85 */86 public static <T> T unmarshall(File file, Class<T> resultClazz) {87 return marshaller.unmarshall(file, resultClazz);88 }89 /**90 * Create JAXBElement from File91 * 92 * @param <T> Generic93 * @param is Input Stream94 * @param resultClazz expected class95 * 96 * @return T &lt;T&gt;97 */98 public static <T> T unmarshall(InputStream is, Class<T> resultClazz) {99 return marshaller.unmarshall(is, resultClazz);100 }101 public static <T> T unmarshall(String string, Class<T> resultClazz) {102 return marshaller.unmarshall(string, resultClazz);103 }104 /**105 * Serializes JAXBElement into Writer106 * 107 * @param jaxbElement jaxbElement108 * @param writer writer109 */110 public static void marshall(Object jaxbElement, Writer writer) {111 marshaller.marshall(jaxbElement, writer);112 }113}...

Full Screen

Full Screen

marshall

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.core.foundation.utils.marshaller;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.testng.Assert;7import org.testng.annotations.Test;8import com.qaprosoft.carina.core.foundation.utils.R;9public class MarshallerTest {10 public void testMarshall() throws IOException {11 List<String> list = new ArrayList<String>();12 list.add("one");13 list.add("two");14 list.add("three");15 list.add("four");16 list.add("five");17 list.add("six");18 list.add("seven");19 list.add("eight");20 list.add("nine");21 list.add("ten");22 MarshallerHelper.marshall(list, new File(R.TESTDATA.get("marshaller.path")));23 }24 @Test(dependsOnMethods = "testMarshall")25 public void testUnmarshall() throws IOException {26 List<String> list = MarshallerHelper.unmarshall(List.class, new File(R.TESTDATA.get("marshaller.path")));27 Assert.assertNotNull(list, "Failed to unmarshall list!");28 }29}30package com.qaprosoft.carina.core.foundation.utils.marshaller;31import java.io.File;32import java.io.IOException;33import java.util.ArrayList;34import java.util.List;35import org.testng.Assert;36import org.testng.annotations.Test;37import com.qaprosoft.carina.core.foundation.utils.R;38public class MarshallerTest {39 public void testMarshall() throws IOException {40 List<String> list = new ArrayList<String>();41 list.add("one");42 list.add("two");43 list.add("three");44 list.add("four");45 list.add("five");46 list.add("six");47 list.add("seven");48 list.add("eight");49 list.add("nine");50 list.add("ten");51 MarshallerHelper.marshall(list, new File(R.TESTDATA.get("marshaller.path")));52 }53 @Test(dependsOnMethods = "testMarshall")54 public void testUnmarshall() throws IOException {55 List<String> list = MarshallerHelper.unmarshall(List.class, new File(R.TESTDATA.get("marshaller.path")));56 Assert.assertNotNull(list, "Failed to unmarshall list!");57 }58}

Full Screen

Full Screen

marshall

Using AI Code Generation

copy

Full Screen

1import org.testng.Assert;2import org.testng.annotations.Test;3import com.qaprosoft.carina.core.foundation.utils.marshaller.MarshallerHelper;4public class MarshallerTest {5 public void testMarshall() {6 String json = MarshallerHelper.marshall(new User("John", "Doe", 25, "

Full Screen

Full Screen

marshall

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.core.foundation.utils.marshaller;2import org.testng.Assert;3import org.testng.annotations.Test;4public class MarshallerHelperTest {5 public void testMarshall() throws Exception{6 Test test = new Test();7 test.setName("test");8 test.setValue("value");9 String xml = MarshallerHelper.marshall(test);10 Test test2 = (Test) MarshallerHelper.unmarshall(xml, Test.class);11 Assert.assertEquals(test.getName(), test2.getName());12 Assert.assertEquals(test.getValue(), test2.getValue());13 }14 public static class Test{15 private String name;16 private String value;17 public String getName() {18 return name;19 }20 public void setName(String name) {21 this.name = name;22 }23 public String getValue() {24 return value;25 }26 public void setValue(String value) {27 this.value = value;28 }29 }30}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Carina automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in MarshallerHelper

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful