/* * Created on Feb 28, 2007 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package it.carige.nefin.datadictionary.test; 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.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 it.carige.nefin.datadictionary.Allegato; import junit.framework.TestCase; /** * @author MLazze * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class MappingTestCase extends TestCase { /** * crea il Pojo leggendo dall l' xml che soddisfa il suo schema xsd da cui è stato generato * @param xml l'xml * @return il Pojo corrispondente all'xml */ public static Object xml2Pojo(String name, 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; } /** * extrae l'xml corrispondente al Pojo * @param pojo il Pojo * @return xml corrispondente al Pojo */ 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; } /** * Caricamento dei descriptor durante il load della classe */ private static ClassDescriptorResolver getClassDescriptorResolver() throws ResolverException, MappingException { System.out.println("Load Class Descriptors!!!"); ClassDescriptorResolver classDescriptorResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML); Mapping mapping = new Mapping(Thread.currentThread().getContextClassLoader()); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("allegato.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; } public void testAllegato() { try { Allegato allegatoIn = new Allegato(); allegatoIn.setGuid("guid_in"); String xmlAllegato = MappingTestCase.pojo2Xml(allegatoIn); System.out.println("\n\n\n xml " +xmlAllegato); Allegato allegatoOut = (Allegato) MappingTestCase.xml2Pojo("",xmlAllegato); allegatoOut.setGuid("guid_output"); System.out.println("\n\n\n pojo " +allegatoOut.getGuid()); assertEquals(allegatoOut.getGuid(),"guid_output"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); fail(); } } }