001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.injection.handler;
016:
017: import static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.expect;
019: import static org.easymock.classextension.EasyMock.createStrictMock;
020: import static org.easymock.classextension.EasyMock.replay;
021: import static org.easymock.classextension.EasyMock.verify;
022:
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028:
029: import org.apache.struts.Globals;
030: import org.apache.struts.config.ModuleConfig;
031: import org.apache.struts.util.MessageResources;
032: import org.strecks.context.ActionContext;
033: import org.strecks.context.impl.TestContextImpl;
034: import org.strecks.injection.handler.impl.ActionWithMessageResourcesHelper;
035: import org.strecks.injection.internal.InjectionAnnotationReader;
036: import org.strecks.injection.internal.InjectionWrapper;
037: import org.strecks.message.MessageResourcesHelper;
038: import org.strecks.message.MessageResourcesHelperImpl;
039: import org.testng.annotations.BeforeMethod;
040: import org.testng.annotations.Test;
041:
042: /**
043: * @author Phil Zoio
044: */
045: public class TestMessageResourcesHelperHandler {
046:
047: private Map<String, InjectionWrapper> inputs;
048:
049: private ServletContext context;
050:
051: private HttpServletRequest request;
052:
053: private ModuleConfig moduleConfig;
054:
055: private MessageResources messageResources;
056:
057: private Locale locale;
058:
059: private InjectionAnnotationReader reader;
060:
061: @BeforeMethod
062: public void beforeTest() {
063:
064: reader = new InjectionAnnotationReader();
065: context = createStrictMock(ServletContext.class);
066: request = createStrictMock(HttpServletRequest.class);
067: moduleConfig = createStrictMock(ModuleConfig.class);
068: messageResources = createStrictMock(MessageResources.class);
069: locale = Locale.getDefault();
070:
071: }
072:
073: @Test
074: public void testNamedBundle() {
075:
076: ActionWithMessageResourcesHelper action = new ActionWithMessageResourcesHelper();
077: reader.readAnnotations(action.getClass());
078: inputs = reader.getInjectionMap();
079:
080: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
081: moduleConfig);
082: expect(moduleConfig.getPrefix()).andReturn("/mymodule");
083: expect(context.getAttribute("my.key/mymodule")).andReturn(
084: messageResources);
085: expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(
086: locale);
087:
088: //expected from helper getMessage() invocation
089: expect(
090: messageResources.getMessage(eq(Locale.getDefault()),
091: eq("some.key"))).andReturn("My Message");
092:
093: replay(context);
094: replay(request);
095: replay(moduleConfig);
096: replay(messageResources);
097:
098: InjectionWrapper inputWrapper = inputs.get("namedHelper");
099:
100: ActionContext injectionContext = new TestContextImpl(request,
101: null, context, null, null);
102: inputWrapper.inject(action, injectionContext);
103: MessageResourcesHelper namedHelper = action.getNamedHelper();
104:
105: namedHelper.getMessage("some.key");
106:
107: verify(context);
108: verify(request);
109: verify(moduleConfig);
110: verify(messageResources);
111:
112: assert namedHelper != null;
113: assert namedHelper instanceof MessageResourcesHelperImpl;
114: assert namedHelper.getMessageResources() == messageResources;
115: assert namedHelper.getLocale() == locale;
116:
117: }
118:
119: @Test
120: public void testDefaultBundle() {
121:
122: ActionWithMessageResourcesHelper action = new ActionWithMessageResourcesHelper();
123: reader.readAnnotations(action.getClass());
124: inputs = reader.getInjectionMap();
125:
126: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
127: moduleConfig);
128: expect(moduleConfig.getPrefix()).andReturn("");
129: expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(
130: messageResources);
131: expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(
132: locale);
133:
134: //expected from helper getMessage() invocation
135: expect(
136: messageResources.getMessage(eq(Locale.getDefault()),
137: eq("some.key"))).andReturn("My Message");
138:
139: replay(context);
140: replay(request);
141: replay(moduleConfig);
142: replay(messageResources);
143:
144: InjectionWrapper inputWrapper = inputs.get("defaultHelper");
145:
146: ActionContext injectionContext = new TestContextImpl(request,
147: null, context, null, null);
148: inputWrapper.inject(action, injectionContext);
149: MessageResourcesHelper defaultHelper = action
150: .getDefaultHelper();
151: defaultHelper.getMessage("some.key");
152:
153: verify(context);
154: verify(request);
155: verify(moduleConfig);
156: verify(messageResources);
157:
158: assert defaultHelper != null;
159: assert defaultHelper instanceof MessageResourcesHelperImpl;
160: assert defaultHelper.getMessageResources() == messageResources;
161: assert defaultHelper.getLocale() == locale;
162:
163: }
164:
165: }
|