001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import java.util.LinkedHashMap;
008: import java.util.Map;
009:
010: import com.opensymphony.webwork.WebWorkTestCase;
011: import com.opensymphony.xwork.Action;
012: import com.opensymphony.xwork.ActionContext;
013: import com.opensymphony.xwork.ActionProxy;
014: import com.opensymphony.xwork.ActionProxyFactory;
015: import com.opensymphony.xwork.DefaultTextProvider;
016: import com.opensymphony.xwork.config.Configuration;
017: import com.opensymphony.xwork.config.ConfigurationException;
018: import com.opensymphony.xwork.config.ConfigurationManager;
019: import com.opensymphony.xwork.config.ConfigurationProvider;
020: import com.opensymphony.xwork.config.entities.ActionConfig;
021: import com.opensymphony.xwork.config.entities.InterceptorMapping;
022: import com.opensymphony.xwork.config.entities.PackageConfig;
023: import com.opensymphony.xwork.config.entities.ResultConfig;
024: import com.opensymphony.xwork.config.impl.DefaultConfiguration;
025: import com.opensymphony.xwork.util.OgnlValueStack;
026:
027: /**
028: * @author tmjee
029: * @version $Date: 2006-12-11 13:44:39 +0100 (Mon, 11 Dec 2006) $ $Id: FlashInterceptorTest.java 2756 2006-12-11 12:44:39Z tmjee $
030: */
031: public class FlashInterceptorTest extends WebWorkTestCase {
032:
033: public void testRetrieveActionOk() throws Exception {
034: FlashInterceptor flashInterceptor = new FlashInterceptor();
035: flashInterceptor.setOperation(FlashInterceptor.RETRIEVE);
036:
037: Action action = new InternalAction2();
038: final OgnlValueStack stack = new OgnlValueStack();
039: final Map sessionMap = new LinkedHashMap();
040: sessionMap.put(FlashInterceptor.DEFAULT_KEY, action);
041:
042: ResultConfig resultConfig = new ResultConfig();
043: resultConfig.setName("success");
044: resultConfig
045: .setClassName("com.opensymphony.xwork.mock.MockResult");
046:
047: ActionConfig actionConfig = new ActionConfig();
048: actionConfig.setPackageName("myPackage");
049: actionConfig
050: .setClassName("com.opensymphony.webwork.interceptor.FlashInterceptorTest$InternalAction");
051: actionConfig.addInterceptor(new InterceptorMapping("flash",
052: flashInterceptor));
053: actionConfig.addResultConfig(resultConfig);
054:
055: final PackageConfig packageConfig = new PackageConfig();
056: packageConfig.setName("myPackage");
057: packageConfig.setNamespace("/namespace");
058: packageConfig.addActionConfig("action", actionConfig);
059:
060: DefaultConfiguration configuration = new DefaultConfiguration();
061: ConfigurationManager.setConfiguration(configuration);
062: ConfigurationManager
063: .addConfigurationProvider(new ConfigurationProvider() {
064: public void destroy() {
065: }
066:
067: public void init(Configuration configuration)
068: throws ConfigurationException {
069: configuration.addPackageConfig("myPackage",
070: packageConfig);
071: }
072:
073: public boolean needsReload() {
074: return false;
075: }
076: });
077: configuration.reload();
078:
079: ActionProxy actionProxy = ActionProxyFactory.getFactory()
080: .createActionProxy("/namespace", "action",
081: new LinkedHashMap() {
082: private static final long serialVersionUID = 2184375524248962956L;
083: {
084: put(ActionContext.SESSION, sessionMap);
085: put(ActionContext.VALUE_STACK, stack);
086: }
087: }, true, true);
088:
089: actionProxy.execute();
090:
091: // 1] our flash action
092: // 2] our current action
093: // 3] DefaultTextProvider (pushed in by default to provide i18n msgs)
094: assertEquals(stack.size(), 3);
095: assertEquals(stack.pop().getClass(), InternalAction2.class);
096: assertEquals(stack.pop().getClass(), InternalAction.class);
097: assertEquals(stack.pop().getClass(), DefaultTextProvider.class);
098:
099: // make sure we clean up the session after pushing it to the stack
100: assertEquals(sessionMap.size(), 0);
101: assertFalse(sessionMap
102: .containsKey(FlashInterceptor.DEFAULT_KEY));
103: }
104:
105: public void testSaveActionOk() throws Exception {
106: FlashInterceptor flashInterceptor = new FlashInterceptor();
107: flashInterceptor.setOperation(FlashInterceptor.STORE);
108: final Map sessionMap = new LinkedHashMap();
109:
110: ResultConfig resultConfig = new ResultConfig();
111: resultConfig.setName("success");
112: resultConfig
113: .setClassName("com.opensymphony.xwork.mock.MockResult");
114:
115: ActionConfig actionConfig = new ActionConfig();
116: actionConfig.setPackageName("myPackage");
117: actionConfig
118: .setClassName("com.opensymphony.webwork.interceptor.FlashInterceptorTest$InternalAction");
119: actionConfig.addInterceptor(new InterceptorMapping("flash",
120: flashInterceptor));
121: actionConfig.addResultConfig(resultConfig);
122:
123: final PackageConfig packageConfig = new PackageConfig();
124: packageConfig.setName("myPackage");
125: packageConfig.setNamespace("/namespace");
126: packageConfig.addActionConfig("action", actionConfig);
127:
128: DefaultConfiguration configuration = new DefaultConfiguration();
129: ConfigurationManager.setConfiguration(configuration);
130: ConfigurationManager
131: .addConfigurationProvider(new ConfigurationProvider() {
132: public void destroy() {
133: }
134:
135: public void init(Configuration configuration)
136: throws ConfigurationException {
137: configuration.addPackageConfig("myPackage",
138: packageConfig);
139: }
140:
141: public boolean needsReload() {
142: return false;
143: }
144: });
145: configuration.reload();
146:
147: ActionProxy actionProxy = ActionProxyFactory.getFactory()
148: .createActionProxy("/namespace", "action",
149: new LinkedHashMap() {
150: private static final long serialVersionUID = 2184375524248962956L;
151: {
152: put(ActionContext.SESSION, sessionMap);
153: }
154: }, true, true);
155:
156: actionProxy.execute();
157:
158: assertEquals(sessionMap.size(), 1);
159: assertTrue(sessionMap.containsKey(FlashInterceptor.DEFAULT_KEY));
160: assertEquals(sessionMap.get(FlashInterceptor.DEFAULT_KEY)
161: .getClass(), InternalAction.class);
162: }
163:
164: public static class InternalAction implements Action {
165: public String execute() throws Exception {
166: return SUCCESS;
167: }
168: }
169:
170: public static class InternalAction2 implements Action {
171: public String execute() throws Exception {
172: return SUCCESS;
173: }
174: }
175: }
|