KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.appfuse.webapp.action;
2
3 import java.lang.reflect.AccessibleObject JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.util.*;
6
7 import junit.framework.TestCase;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.appfuse.Constants;
12 import org.appfuse.util.DateUtil;
13 import org.appfuse.model.BaseObject;
14 import org.appfuse.model.User;
15 import org.appfuse.service.UserManager;
16 import org.springframework.mail.javamail.JavaMailSenderImpl;
17 import org.springframework.mock.web.MockHttpServletRequest;
18 import org.springframework.mock.web.MockServletContext;
19 import org.springframework.util.ClassUtils;
20 import org.springframework.web.context.support.XmlWebApplicationContext;
21
22 public abstract class BaseControllerTestCase extends TestCase {
23     protected transient final Log log = LogFactory.getLog(getClass());
24     protected static XmlWebApplicationContext ctx;
25     protected User user;
26
27     // This static block ensures that Spring's BeanFactory is only loaded
28
// once for all tests
29
static {
30         String JavaDoc pkg = ClassUtils.classPackageAsResourcePath(Constants.class);
31         String JavaDoc[] paths = {
32                 "classpath*:/" + pkg + "/dao/applicationContext-*.xml",
33                 "classpath*:META-INF/applicationContext-*.xml",
34                 "/WEB-INF/applicationContext-validation.xml",
35                 "/WEB-INF/action-servlet.xml"
36             };
37
38         ctx = new XmlWebApplicationContext();
39         ctx.setConfigLocations(paths);
40         ctx.setServletContext(new MockServletContext(""));
41         ctx.refresh();
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         // populate the userForm and place into session
46
UserManager userMgr = (UserManager) ctx.getBean("userManager");
47         user = (User) userMgr.getUserByUsername("tomcat");
48
49         // change the port on the mailSender so it doesn't conflict with an
50
// existing SMTP server on localhost
51
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) ctx.getBean("mailSender");
52         mailSender.setPort(2525);
53         mailSender.setHost("localhost");
54     }
55
56     /**
57      * Convenience methods to make tests simpler
58      */

59     public MockHttpServletRequest newPost(String JavaDoc url) {
60         return new MockHttpServletRequest("POST", url);
61     }
62
63     public MockHttpServletRequest newGet(String JavaDoc url) {
64         return new MockHttpServletRequest("GET", url);
65     }
66
67     public void objectToRequestParameters(Object JavaDoc o, MockHttpServletRequest request) throws Exception JavaDoc {
68         objectToRequestParameters(o, request, null);
69     }
70
71     public void objectToRequestParameters(Object JavaDoc o, MockHttpServletRequest request, String JavaDoc prefix) throws Exception JavaDoc {
72         Class JavaDoc clazz = o.getClass();
73         Field JavaDoc[] fields = getDeclaredFields(clazz);
74         AccessibleObject.setAccessible(fields, true);
75
76         for (int i = 0; i < fields.length; i++) {
77             Object JavaDoc field = (fields[i].get(o));
78             if (field != null) {
79                 if (field instanceof BaseObject) {
80                     // Fix for http://issues.appfuse.org/browse/APF-429
81
if (prefix != null) {
82                         objectToRequestParameters(field, request, prefix + "." + fields[i].getName());
83                     } else {
84                         objectToRequestParameters(field, request, fields[i].getName());
85                     }
86                 } else if (!(field instanceof List) && !(field instanceof Set)) {
87                     String JavaDoc paramName = fields[i].getName();
88     
89                     if (prefix != null) {
90                         paramName = prefix + "." + paramName;
91                     }
92     
93                     String JavaDoc paramValue = String.valueOf(fields[i].get(o));
94     
95                     // handle Dates
96
if (field instanceof java.util.Date JavaDoc) {
97                         paramValue = DateUtil.convertDateToString((Date)fields[i].get(o));
98                         if ("null".equals(paramValue)) paramValue = "";
99                     }
100     
101                     request.addParameter(paramName, paramValue);
102                 }
103             }
104         }
105     }
106     
107     private Field JavaDoc[] getDeclaredFields(Class JavaDoc clazz) {
108         Field JavaDoc[] f = new Field JavaDoc[0];
109         Class JavaDoc superClazz = clazz.getSuperclass();
110         Collection rval = new ArrayList();
111         
112         if (superClazz != null) {
113             rval.addAll(Arrays.asList(getDeclaredFields(superClazz)));
114         }
115         
116         rval.addAll(Arrays.asList(clazz.getDeclaredFields()));
117         return (Field JavaDoc[]) rval.toArray(f);
118     }
119 }
120
Popular Tags