@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)); } }
/** * @return {@link TikXml} instance with the specified config */ public TikXml build() { return new TikXml(config); } }
@Override public T convert(ResponseBody responseBody) throws IOException { try { return tikXml.read(responseBody.source(), clazz); } finally { responseBody.close(); } } }
public <T> void write(BufferedSink sink, T valueToWrite) throws IOException { write(sink, valueToWrite, valueToWrite.getClass()); }
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); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); tikXml.write(buffer, value); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
@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)); } }
public void parse (String xml) throws IOException { Feed employee = new TikXml.Builder().exceptionOnUnreadXml(false).build().read(new Buffer().writeUtf8(xml), Feed.class); System.out.println(getClass().getSimpleName() + " " + employee); } }
@Test public void zeroLongForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyLongPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyLongPropertyElement.class); Assert.assertEquals(0, 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>0</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyLongPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyLongPropertyElement.class); Assert.assertEquals(item, item2); }
@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 zeroIntForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyIntPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyIntPropertyElement.class); Assert.assertEquals(0, 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>0</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyIntPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyIntPropertyElement.class); Assert.assertEquals(item, item2); }
@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 zeroDoubleForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyDoublePropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyDoublePropertyElement.class); Assert.assertEquals(0, 0, 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>0.0</empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyDoublePropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyDoublePropertyElement.class); Assert.assertEquals(item, item2); }
@Test public void failSkippingAttributesDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); try { PropertyItemWithGetterSettersDataClass item = xml.read(TestUtils.sourceForFile("property_item_with_attributes.xml"), PropertyItemWithGetterSettersDataClass.class); Assert.fail("Exception expected"); } catch (IOException e) { Assert.assertEquals("Unread attribute 'a' at path /item/aString[@a]", e.getMessage()); } } }
@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 failSkippingAttributesDataClass() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); try { PropertyItemConstructorDataClass item = xml.read(TestUtils.sourceForFile("property_item_with_attributes.xml"), PropertyItemConstructorDataClass.class); Assert.fail("Exception expected"); } catch (IOException e) { Assert.assertEquals("Unread attribute 'a' at path /item/aString[@a]", e.getMessage()); } } }
@Test public void emptyStringForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyStringPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyStringPropertyElement.class); Assert.assertEquals("", 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></empty></emptyPropertyTag>"; Assert.assertEquals(xmlStr, TestUtils.bufferToString(buffer)); EmptyStringPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyStringPropertyElement.class); Assert.assertEquals(item, item2); }
@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 zeroIntForEmptyTag() throws IOException, ParseException { TikXml xml = new TikXml.Builder().exceptionOnUnreadXml(true).build(); EmptyIntPropertyElement item = xml.read(TestUtils.sourceForFile("empty_property_tag.xml"), EmptyIntPropertyElement.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)); EmptyIntPropertyElement item2 = xml.read(TestUtils.sourceFrom(xmlStr), EmptyIntPropertyElement.class); Assert.assertEquals(item, item2); }
@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()); } } }