@Override public T convert(ResponseBody responseBody) throws IOException { try { return tikXml.read(responseBody.source(), clazz); } finally { responseBody.close(); } } }
public void parse (String xml) throws IOException { Employee employee = new TikXml.Builder().exceptionOnUnreadXml(false).build().read(new Buffer().writeUtf8(xml), Employee.class); System.out.println(getClass().getSimpleName() + " " + employee.name); } }
@Test public void failSkippingAttributes() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); try { PropertyItemWithGetterSetters item = xml.read(TestUtils.sourceForFile("property_item_with_attributes.xml"), PropertyItemWithGetterSetters.class); Assert.fail("Exception expected"); } catch (IOException e) { Assert.assertEquals("Unread attribute 'a' at path /item/aString[@a]", e.getMessage()); } }
@Test public void failSkippingAttributes() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); try { PropertyItemConstructor item = xml.read(TestUtils.sourceForFile("property_item_with_attributes.xml"), PropertyItemConstructor.class); Assert.fail("Exception expected"); } catch (IOException e) { Assert.assertEquals("Unread attribute 'a' at path /item/aString[@a]", e.getMessage()); } }
@Test public void namespaceDefaultDefinitions() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); RootDefaultNamespace item = xml.read(TestUtils.sourceForFile("regression/namespaces_default_fix.xml"), RootDefaultNamespace.class); Assert.assertEquals("Foo", item.name); }
@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 test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTag emptyTag = xml.read(TestUtils.sourceForFile("empty_tag.xml"), EmptyTag.class); Assert.assertNotNull(emptyTag); Buffer buffer = new Buffer(); xml.write(buffer, emptyTag); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empty/>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); } }
@Test public void test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTag emptyTag = xml.read(TestUtils.sourceForFile("empty_tag.xml"), EmptyTag.class); Assert.assertNotNull(emptyTag); Buffer buffer = new Buffer(); xml.write(buffer, emptyTag); String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empty/>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); } }
@Test public void namespaceDefaultDefinitionsDataClass() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); RootDefaultNamespaceDataClass item = xml.read(TestUtils.sourceForFile("regression/namespaces_default_fix.xml"), RootDefaultNamespaceDataClass.class); Assert.assertEquals("Foo", item.getName()); }
@Test public void test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTagListConstructor emptyTagList = xml.read(TestUtils.sourceForFile("empty_tag_list.xml"), EmptyTagListConstructor.class); Assert.assertNotNull(emptyTagList); Assert.assertNotNull(emptyTagList.getTags()); Assert.assertEquals(3, emptyTagList.getTags().size()); } }
@Test public void testDataClass() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTagListConstructorDataClass emptyTagList = xml.read(TestUtils.sourceForFile("empty_tag_list.xml"), EmptyTagListConstructorDataClass.class); Assert.assertNotNull(emptyTagList); Assert.assertNotNull(emptyTagList.getTags()); Assert.assertEquals(3, emptyTagList.getTags().size()); } }
@Test public void ignoreNamespaceDefinitions() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); Root item = xml.read(TestUtils.sourceForFile("namespaces.xml"), Root.class); Assert.assertEquals("Foo", item.name); Assert.assertEquals(123, item.child.id); }
@Test public void namespaceDefaultDefinitions() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(false).build(); RootDefaultNamespace item = xml.read(TestUtils.sourceForFile("regression/namespaces_default_fix.xml"), RootDefaultNamespace.class); Assert.assertEquals("Foo", item.getName()); }
@Test public void test() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyTagListConstructor emptyTagList = xml.read(TestUtils.sourceForFile("empty_tag_list.xml"), EmptyTagListConstructor.class); Assert.assertNotNull(emptyTagList); Assert.assertNotNull(emptyTagList.getTags()); Assert.assertEquals(3, emptyTagList.getTags().size()); }
@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 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 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 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); }
@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 ignoreNamespaceDefinitions() throws IOException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); Root item = xml.read(TestUtils.sourceForFile("namespaces.xml"), Root.class); Assert.assertEquals("Foo", item.getName()); Assert.assertEquals(123, item.getChild().getId()); }