@Test public void readAndWriteGenericList() throws IOException { ParameterizedType stringListType = Types.newParameterizedType(List.class, String.class); TikXml tikXml = new TikXml.Builder() .addTypeAdapter(stringListType, new StringListTypeAdapter()) .build(); // // Read xml // List<String> list = tikXml.read(TestUtils.sourceForFile("stringlist.xml"), stringListType); Assert.assertEquals(Arrays.asList("a", "b", "c"), list); // // write xml // Buffer buffer = new Buffer(); tikXml.write(buffer, list, stringListType); String writtenXml = TestUtils.bufferToString(buffer); Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><items><a>a</a><b>b</b><c>c</c></items>", writtenXml); }
@Test public void test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTagList emptyTagList = xml.read(TestUtils.sourceForFile("empty_tag_list.xml"), EmptyTagList.class); Assert.assertNotNull(emptyTagList); Assert.assertNotNull(emptyTagList.tags); Assert.assertEquals(3, emptyTagList.tags.size()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, emptyTagList); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyTagList><empty/><empty/><empty/></emptyTagList>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyTagList emptyTagList2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyTagList.class); Assert.assertEquals(emptyTagList, emptyTagList2); } }
@Test public void fieldAccessDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(true).build(); // Reading test ItemDataClass item = xml.read(TestUtils.sourceForFile("attributes.xml"), ItemDataClass.class); Date date = DateConverter.Companion.getFormat().parse("1988-03-04"); Assert.assertEquals("foo", item.aString); Assert.assertEquals(123, item.anInt); Assert.assertEquals(true, item.aBoolean); Assert.assertEquals(23.42, item.aDouble, 0); Assert.assertEquals(2147483648L, item.aLong); Assert.assertEquals(date, item.aDate); Assert.assertEquals(123, (int) item.intWrapper); Assert.assertEquals(true, item.booleanWrapper); Assert.assertEquals(23.42, item.doubleWrapper, 0); Assert.assertEquals(2147483648L, (long) item.longWrapper); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item aString=\"foo\" anInt=\"123\" aBoolean=\"true\" aDouble=\"23.42\" aLong=\"2147483648\" aDate=\"1988-03-04\" intWrapper=\"123\" booleanWrapper=\"true\" doubleWrapper=\"23.42\" longWrapper=\"2147483648\"/>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); ItemDataClass item2 = xml.read(TestUtils.sourceFrom(xmlStr), ItemDataClass.class); Assert.assertEquals(item, item2); }
@Test public void fieldAccess() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(true).build(); // Reading test Item item = xml.read(TestUtils.sourceForFile("attributes.xml"), Item.class); Date date = DateConverter.Companion.getFormat().parse("1988-03-04"); Assert.assertEquals("foo", item.aString); Assert.assertEquals(123, item.anInt); Assert.assertEquals(true, item.aBoolean); Assert.assertEquals(23.42, item.aDouble, 0); Assert.assertEquals(2147483648L, item.aLong); Assert.assertEquals(date, item.aDate); Assert.assertEquals(123, (int) item.intWrapper); Assert.assertEquals(true, item.booleanWrapper); Assert.assertEquals(23.42, item.doubleWrapper, 0); Assert.assertEquals(2147483648L, (long) item.longWrapper); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item aString=\"foo\" anInt=\"123\" aBoolean=\"true\" aDouble=\"23.42\" aLong=\"2147483648\" aDate=\"1988-03-04\" intWrapper=\"123\" booleanWrapper=\"true\" doubleWrapper=\"23.42\" longWrapper=\"2147483648\"/>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); Item item2 = xml.read(TestUtils.sourceFrom(xmlStr), Item.class); Assert.assertEquals(item, item2); }
@Test public void fieldAccess() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(true).build(); // Reading test Item item = xml.read(TestUtils.sourceForFile("attributes.xml"), Item.class); Date date = DateConverter.format.parse("1988-03-04"); Assert.assertEquals("foo", item.aString); Assert.assertEquals(123, item.anInt); Assert.assertEquals(true, item.aBoolean); Assert.assertEquals(23.42, item.aDouble, 0); Assert.assertEquals(2147483648L, item.aLong); Assert.assertEquals(date, item.aDate); Assert.assertEquals(123, (int) item.intWrapper); Assert.assertEquals(true, item.booleanWrapper); Assert.assertEquals(23.42, item.doubleWrapper, 0); Assert.assertEquals(2147483648L, (long) item.longWrapper); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item aString=\"foo\" anInt=\"123\" aBoolean=\"true\" aDouble=\"23.42\" aLong=\"2147483648\" aDate=\"1988-03-04\" intWrapper=\"123\" booleanWrapper=\"true\" doubleWrapper=\"23.42\" longWrapper=\"2147483648\"/>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); Item item2 = xml.read(TestUtils.sourceFrom(xmlStr), Item.class); Assert.assertEquals(item, item2); }
@Test public void readAllMultilineTextContent() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapter()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder_multiline_text_content.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertEquals("Foo Inc.", company.name); Assert.assertEquals(dateFormatter.parse("1999-12-31"), company.founded); Assert.assertEquals("Inc.", company.legalForm); Assert.assertEquals("This\n" + " is the\n" + " text content\n" + " ", company.description); Assert.assertTrue(company.legalFormPartOfTheName); Assert.assertTrue(company.shortInfo); Assert.assertEquals("Others Content", company.otherText); }
@Test public void test() throws IOException { // Test reading TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(false).build(); Server server = xml.read(TestUtils.sourceForFile("server.xml"), Server.class); Assert.assertEquals("fooServer", server.name()); Assert.assertTrue(server.config().enabled()); Assert.assertEquals("127.0.0.1", server.config().ip()); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, server); String xmlStr = "<server name=\"fooServer\"><serverConfig enabled=\"true\"><ip>127.0.0.1</ip></serverConfig></server>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); Server server2 = xml.read(TestUtils.sourceFrom(xmlStr), Server.class); Assert.assertEquals(server, server2); } }
@Test public void skipElementWithAttributesWithPath() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); Document document = xml.read(TestUtils.sourceForFile("element_with_tag_with_attributes_to_skip_with_path.xml"), Document.class); Assert.assertNotNull(document); Assert.assertNotNull(document.image()); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, document); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><document><toSkip><image/></toSkip></document>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); Document document2 = xml.read(TestUtils.sourceFrom(xmlStr), Document.class); Assert.assertEquals(document, document2); } }
@Test public void testDataClass() throws IOException { // Test reading TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(false).build(); ServerDataClass server = xml.read(TestUtils.sourceForFile("server.xml"), ServerDataClass.class); Assert.assertEquals("fooServer", server.getName()); Assert.assertTrue(server.getConfig().getEnabled()); Assert.assertEquals("127.0.0.1", server.getConfig().getIp()); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, server); String xmlStr = "<server name=\"fooServer\"><serverConfig enabled=\"true\"><ip>127.0.0.1</ip></serverConfig></server>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); ServerDataClass server2 = xml.read(TestUtils.sourceFrom(xmlStr), ServerDataClass.class); Assert.assertEquals(server, server2); } }
@Test public void skipAttributesDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); PropertyItemWithGetterSettersDataClass item = xml.read(TestUtils.sourceForFile("property_item_with_attributes.xml"), PropertyItemWithGetterSettersDataClass.class); Assert.assertEquals("foo", item.getAString()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item><aString>foo</aString><anInt>0</anInt><aBoolean>false</aBoolean><aDouble>0.0</aDouble><aLong>0</aLong></item>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); PropertyItemWithGetterSettersDataClass item2 = xml.read(TestUtils.sourceFrom(xmlStr), PropertyItemWithGetterSettersDataClass.class); Assert.assertEquals(item, item2); }
@Test public void falseBooleanForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyBooleanPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyBooleanPropertyElement.class); Assert.assertEquals(false, item.getEmpty()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyPropertyTag><empty>false</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyBooleanPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyBooleanPropertyElement.class); Assert.assertEquals(item, item2); }
@Test public void test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTagList emptyTagList = xml.read(TestUtils.sourceForFile("empty_tag_list.xml"), EmptyTagList.class); Assert.assertNotNull(emptyTagList); Assert.assertNotNull(emptyTagList.getTags()); Assert.assertEquals(3, emptyTagList.getTags().size()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, emptyTagList); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyTagList><empty/><empty/><empty/></emptyTagList>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyTagList emptyTagList2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyTagList.class); Assert.assertEquals(emptyTagList, emptyTagList2); } }
@Test public void falseBooleanForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyBooleanPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyBooleanPropertyElement.class); Assert.assertEquals(false, item.empty); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyPropertyTag><empty>false</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyBooleanPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyBooleanPropertyElement.class); Assert.assertEquals(item, item2); } }
@Test public void falseBooleanForEmptyTagDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyBooleanPropertyElementDataClass item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyBooleanPropertyElementDataClass.class); Assert.assertEquals(false, item.getEmpty()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyPropertyTag><empty>false</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyBooleanPropertyElementDataClass item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyBooleanPropertyElementDataClass.class); Assert.assertEquals(item, item2); } }
@Test public void ignoreMissingMappingTextContent() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutReadingTextContent()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertEquals("Foo Inc.", company.name); Assert.assertEquals(dateFormatter.parse("1999-12-31"), company.founded); Assert.assertEquals("Inc.", company.legalForm); Assert.assertNull(company.description); Assert.assertTrue(company.legalFormPartOfTheName); Assert.assertTrue(company.shortInfo); Assert.assertEquals("Others Content", company.otherText); }
@Test public void ignoringMissingMappingFounded() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutFounded()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertEquals("Foo Inc.", company.name); Assert.assertNull(company.founded); Assert.assertEquals("Inc.", company.legalForm); Assert.assertEquals("This is the text content\n ", company.description); Assert.assertTrue(company.legalFormPartOfTheName); Assert.assertTrue(company.shortInfo); Assert.assertEquals("Others Content", company.otherText); }
@Test public void test() throws IOException { // Test reading TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).writeDefaultXmlDeclaration(false).build(); Server server = xml.read(TestUtils.sourceForFile("server.xml"), Server.class); Assert.assertEquals("fooServer", server.name()); Assert.assertTrue(server.config().enabled()); Assert.assertEquals("127.0.0.1", server.config().ip()); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, server); String xmlStr = "<server name=\"fooServer\"><serverConfig enabled=\"true\"><ip>127.0.0.1</ip></serverConfig></server>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); Server server2 = xml.read(TestUtils.sourceFrom(xmlStr), Server.class); Assert.assertEquals(server, server2); } }
@Test public void skipElementWithAttributesWithPathDataClass() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); DocumentConstructorDataClass document = xml.read(TestUtils.sourceForFile("element_with_tag_with_attributes_to_skip_with_path.xml"), DocumentConstructorDataClass.class); Assert.assertNotNull(document); Assert.assertNotNull(document.getImage()); // Writing xml test Buffer buffer = new Buffer(); xml.write(buffer, document); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><document><toSkip><image/></toSkip></document>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); DocumentConstructorDataClass document2 = xml.read(TestUtils.sourceFrom(xmlStr), DocumentConstructorDataClass.class); Assert.assertEquals(document, document2); } }
@Test public void zeroLongForEmptyTagDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyLongPropertyElementDataClass item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyLongPropertyElementDataClass.class); Assert.assertEquals(0, item.getEmpty()); // Write XML // Writing tests Buffer buffer = new Buffer(); xml.write(buffer, item); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><emptyPropertyTag><empty>0</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyLongPropertyElementDataClass item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyLongPropertyElementDataClass.class); Assert.assertEquals(item, item2); }
@Test public void ignoringMissingMappingShortInfoAttribute() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutAttribute()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertEquals("Foo Inc.", company.name); Assert.assertEquals(dateFormatter.parse("1999-12-31"), company.founded); Assert.assertEquals("Inc.", company.legalForm); Assert.assertEquals("This is the text content\n ", company.description); Assert.assertTrue(company.legalFormPartOfTheName); Assert.assertFalse(company.shortInfo); Assert.assertEquals("Others Content", company.otherText); } }