|
1 | 1 | package com.baeldung.test; |
2 | 2 |
|
3 | | -import org.junit.Assert; |
4 | | -import org.junit.Before; |
5 | | -import org.junit.Test; |
6 | | - |
7 | 3 | import com.baeldung.initializer.SimpleXstreamInitializer; |
8 | 4 | import com.baeldung.pojo.ContactDetails; |
9 | 5 | import com.baeldung.pojo.Customer; |
10 | 6 | import com.baeldung.utility.SimpleDataGeneration; |
11 | 7 | import com.thoughtworks.xstream.XStream; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
12 | 11 |
|
13 | 12 | public class XStreamJsonHierarchicalTest { |
14 | 13 |
|
15 | | - private Customer customer = null; |
16 | | - private String dataJson = null; |
17 | | - private XStream xstream = null; |
18 | | - |
19 | | - @Before |
20 | | - public void dataSetup() { |
21 | | - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); |
22 | | - xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance(); |
23 | | - xstream.processAnnotations(Customer.class); |
24 | | - } |
25 | | - |
26 | | - @Test |
27 | | - public void convertObjectToJson() { |
28 | | - customer = SimpleDataGeneration.generateData(); |
29 | | - xstream.alias("customer", Customer.class); |
30 | | - xstream.alias("contactDetails", ContactDetails.class); |
31 | | - xstream.aliasField("fn", Customer.class, "firstName"); |
32 | | - dataJson = xstream.toXML(customer); |
33 | | - System.out.println(dataJson); |
34 | | - Assert.assertNotNull(dataJson); |
35 | | - } |
36 | | - |
37 | | - @Test |
38 | | - public void convertJsonToObject() { |
39 | | - customer = SimpleDataGeneration.generateData(); |
40 | | - dataJson = xstream.toXML(customer); |
41 | | - // customer = (Customer) xstream.fromXML(dataJson); |
42 | | - // Assert.assertNotNull(customer); |
43 | | - } |
| 14 | + private Customer customer = null; |
| 15 | + private String dataJson = null; |
| 16 | + private XStream xstream = null; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void dataSetup() { |
| 20 | + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); |
| 21 | + xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance(); |
| 22 | + xstream.processAnnotations(Customer.class); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void convertObjectToJson() { |
| 27 | + customer = SimpleDataGeneration.generateData(); |
| 28 | + xstream.alias("customer", Customer.class); |
| 29 | + xstream.alias("contactDetails", ContactDetails.class); |
| 30 | + xstream.aliasField("fn", Customer.class, "firstName"); |
| 31 | + dataJson = xstream.toXML(customer); |
| 32 | + System.out.println(dataJson); |
| 33 | + Assert.assertNotNull(dataJson); |
| 34 | + } |
| 35 | + |
| 36 | + @Test(expected = UnsupportedOperationException.class) |
| 37 | + public void convertJsonToObject() { |
| 38 | + customer = SimpleDataGeneration.generateData(); |
| 39 | + dataJson = xstream.toXML(customer); |
| 40 | + customer = (Customer) xstream.fromXML(dataJson); |
| 41 | + Assert.assertNotNull(customer); |
| 42 | + } |
44 | 43 |
|
45 | 44 | } |
0 commit comments