001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.marshall;
017:
018: import java.lang.reflect.InvocationHandler;
019: import java.lang.reflect.Method;
020: import java.lang.reflect.Proxy;
021: import java.math.BigDecimal;
022: import java.math.BigInteger;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Date;
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Set;
034: import java.util.TreeMap;
035: import java.util.TreeSet;
036:
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletContext;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import javax.servlet.http.HttpSession;
042:
043: import org.apache.commons.logging.LogFactory;
044: import org.apache.commons.logging.Log;
045: import org.directwebremoting.ScriptSession;
046: import org.directwebremoting.WebContextFactory;
047: import org.directwebremoting.extend.InboundContext;
048: import org.xml.sax.SAXParseException;
049:
050: /**
051: * Methods to help unit test DWR.
052: * @author Joe Walker [joe at getahead dot ltd dot uk]
053: */
054: public class Test {
055: public void throwNPE() {
056: // This is exported by dwr.xml
057: throw new NullPointerException("NullPointerException");
058: }
059:
060: public void throwIAE() {
061: // This is NOT exported by dwr.xml
062: throw new IllegalArgumentException("IllegalArgumentException");
063: }
064:
065: public void throwSPE() throws SAXParseException {
066: // This is exported by dwr.xml as a result of it being a SAXException
067: throw new SAXParseException("SAXParseException", "publicId",
068: "systemId", 42, 24, new NullPointerException(
069: "NullPointerException"));
070: }
071:
072: public int waitFor(int wait) {
073: try {
074: Thread.sleep(wait);
075: return wait;
076: } catch (InterruptedException ex) {
077: return 0;
078: }
079: }
080:
081: public void doNothing() {
082: }
083:
084: public boolean areIdentical(List<?> a, List<?> b) {
085: return a == b;
086: }
087:
088: public ObjA getLooped() {
089: ObjA objA = new ObjA();
090: ObjB objB = new ObjB();
091: objA.setObjB(objB);
092: objB.setObjA(objA);
093: return objA;
094: }
095:
096: public ObjA testLooped(ObjA objA) {
097: ObjA nestedA = objA.getObjB().getObjA();
098:
099: if (nestedA != objA) {
100: throw new IllegalStateException(
101: "Non matching obja != obja.objb.obja");
102: }
103:
104: if (nestedA.getObjB() != objA.getObjB()) {
105: throw new IllegalStateException(
106: "Non matching objb != objb.obja.objb");
107: }
108:
109: return objA;
110: }
111:
112: public void voidParam() {
113: }
114:
115: public boolean booleanParam(boolean test) {
116: return test;
117: }
118:
119: public byte byteParam(byte test) {
120: return test;
121: }
122:
123: public char charParam(char test) {
124: return test;
125: }
126:
127: public short shortParam(short test) {
128: return test;
129: }
130:
131: public int intParam(int test) {
132: return test;
133: }
134:
135: public long longParam(long test) {
136: return test;
137: }
138:
139: public float floatParam(float test) {
140: return test;
141: }
142:
143: public double doubleParam(double test) {
144: return test;
145: }
146:
147: public String stringParam(String test) {
148: return test;
149: }
150:
151: public boolean[] booleanArrayParam(boolean[] test) {
152: return test;
153: }
154:
155: public char[] charArrayParam(char[] test) {
156: return test;
157: }
158:
159: public byte[] byteArrayParam(byte[] test) {
160: return test;
161: }
162:
163: public short[] shortArrayParam(short[] test) {
164: return test;
165: }
166:
167: public int[] intArrayParam(int[] test) {
168: return test;
169: }
170:
171: public long[] longArrayParam(long[] test) {
172: return test;
173: }
174:
175: public float[] floatArrayParam(float[] test) {
176: return test;
177: }
178:
179: public double[] doubleArrayParam(double[] test) {
180: return test;
181: }
182:
183: public double[][] double2DArrayParam(double[][] test) {
184: return test;
185: }
186:
187: public double[][][] double3DArrayParam(double[][][] test) {
188: return test;
189: }
190:
191: public double[][][][] double4DArrayParam(double[][][][] test) {
192: return test;
193: }
194:
195: public double[][][][][] double5DArrayParam(double[][][][][] test) {
196: return test;
197: }
198:
199: public BigInteger bigIntegerParam(BigInteger test) {
200: return test;
201: }
202:
203: public BigDecimal bigDecimalParam(BigDecimal test) {
204: return test;
205: }
206:
207: public String[] stringArrayParam(String[] test) {
208: return test;
209: }
210:
211: public Collection<String> stringCollectionParam(
212: Collection<String> test) {
213: return test;
214: }
215:
216: public LinkedList<String> stringLinkedListParam(
217: LinkedList<String> test) {
218: return test;
219: }
220:
221: public ArrayList<String> stringArrayListParam(ArrayList<String> test) {
222: return test;
223: }
224:
225: public List<String> stringListParam(List<String> test) {
226: return test;
227: }
228:
229: public Set<String> stringSetParam(Set<String> test) {
230: return test;
231: }
232:
233: public org.dom4j.Element dom4jElementParam(org.dom4j.Element test) {
234: return test;
235: }
236:
237: public org.dom4j.Document dom4jDocumentParam(org.dom4j.Document test) {
238: return test;
239: }
240:
241: public nu.xom.Element xomElementParam(nu.xom.Element test) {
242: return test;
243: }
244:
245: public nu.xom.Document xomDocumentParam(nu.xom.Document test) {
246: return test;
247: }
248:
249: public org.jdom.Element jdomElementParam(org.jdom.Element test) {
250: return test;
251: }
252:
253: public org.jdom.Document jdomDocumentParam(org.jdom.Document test) {
254: return test;
255: }
256:
257: public org.w3c.dom.Element domElementParam(org.w3c.dom.Element test) {
258: return test;
259: }
260:
261: public org.w3c.dom.Document domDocumentParam(
262: org.w3c.dom.Document test) {
263: return test;
264: }
265:
266: public Set<TestBean> testBeanSetParam(Set<TestBean> test) {
267: if (test.size() > 1) {
268: for (Iterator<TestBean> it = test.iterator(); it.hasNext();) {
269: TestBean ele = it.next();
270: TestBean ignore = ele;
271: ele = ignore;
272: }
273: }
274:
275: return test;
276: }
277:
278: public List<TestBean> testBeanListParam(List<TestBean> test) {
279: if (test.size() > 1) {
280: for (Iterator<TestBean> it = test.iterator(); it.hasNext();) {
281: TestBean ele = it.next();
282: TestBean ignore = ele;
283: ele = ignore;
284: }
285: }
286:
287: return test;
288: }
289:
290: public HashSet<String> stringHashSetParam(HashSet<String> test) {
291: return test;
292: }
293:
294: public TreeSet<String> stringTreeSetParam(TreeSet<String> test) {
295: return test;
296: }
297:
298: public TestBean testBeanParam(TestBean test) {
299: return test;
300: }
301:
302: public Map<String, String> stringStringMapParam(
303: Map<String, String> test) {
304: return test;
305: }
306:
307: public Map<Character, TestBean> charTestBeanMapParam(
308: Map<Character, TestBean> test) {
309: return test;
310: }
311:
312: public Map<String, String> stringStringHashMapParam(
313: HashMap<String, String> test) {
314: return test;
315: }
316:
317: public Map<String, String> stringStringTreeMapParam(
318: TreeMap<String, String> test) {
319: return test;
320: }
321:
322: public TestBean[] testBeanArrayParam(TestBean[] test) {
323: return test;
324: }
325:
326: public List<Set<Map<String, TestBean>>> testComplex(
327: List<Set<Map<String, TestBean>>> test) {
328: return test;
329: }
330:
331: public TestBean inheritanceTest(int type) {
332: switch (type) {
333: case 0:
334: return new TestBean();
335:
336: case 1:
337: return new StaticInnerSubTestBean();
338:
339: case 2:
340: return new InnerSubTestBean();
341:
342: case 3:
343: return new TestBean() {
344: };
345:
346: case 4:
347: return (TestBean) Proxy.newProxyInstance(TestBean.class
348: .getClassLoader(), new Class[] { TestBean.class },
349: new TestBeanInvocationHandler());
350:
351: default:
352: throw new IllegalArgumentException("" + type);
353: }
354: }
355:
356: public class InnerSubTestBean extends TestBean {
357: }
358:
359: public static class StaticInnerSubTestBean extends TestBean {
360: }
361:
362: static class TestBeanInvocationHandler implements InvocationHandler {
363: public Object invoke(Object proxy, Method method, Object[] args)
364: throws Throwable {
365: if (method.getName().equals("getInteger")) {
366: return new Integer(42);
367: }
368:
369: if (method.getName().equals("getString")) {
370: return "Slartibartfast";
371: }
372:
373: if (method.getName().equals("equals")) {
374: return new Boolean(equals(args[0]));
375: }
376:
377: if (method.getName().equals("hashCode")) {
378: return new Integer(hashCode());
379: }
380:
381: log.error("Failed on method: " + method);
382: return null;
383: }
384: }
385:
386: public Map<String, Comparable<?>> dateTest(Date client) {
387: Date server = new Date();
388:
389: Map<String, Comparable<?>> reply = new HashMap<String, Comparable<?>>();
390:
391: reply.put("client-object", client);
392: reply.put("client-string", client.toString());
393: reply.put("server-object", server);
394: reply.put("server-string", server.toString());
395:
396: return reply;
397: }
398:
399: public Foo inheritanceFooTest(int type) {
400: switch (type) {
401: case 0:
402: return new InnerFoo();
403:
404: case 1:
405: return new Foo() {
406: public String getString() {
407: return "anon foo";
408: }
409: };
410:
411: case 4:
412: return (Foo) Proxy.newProxyInstance(Foo.class
413: .getClassLoader(), new Class[] { Foo.class },
414: new TestBeanInvocationHandler());
415:
416: default:
417: throw new IllegalArgumentException("" + type);
418: }
419: }
420:
421: public interface Foo {
422: String getString();
423: }
424:
425: public class InnerFoo implements Foo {
426: public String getString() {
427: return "inner foo";
428: }
429: }
430:
431: public String httpServletRequestParam(HttpServletRequest req) {
432: return req.getRemoteAddr();
433: }
434:
435: @SuppressWarnings("unchecked")
436: public Map<String, String> listParameters(HttpServletRequest request) {
437: Map<String, String> reply = new HashMap<String, String>();
438:
439: Enumeration<String> names = request.getAttributeNames();
440: while (names.hasMoreElements()) {
441: String name = names.nextElement();
442: String value = request.getAttribute(name).toString();
443: reply.put(name, value);
444: }
445:
446: return reply;
447: }
448:
449: @SuppressWarnings("unchecked")
450: public Map<String, String> listHeaders(HttpServletRequest request) {
451: Map<String, String> reply = new HashMap<String, String>();
452:
453: Enumeration<String> names = request.getHeaderNames();
454: while (names.hasMoreElements()) {
455: String name = names.nextElement();
456: Enumeration<String> values = request.getHeaders(name);
457: StringBuilder value = new StringBuilder();
458: while (values.hasMoreElements()) {
459: String single = values.nextElement();
460: value.append(single);
461: if (values.hasMoreElements()) {
462: value.append(", ");
463: }
464: }
465:
466: reply.put(name, value.toString());
467: }
468:
469: return reply;
470: }
471:
472: public String httpObjectParams(HttpServletRequest req, int i,
473: HttpServletResponse resp, String s, HttpSession session,
474: short[] ss, ServletContext scx, Date d, ServletConfig scfg) {
475: return req.getRemoteAddr() + i + resp.hashCode() + s
476: + session.getId() + ss.length + scx.getMajorVersion()
477: + d.getTime() + scfg.getServletName();
478: }
479:
480: public TestBean[] getNestingTest() {
481: TestBean a = new TestBean(0, "!\"$%^&*()_1", null);
482: TestBean b = new TestBean(0, "!\"$%^&*()_2", a);
483: TestBean c = new TestBean(0, "!\"$%^&*()_3", b);
484: TestBean d = new TestBean(0, "!\"$%^&*()_4", c);
485:
486: TestBean[] reply = new TestBean[] { a, c, d, d, };
487:
488: return reply;
489: }
490:
491: public String stringStringParam(String param1, String param2) {
492: return "param1='" + param1 + "' param2='" + param2 + "'";
493: }
494:
495: public String slowStringParam(String param, long delay)
496: throws InterruptedException {
497: log.debug("About to wait for: " + delay);
498: synchronized (this ) {
499: wait(delay);
500: }
501: log.debug("Done waiting for: " + delay);
502:
503: return param;
504: }
505:
506: public String delete() {
507: return "You can't touch me";
508: }
509:
510: protected String protectedMethod() {
511: privateMethod();
512: return "You can't touch me";
513: }
514:
515: private String privateMethod() {
516: return "You can't touch me";
517: }
518:
519: public static String staticMethod() {
520: return "static Test.staticMethod() says hello.";
521: }
522:
523: public String dangerOverload(String param1) {
524: return "Test.dangerOverload(" + param1 + ") says hello.";
525: }
526:
527: public String dangerOverload() {
528: return "Test.dangerOverload() says hello.";
529: }
530:
531: public String error(InboundContext cx) {
532: return "You should not see this: " + cx;
533: }
534:
535: public String serverChecks() {
536: ScriptSession scriptSession = WebContextFactory.get()
537: .getScriptSession();
538: scriptSession.invalidate();
539:
540: if (scriptSession.isInvalidated()) {
541: return "invalidateMe() succeeded";
542: } else {
543: return "invalidateMe() failed";
544: }
545: }
546:
547: /**
548: * The log stream
549: */
550: protected static final Log log = LogFactory.getLog(Test.class);
551: }
|