1 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 ; 35 36 41 public class TestHeaderAttrs extends TestCase { 42 static final String PROP_DOUBLEIT = "double_result"; 43 44 static final String GOOD_HEADER_NS = "http://testMU/"; 45 static final String GOOD_HEADER_NAME = "doubleIt"; 46 47 static final String BAD_HEADER_NS = "http://incorrect-ns/"; 48 static final String BAD_HEADER_NAME = "startThermonuclearWar"; 49 50 static final String 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 localURL = "local:///testService"; 64 65 protected SOAPConstants soapVersion = SOAPConstants.SOAP11_CONSTANTS; 67 68 public TestHeaderAttrs(String name) { 69 super(name); 70 } 71 72 76 public void setUp() throws Exception { 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 97 public void testMUBadHeader() throws Exception 98 { 99 badHeader.setActor(ACTOR); 101 badHeader.setMustUnderstand(true); 102 103 assertTrue("Bad result from test", runTest(badHeader, false)); 104 105 badHeader.setActor(soapVersion.getNextRoleURI()); 107 badHeader.setMustUnderstand(true); 108 109 try { 111 runTest(badHeader, false); 112 } catch (Exception 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 127 public void testNonMUBadHeader() throws Exception 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 144 public void testGoodHeader() throws Exception 145 { 146 goodHeader.setActor(soapVersion.getNextRoleURI()); 147 assertTrue("Good header with next actor returned bad result!", 148 runTest(goodHeader, true)); 149 } 150 151 154 public void testGoodHeaderWithActors() throws Exception 155 { 156 goodHeader.setActor(ACTOR); 159 assertTrue("Good header with unrecognized actor returned bad result!", 160 runTest(goodHeader, false)); 161 162 engine.addActorURI(ACTOR); 164 165 assertTrue("Good header with recognized actor returned bad result!", 167 runTest(goodHeader, true)); 168 169 engine.removeActorURI(ACTOR); 170 } 171 172 176 public boolean runTest(SOAPHeaderElement header, 177 boolean doubled) throws Exception 178 { 179 Call call = new Call(new Service()); 180 call.setSOAPVersion(soapVersion); 181 call.setTransport(localTransport); 182 183 call.addHeader(header); 184 185 String str = "a"; 186 int maxChars = new Random ().nextInt(50); 187 for (int i = 0; i < maxChars; i++) { 188 str += "a"; 189 } 190 191 Integer i = (Integer )call.invoke("countChars", new Object [] { str }); 192 193 int desiredResult = str.length(); 194 if (doubled) desiredResult = desiredResult * 2; 195 196 return (i.intValue() == desiredResult); 197 } 198 } 199 | Popular Tags |