@Test public void marshalJaxbUnmarshalCastor() throws Exception { final String xml = marshalToXmlWithJaxb(); final T obj = CastorUtils.unmarshal(getSampleClass(), new ByteArrayInputStream(xml.getBytes())); LOG.debug("Generated Object: {}", obj); assertDepthEquals(getSampleObject(), obj); }
@Test public void marshalCastorUnmarshalCastor() throws Exception { final String xml = marshalToXmlWithCastor(); final ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); final T obj = CastorUtils.unmarshal(getSampleClass(), is, false); assertDepthEquals(getSampleObject(), obj); }
@Test public void marshalCastorUnmarshalJaxb() throws Exception { final String xml = marshalToXmlWithCastor(); final T obj = JaxbUtils.unmarshal(getSampleClass(), xml); LOG.debug("Generated Object: {}", obj); assertDepthEquals(getSampleObject(), obj); }
@Test public void unmarshalXmlAndCompareToJaxb() throws Exception { LOG.debug("xml: {}", getSampleXml()); final T obj = JaxbUtils.unmarshal(getSampleClass(), new InputSource(getSampleXmlInputStream()), null); LOG.debug("Sample object: {}\n\nJAXB object: {}", getSampleObject(), obj); assertDepthEquals(getSampleObject(), obj); }
@Test public void testImportEmpty() { final String old = "<datacollection-config rrdRepository=\"${install.share.dir}/rrd/snmp/\">\n" + " <snmp-collection name=\"default\" snmpStorageFlag=\"select\">\n" + " <rrd step=\"300\">\n" + " <rra>RRA:AVERAGE:0.5:1:2016</rra>\n" + " <rra>RRA:AVERAGE:0.5:12:1488</rra>\n" + " <rra>RRA:AVERAGE:0.5:288:366</rra>\n" + " <rra>RRA:MAX:0.5:288:366</rra>\n" + " <rra>RRA:MIN:0.5:288:366</rra>\n" + " </rrd>\n" + " </snmp-collection>\n" + "</datacollection-config>"; final String expected = "<datacollection-config>\n" + " <snmp-collection name=\"default\">\n" + " <datacollection-group name=\"default-all\">\n" + " <resourceType name=\"ifIndex\" label=\"Interfaces (MIB-2 ifTable)\">\n" + " <resourceName><template>${ifDescr}-${ifPhysAddr}</template></resourceName>\n" + " <resourceLabel><template>${ifDescr}-${ifPhysAddr}</template></resourceLabel>\n" + " <resourceKind><template>${ifType}</template></resourceKind>\n" + " <column oid=\".1.3.6.1.2.1.2.2.1.2\" alias=\"ifDescr\" type=\"string\" />\n" + " <column oid=\".1.3.6.1.2.1.2.2.1.6\" alias=\"ifPhysAddr\" type=\"string\" display-hint=\"1x:\"/>\n" + " <column oid=\".1.3.6.1.2.1.2.2.1.3\" alias=\"ifType\" type=\"string\" /> \n" + " <column oid=\".1.3.6.1.2.1.31.1.1.1.1\" alias=\"ifName\" type=\"string\" />\n" + " </resourceType>\n" + " </datacollection-group>\n" + " </snmp-collection>\n" + "</datacollection-config>"; final DatacollectionConfig oldConfig = JaxbUtils.unmarshal(DatacollectionConfig.class, old); final DataCollectionConfigImpl expectedNewConfig = JaxbUtils.unmarshal(DataCollectionConfigImpl.class, expected); final DataCollectionConfigConverter generator = new DataCollectionConfigConverter(); oldConfig.visit(generator); final DataCollectionConfigImpl actualNewConfig = generator.getDataCollectionConfig(); XmlTest.assertDepthEquals(expectedNewConfig, actualNewConfig); }
@Test public void testOldOnefileDatacollectionConfig() throws Exception { final String oldXml = IOUtils.toString(getClass().getResource("old-datacollection-config-mib2.xml")); final DatacollectionConfig oldConfig = JaxbUtils.unmarshal(DatacollectionConfig.class, oldXml); assertNotNull(oldConfig); final String expectedXml = IOUtils.toString(getClass().getResource("new-datacollection-config-mib2.xml")); final DataCollectionConfigImpl expectedNewConfig = JaxbUtils.unmarshal(DataCollectionConfigImpl.class, expectedXml); final DataCollectionConfigConverter generator = new DataCollectionConfigConverter(); oldConfig.visit(generator); final DataCollectionConfigImpl actualNewConfig = generator.getDataCollectionConfig(); // final String newXml = JaxbUtils.marshal(actualNewConfig); // XmlTest.assertXmlEquals(expectedXml, newXml); XmlTest.assertDepthEquals(expectedNewConfig, actualNewConfig); }
private static void assertDepthEquals( final int depth, final String propertyName, final Object expected, Object actual) { if (expected == null && actual == null) { return; } else if (expected == null) { fail("expected " + propertyName + " was null but actual was not!"); } else if (actual == null) { fail("actual " + propertyName + " was null but expected was not!"); } final String assertionMessage = propertyName == null ? ("Top-level objects (" + expected.getClass().getName() + ") do not match.") : ("Properties " + propertyName + " do not match."); if (expected.getClass().getName().startsWith("java") || actual.getClass().getName().startsWith("java")) { // java primitives, just do assertEquals if (expected instanceof Object[] || actual instanceof Object[]) { assertTrue(assertionMessage, Arrays.equals((Object[]) expected, (Object[]) actual)); } else { assertEquals(assertionMessage, expected, actual); } return; } final BeanWrapper expectedWrapper = new BeanWrapperImpl(expected); final BeanWrapper actualWrapper = new BeanWrapperImpl(actual); final Set<String> properties = new TreeSet<String>(); for (final PropertyDescriptor descriptor : expectedWrapper.getPropertyDescriptors()) { properties.add(descriptor.getName()); } for (final PropertyDescriptor descriptor : actualWrapper.getPropertyDescriptors()) { properties.add(descriptor.getName()); } properties.remove("class"); for (final String property : properties) { final PropertyDescriptor expectedDescriptor = expectedWrapper.getPropertyDescriptor(property); final PropertyDescriptor actualDescriptor = actualWrapper.getPropertyDescriptor(property); if (expectedDescriptor != null && actualDescriptor != null) { // both have descriptors, so walk the sub-objects Object expectedValue = null; Object actualValue = null; try { expectedValue = expectedWrapper.getPropertyValue(property); } catch (final Exception e) { } try { actualValue = actualWrapper.getPropertyValue(property); } catch (final Exception e) { } assertDepthEquals(depth + 1, property, expectedValue, actualValue); } else if (expectedDescriptor != null) { fail("Should have '" + property + "' property on actual object, but there was none!"); } else if (actualDescriptor != null) { fail("Should have '" + property + "' property on expected object, but there was none!"); } } if (expected instanceof Object[] || actual instanceof Object[]) { final Object[] expectedArray = (Object[]) expected; final Object[] actualArray = (Object[]) actual; assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray)); } else { assertEquals(assertionMessage, expected, actual); } }
public static void assertDepthEquals(final Object expected, Object actual) { assertDepthEquals(0, "", expected, actual); }
@Test public void marshalJaxbUnmarshalJaxb() throws Exception { final String xml = marshalToXmlWithJaxb(); final T obj = JaxbUtils.unmarshal(getSampleClass(), xml); assertDepthEquals(getSampleObject(), obj); }
@Test public void unmarshalXmlAndCompareToCastor() throws Exception { final T obj = CastorUtils.unmarshal(getSampleClass(), getSampleXmlInputStream()); LOG.debug("Sample object: {}\n\nCastor object: {}", getSampleObject(), obj); assertDepthEquals(getSampleObject(), obj); }