1   /*
2    *
3    * The Seasar Software License, Version 1.1
4    *
5    * Copyright (c) 2003-2004 The Seasar Project. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   *
11   * 1. Redistributions of source code must retain the above
12   *    copyright notice, this list of conditions and the following
13   *    disclaimer.
14   *
15   * 2. Redistributions in binary form must reproduce the above
16   *    copyright notice, this list of conditions and the following
17   *    disclaimer in the documentation and/or other materials provided
18   *    with the distribution.
19   *
20   * 3. The end-user documentation included with the redistribution,
21   *    if any, must include the following acknowledgement:
22   *    "This product includes software developed by the
23   *    Seasar Project (http://www.seasar.org/)."
24   *    Alternately, this acknowledgement may appear in the software
25   *    itself, if and wherever such third-party acknowledgements
26   *    normally appear.
27   *
28   * 4. Neither the name "The Seasar Project" nor the names of its
29   *    contributors may be used to endorse or promote products derived
30   *    from this software without specific prior written permission of
31   *    the Seasar Project.
32   *
33   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR
34   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE SEASAR PROJECT
37   * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38   * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42   * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
43   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  package org.seasar.remoting.axis.deployment;
47  
48  import java.awt.Color;
49  import java.io.InputStream;
50  import java.io.Serializable;
51  import java.util.Arrays;
52  import java.util.Enumeration;
53  import java.util.HashSet;
54  import java.util.Set;
55  import java.util.Vector;
56  
57  import javax.xml.namespace.QName;
58  
59  import org.apache.axis.deployment.wsdd.WSDDConstants;
60  import org.apache.axis.deployment.wsdd.WSDDTypeMapping;
61  import org.apache.axis.encoding.ser.BeanDeserializerFactory;
62  import org.apache.axis.encoding.ser.BeanSerializerFactory;
63  import org.apache.axis.providers.java.JavaProvider;
64  import org.apache.axis.utils.XMLUtils;
65  import org.seasar.extension.unit.S2TestCase;
66  import org.seasar.framework.container.ComponentDef;
67  import org.seasar.framework.util.ResourceUtil;
68  import org.seasar.remoting.axis.ServiceDef;
69  import org.seasar.remoting.axis.TypeMappingDef;
70  import org.w3c.dom.Element;
71  import org.w3c.dom.Node;
72  import org.w3c.dom.NodeList;
73  
74  /***
75   * @author koichik
76   */
77  public class WSDDS2ServiceTest extends S2TestCase {
78      public WSDDS2ServiceTest(String name) {
79          super(name);
80      }
81  
82      public void setUp() {
83          include("WSDDS2ServiceTest.dicon");
84      }
85  
86      public void testNoInterface() throws Exception {
87          ComponentDef cd = getComponentDef("no");
88          WSDDS2Service s2Service = new WSDDS2Service(cd);
89  
90          assertEquals("1", new QName("no"), s2Service.getQName());
91          assertEquals("2", "no", s2Service.getServiceDesc().getName());
92          assertEquals("3", "org.seasar.remoting.axis.deployment.WSDDS2ServiceTest$NoInterface",
93                  s2Service.getParameter(JavaProvider.OPTION_CLASSNAME));
94          assertEquals("4", new QName(WSDDConstants.URI_WSDD_JAVA, "S2RPC"), s2Service
95                  .getProviderQName());
96          assertNull("5", s2Service.getParameter(JavaProvider.OPTION_ALLOWEDMETHODS));
97      }
98  
99      public void testIgnoreInterface() throws Exception {
100         ComponentDef cd = getComponentDef("ignore");
101         WSDDS2Service s2Service = new WSDDS2Service(cd);
102 
103         assertEquals("1", new QName("ignore"), s2Service.getQName());
104         assertEquals("2", "ignore", s2Service.getServiceDesc().getName());
105         assertEquals("3", "org.seasar.remoting.axis.deployment.WSDDS2ServiceTest$IgnoreInterface",
106                 s2Service.getParameter(JavaProvider.OPTION_CLASSNAME));
107         assertEquals("4", new QName(WSDDConstants.URI_WSDD_JAVA, "S2RPC"), s2Service
108                 .getProviderQName());
109         assertNull("5", s2Service.getParameter(JavaProvider.OPTION_ALLOWEDMETHODS));
110     }
111 
112     public void testOneInterface() throws Exception {
113         ComponentDef cd = getComponentDef("one");
114         WSDDS2Service s2Service = new WSDDS2Service(cd);
115 
116         assertEquals("1", new QName("one"), s2Service.getQName());
117         assertEquals("2", "one", s2Service.getServiceDesc().getName());
118         assertEquals("3", "java.lang.Runnable", s2Service
119                 .getParameter(JavaProvider.OPTION_CLASSNAME));
120         assertEquals("4", new QName(WSDDConstants.URI_WSDD_JAVA, "S2RPC"), s2Service
121                 .getProviderQName());
122         assertNull("5", s2Service.getParameter(JavaProvider.OPTION_ALLOWEDMETHODS));
123     }
124 
125     public void testTwoInterface() throws Exception {
126         ComponentDef cd = getComponentDef("two");
127         WSDDS2Service s2Service = new WSDDS2Service(cd);
128 
129         assertEquals("1", new QName("two"), s2Service.getQName());
130         assertEquals("2", "two", s2Service.getServiceDesc().getName());
131         assertEquals("3", "org.seasar.remoting.axis.deployment.WSDDS2ServiceTest$TwoInterface",
132                 s2Service.getParameter(JavaProvider.OPTION_CLASSNAME));
133         assertEquals("4", new QName(WSDDConstants.URI_WSDD_JAVA, "S2RPC"), s2Service
134                 .getProviderQName());
135 
136         Set allowMethods = new HashSet(Arrays.asList(s2Service.getParameter(
137                 JavaProvider.OPTION_ALLOWEDMETHODS).split(" ")));
138         assertEquals("5", 3, allowMethods.size());
139         assertTrue("6", allowMethods.contains("run"));
140         assertTrue("7", allowMethods.contains("hasMoreElements"));
141         assertTrue("8", allowMethods.contains("nextElement"));
142     }
143 
144     public void testServiceType() throws Exception {
145         ComponentDef cd = getComponentDef("two");
146         ServiceDef sd = new ServiceDef();
147         sd.setServiceType(Comparable.class);
148         WSDDS2Service s2Service = new WSDDS2Service(cd, sd);
149 
150         assertEquals("1", "java.lang.Comparable", s2Service
151                 .getParameter(JavaProvider.OPTION_CLASSNAME));
152         assertNull("2", s2Service.getParameter(JavaProvider.OPTION_ALLOWEDMETHODS));
153     }
154 
155     public void testAlloeMethods() throws Exception {
156         ComponentDef cd = getComponentDef("two");
157         ServiceDef sd = new ServiceDef();
158         sd.setAllowedMethods("run hasMoreElements");
159         WSDDS2Service s2Service = new WSDDS2Service(cd, sd);
160 
161         assertEquals("1", "org.seasar.remoting.axis.deployment.WSDDS2ServiceTest$TwoInterface",
162                 s2Service.getParameter(JavaProvider.OPTION_CLASSNAME));
163 
164         Set allowMethods = new HashSet(Arrays.asList(s2Service.getParameter(
165                 JavaProvider.OPTION_ALLOWEDMETHODS).split(" ")));
166         assertEquals("2", 2, allowMethods.size());
167         assertTrue("3", allowMethods.contains("run"));
168         assertTrue("4", allowMethods.contains("hasMoreElements"));
169     }
170 
171     public void testProvider() throws Exception {
172         ComponentDef cd = getComponentDef("no");
173         ServiceDef sd = new ServiceDef();
174         sd.setProvider("MSG");
175         WSDDS2Service s2Service = new WSDDS2Service(cd, sd);
176 
177         assertEquals("1", new QName(WSDDConstants.URI_WSDD_JAVA, "S2MSG"), s2Service
178                 .getProviderQName());
179     }
180 
181     public void testTypeMapping() throws Exception {
182         ComponentDef cd = getComponentDef("no");
183         TypeMappingDef tmd = new TypeMappingDef();
184         tmd.setType(Color.class);
185         tmd.setLocalPart("COLOR");
186         tmd.setNamespaceURI("http://www.seasar.org/");
187         tmd.setNamespacePrefix("cc");
188         ServiceDef sd = new ServiceDef();
189         sd.addTypeMapping(tmd);
190         WSDDS2Service s2Service = new WSDDS2Service(cd, sd);
191 
192         Vector typeMappings = s2Service.getTypeMappings();
193         assertEquals("1", 1, typeMappings.size());
194         WSDDTypeMapping tm = (WSDDTypeMapping) typeMappings.get(0);
195         assertEquals("2", new QName("http://www.seasar.org/", "COLOR", "cc"), tm.getQName());
196         assertEquals("3", BeanSerializerFactory.class, tm.getSerializer());
197         assertEquals("4", BeanDeserializerFactory.class, tm.getDeserializer());
198     }
199 
200     public void testWSDD() throws Exception {
201         InputStream is = ResourceUtil
202                 .getResourceAsStream("org/seasar/remoting/axis/deployment/WSDDS2ServiceTest.wsdd");
203         Element documentElement = XMLUtils.newDocument(is).getDocumentElement();
204         Element serviceElement = null;
205         NodeList children = documentElement.getChildNodes();
206         for (int i = 0; i < children.getLength(); i++) {
207             Node thisNode = children.item(i);
208             if (thisNode instanceof Element) {
209                 serviceElement = (Element) thisNode;
210                 break;
211             }
212         }
213 
214         ComponentDef cd = getComponentDef("one");
215         WSDDS2Service s2Service = new WSDDS2Service(cd, serviceElement);
216         assertEquals("0", new QName(WSDDConstants.URI_WSDD_JAVA, "S2RPC"), s2Service
217                 .getProviderQName());
218     }
219 
220     public static class NoInterface {
221         public void foo() {
222         }
223     }
224 
225     public static class IgnoreInterface implements Serializable {
226         public void foo() {
227         }
228     }
229 
230     public static class OneInterface implements Runnable, Serializable {
231         public void run() {
232         }
233     }
234 
235     public static class TwoInterface implements Runnable, Enumeration, Serializable {
236         public void run() {
237         }
238 
239         public boolean hasMoreElements() {
240             return false;
241         }
242 
243         public Object nextElement() {
244             return null;
245         }
246     }
247 }