KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > MockRequestCycle


1 package org.appfuse.webapp.action;
2
3 import org.apache.tapestry.IPage;
4 import org.apache.tapestry.engine.RequestCycle;
5 import org.apache.tapestry.html.BasePage;
6 import org.apache.tapestry.test.Creator;
7
8 public class MockRequestCycle extends RequestCycle {
9     private String JavaDoc pkg;
10
11     public MockRequestCycle() {
12         super();
13     }
14
15     public MockRequestCycle(String JavaDoc pkg) {
16         super();
17         this.pkg = pkg;
18     }
19
20     public IPage getPage(final String JavaDoc name) {
21         // convert the first character to uppercase
22
char first = Character.toUpperCase(name.charAt(0));
23         String JavaDoc className = first + name.substring(1);
24
25         // if it ends in an "s", replace "s" with "List"
26
if (className.endsWith("s")) {
27             className = className.substring(0, className.length() - 1) + "List";
28         }
29
30         if (pkg != null) {
31             className = pkg + "." + className;
32         } else {
33             className = MockRequestCycle.class.getPackage().getName() + "." + className;
34         }
35
36         IPage page;
37
38         try {
39             page = (IPage) new Creator().newInstance(Class.forName(className));
40         } catch (Exception JavaDoc e) {
41             // Instantiate a BasePage and hope that works
42
try {
43                 page = (IPage) new Creator().newInstance(BasePage.class);
44             } catch (Exception JavaDoc e2) {
45                 e.printStackTrace();
46                 throw new RuntimeException JavaDoc("Unable to instantiate '" + className + "'");
47             }
48         }
49
50         page.setPageName(name);
51         return page;
52     }
53 }
54
Popular Tags