MaskPersistable implements DimapPersistable { @Override public final Element createXmlFromObject(Object object) { final Mask mask = (Mask) object; final Element root = new Element(TAG_MASK); root.setAttribute(ATTRIB_TYPE, mask.getImageType().getName()); final PropertyContainer config = mask.getImageConfig(); root.addContent(createElement(TAG_NAME, mask.getName())); root.addContent(createElement(TAG_DESCRIPTION, mask.getDescription())); final Element colorElement = new Element(TAG_COLOR); final Color color = (Color) config.getValue(Mask.ImageType.PROPERTY_NAME_COLOR); colorElement.setAttribute(ATTRIB_RED, String.valueOf(color.getRed())); colorElement.setAttribute(ATTRIB_GREEN, String.valueOf(color.getGreen())); colorElement.setAttribute(ATTRIB_BLUE, String.valueOf(color.getBlue())); colorElement.setAttribute(ATTRIB_ALPHA, String.valueOf(color.getAlpha())); root.addContent(colorElement); Object transparencyValue = config.getValue(Mask.ImageType.PROPERTY_NAME_TRANSPARENCY); final String transparency = String.valueOf(transparencyValue); root.addContent(createElement(TAG_TRANSPARENCY, transparency)); configureElement(root, mask); return root; }  @Override final Mask createObjectFromXml(Element element, Product product); @Override final Element createXmlFromObject(Object object);  }
@Test public void createXmlFromObject() { final Mask mask = new Mask("myRange", 10, 10, Mask.RangeType.INSTANCE); mask.setDescription("Carefully defined range"); mask.setImageColor(new Color(0, 255, 0, 128)); mask.setImageTransparency(0.78); final RangeTypeMaskPersistable maskPersistable = new RangeTypeMaskPersistable(); final Element element = maskPersistable.createXmlFromObject(mask); assertNotNull(element); assertEquals(TAG_MASK, element.getName()); assertEquals(Mask.RangeType.TYPE_NAME, getAttributeString(element, ATTRIB_TYPE)); final Element name = element.getChild(TAG_NAME); assertEquals("myRange", getAttributeString(name, ATTRIB_VALUE)); final Element description = element.getChild(TAG_DESCRIPTION); assertEquals("Carefully defined range", getAttributeString(description, ATTRIB_VALUE)); final Element color = element.getChild(TAG_COLOR); assertEquals(0, getAttributeInt(color, ATTRIB_RED)); assertEquals(255, getAttributeInt(color, ATTRIB_GREEN)); assertEquals(0, getAttributeInt(color, ATTRIB_BLUE)); assertEquals(128, getAttributeInt(color, ATTRIB_ALPHA)); final Element transparency = element.getChild(TAG_TRANSPARENCY); assertEquals(0.78, getAttributeDouble(transparency, ATTRIB_VALUE), 0.0); }
@Test public void testXmlCreationWithoutMaskDescription() { final Mask mask = new Mask("name", 10, 10, new TestImageType()); mask.setDescription(null); final DimapPersistable persistable = new TestMaskPersistable(); final Element element = persistable.createXmlFromObject(mask); final Element description = element.getChild(TAG_DESCRIPTION); assertNotNull(description); assertTrue(description.getAttribute(ATTRIB_VALUE).getValue().isEmpty()); }