1 package com.test.junit; 2 3 import com.opensymphony.webwork.util.AttributeMap; 4 import com.opensymphony.xwork.ActionContext; 5 import com.opensymphony.xwork.ActionProxy; 6 import com.opensymphony.xwork.ActionProxyFactory; 7 import com.test.HelloAction; 8 import junit.framework.TestCase; 9 10 import java.util.Date ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 public class HelloActionTest extends TestCase { 15 16 HashMap extraContext = new HashMap (); 17 18 protected void setUp() throws Exception { 19 super.setUp(); 20 21 Map parameterMap = new HashMap (); 22 parameterMap.put("userName", "HenryHu"); 23 extraContext.put(ActionContext.PARAMETERS, parameterMap); 24 25 AttributeMap attrMap = new AttributeMap(extraContext); 26 extraContext.put("attr", attrMap); 27 } 28 29 public void testGetUserName() { 30 try { 31 HelloAction action = execute(); 32 33 String userName = action.getUserName(); 34 assertEquals(userName, "HenryHu"); 35 } catch (Throwable e) { 36 e.printStackTrace(); 37 } 38 } 39 40 public void testGetNow() { 41 try { 42 HelloAction action = execute(); 43 44 Date now = action.getNow(); 45 System.out.println(now); 46 assertNotNull(now); 47 48 } catch (Throwable e) { 49 e.printStackTrace(); 50 } 51 } 52 53 public void testExecute() { 54 try { 55 56 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", "hello", extraContext); 57 proxy.setExecuteResult(false); 58 String result = proxy.execute(); 59 60 String userName = ((HelloAction) proxy.getAction()).getUserName(); 61 assertEquals(result, "success"); 62 } catch (Throwable e) { 63 e.printStackTrace(); 64 } 65 } 66 67 68 private HelloAction execute() throws Exception { 69 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", "hello", extraContext); 70 proxy.setExecuteResult(false); 71 proxy.execute(); 72 return (HelloAction) proxy.getAction(); 73 } 74 75 } | Popular Tags |