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.expect;
018: import static org.easymock.classextension.EasyMock.createStrictMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021:
022: import java.util.Map;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.struts.Globals;
028: import org.apache.struts.config.ModuleConfig;
029: import org.apache.struts.util.MessageResources;
030: import org.strecks.context.ActionContext;
031: import org.strecks.context.impl.TestContextImpl;
032: import org.strecks.injection.handler.impl.ActionWithDefaultMessageResources;
033: import org.strecks.injection.handler.impl.ActionWithMessageResources;
034: import org.strecks.injection.internal.InjectionAnnotationReader;
035: import org.strecks.injection.internal.InjectionWrapper;
036: import org.testng.annotations.BeforeMethod;
037: import org.testng.annotations.Test;
038:
039: /**
040: * @author Phil Zoio
041: */
042: public class TestMessageResourcesHandler {
043:
044: private Map<String, InjectionWrapper> inputs;
045:
046: private ServletContext context;
047:
048: private HttpServletRequest request;
049:
050: private ModuleConfig moduleConfig;
051:
052: private MessageResources messageResources;
053:
054: private InjectionAnnotationReader reader;
055:
056: @BeforeMethod
057: public void beforeTest() {
058:
059: reader = new InjectionAnnotationReader();
060: context = createStrictMock(ServletContext.class);
061: request = createStrictMock(HttpServletRequest.class);
062: moduleConfig = createStrictMock(ModuleConfig.class);
063: messageResources = createStrictMock(MessageResources.class);
064:
065: }
066:
067: @Test
068: public void testNamedBundle() {
069:
070: ActionWithMessageResources action = new ActionWithMessageResources();
071: reader.readAnnotations(action.getClass());
072: inputs = reader.getInjectionMap();
073:
074: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
075: moduleConfig);
076: expect(moduleConfig.getPrefix()).andReturn("/mymodule");
077: expect(context.getAttribute("my.key/mymodule")).andReturn(
078: messageResources);
079:
080: replay(context);
081: replay(request);
082: replay(moduleConfig);
083: replay(messageResources);
084:
085: InjectionWrapper inputWrapper = inputs.get("resources");
086:
087: ActionContext injectionContext = new TestContextImpl(request,
088: null, context, null, null);
089: inputWrapper.inject(action, injectionContext);
090:
091: verify(context);
092: verify(request);
093: verify(moduleConfig);
094: verify(messageResources);
095:
096: assert action.getResources() != null;
097:
098: }
099:
100: @Test
101: public void testDefaultBundle() {
102:
103: ActionWithDefaultMessageResources action = new ActionWithDefaultMessageResources();
104: reader.readAnnotations(action.getClass());
105: inputs = reader.getInjectionMap();
106:
107: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
108: moduleConfig);
109: expect(moduleConfig.getPrefix()).andReturn("");
110: expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(
111: messageResources);
112:
113: replay(context);
114: replay(request);
115: replay(moduleConfig);
116: replay(messageResources);
117:
118: InjectionWrapper inputWrapper = inputs.get("resources");
119:
120: ActionContext injectionContext = new TestContextImpl(request,
121: null, context, null, null);
122: inputWrapper.inject(action, injectionContext);
123:
124: verify(context);
125: verify(request);
126: verify(moduleConfig);
127: verify(messageResources);
128:
129: assert action.getResources() != null;
130:
131: }
132:
133: }
|