001: /*
002: *
003: * Copyright 2005 Joe Walker
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package uk.ltd.getahead.dwrdemo.test;
019:
020: import java.io.File;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.io.Writer;
025: import java.lang.reflect.InvocationHandler;
026: import java.lang.reflect.Method;
027: import java.lang.reflect.Proxy;
028: import java.math.BigDecimal;
029: import java.math.BigInteger;
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.Date;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.LinkedList;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.Set;
040: import java.util.TreeMap;
041: import java.util.TreeSet;
042:
043: import javax.servlet.ServletConfig;
044: import javax.servlet.ServletContext;
045: import javax.servlet.ServletException;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048: import javax.servlet.http.HttpSession;
049:
050: import org.directwebremoting.WebContextFactory;
051: import org.directwebremoting.extend.InboundContext;
052: import org.directwebremoting.util.Logger;
053:
054: /**
055: * Methods to help unit test DWR.
056: * @author Joe Walker [joe at eireneh dot com]
057: */
058: public class Test {
059: /**
060: *
061: */
062: public void throwNPE() {
063: throw new NullPointerException("NullPointerException");
064: }
065:
066: /**
067: * @param wait
068: * @return wait
069: */
070: public int waitFor(int wait) {
071: try {
072: Thread.sleep(wait);
073: return wait;
074: } catch (InterruptedException ex) {
075: return 0;
076: }
077: }
078:
079: /**
080: *
081: */
082: public void doNothing() {
083: }
084:
085: /**
086: * @param a
087: * @param b
088: * @return are they equal
089: */
090: public boolean areIdentical(List a, List b) {
091: return a == b;
092: }
093:
094: /**
095: * @return obja
096: */
097: public ObjA getLooped() {
098: ObjA objA = new ObjA();
099: ObjB objB = new ObjB();
100: objA.setObjB(objB);
101: objB.setObjA(objA);
102: return objA;
103: }
104:
105: /**
106: * @param objA
107: * @return objA
108: */
109: public ObjA testLooped(ObjA objA) {
110: return objA;
111: }
112:
113: /**
114: *
115: */
116: public void voidParam() {
117: }
118:
119: /**
120: * @param test
121: * @return string
122: */
123: public boolean booleanParam(boolean test) {
124: return test;
125: }
126:
127: /**
128: * @param test
129: * @return string
130: */
131: public byte byteParam(byte test) {
132: return test;
133: }
134:
135: /**
136: * @param test
137: * @return string
138: */
139: public char charParam(char test) {
140: return test;
141: }
142:
143: /**
144: * @param test
145: * @return string
146: */
147: public short shortParam(short test) {
148: return test;
149: }
150:
151: /**
152: * @param test
153: * @return string
154: */
155: public int intParam(int test) {
156: return test;
157: }
158:
159: /**
160: * @param test
161: * @return string
162: */
163: public long longParam(long test) {
164: return test;
165: }
166:
167: /**
168: * @param test
169: * @return string
170: */
171: public float floatParam(float test) {
172: return test;
173: }
174:
175: /**
176: * @param test
177: * @return string
178: */
179: public double doubleParam(double test) {
180: return test;
181: }
182:
183: /**
184: * @param test
185: * @return string
186: */
187: public String stringParam(String test) {
188: return test;
189: }
190:
191: /**
192: * @param test
193: * @return string
194: */
195: public boolean[] booleanArrayParam(boolean[] test) {
196: return test;
197: }
198:
199: /**
200: * @param test
201: * @return string
202: */
203: public char[] charArrayParam(char[] test) {
204: return test;
205: }
206:
207: /**
208: * @param test
209: * @return string
210: */
211: public byte[] byteArrayParam(byte[] test) {
212: return test;
213: }
214:
215: /**
216: * @param test
217: * @return string
218: */
219: public short[] shortArrayParam(short[] test) {
220: return test;
221: }
222:
223: /**
224: * @param test
225: * @return string
226: */
227: public int[] intArrayParam(int[] test) {
228: return test;
229: }
230:
231: /**
232: * @param test
233: * @return string
234: */
235: public long[] longArrayParam(long[] test) {
236: return test;
237: }
238:
239: /**
240: * @param test
241: * @return string
242: */
243: public float[] floatArrayParam(float[] test) {
244: return test;
245: }
246:
247: /**
248: * @param test
249: * @return string
250: */
251: public double[] doubleArrayParam(double[] test) {
252: return test;
253: }
254:
255: /**
256: * @param test
257: * @return string
258: */
259: public double[][] double2DArrayParam(double[][] test) {
260: return test;
261: }
262:
263: /**
264: * @param test
265: * @return string
266: */
267: public double[][][] double3DArrayParam(double[][][] test) {
268: return test;
269: }
270:
271: /**
272: * @param test
273: * @return string
274: */
275: public double[][][][] double4DArrayParam(double[][][][] test) {
276: return test;
277: }
278:
279: /**
280: * @param test
281: * @return string
282: */
283: public double[][][][][] double5DArrayParam(double[][][][][] test) {
284: return test;
285: }
286:
287: /**
288: * @param test
289: * @return string
290: */
291: public BigInteger bigIntegerParam(BigInteger test) {
292: return test;
293: }
294:
295: /**
296: * @param test
297: * @return string
298: */
299: public BigDecimal bigDecimalParam(BigDecimal test) {
300: return test;
301: }
302:
303: /**
304: * @param test
305: * @return string
306: */
307: public String[] stringArrayParam(String[] test) {
308: return test;
309: }
310:
311: /**
312: * @param test
313: * @return string
314: */
315: public Collection stringCollectionParam(Collection test) {
316: return test;
317: }
318:
319: /**
320: * @param test
321: * @return string
322: */
323: public LinkedList stringLinkedListParam(LinkedList test) {
324: return test;
325: }
326:
327: /**
328: * @param test
329: * @return string
330: */
331: public ArrayList stringArrayListParam(ArrayList test) {
332: return test;
333: }
334:
335: /**
336: * @param test
337: * @return string
338: */
339: public List stringListParam(List test) {
340: return test;
341: }
342:
343: /**
344: * @param test
345: * @return string
346: */
347: public Set stringSetParam(Set test) {
348: return test;
349: }
350:
351: /**
352: * @param test
353: * @return set of beans
354: */
355: public Set testBeanSetParam(Set test) {
356: if (test.size() > 1) {
357: for (Iterator it = test.iterator(); it.hasNext();) {
358: TestBean ele = (TestBean) it.next();
359: TestBean ignore = ele;
360: ele = ignore;
361: }
362: }
363:
364: return test;
365: }
366:
367: /**
368: * @param test
369: * @return set of beans
370: */
371: public List testBeanListParam(List test) {
372: if (test.size() > 1) {
373: for (Iterator it = test.iterator(); it.hasNext();) {
374: TestBean ele = (TestBean) it.next();
375: TestBean ignore = ele;
376: ele = ignore;
377: }
378: }
379:
380: return test;
381: }
382:
383: /**
384: * @param test
385: * @return string
386: */
387: public HashSet stringHashSetParam(HashSet test) {
388: return test;
389: }
390:
391: /**
392: * @param test
393: * @return string
394: */
395: public TreeSet stringTreeSetParam(TreeSet test) {
396: return test;
397: }
398:
399: /**
400: * @param test
401: * @return string
402: */
403: public TestBean testBeanParam(TestBean test) {
404: return test;
405: }
406:
407: /**
408: * @param test
409: * @return string
410: */
411: public Map stringStringMapParam(Map test) {
412: return test;
413: }
414:
415: /**
416: * @param test
417: * @return string
418: */
419: public Map charTestBeanMapParam(Map test) {
420: return test;
421: }
422:
423: /**
424: * @param test
425: * @return string
426: */
427: public Map stringStringHashMapParam(HashMap test) {
428: return test;
429: }
430:
431: /**
432: * @param test
433: * @return string
434: */
435: public Map stringStringTreeMapParam(TreeMap test) {
436: return test;
437: }
438:
439: /**
440: * @param test
441: * @return string
442: */
443: public TestBean[] testBeanArrayParam(TestBean[] test) {
444: return test;
445: }
446:
447: /**
448: * @param type
449: * @return test bean
450: */
451: public TestBean inheritanceTest(int type) {
452: switch (type) {
453: case 0:
454: return new TestBean();
455:
456: case 1:
457: return new StaticInnerSubTestBean();
458:
459: case 2:
460: return new InnerSubTestBean();
461:
462: case 3:
463: return new TestBean() {
464: };
465:
466: case 4:
467: return (TestBean) Proxy.newProxyInstance(TestBean.class
468: .getClassLoader(), new Class[] { TestBean.class },
469: new TestBeanInvocationHandler());
470:
471: default:
472: throw new IllegalArgumentException("" + type);
473: }
474: }
475:
476: /** */
477: public class InnerSubTestBean extends TestBean {
478: }
479:
480: /** */
481: public static class StaticInnerSubTestBean extends TestBean {
482: }
483:
484: static class TestBeanInvocationHandler implements InvocationHandler {
485: public Object invoke(Object proxy, Method method, Object[] args)
486: throws Throwable {
487: if (method.getName().equals("getInteger")) {
488: return new Integer(42);
489: }
490:
491: if (method.getName().equals("getString")) {
492: return "Slartibartfast";
493: }
494:
495: if (method.getName().equals("equals")) {
496: return new Boolean(equals(args[0]));
497: }
498:
499: if (method.getName().equals("hashCode")) {
500: return new Integer(hashCode());
501: }
502:
503: log.error("Failed on method: " + method);
504: return null;
505: }
506: }
507:
508: /**
509: * @param client
510: * @return Debugging map
511: */
512: public Map dateTest(Date client) {
513: Date server = new Date();
514:
515: Map reply = new HashMap();
516:
517: reply.put("client-object", client);
518: reply.put("client-string", client.toString());
519: reply.put("server-object", server);
520: reply.put("server-string", server.toString());
521:
522: return reply;
523: }
524:
525: /**
526: * @param type
527: * @return test bean
528: */
529: public Foo inheritanceFooTest(int type) {
530: switch (type) {
531: case 0:
532: return new InnerFoo();
533:
534: case 1:
535: return new Foo() {
536: public String getString() {
537: return "anon foo";
538: }
539: };
540:
541: case 4:
542: return (Foo) Proxy.newProxyInstance(Foo.class
543: .getClassLoader(), new Class[] { Foo.class },
544: new TestBeanInvocationHandler());
545:
546: default:
547: throw new IllegalArgumentException("" + type);
548: }
549: }
550:
551: /** */
552: public interface Foo {
553: /** @return string */
554: String getString();
555: }
556:
557: /** */
558: public class InnerFoo implements Foo {
559: public String getString() {
560: return "inner foo";
561: }
562: }
563:
564: /**
565: * @param req
566: * @return string
567: */
568: public String httpServletRequestParam(HttpServletRequest req) {
569: return req.getRemoteAddr();
570: }
571:
572: /**
573: * @param req
574: * @param i
575: * @param resp
576: * @param s
577: * @param session
578: * @param ss
579: * @param scx
580: * @param d
581: * @param scfg
582: * @return string
583: */
584: public String httpObjectParams(HttpServletRequest req, int i,
585: HttpServletResponse resp, String s, HttpSession session,
586: short[] ss, ServletContext scx, Date d, ServletConfig scfg) {
587: return req.getRemoteAddr() + i + resp.hashCode() + s
588: + session.getId() + ss.length + scx.getMajorVersion()
589: + d.getTime() + scfg.getServletName();
590: }
591:
592: /**
593: * @return nest
594: */
595: public TestBean[] getNestingTest() {
596: TestBean a = new TestBean(0, "!\"$%^&*()_1", null);
597: TestBean b = new TestBean(0, "!\"$%^&*()_2", a);
598: TestBean c = new TestBean(0, "!\"$%^&*()_3", b);
599: TestBean d = new TestBean(0, "!\"$%^&*()_4", c);
600:
601: TestBean[] reply = new TestBean[] { a, c, d, d, };
602:
603: return reply;
604: }
605:
606: /**
607: * @param param1
608: * @param param2
609: * @return string
610: */
611: public String stringStringParam(String param1, String param2) {
612: return "param1='" + param1 + "' param2='" + param2 + "'";
613: }
614:
615: /**
616: * @param param
617: * @param delay
618: * @return string
619: * @throws InterruptedException
620: */
621: public String slowStringParam(String param, long delay)
622: throws InterruptedException {
623: log.debug("About to wait for: " + delay);
624: synchronized (this ) {
625: wait(delay);
626: }
627: log.debug("Done waiting for: " + delay);
628:
629: return param;
630: }
631:
632: /**
633: * @param data The results of the current test
634: * @return A summary of all the received results
635: */
636: public Map reply(Map data) {
637: String failReport = (String) data.get("failreport");
638:
639: HttpServletRequest request = WebContextFactory.get()
640: .getHttpServletRequest();
641: String userAgentHttp = request.getHeader("User-Agent");
642: userAgentHttp = simplfyUserAgent(userAgentHttp);
643: if (userAgentHttp.length() > 100) {
644: userAgentHttp = userAgentHttp.substring(0, 100);
645: }
646:
647: if (logfile == null) {
648: try {
649: String home = System.getProperty("user.home");
650: Writer out = new FileWriter(home + File.separator
651: + "test.log", true);
652: // URL url = ExecutionContext.get().getServletContext().getResource("/test.log");
653: // OutputStream out = url.openConnection().getOutputStream();
654: logfile = new PrintWriter(out);
655: } catch (Exception ex) {
656: log.error("Failed to open test log file", ex);
657: }
658: }
659:
660: if (logfile != null) {
661: for (Iterator it = data.keySet().iterator(); it.hasNext();) {
662: String key = (String) it.next();
663: if (key.length() > 100) {
664: key = key.substring(0, 100);
665: }
666: String value = (String) data.get(key);
667: if (value.length() > 1000) {
668: value = value.substring(0, 1000);
669: }
670: logfile.write("\n" + key + "=" + value);
671: }
672: logfile.write("\nuseragent-http=" + userAgentHttp);
673: logfile.write("\naddr=" + request.getRemoteAddr());
674: logfile.write("\n");
675: logfile.flush();
676: }
677:
678: // Update the results summary
679: Map failCounts = (Map) results.get(userAgentHttp);
680: if (failCounts == null) {
681: failCounts = new HashMap();
682: results.put(userAgentHttp, failCounts);
683: }
684:
685: Integer reports = (Integer) failCounts.get(failReport);
686: if (reports == null) {
687: reports = new Integer(1);
688: } else {
689: reports = new Integer(reports.intValue() + 1);
690: }
691: failCounts.put(failReport, reports);
692:
693: return results;
694: }
695:
696: /**
697: * Attempt to simplfy a user-agent string
698: * @param sent The user-agent from the browser
699: * @return A user friendly version if possible
700: */
701: public static String simplfyUserAgent(String sent) {
702: // Firefox
703: int offset = sent.indexOf("Firefox");
704: if (offset > 10) {
705: return sent.substring(offset);
706: }
707:
708: // IE
709: offset = sent.indexOf("MSIE");
710: if (offset > 10) {
711: int end = sent.indexOf(";", offset);
712: if (end == -1) {
713: end = sent.length();
714: }
715: return sent.substring(offset, end);
716: }
717:
718: // Konq
719: offset = sent.indexOf("Konqueror");
720: if (offset > 10) {
721: int end = sent.indexOf(";", offset);
722: if (end == -1) {
723: end = sent.length();
724: }
725: return sent.substring(offset, end);
726: }
727:
728: // Safari
729: offset = sent.indexOf("Safari");
730: if (offset > 10) {
731: int end = sent.indexOf(";", offset);
732: if (end == -1) {
733: end = sent.length();
734: }
735: return sent.substring(offset, end);
736: }
737:
738: // Opera
739: offset = sent.indexOf("Opera");
740: if (offset > -1) {
741: int end = sent.indexOf(" ", offset);
742: if (end == -1) {
743: end = sent.length();
744: }
745: return sent.substring(offset, end);
746: }
747:
748: return sent;
749: }
750:
751: /**
752: * @return string
753: */
754: public String delete() {
755: return "You can't touch me";
756: }
757:
758: /**
759: * @return string
760: */
761: protected String protectedMethod() {
762: privateMethod();
763: return "You can't touch me";
764: }
765:
766: /**
767: * @return string
768: */
769: private String privateMethod() {
770: return "You can't touch me";
771: }
772:
773: /**
774: * @return string
775: */
776: public static String staticMethod() {
777: return "static Test.staticMethod() says hello.";
778: }
779:
780: /**
781: * @param param1
782: * @return string
783: */
784: public String dangerOverload(String param1) {
785: return "Test.dangerOverload(" + param1 + ") says hello."; //$NON-NLS-2$
786: }
787:
788: /**
789: * @return string
790: */
791: public String dangerOverload() {
792: return "Test.dangerOverload() says hello.";
793: }
794:
795: /**
796: * This method should not be callable
797: * @param cx Illegal param
798: * @return Error message
799: */
800: public String error(InboundContext cx) {
801: return "You should not see this: " + cx;
802: }
803:
804: /**
805: * Get some text that has been fetched from a JSP page.
806: * @return A part of a web page
807: * @throws IOException Forwarded from the insert processing
808: * @throws ServletException Forwarded from the insert processing
809: */
810: public String getInsert() throws ServletException, IOException {
811: return WebContextFactory.get().forwardToString("/insert.html");
812: }
813:
814: /**
815: * The log stream
816: */
817: protected static final Logger log = Logger.getLogger(Test.class);
818:
819: private static PrintWriter logfile = null;
820:
821: private static Map results = new HashMap();
822: }
|