KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestDerivatedBeanSerializer


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package test.encoding;
18
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import javax.xml.namespace.QName JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.axis.Constants;
27 import org.apache.axis.MessageContext;
28 import org.apache.axis.utils.XMLUtils;
29 import org.apache.axis.encoding.SerializationContext;
30 import org.apache.axis.encoding.SerializationContext;
31 import org.apache.axis.encoding.TypeMapping;
32 import org.apache.axis.encoding.TypeMappingRegistry;
33 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
34 import org.apache.axis.encoding.ser.BeanSerializer;
35 import org.apache.axis.encoding.ser.BeanSerializerFactory;
36 import org.apache.axis.server.AxisServer;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40
41 /**
42  * A little testcase for validating the serialization of inherited type.
43  */

44 public class TestDerivatedBeanSerializer extends TestCase {
45     
46     QName JavaDoc superTypeQName = new QName JavaDoc("typeNS", "SuperBean");
47     QName JavaDoc inheritedTypeQName = new QName JavaDoc("typeNS", "DerivatedBean");
48
49
50     StringWriter JavaDoc stringWriter;
51     SerializationContext context;
52
53     /**
54      * Constructor for DerivatedBeanSerializerTest.
55      * @param arg0
56      */

57     public TestDerivatedBeanSerializer(String JavaDoc arg0) {
58         super(arg0);
59     }
60
61     /**
62      * @see TestCase#setUp()
63      */

64     protected void setUp() throws Exception JavaDoc {
65         super.setUp();
66
67         // Initialisation of attribute used in the testMethods.
68
stringWriter = new StringWriter JavaDoc();
69         MessageContext msgContext = new MessageContext(new AxisServer());
70         context = new SerializationContext(stringWriter, msgContext);
71
72         // Create a TypeMapping and register the specialized Type Mapping
73
TypeMappingRegistry reg = context.getTypeMappingRegistry();
74         TypeMapping tm = (TypeMapping) reg.createTypeMapping();
75         tm.setSupportedEncodings(new String JavaDoc[] {Constants.URI_DEFAULT_SOAP_ENC});
76         reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
77
78         tm.register(SuperBean.class, superTypeQName, new BeanSerializerFactory(SuperBean.class,superTypeQName), new BeanDeserializerFactory(SuperBean.class,superTypeQName));
79         tm.register(DerivatedBean.class, inheritedTypeQName, new BeanSerializerFactory(DerivatedBean.class,inheritedTypeQName), new BeanDeserializerFactory(DerivatedBean.class,inheritedTypeQName));
80     }
81
82
83     /**
84      * Test the serialization of an simple sequence. The bean contains three
85      * elements (zero, one, two). The excepted result is something like:
86      * <BR>
87      * <PRE>
88      * &lt;SuperBean&gt;
89      * &lt;zero/&gt;
90      * &lt;one/&gt;
91      * &lt;two/&gt;
92      * &lt;/SuperBean&gt;
93      * </PRE>
94      */

95     /*
96     public void testSuperBeanSerialize() throws Exception {
97         BeanSerializer ser = new BeanSerializer(SuperBean.class, superTypeQName);
98
99         Object object = new SuperBean();
100         ser.serialize(superTypeQName,null,object,context);
101         
102         // Check the result
103         String msgString = stringWriter.toString();
104         StringReader reader = new StringReader(msgString);
105         DOMParser parser = new DOMParser();
106         parser.parse(new InputSource(reader));
107         Document doc = parser.getDocument();
108         
109         // We only test the order of the attributes.
110         NodeList nodes = doc.getFirstChild().getChildNodes();
111         assertEquals("1st Attribute", "zero", nodes.item(0).getLocalName());
112         assertEquals("2nd Attribute", "one", nodes.item(1).getLocalName());
113         assertEquals("3rd Attribute", "two", nodes.item(2).getLocalName());
114     }
115     */

116     
117     /**
118      * Test the serialization of an derivated sequence. The derivated bean contains two elements
119      * (three, four) and the super class has three elements (zero, one, two). The excepted
120      * result is something like: <BR>
121      * <PRE>
122      * &lt;DerivatedBean&gt;
123      * &lt;zero/&gt;
124      * &lt;one/&gt;
125      * &lt;two/&gt;
126      * &lt;three/&gt;
127      * &lt;four/&gt;
128      * &lt;/DerivatedBean&gt;
129      * </PRE>
130      */

131     public void testDerivatedBeanSerialize() throws Exception JavaDoc {
132         BeanSerializer ser = new BeanSerializer(DerivatedBean.class, inheritedTypeQName);
133
134         Object JavaDoc object = new DerivatedBean();
135         ser.serialize(inheritedTypeQName,null,object,context);
136         
137
138         // Check the result
139
String JavaDoc msgString = stringWriter.toString();
140         StringReader JavaDoc reader = new StringReader JavaDoc(msgString);
141         Document JavaDoc doc = XMLUtils.newDocument(new InputSource JavaDoc(reader));
142         
143         NodeList JavaDoc nodes = doc.getFirstChild().getChildNodes();
144         assertEquals("1st Attribute", "zero", nodes.item(0).getLocalName());
145         assertEquals("2nd Attribute", "one", nodes.item(1).getLocalName());
146         assertEquals("3rd Attribute", "two", nodes.item(2).getLocalName());
147         assertEquals("4th Attribute", "three", nodes.item(3).getLocalName());
148         assertEquals("First Attribute", "four", nodes.item(4).getLocalName());
149     }
150
151
152 }
153
154
155
156
157
Popular Tags