| 1 |
package test.encoding; |
| 2 |
|
| 3 |
import junit.framework.TestCase; |
| 4 |
import org.apache.axis.Constants; |
| 5 |
import org.apache.axis.Message; |
| 6 |
import org.apache.axis.MessageContext; |
| 7 |
import org.apache.axis.components.logger.LogFactory; |
| 8 |
import org.apache.axis.configuration.BasicServerConfig; |
| 9 |
import org.apache.axis.encoding.SerializationContext; |
| 10 |
import org.apache.axis.encoding.SerializationContext; |
| 11 |
import org.apache.axis.encoding.TypeMapping; |
| 12 |
import org.apache.axis.encoding.TypeMappingRegistry; |
| 13 |
import org.apache.axis.encoding.ser.BeanDeserializerFactory; |
| 14 |
import org.apache.axis.encoding.ser.BeanSerializerFactory; |
| 15 |
import org.apache.axis.encoding.ser.SimpleDeserializerFactory; |
| 16 |
import org.apache.axis.encoding.ser.SimpleSerializerFactory; |
| 17 |
import org.apache.axis.message.RPCElement; |
| 18 |
import org.apache.axis.message.RPCParam; |
| 19 |
import org.apache.axis.message.SOAPEnvelope; |
| 20 |
import org.apache.axis.server.AxisServer; |
| 21 |
import org.apache.commons.logging.Log; |
| 22 |
|
| 23 |
import javax.xml.namespace.QName; |
| 24 |
import java.io.StringWriter; |
| 25 |
import java.io.Writer; |
| 26 |
import java.util.Vector; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Test the serialization of a bean with attributes |
| 31 |
* |
| 32 |
* @author Tom Jordahl ([email protected]) |
| 33 |
* @author Glen Daniels ([email protected]) |
| 34 |
*/ |
| 35 |
public class TestAttributes extends TestCase { |
| 36 |
static Log log = |
| 37 |
LogFactory.getLog(TestAttributes.class.getName()); |
| 38 |
|
| 39 |
public static final String myNS = "urn:myNS"; |
| 40 |
|
| 41 |
public TestAttributes(String name) { |
| 42 |
super(name); |
| 43 |
} |
| 44 |
|
| 45 |
public void testBean () throws Exception { |
| 46 |
MessageContext msgContext = new MessageContext(new AxisServer(new BasicServerConfig())); |
| 47 |
SOAPEnvelope msg = new SOAPEnvelope(); |
| 48 |
|
| 49 |
// Create bean with data |
| 50 |
AttributeBean bean = new AttributeBean(); |
| 51 |
bean.setAge(35); |
| 52 |
bean.setID(1.15F); |
| 53 |
bean.setMale(true); |
| 54 |
bean.company = "Majesty's Secret Service"; |
| 55 |
bean.setName("James Bond"); |
| 56 |
|
| 57 |
RPCParam arg = new RPCParam("", "struct", bean); |
| 58 |
RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg }); |
| 59 |
msg.addBodyElement(body); |
| 60 |
body.setEncodingStyle(null); |
| 61 |
|
| 62 |
Writer stringWriter = new StringWriter(); |
| 63 |
SerializationContext context = new SerializationContext(stringWriter, msgContext); |
| 64 |
context.setDoMultiRefs(false); // no multirefs |
| 65 |
context.setPretty(false); |
| 66 |
|
| 67 |
// Create a TypeMapping and register the Bean serializer/deserializer |
| 68 |
TypeMappingRegistry reg = context.getTypeMappingRegistry(); |
| 69 |
TypeMapping tm = (TypeMapping) reg.createTypeMapping(); |
| 70 |
// The "" namespace is literal (no encoding). |
| 71 |
tm.setSupportedEncodings(new String[] {Constants.URI_DEFAULT_SOAP_ENC}); |
| 72 |
reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm); |
| 73 |
|
| 74 |
QName beanQName = new QName("typeNS", "TheBean"); |
| 75 |
tm.register(AttributeBean.class, |
| 76 |
beanQName, |
| 77 |
new BeanSerializerFactory(AttributeBean.class, beanQName), |
| 78 |
new BeanDeserializerFactory(AttributeBean.class, beanQName)); |
| 79 |
|
| 80 |
// Serialize the bean in to XML |
| 81 |
msg.output(context); |
| 82 |
// Get the XML as a string |
| 83 |
String msgString = stringWriter.toString(); |
| 84 |
|
| 85 |
log.debug("---"); |
| 86 |
log.debug(msgString); |
| 87 |
log.debug("---"); |
| 88 |
|
| 89 |
System.out.println(msgString); |
| 90 |
|
| 91 |
Message message = new Message(msgString); |
| 92 |
message.setMessageContext(msgContext); |
| 93 |
SOAPEnvelope env = message.getSOAPEnvelope(); |
| 94 |
RPCElement rpcEl = (RPCElement)env.getFirstBody(); |
| 95 |
Vector params = rpcEl.getParams(); |
| 96 |
assertEquals("Wrong # of params in deserialized message!", |
| 97 |
1, |
| 98 |
params.size()); |
| 99 |
|
| 100 |
Object obj = ((RPCParam)params.get(0)).getObjectValue(); |
| 101 |
assertTrue("Deserialized param not an AttributeBean!", |
| 102 |
(obj instanceof AttributeBean)); |
| 103 |
AttributeBean deserBean = (AttributeBean)obj; |
| 104 |
assertTrue("Deserialized bean not equal to expected values!", |
| 105 |
(bean.equals(deserBean))); |
| 106 |
} |
| 107 |
|
| 108 |
public void testSimpleType() throws Exception { |
| 109 |
checkSimpleBeanRoundTrip("test value", 85.0F); |
| 110 |
} |
| 111 |
|
| 112 |
public void testSimpleType2() throws Exception { |
| 113 |
//Testcase for 12452 - Axis does not correctly XML-encode ampersands |
| 114 |
checkSimpleBeanRoundTrip("http://mysite.com?a=1&b=2", 85.0F); |
| 115 |
} |
| 116 |
|
| 117 |
public void testSimpleType3() throws Exception { |
| 118 |
//Testcase for 12453 - Axis does not correctly XML-encode <'s and >'s |
| 119 |
checkSimpleBeanRoundTrip("</name>", 85.0F); |
| 120 |
} |
| 121 |
|
| 122 |
private void checkSimpleBeanRoundTrip(String text, float temp) throws Exception { |
| 123 |
SimpleBean bean = new SimpleBean(text); |
| 124 |
bean.temp = temp; |
| 125 |
|
| 126 |
MessageContext msgContext = new MessageContext(new AxisServer(new BasicServerConfig())); |
| 127 |
SOAPEnvelope msg = new SOAPEnvelope(); |
| 128 |
|
| 129 |
RPCParam arg = new RPCParam("", "simple", bean); |
| 130 |
RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg }); |
| 131 |
msg.addBodyElement(body); |
| 132 |
body.setEncodingStyle(null); |
| 133 |
|
| 134 |
StringWriter writer = new StringWriter(); |
| 135 |
SerializationContext context = new SerializationContext(writer, |
| 136 |
msgContext); |
| 137 |
context.setDoMultiRefs(false); |
| 138 |
|
| 139 |
// Create a TypeMapping and register the Bean serializer/deserializer |
| 140 |
TypeMappingRegistry reg = context.getTypeMappingRegistry(); |
| 141 |
TypeMapping tm = (TypeMapping) reg.createTypeMapping(); |
| 142 |
// The "" namespace is literal (no encoding). |
| 143 |
tm.setSupportedEncodings(new String[] {Constants.URI_DEFAULT_SOAP_ENC}); |
| 144 |
reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm); |
| 145 |
|
| 146 |
QName beanQName = new QName("typeNS", "Bean"); |
| 147 |
tm.register(SimpleBean.class, |
| 148 |
beanQName, |
| 149 |
new SimpleSerializerFactory(SimpleBean.class, beanQName), |
| 150 |
new SimpleDeserializerFactory(SimpleBean.class, beanQName)); |
| 151 |
|
| 152 |
// Serialize the bean in to XML |
| 153 |
msg.output(context); |
| 154 |
// Get the XML as a string |
| 155 |
String msgString = writer.toString(); |
| 156 |
|
| 157 |
Message message = new Message(msgString); |
| 158 |
message.setMessageContext(msgContext); |
| 159 |
SOAPEnvelope env = message.getSOAPEnvelope(); |
| 160 |
RPCElement rpcEl = (RPCElement)env.getFirstBody(); |
| 161 |
Vector params = rpcEl.getParams(); |
| 162 |
assertEquals("Wrong # of params in deserialized message!", |
| 163 |
1, |
| 164 |
params.size()); |
| 165 |
|
| 166 |
Object obj = ((RPCParam)params.get(0)).getObjectValue(); |
| 167 |
assertTrue("Deserialized param not a SimpleBean!", |
| 168 |
(obj instanceof SimpleBean)); |
| 169 |
|
| 170 |
SimpleBean deserBean = (SimpleBean)obj; |
| 171 |
assertTrue("Deserialized bean not equal to expected values!", |
| 172 |
(bean.equals(deserBean))); |
| 173 |
} |
| 174 |
} |