File: TestHeaderAttrs.java

package info (click to toggle)
axis 1.4-29
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 52,100 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (198 lines) | stat: -rw-r--r-- 7,124 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test.soap;

import junit.framework.TestCase;
import org.apache.axis.AxisFault;
import org.apache.axis.Constants;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.providers.java.RPCProvider;
import org.apache.axis.server.AxisServer;
import org.apache.axis.soap.SOAPConstants;
import org.apache.axis.transport.local.LocalResponder;
import org.apache.axis.transport.local.LocalTransport;

import java.util.Random;

/**
 * A fairly comprehensive test of MustUnderstand/Actor combinations.
 *
 * @author Glen Daniels (gdaniels@apache.org)
 */
public class TestHeaderAttrs extends TestCase {
    static final String PROP_DOUBLEIT = "double_result";
    
    static final String GOOD_HEADER_NS = "http://testMU/";
    static final String GOOD_HEADER_NAME = "doubleIt";
    
    static final String BAD_HEADER_NS = "http://incorrect-ns/";
    static final String BAD_HEADER_NAME = "startThermonuclearWar";
    
    static final String ACTOR = "http://some.actor/";
    
    static SOAPHeaderElement goodHeader = 
                                       new SOAPHeaderElement(GOOD_HEADER_NS, 
                                                             GOOD_HEADER_NAME);
    static SOAPHeaderElement badHeader = 
                                       new SOAPHeaderElement(BAD_HEADER_NS, 
                                                             BAD_HEADER_NAME);

    private SimpleProvider provider = new SimpleProvider();
    private AxisServer engine = new AxisServer(provider);
    private LocalTransport localTransport = new LocalTransport(engine);

    static final String localURL = "local:///testService";

    // Which SOAP version are we using?  Default to SOAP 1.1
    protected SOAPConstants soapVersion = SOAPConstants.SOAP11_CONSTANTS;

    public TestHeaderAttrs(String name) {
        super(name);
    }
    
    /**
     * Prep work.  Make a server, tie a LocalTransport to it, and deploy
     * our little test service therein.
     */ 
    public void setUp() throws Exception {
        engine.init();
        localTransport.setUrl(localURL);
        
        SOAPService service = new SOAPService(new TestHandler(),
                                              new RPCProvider(),
                                              null);
        
        service.setOption("className", TestService.class.getName());
        service.setOption("allowedMethods", "*");
        
        provider.deployService("testService", service);

        SimpleTargetedChain serverTransport =
                new SimpleTargetedChain(null, null, new LocalResponder());
        provider.deployTransport("local", serverTransport);
    }
    
    /**
     * Test an unrecognized header with MustUnderstand="true"
     */ 
    public void testMUBadHeader() throws Exception
    {
        // 1. MU header to unrecognized actor -> should work fine
        badHeader.setActor(ACTOR);
        badHeader.setMustUnderstand(true);
        
        assertTrue("Bad result from test", runTest(badHeader, false));
        
        // 2. MU header to NEXT -> should fail
        badHeader.setActor(soapVersion.getNextRoleURI());
        badHeader.setMustUnderstand(true);
        
        // Test (should produce MU failure)
        try {
            runTest(badHeader, false);
        } catch (Exception e) {
            assertTrue("Non AxisFault Exception : " + e, 
                       e instanceof AxisFault);
            AxisFault fault = (AxisFault)e;
            assertEquals("Bad fault code!", soapVersion.getMustunderstandFaultQName(),
                         fault.getFaultCode());
            return;
        }
        
        fail("Should have gotten mustUnderstand fault!");
    }
    
    /**
     * Test an unrecognized header with MustUnderstand="false"
     */ 
    public void testNonMUBadHeader() throws Exception
    {
        badHeader.setActor(soapVersion.getNextRoleURI());
        badHeader.setMustUnderstand(false);

        assertTrue("Non-MU bad header to next actor returned bad result!",
                   runTest(badHeader, false));

        badHeader.setActor(ACTOR);
        
        assertTrue("Non-MU bad header to unrecognized actor returned bad result!", 
                   runTest(badHeader, false));
    }
    
    /**
     * Test a recognized header (make sure it has the desired result)
     */ 
    public void testGoodHeader() throws Exception
    {
        goodHeader.setActor(soapVersion.getNextRoleURI());
        assertTrue("Good header with next actor returned bad result!",
                   runTest(goodHeader, true));
    }
    
    /**
     * Test a recognized header with a particular actor attribute
     */ 
    public void testGoodHeaderWithActors() throws Exception
    {
        // 1. Good header to unrecognized actor -> should be ignored, and
        //    we should get a non-doubled result
        goodHeader.setActor(ACTOR);
        assertTrue("Good header with unrecognized actor returned bad result!",
                   runTest(goodHeader, false));
        
        // Now tell the engine to recognize the ACTOR value
        engine.addActorURI(ACTOR);
        
        // 2. Good header should now be processed and return doubled result
        assertTrue("Good header with recognized actor returned bad result!",
                   runTest(goodHeader, true));
        
        engine.removeActorURI(ACTOR);
    }
    
    /**
     * Call the service with a random string.  Returns true if the result
     * is the length of the string (doubled if the doubled arg is true).
     */ 
    public boolean runTest(SOAPHeaderElement header,
                           boolean doubled) throws Exception
    {
        Call call = new Call(new Service());
        call.setSOAPVersion(soapVersion);
        call.setTransport(localTransport);
        
        call.addHeader(header);
        
        String str = "a";
        int maxChars = new Random().nextInt(50);
        for (int i = 0; i < maxChars; i++) {
            str += "a";
        }

        Integer i = (Integer)call.invoke("countChars", new Object [] { str });
        
        int desiredResult = str.length();
        if (doubled) desiredResult = desiredResult * 2;
        
        return (i.intValue() == desiredResult);
    }
}