1 17 package org.apache.servicemix.http; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.net.URI ; 22 import java.net.URL ; 23 24 import javax.jbi.messaging.ExchangeStatus; 25 import javax.jbi.messaging.InOut; 26 import javax.jbi.messaging.RobustInOnly; 27 import javax.xml.namespace.QName ; 28 import javax.xml.transform.stream.StreamSource ; 29 30 import junit.framework.TestCase; 31 32 import org.apache.servicemix.client.DefaultServiceMixClient; 33 import org.apache.servicemix.components.http.HttpConnector; 34 import org.apache.servicemix.components.util.EchoComponent; 35 import org.apache.servicemix.jbi.container.ActivationSpec; 36 import org.apache.servicemix.jbi.container.JBIContainer; 37 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 38 import org.apache.servicemix.tck.Receiver; 39 import org.apache.servicemix.tck.ReceiverComponent; 40 41 public class HttpProviderTest extends TestCase { 42 43 protected JBIContainer container; 44 45 protected void setUp() throws Exception { 46 container = new JBIContainer(); 47 container.setUseMBeanServer(false); 48 container.setCreateMBeanServer(false); 49 container.setEmbedded(true); 50 container.init(); 51 } 52 53 protected void tearDown() throws Exception { 54 if (container != null) { 55 container.shutDown(); 56 } 57 } 58 59 protected long testInOnly(String msg, boolean streaming) throws Exception { 60 HttpComponent component = new HttpComponent(); 62 ((HttpLifeCycle) component.getLifeCycle()).getConfiguration() 63 .setStreamingEnabled(streaming); 64 container.activateComponent(component, "HttpProviderTest"); 65 66 Receiver receiver = new ReceiverComponent(); 68 ActivationSpec asReceiver = new ActivationSpec("receiver", receiver); 69 asReceiver.setService(new QName ("test", "receiver")); 70 container.activateComponent(asReceiver); 71 72 HttpConnector connector = new HttpConnector("localhost", 8192); 74 connector.setDefaultInOut(false); 75 ActivationSpec asConnector = new ActivationSpec("connector", connector); 76 asConnector.setDestinationService(new QName ("test", "receiver")); 77 container.activateComponent(asConnector); 78 79 container.start(); 81 82 URL url = getClass().getClassLoader().getResource("provider/http.wsdl"); 84 File path = new File (new URI (url.toString())); 85 path = path.getParentFile(); 86 component.getServiceUnitManager().deploy("provider", 87 path.getAbsolutePath()); 88 component.getServiceUnitManager().start("provider"); 89 90 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 92 RobustInOnly in = client.createRobustInOnlyExchange(); 93 in.setInterfaceName(new QName ("http://http.servicemix.org/Test", 94 "ProviderInterface")); 95 in.getInMessage().setContent( 96 new StreamSource (new ByteArrayInputStream (msg.getBytes()))); 97 98 long t0 = System.currentTimeMillis(); 99 client.sendSync(in); 100 long t1 = System.currentTimeMillis(); 101 assertTrue(in.getStatus() == ExchangeStatus.DONE); 102 103 receiver.getMessageList().assertMessagesReceived(1); 105 106 component.getServiceUnitManager().stop("provider"); 107 component.getServiceUnitManager().shutDown("provider"); 108 component.getServiceUnitManager().undeploy("provider", 109 path.getAbsolutePath()); 110 111 return t1 - t0; 112 } 113 114 protected String testInOut(String msg, boolean streaming) throws Exception { 115 HttpComponent component = new HttpComponent(); 117 ((HttpLifeCycle) component.getLifeCycle()).getConfiguration() 118 .setStreamingEnabled(streaming); 119 container.activateComponent(component, "HTTPComponent"); 120 121 EchoComponent echo = new EchoComponent(); 123 ActivationSpec asReceiver = new ActivationSpec("echo", echo); 124 asReceiver.setService(new QName ("test", "echo")); 125 container.activateComponent(asReceiver); 126 127 HttpConnector connector = new HttpConnector("localhost", 8192); 129 connector.setDefaultInOut(true); 130 ActivationSpec asConnector = new ActivationSpec("connector", connector); 131 asConnector.setDestinationService(new QName ("test", "echo")); 132 container.activateComponent(asConnector); 133 134 container.start(); 136 137 URL url = getClass().getClassLoader().getResource("provider/http.wsdl"); 139 File path = new File (new URI (url.toString())); 140 path = path.getParentFile(); 141 component.getServiceUnitManager().deploy("provider", 142 path.getAbsolutePath()); 143 component.getServiceUnitManager().start("provider"); 144 145 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 147 InOut inout = client.createInOutExchange(); 148 inout.setInterfaceName(new QName ("http://http.servicemix.org/Test", 149 "ProviderInterface")); 150 inout.getInMessage().setContent( 151 new StreamSource (new ByteArrayInputStream (msg.getBytes()))); 152 153 long t0 = System.currentTimeMillis(); 154 client.sendSync(inout); 155 long t1 = System.currentTimeMillis(); 156 assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE); 157 158 assertNotNull(inout.getOutMessage()); 160 assertNotNull(inout.getOutMessage().getContent()); 161 SourceTransformer sourceTransformer = new SourceTransformer(); 162 String reply = sourceTransformer.toString(inout.getOutMessage() 163 .getContent()); 164 String inputMesage = sourceTransformer.toString(new StreamSource ( 165 new ByteArrayInputStream (msg.getBytes()))); 166 System.out.println("Msg Sent [" + inputMesage + "]"); 167 System.out.println("Msg Recieved [" + reply + "]"); 168 169 assertEquals(inputMesage.length(), reply.length()); 170 assertEquals(inputMesage, reply); 171 172 component.getServiceUnitManager().stop("provider"); 173 component.getServiceUnitManager().shutDown("provider"); 174 component.getServiceUnitManager().undeploy("provider", 175 path.getAbsolutePath()); 176 177 System.out.println("Executed in " + (t1 - t0) + "ms"); 178 179 return reply; 180 } 181 182 public void testInOnly() throws Exception { 183 testInOnly("<hello>world</hello>", false); 184 } 185 186 public void testInOut() throws Exception { 187 testInOut("<hello>world</hello>", true); 188 } 189 190 public void testPerfInOnlyWithBigMessage() throws Exception { 191 int nbRuns = 10; 192 int sizeInKb = 64; 193 194 StringBuffer sb = new StringBuffer (); 195 sb.append("<hello>\n"); 196 for (int i = 0; i < sizeInKb; i++) { 197 sb.append("\t<hello>"); 198 for (int j = 0; j < 1024 - 15; j++) { 199 sb 200 .append((char) ('A' + (int) (Math.random() * ('Z' - 'A' + 1)))); 201 } 202 sb.append("</hello>\n"); 203 } 204 sb.append("</hello>\n"); 205 String str = sb.toString(); 206 207 212 213 for (int i = 0; i < nbRuns; i++) { 214 System.gc(); 215 long dt = testInOnly(str, true); 216 System.err.println("Streaming: " + dt); 217 tearDown(); 218 setUp(); 219 } 220 } 221 222 public void testInOutWithBigMessage() throws Exception { 223 int sizeInKb = 640*1024; 224 225 StringBuffer sb = new StringBuffer (); 226 sb.append("<hello>\n"); 227 228 for (int j = 0; j < sizeInKb - 15; j++) { 229 sb.append((char) ('A' + (int) (Math.random() * ('Z' - 'A' + 1)))); 230 } 231 232 sb.append("</hello>\n"); 233 String str = sb.toString(); 234 235 testInOut(str, true); 236 } 237 } 238 | Popular Tags |