1 17 package org.apache.ws.jaxme.junit; 18 19 import java.io.StringReader ; 20 import java.io.StringWriter ; 21 22 import javax.xml.bind.JAXBContext; 23 import javax.xml.bind.JAXBException; 24 import javax.xml.bind.Marshaller; 25 import javax.xml.bind.Unmarshaller; 26 27 import org.apache.ws.jaxme.JMElement; 28 import org.apache.ws.jaxme.impl.JMMarshallerImpl; 29 import org.xml.sax.InputSource ; 30 31 import junit.framework.TestCase; 32 33 34 39 public abstract class BaseTestCase extends TestCase { 40 protected BaseTestCase() { 41 } 42 43 protected BaseTestCase(String pName) { 44 super(pName); 45 } 46 47 protected BaseTestCase(Class c) { 48 this(c.getName()); 49 } 50 51 protected String getNamespaceURI(JMElement pElement) { 52 return pElement.getQName().getNamespaceURI(); 53 } 54 55 protected String getPackageName(Class pClass) { 56 String className = pClass.getName(); 57 int offset = className.lastIndexOf('.'); 58 if (offset == -1) { 59 throw new IllegalStateException ("Unable to parse package name: " + className); 60 } else { 61 return className.substring(0, offset); 62 } 63 } 64 65 protected JAXBContext getJAXBContext(Class pClass) throws JAXBException { 66 return JAXBContext.newInstance(getPackageName(pClass)); 67 } 68 69 protected void assertEquals(byte[] pExpect, byte[] pGot) { 71 if (pExpect.length != pGot.length) { 72 fail("Expected " + pExpect.length + " bytes, got " + pGot.length); 73 } else { 74 for (int i = 0; i < pExpect.length; i++) { 75 if (pExpect[i] != pGot[i]) { 76 fail("Expected byte " + ((int) pExpect[i]) + " at offset " + i + 77 ", got byte " + ((int) pGot[i])); 78 } 79 } 80 } 81 } 82 83 protected void unmarshalMarshalUnmarshal(Class pClass, String pXML, boolean pIndenting) 84 throws Exception { 85 Object o = unmarshal(pClass, pXML); 86 assertNotNull(o); 87 String s = marshal(o, pClass, pIndenting); 88 assertEquals(pXML, s); 89 Object comp = unmarshal(pClass, s); 90 assertNotNull(comp); 91 } 92 93 protected void unmarshalMarshalUnmarshal(Class pClass, String pXML) throws Exception { 94 unmarshalMarshalUnmarshal(pClass, pXML, true); 95 } 96 97 protected Object unmarshal(Class pClass, String pXML) throws JAXBException { 98 JAXBContext context = JAXBContext.newInstance(getPackageName(pClass)); 99 Unmarshaller unmarshaller = context.createUnmarshaller(); 100 return unmarshaller.unmarshal(new InputSource (new StringReader (pXML))); 101 } 102 103 protected String marshal(Object o, Class pClass) throws JAXBException { 104 return marshal(o, pClass, true); 105 } 106 107 protected String marshal(Object o, Class pClass, boolean pIndenting) throws JAXBException { 108 JAXBContext context = JAXBContext.newInstance(getPackageName(pClass)); 109 Marshaller marshaller = context.createMarshaller(); 110 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, pIndenting ? Boolean.TRUE : Boolean.FALSE); 111 marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE); 112 StringWriter sw = new StringWriter (); 113 marshaller.marshal(o, sw); 114 return sw.toString(); 115 } 116 } 117 | Popular Tags |