KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap > TestHeaderAttrs


1 /*
2  * Copyright 2001-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.soap;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Constants;
22 import org.apache.axis.SimpleTargetedChain;
23 import org.apache.axis.client.Call;
24 import org.apache.axis.client.Service;
25 import org.apache.axis.configuration.SimpleProvider;
26 import org.apache.axis.handlers.soap.SOAPService;
27 import org.apache.axis.message.SOAPHeaderElement;
28 import org.apache.axis.providers.java.RPCProvider;
29 import org.apache.axis.server.AxisServer;
30 import org.apache.axis.soap.SOAPConstants;
31 import org.apache.axis.transport.local.LocalResponder;
32 import org.apache.axis.transport.local.LocalTransport;
33
34 import java.util.Random JavaDoc;
35
36 /**
37  * A fairly comprehensive test of MustUnderstand/Actor combinations.
38  *
39  * @author Glen Daniels (gdaniels@apache.org)
40  */

41 public class TestHeaderAttrs extends TestCase {
42     static final String JavaDoc PROP_DOUBLEIT = "double_result";
43     
44     static final String JavaDoc GOOD_HEADER_NS = "http://testMU/";
45     static final String JavaDoc GOOD_HEADER_NAME = "doubleIt";
46     
47     static final String JavaDoc BAD_HEADER_NS = "http://incorrect-ns/";
48     static final String JavaDoc BAD_HEADER_NAME = "startThermonuclearWar";
49     
50     static final String JavaDoc ACTOR = "http://some.actor/";
51     
52     static SOAPHeaderElement goodHeader =
53                                        new SOAPHeaderElement(GOOD_HEADER_NS,
54                                                              GOOD_HEADER_NAME);
55     static SOAPHeaderElement badHeader =
56                                        new SOAPHeaderElement(BAD_HEADER_NS,
57                                                              BAD_HEADER_NAME);
58
59     private SimpleProvider provider = new SimpleProvider();
60     private AxisServer engine = new AxisServer(provider);
61     private LocalTransport localTransport = new LocalTransport(engine);
62
63     static final String JavaDoc localURL = "local:///testService";
64
65     // Which SOAP version are we using? Default to SOAP 1.1
66
protected SOAPConstants soapVersion = SOAPConstants.SOAP11_CONSTANTS;
67
68     public TestHeaderAttrs(String JavaDoc name) {
69         super(name);
70     }
71     
72     /**
73      * Prep work. Make a server, tie a LocalTransport to it, and deploy
74      * our little test service therein.
75      */

76     public void setUp() throws Exception JavaDoc {
77         engine.init();
78         localTransport.setUrl(localURL);
79         
80         SOAPService service = new SOAPService(new TestHandler(),
81                                               new RPCProvider(),
82                                               null);
83         
84         service.setOption("className", TestService.class.getName());
85         service.setOption("allowedMethods", "*");
86         
87         provider.deployService("testService", service);
88
89         SimpleTargetedChain serverTransport =
90                 new SimpleTargetedChain(null, null, new LocalResponder());
91         provider.deployTransport("local", serverTransport);
92     }
93     
94     /**
95      * Test an unrecognized header with MustUnderstand="true"
96      */

97     public void testMUBadHeader() throws Exception JavaDoc
98     {
99         // 1. MU header to unrecognized actor -> should work fine
100
badHeader.setActor(ACTOR);
101         badHeader.setMustUnderstand(true);
102         
103         assertTrue("Bad result from test", runTest(badHeader, false));
104         
105         // 2. MU header to NEXT -> should fail
106
badHeader.setActor(soapVersion.getNextRoleURI());
107         badHeader.setMustUnderstand(true);
108         
109         // Test (should produce MU failure)
110
try {
111             runTest(badHeader, false);
112         } catch (Exception JavaDoc e) {
113             assertTrue("Non AxisFault Exception : " + e,
114                        e instanceof AxisFault);
115             AxisFault fault = (AxisFault)e;
116             assertEquals("Bad fault code!", soapVersion.getMustunderstandFaultQName(),
117                          fault.getFaultCode());
118             return;
119         }
120         
121         fail("Should have gotten mustUnderstand fault!");
122     }
123     
124     /**
125      * Test an unrecognized header with MustUnderstand="false"
126      */

127     public void testNonMUBadHeader() throws Exception JavaDoc
128     {
129         badHeader.setActor(soapVersion.getNextRoleURI());
130         badHeader.setMustUnderstand(false);
131
132         assertTrue("Non-MU bad header to next actor returned bad result!",
133                    runTest(badHeader, false));
134
135         badHeader.setActor(ACTOR);
136         
137         assertTrue("Non-MU bad header to unrecognized actor returned bad result!",
138                    runTest(badHeader, false));
139     }
140     
141     /**
142      * Test a recognized header (make sure it has the desired result)
143      */

144     public void testGoodHeader() throws Exception JavaDoc
145     {
146         goodHeader.setActor(soapVersion.getNextRoleURI());
147         assertTrue("Good header with next actor returned bad result!",
148                    runTest(goodHeader, true));
149     }
150     
151     /**
152      * Test a recognized header with a particular actor attribute
153      */

154     public void testGoodHeaderWithActors() throws Exception JavaDoc
155     {
156         // 1. Good header to unrecognized actor -> should be ignored, and
157
// we should get a non-doubled result
158
goodHeader.setActor(ACTOR);
159         assertTrue("Good header with unrecognized actor returned bad result!",
160                    runTest(goodHeader, false));
161         
162         // Now tell the engine to recognize the ACTOR value
163
engine.addActorURI(ACTOR);
164         
165         // 2. Good header should now be processed and return doubled result
166
assertTrue("Good header with recognized actor returned bad result!",
167                    runTest(goodHeader, true));
168         
169         engine.removeActorURI(ACTOR);
170     }
171     
172     /**
173      * Call the service with a random string. Returns true if the result
174      * is the length of the string (doubled if the doubled arg is true).
175      */

176     public boolean runTest(SOAPHeaderElement header,
177                            boolean doubled) throws Exception JavaDoc
178     {
179         Call call = new Call(new Service());
180         call.setSOAPVersion(soapVersion);
181         call.setTransport(localTransport);
182         
183         call.addHeader(header);
184         
185         String JavaDoc str = "a";
186         int maxChars = new Random JavaDoc().nextInt(50);
187         for (int i = 0; i < maxChars; i++) {
188             str += "a";
189         }
190
191         Integer JavaDoc i = (Integer JavaDoc)call.invoke("countChars", new Object JavaDoc [] { str });
192         
193         int desiredResult = str.length();
194         if (doubled) desiredResult = desiredResult * 2;
195         
196         return (i.intValue() == desiredResult);
197     }
198 }
199
Popular Tags