1 16 package org.apache.ws.jaxme.junit; 17 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 21 import javax.xml.bind.JAXBContext; 22 import javax.xml.bind.JAXBException; 23 import javax.xml.bind.Marshaller; 24 import javax.xml.bind.Unmarshaller; 25 26 import org.apache.ws.jaxme.impl.JMMarshallerImpl; 27 import org.apache.ws.jaxme.tests.printparse.Test; 28 import org.apache.ws.jaxme.tests.printparse.impl.TestImpl; 29 import org.xml.sax.InputSource ; 30 31 import junit.framework.TestCase; 32 33 34 38 public class PrintParseTest extends TestCase { 39 private String getNamespace() { 40 TestImpl test = new TestImpl(); 41 return test.getQName().getNamespaceURI(); 42 } 43 44 private String getPackageName() { 45 String testClassName = Test.class.getName(); 46 int offset = testClassName.lastIndexOf('.'); 47 return testClassName.substring(0, offset); 48 } 49 50 private JAXBContext getJAXBContext() throws JAXBException { 51 return JAXBContext.newInstance(getPackageName()); 52 } 53 54 56 public void testPrint() throws Exception { 57 boolean[] bools = new boolean[]{false, true}; 58 int[] ints = new int[]{0,1}; 59 for (int i = 0; i < bools.length; i++) { 60 Test test = new TestImpl(); 61 test.setBool(bools[i]); 62 StringWriter sw = new StringWriter (); 63 Marshaller m = getJAXBContext().createMarshaller(); 64 m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE); 65 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); 66 m.marshal(test, sw); 67 String expect = 68 "<Test xmlns=\"" + getNamespace() + "\"><Bool>" + ints[i] + 69 "</Bool></Test>"; 70 String got = sw.toString(); 71 assertEquals(expect, got); 72 } 73 } 74 75 77 public void testParse() throws Exception { 78 boolean[] bools = new boolean[]{false, true}; 79 int[] ints = new int[]{0,1}; 80 for (int i = 0; i < bools.length; i++) { 81 String input = 82 "<Test xmlns=\"" + getNamespace() + "\"><Bool>" + ints[i] + 83 "</Bool></Test>"; 84 Unmarshaller u = getJAXBContext().createUnmarshaller(); 85 Test test = (Test) u.unmarshal(new InputSource (new StringReader (input))); 86 assertEquals(bools[i], test.isBool()); 87 } 88 } 89 } 90 | Popular Tags |