|
17 | 17 |
|
18 | 18 | public class TestXMLSerializeDeserialize { |
19 | 19 |
|
20 | | - @Test |
21 | | - public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException{ |
22 | | - XmlMapper xmlMapper = new XmlMapper(); |
23 | | - String xml = xmlMapper.writeValueAsString(new SimpleBean()); |
24 | | - assertNotNull(xml); |
25 | | - } |
26 | | - @Test |
27 | | - public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException{ |
28 | | - XmlMapper xmlMapper = new XmlMapper(); |
29 | | - xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean()); |
30 | | - File file=new File("simple_bean.xml"); |
31 | | - assertNotNull(file); |
32 | | - } |
33 | | - @Test |
34 | | - public void whenJavaGotFromXmlStr_thenCorrect() throws IOException { |
35 | | - XmlMapper xmlMapper = new XmlMapper(); |
36 | | - SimpleBean value = xmlMapper.readValue( |
37 | | - "<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class); |
38 | | - assertTrue(value.getX() == 1 && value.getY() == 2); |
39 | | - } |
40 | | - @Test |
41 | | - public void whenJavaGotFromXmlFile_thenCorrect() throws IOException { |
42 | | - File file = new File("simple_bean.xml"); |
43 | | - XmlMapper xmlMapper = new XmlMapper(); |
44 | | - String xml = inputStreamToString(new FileInputStream(file)); |
45 | | - SimpleBean value = xmlMapper.readValue(xml,SimpleBean.class); |
46 | | - assertTrue(value.getX() == 1 && value.getY() == 2); |
47 | | - } |
48 | | - |
49 | | - public static String inputStreamToString(InputStream is) throws IOException { |
50 | | - BufferedReader br = null; |
51 | | - StringBuilder sb = new StringBuilder(); |
52 | | - |
53 | | - String line; |
54 | | - br = new BufferedReader(new InputStreamReader(is)); |
55 | | - while ((line = br.readLine()) != null) { |
56 | | - sb.append(line); |
57 | | - } |
58 | | - br.close(); |
59 | | - return sb.toString(); |
60 | | - |
61 | | - } |
| 20 | + @Test |
| 21 | + public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException { |
| 22 | + XmlMapper xmlMapper = new XmlMapper(); |
| 23 | + String xml = xmlMapper.writeValueAsString(new SimpleBean()); |
| 24 | + assertNotNull(xml); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException { |
| 29 | + XmlMapper xmlMapper = new XmlMapper(); |
| 30 | + xmlMapper.writeValue(new File("target/simple_bean.xml"), new SimpleBean()); |
| 31 | + File file = new File("target/simple_bean.xml"); |
| 32 | + assertNotNull(file); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void whenJavaGotFromXmlStr_thenCorrect() throws IOException { |
| 37 | + XmlMapper xmlMapper = new XmlMapper(); |
| 38 | + SimpleBean value = xmlMapper.readValue( |
| 39 | + "<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class); |
| 40 | + assertTrue(value.getX() == 1 && value.getY() == 2); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenJavaGotFromXmlFile_thenCorrect() throws IOException { |
| 45 | + File file = new File("src/test/resources/simple_bean.xml"); |
| 46 | + XmlMapper xmlMapper = new XmlMapper(); |
| 47 | + String xml = inputStreamToString(new FileInputStream(file)); |
| 48 | + SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class); |
| 49 | + assertTrue(value.getX() == 1 && value.getY() == 2); |
| 50 | + } |
| 51 | + |
| 52 | + private static String inputStreamToString(InputStream is) throws IOException { |
| 53 | + BufferedReader br; |
| 54 | + StringBuilder sb = new StringBuilder(); |
| 55 | + |
| 56 | + String line; |
| 57 | + br = new BufferedReader(new InputStreamReader(is)); |
| 58 | + while ((line = br.readLine()) != null) { |
| 59 | + sb.append(line); |
| 60 | + } |
| 61 | + br.close(); |
| 62 | + return sb.toString(); |
| 63 | + } |
62 | 64 | } |
63 | 65 |
|
64 | 66 | class SimpleBean { |
65 | | - private int x = 1; |
66 | | - private int y = 2; |
67 | | - public int getX() { |
68 | | - return x; |
69 | | - } |
70 | | - public void setX(int x) { |
71 | | - this.x = x; |
72 | | - } |
73 | | - public int getY() { |
74 | | - return y; |
75 | | - } |
76 | | - public void setY(int y) { |
77 | | - this.y = y; |
78 | | - } |
79 | | - |
| 67 | + private int x = 1; |
| 68 | + private int y = 2; |
| 69 | + |
| 70 | + public int getX() { |
| 71 | + return x; |
| 72 | + } |
| 73 | + |
| 74 | + public void setX(int x) { |
| 75 | + this.x = x; |
| 76 | + } |
| 77 | + |
| 78 | + public int getY() { |
| 79 | + return y; |
| 80 | + } |
| 81 | + |
| 82 | + public void setY(int y) { |
| 83 | + this.y = y; |
| 84 | + } |
| 85 | + |
80 | 86 | } |
0 commit comments