@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 read() throws IOException { BufferedSource input = TestUtils.sourceForFile("books.xml"); TikXml tikXml = new TikXml.Builder() .addTypeAdapter(Book.class, new BookTypeAdapter()) .addTypeAdapter(Catalogue.class, new CatalogueTypeAdapter()) .build(); Catalogue catalogue = tikXml.read(input, Catalogue.class); Assert.assertEquals(12, catalogue.books.size()); } }
@Test public void readPropertyElements() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapter()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements.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); }
@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 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); } }
@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 readAll() 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.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.assertTrue(company.shortInfo); Assert.assertEquals("Others Content", company.otherText); }
@Test public void readTextContentSplitted() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapter()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements_textcontent_splitted.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); }
@Test public void ignoreUnreadTextContent() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutReadingTextContent()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements_textcontent_splitted.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); }
@Test public void ingnoreUnmappedPropertyElement() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutLegalFormPropertyElement()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements.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.assertNull(company.legalForm); }
@Test public void readTextContent() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapter()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements_textcontent.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); }
@Test public void failingMissingMappingTextContent() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutReadingTextContent()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); exception.expect(IOException.class); exception.expectMessage( "Could not map the xml element's text content at path at path /company/info/text() to java class. Have you annotated such a field in your java class to map the xml element's text content? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }
@Test public void failingMissingMappingShortInfoAttribute() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutAttribute()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); exception.expect(IOException.class); exception.expectMessage( "Could not map the xml attribute with the name 'shortInfo' at path /company/info[@shortInfo]to java class. Have you annotated such a field in your java class to map this xml attribute? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }
@Test public void failingMissingMappingFounded() throws IOException, ParseException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyNestedTypeAdapterWithoutFounded()) .build(); BufferedSource source = TestUtils.sourceForFile("company_nested_childbinder.xml"); exception.expect(IOException.class); exception.expectMessage( "Could not map the xml element with the name 'founded' at path /company/info/founded to java class. Have you annotated such a field in your java class to map this xml element? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }
@Test public void failUnmappedAttribute() throws IOException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutNameAttribute()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_test.xml"); exception.expect(IOException.class); exception.expectMessage("Could not map the xml attribute with the name 'name' at path /company[@name] to java class. Have you annotated such a field in your java class to map this xml attribute? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }
@Test public void readTest() throws IOException { TikXml tikXml = new TikXml.Builder(). addTypeAdapter(Company.class, new CompanyTypeAdapter()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_test.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertEquals("Foo Inc.", company.name); }
@Test public void failUnmappedPropertyElement() throws IOException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutLegalFormPropertyElement()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements.xml"); exception.expect(IOException.class); exception.expectMessage("Could not map the xml element with the name 'legalForm' at path /company/legalForm to java class. Have you annotated such a field in your java class to map this xml element? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }
@Test public void ignoreUnmappedAttribute() throws IOException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(false) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutNameAttribute()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_test.xml"); Company company = tikXml.read(source, Company.class); Assert.assertEquals(123, company.id); Assert.assertNull(company.name); }
@Test public void failUnreadTextContent() throws IOException { TikXml tikXml = new TikXml.Builder() .exceptionOnUnreadXml(true) .addTypeConverter(Date.class, dateTypeConverter) .addTypeAdapter(Company.class, new CompanyTypeAdapterWithoutReadingTextContent()) .build(); BufferedSource source = TestUtils.sourceForFile("simple_typeadapater_with_propertyelements_textcontent.xml"); exception.expect(IOException.class); exception.expectMessage("Could not map the xml element's text content at path at path /company/text() to java class. Have you annotated such a field in your java class to map the xml element's text content? Otherwise you can turn this error message off with TikXml.Builder().exceptionOnUnreadXml(false).build()."); Company company = tikXml.read(source, Company.class); }