package bugs.xml.bug1886; import java.io.File; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.io.StringWriter; import org.castor.mapping.BindingType; import org.castor.mapping.MappingUnmarshaller; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.mapping.MappingLoader; import org.exolab.castor.tools.MappingTool; import org.exolab.castor.xml.ClassDescriptorResolver; import org.exolab.castor.xml.ClassDescriptorResolverFactory; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.ResolverException; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.XMLClassDescriptor; import org.exolab.castor.xml.XMLClassDescriptorResolver; import org.xml.sax.InputSource; import bugs.xml.bug1886.Allegato; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Mapping Test Case For Bug CASTOR 1886 */ public class MappingTestCase extends TestCase { /** * convert a xml into a pojo * @param xml the xml * @return the pojo */ public static Object xml2Pojo(String xml) { if (xml == null || "".equalsIgnoreCase(xml.trim())) return null; try { Unmarshaller unmarshaller = new Unmarshaller(); unmarshaller.setResolver((XMLClassDescriptorResolver) getClassDescriptorResolver()); unmarshaller.setValidation(false); return unmarshaller.unmarshal(new StringReader(xml)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * convert a pojo into a xml * @param pojo the Pojo * @return the xml */ public static String pojo2Xml(Object pojo) { if (pojo == null) return null; StringWriter writer; writer = new StringWriter(); try { Marshaller marshaller = new Marshaller(writer); XMLClassDescriptorResolver resolver = (XMLClassDescriptorResolver) getClassDescriptorResolver(); XMLClassDescriptor descr = (XMLClassDescriptor)resolver.resolve(pojo.getClass()); String namespace = descr.getNameSpaceURI(); marshaller.setResolver(resolver); marshaller.setValidation(false); marshaller.setSuppressNamespaces(true); marshaller.marshal(pojo); String xml = writer.toString(); return xml; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * Load the class descriptors * @return * @throws ResolverException * @throws MappingException */ private static ClassDescriptorResolver getClassDescriptorResolver() throws ResolverException, MappingException { ClassDescriptorResolver classDescriptorResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML); Mapping mapping = new Mapping(Thread.currentThread().getContextClassLoader()); InputStream is = MappingTestCase.class.getResourceAsStream("mapping.xml"); InputStreamReader reader = new InputStreamReader(is); InputSource source = new InputSource(reader); mapping.loadMapping(source); classDescriptorResolver = ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML); MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller(); MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML); classDescriptorResolver.setMappingLoader(mappingLoader); return classDescriptorResolver; } /** * test the bug 1886 * */ public void testBug1886() { try { // generate the mapping.xml using MappingTool for class Allegato MappingTool mt = new MappingTool(); mt.addClass(Allegato.class); File fileOut = new File("classes/bugs/xml/bug1886/mapping.xml"); FileWriter fw = new FileWriter(fileOut); mt.write(fw); // create a allegato Allegato allegatoIn = new Allegato(); allegatoIn.setGuid("guid_in"); // marshall a allegato String xmlAllegato = MappingTestCase.pojo2Xml(allegatoIn); System.out.println("\n\n\n xml " +xmlAllegato); // unmarshall a allegato Allegato allegatoOut = (Allegato) MappingTestCase.xml2Pojo(xmlAllegato); allegatoOut.setGuid("guid_output"); System.out.println("\n\n\n pojo " +allegatoOut.getGuid()); // assert equal on guid assertEquals(allegatoOut.getGuid(),"guid_output"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); fail(); } } public static final void main(String[] args){ TestSuite suite = new TestSuite(); suite.addTestSuite(MappingTestCase.class); } }