DefaultDomConverter implements DomConverter { @Override public Object convertDomToValue(DomElement parentElement, Object value) throws ConversionException, ValidationException { PropertySet propertySet; if (value == null) { value = createValueInstance(parentElement, getValueType()); propertySet = getPropertySet(value); propertySet.setDefaultValues(); } else { propertySet = getPropertySet(value); } convertDomToPropertySet(parentElement, propertySet); return value; } DefaultDomConverter(Class<?> valueType);  DefaultDomConverter(Class<?> valueType, PropertyDescriptorFactory propertyDescriptorFactory);  DefaultDomConverter(Class<?> valueType, PropertyDescriptorFactory propertyDescriptorFactory, PropertySetDescriptor propertySetDescriptor); @Override Class<?> getValueType(); PropertyDescriptorFactory getPropertyDescriptorFactory(); PropertySetDescriptor getPropertySetDescriptor(); @Override void convertValueToDom(Object value, DomElement parentElement); @Override Object convertDomToValue(DomElement parentElement, Object value);  }
@Test public void testDomToAnnotatedPojo() throws Exception { final String xml = "" + "<parameters>" + " <targetBand>result</targetBand>" + " <targetBands>" + " <band>a</band>" + " <band>b</band>" + " </targetBands>" + "</parameters>"; final XppDom dom = createDom(xml); final AnnotatedPojo value = new AnnotatedPojo(); assertNull(value.targetBandName); assertNull(value.targetBandNames); convertDomToValue(dom, value); assertEquals("result", value.targetBandName); assertNotNull(value.targetBandNames); assertEquals(2, value.targetBandNames.length); assertEquals("a", value.targetBandNames[0]); assertEquals("b", value.targetBandNames[1]); }
@Test public void testDomToEnumPojo() throws Exception { final String xml = "" + "<parameters>" + " <character>KERMIT</character>" + "</parameters>"; final XppDom dom = createDom(xml); final EnumPojo value = new EnumPojo(); assertNull(value.character); convertDomToValue(dom, value); assertEquals(EnumPojo.Muppet.KERMIT, value.character); }
@Test public void testDomToComplexPojo() throws Exception { final String xmlString = "" + "<parameters>" + " <simple>" + " <stringField>a string</stringField>" + " <intField>42</intField>" + " <doubleArrayField>1.2, 4.5, -0.034</doubleArrayField>" + " </simple>" + " <annotatedPojo>" + " <targetBand>result</targetBand>" + " <targetBands>" + " <band>a</band>" + " <band>b</band>" + " </targetBands>" + " </annotatedPojo>" + "</parameters>"; final XppDom dom = createDom(xmlString); final ComplexPojo value = new ComplexPojo(); assertNull(value.simplePojo); assertNull(value.annotatedPojo); convertDomToValue(dom, value); assertEquals(42, value.simplePojo.intField); assertEquals("a string", value.simplePojo.stringField); assertNotNull(value.simplePojo.doubleArrayField); assertEquals(3, value.simplePojo.doubleArrayField.length); assertEquals(1.2, value.simplePojo.doubleArrayField[0], 1.0e-10); assertEquals(4.5, value.simplePojo.doubleArrayField[1], 1.0e-10); assertEquals(-0.034, value.simplePojo.doubleArrayField[2], 1.0e-10); assertEquals("result", value.annotatedPojo.targetBandName); assertEquals("reflec_13", value.annotatedPojo.defaultBandName); assertNotNull(value.annotatedPojo.targetBandNames); assertEquals(2, value.annotatedPojo.targetBandNames.length); assertEquals("a", value.annotatedPojo.targetBandNames[0]); assertEquals("b", value.annotatedPojo.targetBandNames[1]); }
@Test public void testDomToNamedItemArrayPojo() throws Exception { final String xml = "" + "<parameters>" + " <endmembers>" + " <endmember>" + " <name>Land</name>" + " <size>4</size>" + " <wavelengths>820,830,840,850</wavelengths>" + " <radiances>220,230,240,250</radiances>" + " </endmember>" + " <endmember>" + " <name>Water</name>" + " <size>4</size>" + " <wavelengths>820,830,840,850</wavelengths>" + " <radiances>620,630,640,650</radiances>" + " </endmember>" + " <endmember>" + " <name>Cloud</name>" + " <size>4</size>" + " <wavelengths>820,830,840,850</wavelengths>" + " <radiances>920,930,940,950</radiances>" + " </endmember>" + " </endmembers>" + " <defaultEndmember>" + " <name>Fallback</name>" + " <size>4</size>" + " <wavelengths>820,830,840,850</wavelengths>" + " <radiances>420,430,440,450</radiances>" + " </defaultEndmember>" + "</parameters>"; final XppDom dom = createDom(xml); final NamedItemArrayPojo value = new NamedItemArrayPojo(); assertNull(value.defaultEndmember); assertNull(value.endmembers); convertDomToValue(dom, value); assertNotNull(value.defaultEndmember); assertEquals("Fallback", value.defaultEndmember.name); assertEquals(4, value.defaultEndmember.size); assertNotNull(value.defaultEndmember.wavelengths); assertNotNull(value.defaultEndmember.radiances); assertNotNull(value.endmembers); assertEquals(3, value.endmembers.length); assertEquals("Land", value.endmembers[0].name); assertEquals(4, value.endmembers[0].size); assertNotNull(value.endmembers[0].wavelengths); assertNotNull(value.endmembers[0].radiances); assertEquals("Water", value.endmembers[1].name); assertEquals(4, value.endmembers[1].size); assertNotNull(value.endmembers[1].wavelengths); assertNotNull(value.endmembers[1].radiances); assertEquals("Cloud", value.endmembers[2].name); assertEquals(4, value.endmembers[2].size); assertNotNull(value.endmembers[2].wavelengths); assertNotNull(value.endmembers[2].radiances); }
@Test public void testDomToInterfaceFieldsPojo() throws Exception { final String xml = "<parameters>\n" + " <shape1 class=\"java.awt.Rectangle\">\n" + " <x>10</x>\n" + " <y>10</y>\n" + " <width>20</width>\n" + " <height>25</height>\n" + " </shape1>\n" + " <shape2 class=\"java.awt.geom.Line2D$Float\">\n" + " <x1>0.0</x1>\n" + " <y1>10.3</y1>\n" + " <x2>15.7</x2>\n" + " <y2>34.6</y2>\n" + " </shape2>\n" + "</parameters>"; final XppDom dom = createDom(xml); final InterfaceFieldsPojo value = new InterfaceFieldsPojo(null, null); assertNull(value.shape1); assertNull(value.shape2); convertDomToValue(dom, value); assertNotNull(value.shape1); assertNotNull(value.shape1 instanceof Rectangle); final Rectangle shape1 = (Rectangle) value.shape1; assertEquals(10, shape1.x); assertEquals(10, shape1.x); assertEquals(20, shape1.width); assertEquals(25, shape1.height); assertNotNull(value.shape2); assertNotNull(value.shape2 instanceof Line2D.Float); final Line2D.Float shape2 = (Line2D.Float) value.shape2; assertEquals(0.0f, shape2.x1, 1e-5); assertEquals(10.3f, shape2.y1, 1e-5); assertEquals(15.7f, shape2.x2, 1e-5); assertEquals(34.6f, shape2.y2, 1e-5); }
@Test public void testUnknownElement() throws Exception { final String xml = "" + "<parameters>" + " <kornField>42</kornField>" + "</parameters>"; final XppDom dom = createDom(xml); final SimplePojo value = new SimplePojo(); try { convertDomToValue(dom, value); fail("ConversionException expected"); } catch (ConversionException ignored) { } }
@Test public void testDomToSimplePojo() throws Exception { final String xml = "" + "<parameters>" + " <fileField>C:/data/MER.N1</fileField>" + " <stringField>a string</stringField>" + " <intField>42</intField>" + " <doubleArrayField>1.2, 4.5, -0.034</doubleArrayField>" + "</parameters>"; final XppDom dom = createDom(xml); final SimplePojo value = new SimplePojo(); assertEquals(0, value.intField); assertEquals(null, value.stringField); assertNull(value.doubleArrayField); assertNull(value.fileField); convertDomToValue(dom, value); assertEquals(42, value.intField); assertEquals("a string", value.stringField); assertEquals(new File("C:/data/MER.N1"), value.fileField); assertNotNull(value.doubleArrayField); assertEquals(3, value.doubleArrayField.length); assertEquals(1.2, value.doubleArrayField[0], 1.0e-10); assertEquals(4.5, value.doubleArrayField[1], 1.0e-10); assertEquals(-0.034, value.doubleArrayField[2], 1.0e-10); }
@Test public void testArrayInitIfAlreadyInitialized() throws Exception { final String parameters = "" + "<parameters>\n" + " <targetBands>\n" + " <band>band_a</band>\n" + " <band>band_b</band>\n" + " <band>band_c</band>\n" + " </targetBands>\n" + " <targetBand>notImportant</targetBand>\n" + " <defaultBandName>ignored</defaultBandName>\n" + "</parameters>"; final XppDom dom = createDom(parameters); AnnotatedPojo annotatedPojo = new AnnotatedPojo(); annotatedPojo.targetBandNames = new String[]{"band_1","band_2"}; convertDomToValue(dom, annotatedPojo); assertArrayEquals(new String[]{"band_a", "band_b", "band_c"}, annotatedPojo.targetBandNames); }