DefaultDomConverter implements DomConverter { @Override public void convertValueToDom(Object value, DomElement parentElement) throws ConversionException { PropertySet propertySet = getPropertySet(value); for (Property property : propertySet.getProperties()) { convertPropertyToDom(property, parentElement); } } 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 testArrayWithDomConverterPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <allies>\n" + " <member>\n" + " <name>bibo</name>\n" + " </member>\n" + " <member>\n" + " <name>mimi</name>\n" + " </member>\n" + " </allies>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final ArrayWithDomConverterPojo value = new ArrayWithDomConverterPojo(); value.allies = new Member[2]; value.allies[0] = new Member("bibo"); value.allies[1] = new Member("mimi"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testArrayWithDomConverterPojoToDom_NullValue() throws ValidationException, ConversionException { final String expectedXml = "<parameters/>"; final XppDom dom = new XppDom("parameters"); final ArrayWithDomConverterPojo value = new ArrayWithDomConverterPojo(); value.allies = null; convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testWeirdPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <weird/>\n" + " <name>ernie</name>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final WeirdPojo value = new WeirdPojo(); value.weird = new Weird(); value.name = "ernie"; convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testDomToArrayPojo() throws ConversionException { final String expectedXml = "" + "<parameters>\n" + " <fileField>" + new File("C:/data/dat.ini") + "</fileField>\n" + " <stringField>This is a test.</stringField>\n" + " <intField>43</intField>\n" + " <doubleArrayField>0.1,0.2,-0.4</doubleArrayField>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final SimplePojo value = new SimplePojo(); value.intField = 43; value.stringField = "This is a test."; value.doubleArrayField = new double[]{0.1, 0.2, -0.4}; value.fileField = new File("C:/data/dat.ini"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testArrayPojoToDom() throws ConversionException { ConverterRegistry.getInstance().setConverter(Member.class, new Converter<Member>() { @Override public Class<? extends Member> getValueType() { return Member.class; } @Override public Member parse(String text) throws ConversionException { return new Member(text); } @Override public String format(Member field) { return field.name; } }); final String expectedXml = "<parameters>\n" + " <prince>bert</prince>\n" + " <allies>bibo,mimi</allies>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final ArrayPojo arrayPojo = new ArrayPojo(); arrayPojo.prince = new Member("bert"); arrayPojo.allies = new Member[2]; arrayPojo.allies[0] = new Member("bibo"); arrayPojo.allies[1] = new Member("mimi"); convertValueToDom(arrayPojo, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testAnnotatedPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "<parameters>\n" + " <targetBand>radiance_13</targetBand>\n" + " <defaultBandName>radiance_12</defaultBandName>\n" + " <targetBands>\n" + " <band>u</band>\n" + " <band>v</band>\n" + " <band>w</band>\n" + " </targetBands>\n" + "</parameters>"; final AnnotatedPojo value = new AnnotatedPojo(); value.targetBandName = "radiance_13"; value.defaultBandName = "radiance_12"; value.targetBandNames = new String[]{"u", "v", "w"}; final XppDom dom = new XppDom("parameters"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testEnumPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <character>MISS_PIGGY</character>\n" + "</parameters>"; final EnumPojo value = new EnumPojo(); value.character = DefaultDomConverterTest.EnumPojo.Muppet.MISS_PIGGY; final XppDom dom = new XppDom("parameters"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testComplexPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "<parameters>\n" + " <simple>\n" + " <stringField>Test, test, test!</stringField>\n" + " <intField>87</intField>\n" + " <doubleArrayField>0.5,1.0</doubleArrayField>\n" + " </simple>\n" + " <annotatedPojo>\n" + " <targetBand>reflec_4</targetBand>\n" + " <defaultBandName>reflec_27</defaultBandName>\n" + " <targetBands>\n" + " <band>real</band>\n" + " <band>imag</band>\n" + " </targetBands>\n" + " </annotatedPojo>\n" + "</parameters>"; final ComplexPojo value = new ComplexPojo(); value.simplePojo = new SimplePojo(); value.simplePojo.intField = 87; value.simplePojo.stringField = "Test, test, test!"; value.simplePojo.doubleArrayField = new double[]{0.5, 1.0}; value.annotatedPojo = new AnnotatedPojo(); value.annotatedPojo.targetBandName = "reflec_4"; value.annotatedPojo.defaultBandName = "reflec_27"; value.annotatedPojo.targetBandNames = new String[]{"real", "imag"}; final XppDom dom = new XppDom("parameters"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testNamedItemArrayPojoToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <defaultEndmember>\n" + " <name>Fallback</name>\n" + " <size>4</size>\n" + " <wavelengths>820.0,830.0,840.0,850.0</wavelengths>\n" + " <radiances>420.0,430.0,440.0,450.0</radiances>\n" + " </defaultEndmember>\n" + " <endmembers>\n" + " <endmember>\n" + " <name>Land</name>\n" + " <size>4</size>\n" + " <wavelengths>820.0,830.0,840.0,850.0</wavelengths>\n" + " <radiances>220.0,230.0,240.0,250.0</radiances>\n" + " </endmember>\n" + " <endmember>\n" + " <name>Water</name>\n" + " <size>4</size>\n" + " <wavelengths>820.0,830.0,840.0,850.0</wavelengths>\n" + " <radiances>620.0,630.0,640.0,650.0</radiances>\n" + " </endmember>\n" + " <endmember>\n" + " <name>Cloud</name>\n" + " <size>4</size>\n" + " <wavelengths>820.0,830.0,840.0,850.0</wavelengths>\n" + " <radiances>920.0,930.0,940.0,950.0</radiances>\n" + " </endmember>\n" + " </endmembers>\n" + "</parameters>"; final NamedItemArrayPojo value = new NamedItemArrayPojo(); value.defaultEndmember = new Endmember("Fallback", new double[]{820, 830, 840, 850}, new double[]{420, 430, 440, 450}); value.endmembers = new Endmember[3]; value.endmembers[0] = new Endmember("Land", new double[]{820, 830, 840, 850}, new double[]{220, 230, 240, 250}); value.endmembers[1] = new Endmember("Water", new double[]{820, 830, 840, 850}, new double[]{620, 630, 640, 650}); value.endmembers[2] = new Endmember("Cloud", new double[]{820, 830, 840, 850}, new double[]{920, 930, 940, 950}); final XppDom dom = new XppDom("parameters"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testInterfaceFieldsPojoToDom() throws ConversionException { final String expectedXml = "<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 InterfaceFieldsPojo interfacePojo = new InterfaceFieldsPojo(new Rectangle(10, 10, 20, 25), new Line2D.Float(0.0f, 10.3f, 15.7f, 34.6f)); final XppDom dom = new XppDom("parameters"); convertValueToDom(interfacePojo, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testSimplePojoToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <fileField>" + new File("C:/data/dat.ini") + "</fileField>\n" + " <stringField>This is a test.</stringField>\n" + " <intField>43</intField>\n" + " <doubleArrayField>0.1,0.2,-0.4</doubleArrayField>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final SimplePojo value = new SimplePojo(); value.intField = 43; value.stringField = "This is a test."; value.doubleArrayField = new double[]{0.1, 0.2, -0.4}; value.fileField = new File("C:/data/dat.ini"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testSimplePojoWithNullArrayToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <fileField>" + new File("C:/data/dat.ini") + "</fileField>\n" + " <stringField>This is a test.</stringField>\n" + " <intField>43</intField>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final SimplePojo value = new SimplePojo(); value.intField = 43; value.stringField = "This is a test."; value.doubleArrayField = null; value.fileField = new File("C:/data/dat.ini"); convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }
@Test public void testSimplePojoWithNullObjectToDom() throws ValidationException, ConversionException { final String expectedXml = "" + "<parameters>\n" + " <stringField>This is a test.</stringField>\n" + " <intField>43</intField>\n" + " <doubleArrayField>0.1,0.2,-0.4</doubleArrayField>\n" + "</parameters>"; final XppDom dom = new XppDom("parameters"); final SimplePojo value = new SimplePojo(); value.intField = 43; value.stringField = "This is a test."; value.doubleArrayField = new double[]{0.1, 0.2, -0.4}; value.fileField = null; convertValueToDom(value, dom); assertEquals(expectedXml, getXml(dom)); }