View Javadoc

1   package test.crispy.impl.caucho;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Properties;
6   
7   import junit.framework.TestCase;
8   import net.sf.crispy.InterceptorContext;
9   import net.sf.crispy.Property;
10  import net.sf.crispy.impl.MiniHttpServer;
11  import net.sf.crispy.impl.ServiceManager;
12  import net.sf.crispy.impl.StaticBurlapProxy;
13  import net.sf.crispy.impl.caucho.BurlapInvoker;
14  import net.sf.crispy.impl.caucho.BurlapServlet;
15  import net.sf.crispy.impl.caucho.BurlapServlet2;
16  import net.sf.crispy.impl.caucho.ConverterModifier;
17  import net.sf.crispy.impl.caucho.HessianServlet;
18  import net.sf.crispy.impl.http.HttpServlet;
19  import test.crispy.JUnitTestException;
20  import test.crispy.example.model.Kunde;
21  import test.crispy.example.service.Calculator;
22  import test.crispy.example.service.CalculatorImpl;
23  import test.crispy.example.service.Echo;
24  import test.crispy.example.service.EchoImpl;
25  import test.crispy.mock.MockServletConfig;
26  
27  import com.caucho.burlap.io.BurlapInput;
28  import com.caucho.burlap.io.BurlapOutput;
29  
30  public class CauchoTest extends TestCase {
31  
32  	public void testConverterModifier() throws Exception {
33  		ConverterModifier lvConverterModifier = new ConverterModifier();
34  		InterceptorContext ic = new InterceptorContext(null, null, null);
35  		InterceptorContext icAfter = lvConverterModifier.modifyBeforeInvocation(ic);
36  		assertEquals(icAfter.getResult(), ic.getResult());
37  		assertEquals(icAfter.getArgs(), ic.getArgs());
38  		
39  		Map lvMap = new HashMap();
40  		lvMap.put("class", Kunde.class.getName());
41  		lvMap.put("name", "TEST-NAME");
42  		ic.setResult(lvMap);
43  		icAfter = lvConverterModifier.modifyAfterInvocation(ic);
44  		assertEquals(icAfter.getArgs(), ic.getArgs());
45  		assertTrue(icAfter.getResult() instanceof Kunde);
46  		Kunde k = (Kunde) icAfter.getResult();
47  		assertEquals(k.getName(), "TEST-NAME");
48  	}
49  
50  	public void testConverterModifierThrowException() throws Exception {
51  		ConverterModifier lvConverterModifier = new ConverterModifier();
52  		InterceptorContext ic = new InterceptorContext(null, null, null);
53  		
54  		Map lvMap = new HashMap();
55  		lvMap.put("class", "no.valid.Class");
56  		lvMap.put("name", "TEST-NAME");
57  		ic.setResult(lvMap);
58  		try {
59  			lvConverterModifier.modifyAfterInvocation(ic);
60  			fail("Exception must thrown!");
61  		} catch (Exception e) {
62  			assertTrue(true);
63  		}
64  	}
65  
66  	public void testBurlapInvoker() throws Exception {
67  		BurlapInvoker lvBurlapInvoker = new BurlapInvoker(new EchoImpl(), Echo.class);
68  		lvBurlapInvoker.setWithConverter(true);
69  		assertEquals(lvBurlapInvoker.getWithConverter(), true);
70  		try {
71  			lvBurlapInvoker.invoke(null, null);
72  			fail("Exception with null parameter");
73  		} catch (Throwable e) {
74  			assertTrue(true);
75  		}
76  		try {
77  			lvBurlapInvoker.invoke(new BurlapInput(), new BurlapOutput());
78  			fail("Exception with null parameter");
79  		} catch (Throwable e) {
80  			assertTrue(true);
81  		}		
82  		lvBurlapInvoker.writeFault("Test-Fault", null, new BurlapOutput(System.out));
83  	}
84  	
85  	public void testBurlapInvokerWithServlet() throws Exception {
86  		MiniHttpServer server = new MiniHttpServer(8022);
87  		server.setContext("/crispy");
88  		server.addServlet("/burlap2/*", BurlapServlet2.class.getName(), Echo.class.getName(), EchoImpl.class.getName());
89  		server.addServlet("/burlap2/*", BurlapServlet2.class.getName(), Calculator.class.getName(), CalculatorImpl.class.getName());
90  		server.start();
91  		
92  		Properties props = new Properties();
93  		props.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8022/crispy/burlap2");
94  		props.put(Property.STATIC_PROXY_CLASS, StaticBurlapProxy.class.getName());
95  		ServiceManager lvManager = new ServiceManager(props);
96  		Echo lvEcho = (Echo) lvManager.createService(Echo.class);
97  		assertEquals(lvEcho.echo("ECHO"), "ECHO");
98  		
99  		boolean throwException = false;
100 		try {
101 			lvEcho.throwException("A Test-Excption.");
102 		} catch (JUnitTestException e) {
103 			throwException = true;
104 		}
105 		assertTrue("No Exception thrown: " + throwException, throwException);
106 		
107 		Calculator lvCalculator = (Calculator) lvManager.createService(Calculator.class);
108 		assertEquals(11, lvCalculator.add(5, 6));
109 		
110 		server.stop();
111 	}
112 	
113 	public void testBurlapServlet() throws Exception {
114 		BurlapServlet lvBurlapServlet = new BurlapServlet();
115 		MockServletConfig config = new MockServletConfig("BurlapServlet");
116 		config.addInitParameter("test.service", "not.valid.Class");
117 		try {
118 			lvBurlapServlet.init(config);
119 			fail("Exception with null parameter");
120 		} catch (Exception e) {
121 			assertTrue(true);
122 		}
123 	}
124 	
125 	public void testBurlapServlet2() throws Exception {
126 		BurlapServlet2 lvBurlapServlet = new BurlapServlet2();
127 		MockServletConfig config = new MockServletConfig("BurlapServlet2");
128 		config.addInitParameter("test.service", "not.valid.Class");
129 		try {
130 			lvBurlapServlet.init(config);
131 			fail("Exception with null parameter");
132 		} catch (Exception e) {
133 			assertTrue(true);
134 		}		
135 	}
136 	
137 	public void testHessianServlet() throws Exception {
138 		HessianServlet lvHessianServlet = new HessianServlet();
139 		MockServletConfig config = new MockServletConfig("HessianServlet");
140 		config.addInitParameter("test.service", "not.valid.Class");
141 		try {
142 			lvHessianServlet.init(config);
143 			fail("Exception with null parameter");
144 		} catch (Exception e) {
145 			assertTrue(true);
146 		}		
147 	}
148 
149 	public void testHttpServlet() throws Exception {
150 		HttpServlet lvHttpServlet = new HttpServlet();
151 		MockServletConfig config = new MockServletConfig("HttpServlet");
152 		config.addInitParameter("test.service", "not.valid.Class");
153 		try {
154 			lvHttpServlet.init(config);
155 			fail("Exception with null parameter");
156 		} catch (Exception e) {
157 			assertTrue(true);
158 		}		
159 	}
160 
161 }