| 1 | | 单元测试应用示例——测试Servlet类 |
| | 1 | == 单元测试应用示例——测试Servlet类 == |
| | 2 | Servlet对象由Web容器创建,它依赖HttpServletRequest,HttpServletResponse,ServletConfig等对象,很难使用Junit进行单元测试.但如果借助Spring测试框架的话,就比较容易了.[[BR]] |
| | 3 | Spring测试框架提供了MockHttpServletRequest,MockHttpServletResponse,MockServletConfig,等对象,可以很方便地模拟Servlet依赖的环境。[[BR]] |
| | 4 | |
| | 5 | AdRegistedServlet在用户在游戏的注册引导页完成注册后,将用户信息统计到数据库.[[BR]] |
| | 6 | {{{ #!java |
| | 7 | /** |
| | 8 | * |
| | 9 | * @author ChenYang |
| | 10 | * Date: 2011-12-30 |
| | 11 | */ |
| | 12 | public class AdRegistedServlet extends HttpServlet { |
| | 13 | |
| | 14 | private static Log log = LogFactory.getLog(AdRegistedServlet.class); |
| | 15 | |
| | 16 | |
| | 17 | private ApplicationContext applicationContext = null; |
| | 18 | |
| | 19 | public void setApplicationContext(ApplicationContext applicationContext) { |
| | 20 | this.applicationContext = applicationContext; |
| | 21 | } |
| | 22 | |
| | 23 | @Override |
| | 24 | protected void doGet(HttpServletRequest req, HttpServletResponse res) |
| | 25 | throws ServletException, IOException { |
| | 26 | doPost(req,res); |
| | 27 | } |
| | 28 | |
| | 29 | @Override |
| | 30 | protected void doPost(HttpServletRequest req, HttpServletResponse res) |
| | 31 | throws ServletException, IOException { |
| | 32 | |
| | 33 | String accountId = req.getParameter("accountId"); |
| | 34 | |
| | 35 | String returnUrl = req.getParameter("return"); |
| | 36 | |
| | 37 | if(StringUtils.isBlank(accountId)){ |
| | 38 | return; |
| | 39 | } |
| | 40 | |
| | 41 | Passport passport = (Passport) applicationContext.getBean("passport"); |
| | 42 | try { |
| | 43 | Session realmSession = passport.login(req, res); |
| | 44 | log.info(realmSession.getAccountId()); |
| | 45 | } catch (LoginException e) { |
| | 46 | log.error(e.getMessage()); |
| | 47 | } |
| | 48 | |
| | 49 | String srcid = req.getParameter("srcid"); |
| | 50 | |
| | 51 | if(StringUtils.isBlank(srcid)){ |
| | 52 | res.sendRedirect(returnUrl); |
| | 53 | }else{ |
| | 54 | |
| | 55 | AdTypeDAO adTypeDAO = (AdTypeDAO) applicationContext.getBean("adTypeDAO"); |
| | 56 | AdType adType = adTypeDAO.find(Long.valueOf(srcid)); |
| | 57 | |
| | 58 | if(adType == null){ |
| | 59 | log.info("非法的推广来源:"+srcid); |
| | 60 | res.sendRedirect(returnUrl); |
| | 61 | return; |
| | 62 | } |
| | 63 | |
| | 64 | String ip = IpUtils.getIp(req); |
| | 65 | AdRequest ad_request = new AdRequest(); |
| | 66 | ad_request.setSrcId(StringUtils.isBlank(srcid) ? -1 : Long.valueOf(srcid)); |
| | 67 | ad_request.setSrc(adType == null ? "" : adType.getSrc()); |
| | 68 | |
| | 69 | ad_request.setIp(ip); |
| | 70 | |
| | 71 | String referer = req.getHeader("Referer"); |
| | 72 | |
| | 73 | if(referer == null){ |
| | 74 | return; |
| | 75 | } |
| | 76 | |
| | 77 | long gameid = AdRequestUtils.refererToGameId(referer); |
| | 78 | |
| | 79 | if(gameid == 0){ |
| | 80 | log.info("未配置游戏id的请求来源:"+referer); |
| | 81 | res.sendRedirect(returnUrl); |
| | 82 | return; |
| | 83 | } |
| | 84 | |
| | 85 | ad_request.setGameid(gameid); |
| | 86 | |
| | 87 | ad_request.setUserid(Long.valueOf(accountId)); |
| | 88 | Date now = new Date(); |
| | 89 | ad_request.setCreateAt(now); |
| | 90 | ad_request.setStatus(AdRequestUtils.REGISTED_STATUS); |
| | 91 | ad_request.setReferer(req.getParameter("referer")); |
| | 92 | |
| | 93 | AdRequestDAO adRequestDAO = (AdRequestDAO) applicationContext.getBean("adRequestDAO"); |
| | 94 | adRequestDAO.createAdRequest(ad_request); |
| | 95 | res.sendRedirect(returnUrl); |
| | 96 | } |
| | 97 | |
| | 98 | } |
| | 99 | |
| | 100 | @Override |
| | 101 | public void init(ServletConfig config) throws ServletException { |
| | 102 | if(applicationContext == null){ |
| | 103 | ServletContext ctx = config.getServletContext(); |
| | 104 | this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(ctx); |
| | 105 | } |
| | 106 | } |
| | 107 | |
| | 108 | |
| | 109 | |
| | 110 | } |
| | 111 | }}} |