1 package test.saaj; 2 3 import javax.xml.soap.MessageFactory ; 4 import javax.xml.soap.SOAPBody ; 5 import javax.xml.soap.SOAPElement ; 6 import javax.xml.soap.SOAPEnvelope ; 7 import javax.xml.soap.SOAPMessage ; 8 import javax.xml.soap.SOAPPart ; 9 import javax.xml.soap.MimeHeaders ; 10 import javax.xml.soap.Name ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.ByteArrayInputStream ; 13 import java.util.Iterator ; 14 15 18 public class TestPrefixes extends junit.framework.TestCase { 19 20 public TestPrefixes(String name) { 21 super(name); 22 } 23 24 28 public void testAddingPrefixesForChildElements() throws Exception { 29 MessageFactory factory = MessageFactory.newInstance(); 30 SOAPMessage msg = factory.createMessage(); 31 SOAPPart sp = msg.getSOAPPart(); 32 SOAPEnvelope se = sp.getEnvelope(); 33 SOAPBody sb = se.getBody(); 34 SOAPElement el1 = sb.addBodyElement(se.createName 35 ("element1", "prefix1", "http://www.sun.com")); 36 SOAPElement el2 = el1.addChildElement(se.createName 37 ("element2", "prefix2", "http://www.apache.org")); 38 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 39 msg.writeTo(baos); 40 String xml = new String (baos.toByteArray()); 41 assertTrue(xml.indexOf("prefix1") != -1); 42 assertTrue(xml.indexOf("prefix2") != -1); 43 assertTrue(xml.indexOf("http://www.sun.com") != -1); 44 assertTrue(xml.indexOf("http://www.apache.org") != -1); 45 } 46 47 public void testAttribute() throws Exception { 48 String soappacket = "<SOAP-ENV:Envelope xmlns:SOAP-ENV =\"http://schemas.xmlsoap.org/soap/envelope/\"" + 49 " xmlns:xsi =\"http://www.w3.org/1999/XMLSchema-instance\"" + 50 " xmlns:xsd =\"http://www.w3.org/1999/XMLSchema\">" + 51 " <SOAP-ENV:Body> " + 52 " <helloworld name=\"tester\" />" + 53 " </SOAP-ENV:Body>" + 54 "</SOAP-ENV:Envelope>"; 55 SOAPMessage msg = MessageFactory.newInstance().createMessage(new MimeHeaders (), new ByteArrayInputStream (soappacket.getBytes())); 56 SOAPBody body = msg.getSOAPPart().getEnvelope().getBody(); 57 58 SOAPElement ele = (SOAPElement ) body.getChildElements().next(); 59 Iterator attit = ele.getAllAttributes(); 60 61 Name n = (Name ) attit.next(); 62 assertEquals("Test fail prefix problem",n.getQualifiedName(),"name"); 63 } 64 } 65 | Popular Tags |