1 package test.crispy.server; 2 3 import java.util.Date; 4 import java.util.Properties; 5 6 import junit.framework.TestCase; 7 import net.sf.crispy.Property; 8 import net.sf.crispy.impl.HttpExecutor; 9 import net.sf.crispy.impl.MiniHttpServer; 10 import net.sf.crispy.impl.ServiceManager; 11 import net.sf.crispy.impl.XmlRpcExecutor; 12 import net.sf.crispy.server.HttpServiceEndpoint; 13 import test.crispy.JUnitTestException; 14 import test.crispy.example.SubHttpServiceEndpoint; 15 import test.crispy.example.SubXmlRpcServiceEndpoint; 16 import test.crispy.example.interceptor.AddSecurityTokenModifier; 17 import test.crispy.example.model.Adresse; 18 import test.crispy.example.model.Kunde; 19 import test.crispy.example.service.Echo; 20 import test.crispy.example.service.EchoImpl; 21 22 public class HttpServiceEndpointTest extends TestCase { 23 24 private static MiniHttpServer miniHttpServer = null; 25 26 public HttpServiceEndpointTest() { 27 if (miniHttpServer == null) { 28 miniHttpServer = new MiniHttpServer(8124); 29 30 miniHttpServer.setContext("/crispy"); 31 miniHttpServer.addServlet("/http-test/*", SubHttpServiceEndpoint.class.getName(), Echo.class.getName(), EchoImpl.class.getName()); 32 miniHttpServer.addServlet("/xmlrpc-test/*", SubXmlRpcServiceEndpoint.class.getName(), Echo.class.getName(), EchoImpl.class.getName()); 33 miniHttpServer.start(); 34 } 35 } 36 37 private Properties getPropertiesHttp() { 38 Properties lvProperties = new Properties(); 39 lvProperties.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8124/crispy/http-test"); 40 lvProperties.put(Property.EXECUTOR_CLASS, HttpExecutor.class.getName()); 41 return lvProperties; 42 } 43 44 private Properties getPropertiesXmlRpc() { 45 Properties lvProperties = new Properties(); 46 lvProperties.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8124/crispy/xmlrpc-test"); 47 lvProperties.put(Property.EXECUTOR_CLASS, XmlRpcExecutor.class.getName()); 48 return lvProperties; 49 } 50 51 52 53 54 public void testSimpleEchoHttp() throws Exception { 55 ServiceManager lvServiceManager = new ServiceManager(getPropertiesHttp()); 56 57 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 58 assertNotNull(lvEcho); 59 String lvEchoStr = "Hallo Echo"; 60 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 61 } 62 63 public void testClientAndServersideModifierHttp() throws Exception { 64 ServiceManager lvServiceManager = new ServiceManager(getPropertiesHttp()); 65 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 66 67 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 68 assertNotNull(lvEcho); 69 String lvEchoStr = "Hallo Echo"; 70 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 71 } 72 73 public void testComplexClientAndServersideModifierHttp() throws Exception { 74 ServiceManager lvServiceManager = new ServiceManager(getPropertiesHttp()); 75 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 76 77 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 78 assertNotNull(lvEcho); 79 String lvNewName = "NewName"; 80 Date d = new Date(); 81 Kunde k = new Kunde("OldName"); 82 k.setDate(d); 83 Adresse a = new Adresse(); 84 a.setPlz("123"); 85 a.setOrt("MD"); 86 k.getAdressen().add(a); 87 88 Kunde kAfter = lvEcho.renameKunde(k, lvNewName); 89 assertEquals(lvNewName, kAfter.getName()); 90 assertEquals(d, k.getDate()); 91 assertEquals(1, k.getAdressen().size()); 92 assertEquals("123", ((Adresse) k.getAdressen().get(0)).getPlz()); 93 assertEquals("MD", ((Adresse) k.getAdressen().get(0)).getOrt()); 94 } 95 96 public void testThrowExceptionAndServersideModifierHttp() throws Exception { 97 ServiceManager lvServiceManager = new ServiceManager(getPropertiesHttp()); 98 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 99 100 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 101 assertNotNull(lvEcho); 102 boolean throwException = false; 103 try { 104 lvEcho.throwException("A Test-Excption."); 105 } catch (JUnitTestException e) { 106 throwException = true; 107 } 108 assertTrue("No Exception thrown: " + throwException, throwException); 109 } 110 111 112 113 114 115 public void testSimpleEchoXmlRpc() throws Exception { 116 ServiceManager lvServiceManager = new ServiceManager(getPropertiesXmlRpc()); 117 118 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 119 assertNotNull(lvEcho); 120 String lvEchoStr = "Hallo Echo"; 121 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 122 } 123 124 public void testSimpleClientAndServersideModifierXmlRpc() throws Exception { 125 ServiceManager lvServiceManager = new ServiceManager(getPropertiesXmlRpc()); 126 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 127 128 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 129 assertNotNull(lvEcho); 130 String lvEchoStr = "Hallo Echo"; 131 assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr); 132 } 133 134 public void testComplexClientAndServersideModifierXmlRpc() throws Exception { 135 ServiceManager lvServiceManager = new ServiceManager(getPropertiesXmlRpc()); 136 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 137 138 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 139 assertNotNull(lvEcho); 140 String lvNewName = "NewName"; 141 Date d = new Date(); 142 Kunde k = new Kunde("OldName"); 143 k.setDate(d); 144 Adresse a = new Adresse(); 145 a.setPlz("123"); 146 a.setOrt("MD"); 147 k.getAdressen().add(a); 148 149 Kunde kAfter = lvEcho.renameKunde(k, lvNewName); 150 assertEquals(lvNewName, kAfter.getName()); 151 assertEquals(d, k.getDate()); 152 assertEquals(1, k.getAdressen().size()); 153 assertEquals("123", ((Adresse) k.getAdressen().get(0)).getPlz()); 154 assertEquals("MD", ((Adresse) k.getAdressen().get(0)).getOrt()); 155 } 156 157 public void testThrowExceptionAndServersideModifierXmlRpc() throws Exception { 158 ServiceManager lvServiceManager = new ServiceManager(getPropertiesXmlRpc()); 159 lvServiceManager.setModifier(new AddSecurityTokenModifier()); 160 161 Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class); 162 assertNotNull(lvEcho); 163 boolean throwException = false; 164 try { 165 lvEcho.throwException("A Test-Excption."); 166 } catch (JUnitTestException e) { 167 throwException = true; 168 } 169 assertTrue("No Exception thrown: " + throwException, throwException); 170 } 171 172 public void testHttpServiceEndpoint() throws Exception { 173 HttpServiceEndpoint lvHttpServiceEndpoint = new HttpServiceEndpoint(); 174 assertNull(lvHttpServiceEndpoint.getInterceptorHandlerCreator()); 175 } 176 177 public void testHttpServiceEndpointGetService() throws Exception { 178 HttpServiceEndpoint lvHttpServiceEndpoint = new HttpServiceEndpoint(); 179 assertNull(lvHttpServiceEndpoint.getService(null, null)); 180 } 181 182 }