xml-serialize-deserialize - minor changes

This commit is contained in:
Slavisa Avramovic 2016-05-22 11:45:07 +02:00
parent 61e7164703
commit 263d17142c

View File

@ -23,13 +23,15 @@ public class TestXMLSerializeDeserialize {
String xml = xmlMapper.writeValueAsString(new SimpleBean()); String xml = xmlMapper.writeValueAsString(new SimpleBean());
assertNotNull(xml); assertNotNull(xml);
} }
@Test @Test
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException { public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
XmlMapper xmlMapper = new XmlMapper(); XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean()); xmlMapper.writeValue(new File("target/simple_bean.xml"), new SimpleBean());
File file=new File("simple_bean.xml"); File file = new File("target/simple_bean.xml");
assertNotNull(file); assertNotNull(file);
} }
@Test @Test
public void whenJavaGotFromXmlStr_thenCorrect() throws IOException { public void whenJavaGotFromXmlStr_thenCorrect() throws IOException {
XmlMapper xmlMapper = new XmlMapper(); XmlMapper xmlMapper = new XmlMapper();
@ -37,17 +39,18 @@ public class TestXMLSerializeDeserialize {
"<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class); "<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class);
assertTrue(value.getX() == 1 && value.getY() == 2); assertTrue(value.getX() == 1 && value.getY() == 2);
} }
@Test @Test
public void whenJavaGotFromXmlFile_thenCorrect() throws IOException { public void whenJavaGotFromXmlFile_thenCorrect() throws IOException {
File file = new File("simple_bean.xml"); File file = new File("src/test/resources/simple_bean.xml");
XmlMapper xmlMapper = new XmlMapper(); XmlMapper xmlMapper = new XmlMapper();
String xml = inputStreamToString(new FileInputStream(file)); String xml = inputStreamToString(new FileInputStream(file));
SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class); SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class);
assertTrue(value.getX() == 1 && value.getY() == 2); assertTrue(value.getX() == 1 && value.getY() == 2);
} }
public static String inputStreamToString(InputStream is) throws IOException { private static String inputStreamToString(InputStream is) throws IOException {
BufferedReader br = null; BufferedReader br;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String line; String line;
@ -57,22 +60,25 @@ public class TestXMLSerializeDeserialize {
} }
br.close(); br.close();
return sb.toString(); return sb.toString();
} }
} }
class SimpleBean { class SimpleBean {
private int x = 1; private int x = 1;
private int y = 2; private int y = 2;
public int getX() { public int getX() {
return x; return x;
} }
public void setX(int x) { public void setX(int x) {
this.x = x; this.x = x;
} }
public int getY() { public int getY() {
return y; return y;
} }
public void setY(int y) { public void setY(int y) {
this.y = y; this.y = y;
} }