Commit d9ae974360c4e326d274173d9ce9cad8fbb55fc3
1 parent
d76ee5a9
Exists in
master
Refatoração de teste unitário: ParamenterImplTest
Showing
1 changed file
with
371 additions
and
325 deletions
Show diff stats
impl/extension/jsf/src/test/java/br/gov/frameworkdemoiselle/internal/implementation/ParameterImplTest.java
| 1 | -///* | ||
| 2 | -// * Demoiselle Framework | ||
| 3 | -// * Copyright (C) 2010 SERPRO | ||
| 4 | -// * ---------------------------------------------------------------------------- | ||
| 5 | -// * This file is part of Demoiselle Framework. | ||
| 6 | -// * | ||
| 7 | -// * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | -// * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | -// * as published by the Free Software Foundation. | ||
| 10 | -// * | ||
| 11 | -// * This program is distributed in the hope that it will be useful, | ||
| 12 | -// * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | -// * GNU General Public License for more details. | ||
| 15 | -// * | ||
| 16 | -// * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | -// * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | -// * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | -// * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | -// * ---------------------------------------------------------------------------- | ||
| 21 | -// * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | -// * | ||
| 23 | -// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | -// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | -// * do Software Livre (FSF). | ||
| 26 | -// * | ||
| 27 | -// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | -// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | -// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | -// * para maiores detalhes. | ||
| 31 | -// * | ||
| 32 | -// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | -// * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | -// * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | -// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | -// */ | ||
| 37 | -//package br.gov.frameworkdemoiselle.internal.implementation; | ||
| 38 | -// | ||
| 39 | -//import static org.easymock.EasyMock.expect; | ||
| 40 | -//import static org.junit.Assert.assertEquals; | ||
| 41 | -//import static org.powermock.api.easymock.PowerMock.createMock; | ||
| 42 | -//import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 43 | -//import static org.powermock.api.easymock.PowerMock.replayAll; | ||
| 44 | -//import static org.powermock.api.easymock.PowerMock.verifyAll; | ||
| 45 | -// | ||
| 46 | -//import java.lang.reflect.Member; | ||
| 47 | -//import java.util.HashMap; | ||
| 48 | -//import java.util.Map; | ||
| 49 | -// | ||
| 50 | -//import javax.enterprise.context.RequestScoped; | ||
| 51 | -//import javax.enterprise.context.SessionScoped; | ||
| 52 | -//import javax.enterprise.inject.spi.Annotated; | ||
| 53 | -//import javax.enterprise.inject.spi.InjectionPoint; | ||
| 54 | -//import javax.faces.convert.Converter; | ||
| 55 | -//import javax.servlet.http.HttpServletRequest; | ||
| 56 | -//import javax.servlet.http.HttpSession; | ||
| 57 | -// | ||
| 58 | -//import org.easymock.EasyMock; | ||
| 59 | -//import org.junit.Before; | ||
| 60 | -//import org.junit.Test; | ||
| 61 | -//import org.junit.runner.RunWith; | ||
| 62 | -//import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 63 | -//import org.powermock.modules.junit4.PowerMockRunner; | ||
| 64 | -//import org.powermock.reflect.Whitebox; | ||
| 65 | -// | ||
| 66 | -//import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 67 | -//import br.gov.frameworkdemoiselle.annotation.ViewScoped; | ||
| 68 | -//import br.gov.frameworkdemoiselle.util.Faces; | ||
| 69 | -//import br.gov.frameworkdemoiselle.util.Reflections; | ||
| 70 | -// | ||
| 71 | -//@RunWith(PowerMockRunner.class) | ||
| 72 | -//@PrepareForTest({ Reflections.class, Faces.class }) | ||
| 73 | -//public class ParameterImplTest { | ||
| 74 | -// | ||
| 75 | -// private ParameterImpl<Long> param; | ||
| 76 | -// | ||
| 77 | -// private HttpServletRequest request; | ||
| 78 | -// | ||
| 79 | -// private InjectionPoint ip; | ||
| 80 | -// | ||
| 81 | -// private Converter converter; | ||
| 82 | -// | ||
| 83 | -// private Annotated annotated; | ||
| 84 | -// | ||
| 85 | -// private Name name; | ||
| 86 | -// | ||
| 87 | -// private HttpSession session; | ||
| 88 | -// | ||
| 89 | -// private Member member; | ||
| 90 | -// | ||
| 91 | -// @Before | ||
| 92 | -// public void before() { | ||
| 93 | -// ip = createMock(InjectionPoint.class); | ||
| 94 | -// request = createMock(HttpServletRequest.class); | ||
| 95 | -// session = createMock(HttpSession.class); | ||
| 96 | -// annotated = createMock(Annotated.class); | ||
| 97 | -// name = createMock(Name.class); | ||
| 98 | -// converter = createMock(Converter.class); | ||
| 99 | -// member = createMock(Member.class); | ||
| 100 | -// | ||
| 101 | -// mockStatic(Reflections.class); | ||
| 102 | -// mockStatic(Faces.class); | ||
| 103 | -// } | ||
| 104 | -// | ||
| 105 | -// private void prepareForTestWithKeyFromNameAnnotation() { | ||
| 106 | -// expect(ip.getAnnotated()).andReturn(annotated).anyTimes(); | ||
| 107 | -// expect(ip.getMember()).andReturn(null); | ||
| 108 | -// expect(annotated.isAnnotationPresent(Name.class)).andReturn(true); | ||
| 109 | -// expect(annotated.getAnnotation(Name.class)).andReturn(name); | ||
| 110 | -// expect(name.value()).andReturn("name"); | ||
| 111 | -// expect(Reflections.getGenericTypeArgument(EasyMock.anyObject(Member.class), EasyMock.anyInt())).andReturn( | ||
| 112 | -// Object.class); | ||
| 113 | -// expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 114 | -// } | ||
| 115 | -// | ||
| 116 | -// @Test | ||
| 117 | -// public void testConstructorCase1() { | ||
| 118 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 119 | -// | ||
| 120 | -// replayAll(); | ||
| 121 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 122 | -// assertEquals("name", param.getKey()); | ||
| 123 | -// assertEquals(Object.class, Whitebox.getInternalState(param, "type")); | ||
| 124 | -// assertEquals(converter, param.getConverter()); | ||
| 125 | -// verifyAll(); | ||
| 126 | -// } | ||
| 127 | -// | ||
| 128 | -// @Test | ||
| 129 | -// public void testConstructorCase2() { | ||
| 130 | -// expect(member.getName()).andReturn("memberName"); | ||
| 131 | -// expect(ip.getAnnotated()).andReturn(annotated).anyTimes(); | ||
| 132 | -// expect(ip.getMember()).andReturn(member).anyTimes(); | ||
| 133 | -// expect(annotated.isAnnotationPresent(Name.class)).andReturn(false); | ||
| 134 | -// expect(Reflections.getGenericTypeArgument(EasyMock.anyObject(Member.class), EasyMock.anyInt())).andReturn( | ||
| 135 | -// Object.class); | ||
| 136 | -// expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 137 | -// | ||
| 138 | -// replayAll(); | ||
| 139 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 140 | -// assertEquals("memberName", param.getKey()); | ||
| 141 | -// assertEquals(Object.class, Whitebox.getInternalState(param, "type")); | ||
| 142 | -// assertEquals(converter, param.getConverter()); | ||
| 143 | -// verifyAll(); | ||
| 144 | -// } | ||
| 145 | -// | ||
| 146 | -// @Test | ||
| 147 | -// public void testGetValueWhenSessionScopedAndParameterValueNotNull() { | ||
| 148 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 149 | -// | ||
| 150 | -// expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 151 | -// expect(request.getSession()).andReturn(session).anyTimes(); | ||
| 152 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 153 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 154 | -// expect(session.getAttribute("name")).andReturn("return"); | ||
| 155 | -// | ||
| 156 | -// session.setAttribute("name", "return"); | ||
| 157 | -// | ||
| 158 | -// replayAll(); | ||
| 159 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 160 | -// assertEquals("return", param.getValue()); | ||
| 161 | -// verifyAll(); | ||
| 162 | -// } | ||
| 163 | -// | ||
| 164 | -// @Test | ||
| 165 | -// public void testGetValueWhenSessionScopedAndParameterValueNull() { | ||
| 166 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 167 | -// | ||
| 168 | -// expect(request.getSession()).andReturn(session).anyTimes(); | ||
| 169 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn(null); | ||
| 170 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 171 | -// expect(session.getAttribute("name")).andReturn("return"); | ||
| 172 | -// | ||
| 173 | -// replayAll(); | ||
| 174 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 175 | -// assertEquals("return", param.getValue()); | ||
| 176 | -// verifyAll(); | ||
| 177 | -// } | ||
| 178 | -// | ||
| 179 | -// @Test | ||
| 180 | -// public void testGetValueWhenRequestScoped() { | ||
| 181 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 182 | -// | ||
| 183 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 184 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 185 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 186 | -// expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 187 | -// | ||
| 188 | -// replayAll(); | ||
| 189 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 190 | -// assertEquals("return", param.getValue()); | ||
| 191 | -// verifyAll(); | ||
| 192 | -// } | ||
| 193 | -// | ||
| 194 | -// @Test | ||
| 195 | -// public void testGetValueWhenViewScopedWithParamValueNotNull() { | ||
| 196 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 197 | -// Map<String, Object> map = new HashMap<String,Object>(); | ||
| 198 | -// | ||
| 199 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 200 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 201 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 202 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 203 | -// expect(Faces.getViewMap()).andReturn(map); | ||
| 204 | -// expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 205 | -// | ||
| 206 | -// replayAll(); | ||
| 207 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 208 | -// assertEquals("return", param.getValue()); | ||
| 209 | -// assertEquals("return", map.get("name")); | ||
| 210 | -// verifyAll(); | ||
| 211 | -// } | ||
| 212 | -// | ||
| 213 | -// @Test | ||
| 214 | -// public void testGetValueWhenViewScopedWithParamValueNull() { | ||
| 215 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 216 | -// Map<String, Object> map = new HashMap<String,Object>(); | ||
| 217 | -// map.put("name", "ops"); | ||
| 218 | -// | ||
| 219 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 220 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 221 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 222 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn(null); | ||
| 223 | -// expect(Faces.getViewMap()).andReturn(map); | ||
| 224 | -// | ||
| 225 | -// replayAll(); | ||
| 226 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 227 | -// assertEquals("ops", param.getValue()); | ||
| 228 | -// assertEquals("ops", map.get("name")); | ||
| 229 | -// verifyAll(); | ||
| 230 | -// } | ||
| 231 | -// | ||
| 232 | -// @Test | ||
| 233 | -// public void testGetValueElseWithValueNull() { | ||
| 234 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 235 | -// | ||
| 236 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 237 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 238 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 239 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 240 | -// expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 241 | -// | ||
| 242 | -// replayAll(); | ||
| 243 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 244 | -// assertEquals("return", param.getValue()); | ||
| 245 | -// verifyAll(); | ||
| 246 | -// } | ||
| 247 | -// | ||
| 248 | -// @Test | ||
| 249 | -// public void testGetValueElseWithValueNotNull() { | ||
| 250 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 251 | -// | ||
| 252 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 253 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 254 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 255 | -// expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 256 | -// | ||
| 257 | -// replayAll(); | ||
| 258 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 259 | -// Whitebox.setInternalState(param, "value", "myvalue"); | ||
| 260 | -// assertEquals("myvalue", param.getValue()); | ||
| 261 | -// verifyAll(); | ||
| 262 | -// } | ||
| 263 | -// | ||
| 264 | -// @Test | ||
| 265 | -// public void testSetValueIsSessionScoped() { | ||
| 266 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 267 | -// | ||
| 268 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 269 | -// expect(request.getSession()).andReturn(session); | ||
| 270 | -// | ||
| 271 | -// session.setAttribute("name", 1L); | ||
| 272 | -// | ||
| 273 | -// replayAll(); | ||
| 274 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 275 | -// param.setValue(1L); | ||
| 276 | -// verifyAll(); | ||
| 277 | -// } | ||
| 278 | -// | ||
| 279 | -// @Test | ||
| 280 | -// public void testSetValueIsViewScoped() { | ||
| 281 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 282 | -// | ||
| 283 | -// Map<String, Object> map = new HashMap<String, Object>(); | ||
| 284 | -// | ||
| 285 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 286 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 287 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 288 | -// expect(Faces.getViewMap()).andReturn(map); | ||
| 289 | -// | ||
| 290 | -// replayAll(); | ||
| 291 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 292 | -// param.setValue(1L); | ||
| 293 | -// assertEquals(1L, map.get("name")); | ||
| 294 | -// verifyAll(); | ||
| 295 | -// } | ||
| 296 | -// | ||
| 297 | -// @Test | ||
| 298 | -// public void testSetValueElse() { | ||
| 299 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 300 | -// | ||
| 301 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 302 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 303 | -// expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 304 | -// | ||
| 305 | -// replayAll(); | ||
| 306 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 307 | -// param.setValue(1L); | ||
| 308 | -// assertEquals(1L, Whitebox.getInternalState(param, "value")); | ||
| 309 | -// verifyAll(); | ||
| 310 | -// } | ||
| 311 | -// | ||
| 312 | -// @Test | ||
| 313 | -// public void testOthers() { | ||
| 314 | -// this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 315 | -// | ||
| 316 | -// expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 317 | -// expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 318 | -// | ||
| 319 | -// replayAll(); | ||
| 320 | -// param = new ParameterImpl<Long>(ip, request); | ||
| 321 | -// param.setValue(1L); | ||
| 322 | -// verifyAll(); | ||
| 323 | -// } | ||
| 324 | -// | ||
| 325 | -//} | 1 | +/* |
| 2 | + * Demoiselle Framework | ||
| 3 | + * Copyright (C) 2010 SERPRO | ||
| 4 | + * ---------------------------------------------------------------------------- | ||
| 5 | + * This file is part of Demoiselle Framework. | ||
| 6 | + * | ||
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | + * as published by the Free Software Foundation. | ||
| 10 | + * | ||
| 11 | + * This program is distributed in the hope that it will be useful, | ||
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPaOSE. See the | ||
| 14 | + * GNU General Public License for more details. | ||
| 15 | + * | ||
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | + * ---------------------------------------------------------------------------- | ||
| 21 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | + * | ||
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | + * do Software Livre (FSF). | ||
| 26 | + * | ||
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | + * para maiores detalhes. | ||
| 31 | + * | ||
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | + */ | ||
| 37 | +package br.gov.frameworkdemoiselle.internal.implementation; | ||
| 38 | + | ||
| 39 | +import static org.easymock.EasyMock.expect; | ||
| 40 | +import static org.junit.Assert.assertEquals; | ||
| 41 | +import static org.powermock.api.easymock.PowerMock.createMock; | ||
| 42 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 43 | +import static org.powermock.api.easymock.PowerMock.replayAll; | ||
| 44 | +import static org.powermock.api.easymock.PowerMock.verifyAll; | ||
| 45 | + | ||
| 46 | +import java.lang.reflect.Member; | ||
| 47 | +import java.util.HashMap; | ||
| 48 | +import java.util.Map; | ||
| 49 | + | ||
| 50 | +import javax.enterprise.context.RequestScoped; | ||
| 51 | +import javax.enterprise.context.SessionScoped; | ||
| 52 | +import javax.enterprise.inject.spi.Annotated; | ||
| 53 | +import javax.enterprise.inject.spi.InjectionPoint; | ||
| 54 | +import javax.faces.convert.Converter; | ||
| 55 | +import javax.servlet.http.HttpServletRequest; | ||
| 56 | +import javax.servlet.http.HttpSession; | ||
| 57 | +import javax.swing.text.View; | ||
| 58 | + | ||
| 59 | +import org.easymock.EasyMock; | ||
| 60 | +import org.junit.Before; | ||
| 61 | +import org.junit.Test; | ||
| 62 | +import org.junit.runner.RunWith; | ||
| 63 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 64 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
| 65 | +import org.powermock.reflect.Whitebox; | ||
| 66 | + | ||
| 67 | +import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 68 | +import br.gov.frameworkdemoiselle.annotation.ViewScoped; | ||
| 69 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 70 | +import br.gov.frameworkdemoiselle.util.Faces; | ||
| 71 | +import br.gov.frameworkdemoiselle.util.Reflections; | ||
| 72 | + | ||
| 73 | +@RunWith(PowerMockRunner.class) | ||
| 74 | +@PrepareForTest({ Reflections.class, Faces.class, Beans.class }) | ||
| 75 | +public class ParameterImplTest { | ||
| 76 | + | ||
| 77 | + private ParameterImpl<Long> param; | ||
| 78 | + | ||
| 79 | + private HttpServletRequest request; | ||
| 80 | + | ||
| 81 | + private InjectionPoint ip; | ||
| 82 | + | ||
| 83 | + private Converter converter; | ||
| 84 | + | ||
| 85 | + private Annotated annotated; | ||
| 86 | + | ||
| 87 | + private Name name; | ||
| 88 | + | ||
| 89 | + private HttpSession session; | ||
| 90 | + | ||
| 91 | + private Member member; | ||
| 92 | + | ||
| 93 | + @Before | ||
| 94 | + public void before() { | ||
| 95 | + ip = createMock(InjectionPoint.class); | ||
| 96 | + request = createMock(HttpServletRequest.class); | ||
| 97 | + session = createMock(HttpSession.class); | ||
| 98 | + annotated = createMock(Annotated.class); | ||
| 99 | + name = createMock(Name.class); | ||
| 100 | + converter = createMock(Converter.class); | ||
| 101 | + member = createMock(Member.class); | ||
| 102 | + | ||
| 103 | + mockStatic(Reflections.class); | ||
| 104 | + mockStatic(Faces.class); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + private void prepareForTestWithKeyFromNameAnnotation() { | ||
| 108 | + expect(ip.getAnnotated()).andReturn(annotated).anyTimes(); | ||
| 109 | + expect(ip.getMember()).andReturn(null); | ||
| 110 | + expect(annotated.isAnnotationPresent(Name.class)).andReturn(true); | ||
| 111 | + expect(annotated.getAnnotation(Name.class)).andReturn(name); | ||
| 112 | + expect(name.value()).andReturn("name"); | ||
| 113 | + expect(Reflections.getGenericTypeArgument(EasyMock.anyObject(Member.class), EasyMock.anyInt())).andReturn( | ||
| 114 | + Object.class); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Test | ||
| 118 | + public void testConstructorCase1() { | ||
| 119 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 120 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 121 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 122 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 123 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 124 | + | ||
| 125 | + replayAll(); | ||
| 126 | + param = new ParameterImpl<Long>(ip); | ||
| 127 | + assertEquals("name", param.getKey()); | ||
| 128 | + assertEquals(Object.class, Whitebox.getInternalState(param, "type")); | ||
| 129 | + assertEquals(converter, param.getConverter()); | ||
| 130 | + verifyAll(); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + @Test | ||
| 134 | + public void testConstructorCase2() { | ||
| 135 | + expect(member.getName()).andReturn("memberName"); | ||
| 136 | + expect(ip.getAnnotated()).andReturn(annotated).anyTimes(); | ||
| 137 | + expect(ip.getMember()).andReturn(member).anyTimes(); | ||
| 138 | + expect(annotated.isAnnotationPresent(Name.class)).andReturn(false); | ||
| 139 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 140 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 141 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 142 | + expect(Reflections.getGenericTypeArgument(EasyMock.anyObject(Member.class), EasyMock.anyInt())).andReturn( | ||
| 143 | + Object.class); | ||
| 144 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 145 | + | ||
| 146 | + replayAll(); | ||
| 147 | + param = new ParameterImpl<Long>(ip); | ||
| 148 | + assertEquals("memberName", param.getKey()); | ||
| 149 | + assertEquals(Object.class, Whitebox.getInternalState(param, "type")); | ||
| 150 | + assertEquals(converter, param.getConverter()); | ||
| 151 | + verifyAll(); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + @Test | ||
| 155 | + public void testGetValueWhenSessionScopedAndParameterValueNotNull() { | ||
| 156 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 157 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 158 | + | ||
| 159 | + mockStatic(Beans.class); | ||
| 160 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 161 | + | ||
| 162 | + expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 163 | + expect(request.getSession()).andReturn(session).anyTimes(); | ||
| 164 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 165 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 166 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 167 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 168 | + | ||
| 169 | + expect(session.getAttribute("name")).andReturn("return"); | ||
| 170 | + | ||
| 171 | + session.setAttribute("name", "return"); | ||
| 172 | + | ||
| 173 | + replayAll(); | ||
| 174 | + param = new ParameterImpl<Long>(ip); | ||
| 175 | + assertEquals("return", param.getValue()); | ||
| 176 | + verifyAll(); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + @Test | ||
| 180 | + public void testGetValueWhenSessionScopedAndParameterValueNull() { | ||
| 181 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 182 | + | ||
| 183 | + mockStatic(Beans.class); | ||
| 184 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 185 | + | ||
| 186 | + expect(request.getSession()).andReturn(session).anyTimes(); | ||
| 187 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn(null); | ||
| 188 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 189 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 190 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 191 | + expect(session.getAttribute("name")).andReturn("return"); | ||
| 192 | + | ||
| 193 | + replayAll(); | ||
| 194 | + param = new ParameterImpl<Long>(ip); | ||
| 195 | + assertEquals("return", param.getValue()); | ||
| 196 | + verifyAll(); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + @Test | ||
| 200 | + public void testGetValueWhenRequestScoped() { | ||
| 201 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 202 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 203 | + | ||
| 204 | + mockStatic(Beans.class); | ||
| 205 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 206 | + | ||
| 207 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 208 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 209 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 210 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 211 | + expect(request.getSession()).andReturn(session).anyTimes(); | ||
| 212 | + expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 213 | + | ||
| 214 | + replayAll(); | ||
| 215 | + param = new ParameterImpl<Long>(ip); | ||
| 216 | + assertEquals("return", param.getValue()); | ||
| 217 | + verifyAll(); | ||
| 218 | + } | ||
| 219 | + | ||
| 220 | + @Test | ||
| 221 | + public void testGetValueWhenViewScopedWithParamValueNotNull() { | ||
| 222 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 223 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 224 | + Map<String, Object> map = new HashMap<String,Object>(); | ||
| 225 | + | ||
| 226 | + mockStatic(Beans.class); | ||
| 227 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 228 | + | ||
| 229 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 230 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 231 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 232 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 233 | + expect(Faces.getViewMap()).andReturn(map); | ||
| 234 | + expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 235 | + | ||
| 236 | + replayAll(); | ||
| 237 | + param = new ParameterImpl<Long>(ip); | ||
| 238 | + assertEquals("return", param.getValue()); | ||
| 239 | + assertEquals("return", map.get("name")); | ||
| 240 | + verifyAll(); | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + @Test | ||
| 244 | + public void testGetValueWhenViewScopedWithParamValueNull() { | ||
| 245 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 246 | + Map<String, Object> map = new HashMap<String,Object>(); | ||
| 247 | + map.put("name", "ops"); | ||
| 248 | + | ||
| 249 | + mockStatic(Beans.class); | ||
| 250 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 251 | + | ||
| 252 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 253 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 254 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 255 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn(null); | ||
| 256 | + expect(Faces.getViewMap()).andReturn(map); | ||
| 257 | + | ||
| 258 | + replayAll(); | ||
| 259 | + param = new ParameterImpl<Long>(ip); | ||
| 260 | + assertEquals("ops", param.getValue()); | ||
| 261 | + assertEquals("ops", map.get("name")); | ||
| 262 | + verifyAll(); | ||
| 263 | + } | ||
| 264 | + | ||
| 265 | + @Test | ||
| 266 | + public void testGetValueElseWithValueNull() { | ||
| 267 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 268 | + expect(Faces.getConverter(EasyMock.anyObject(Class.class))).andReturn(converter); | ||
| 269 | + | ||
| 270 | + mockStatic(Beans.class); | ||
| 271 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 272 | + | ||
| 273 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 274 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 275 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 276 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 277 | + expect(Faces.convert("1", converter)).andReturn("return"); | ||
| 278 | + | ||
| 279 | + replayAll(); | ||
| 280 | + param = new ParameterImpl<Long>(ip); | ||
| 281 | + assertEquals("return", param.getValue()); | ||
| 282 | + verifyAll(); | ||
| 283 | + } | ||
| 284 | + | ||
| 285 | + @Test | ||
| 286 | + public void testGetValueElseWithValueNotNull() { | ||
| 287 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 288 | + | ||
| 289 | + mockStatic(Beans.class); | ||
| 290 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 291 | + | ||
| 292 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 293 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 294 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 295 | + expect(request.getParameter(EasyMock.anyObject(String.class))).andReturn("1"); | ||
| 296 | + | ||
| 297 | + replayAll(); | ||
| 298 | + param = new ParameterImpl<Long>(ip); | ||
| 299 | + Whitebox.setInternalState(param, "value", "myvalue"); | ||
| 300 | + assertEquals("myvalue", param.getValue()); | ||
| 301 | + verifyAll(); | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + @Test | ||
| 305 | + public void testSetValueIsSessionScoped() { | ||
| 306 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 307 | + | ||
| 308 | + mockStatic(Beans.class); | ||
| 309 | + expect(Beans.getReference(HttpServletRequest.class)).andReturn(request).anyTimes(); | ||
| 310 | + | ||
| 311 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(true); | ||
| 312 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 313 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 314 | + expect(request.getSession()).andReturn(session); | ||
| 315 | + | ||
| 316 | + session.setAttribute("name", 1L); | ||
| 317 | + | ||
| 318 | + replayAll(); | ||
| 319 | + param = new ParameterImpl<Long>(ip); | ||
| 320 | + param.setValue(1L); | ||
| 321 | + verifyAll(); | ||
| 322 | + } | ||
| 323 | + | ||
| 324 | + @Test | ||
| 325 | + public void testSetValueIsViewScoped() { | ||
| 326 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 327 | + | ||
| 328 | + Map<String, Object> map = new HashMap<String, Object>(); | ||
| 329 | + | ||
| 330 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 331 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 332 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(true); | ||
| 333 | + expect(Faces.getViewMap()).andReturn(map); | ||
| 334 | + | ||
| 335 | + replayAll(); | ||
| 336 | + param = new ParameterImpl<Long>(ip); | ||
| 337 | + param.setValue(1L); | ||
| 338 | + assertEquals(1L, map.get("name")); | ||
| 339 | + verifyAll(); | ||
| 340 | + } | ||
| 341 | + | ||
| 342 | + @Test | ||
| 343 | + public void testSetValueElse() { | ||
| 344 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 345 | + | ||
| 346 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 347 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(false); | ||
| 348 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 349 | + | ||
| 350 | + replayAll(); | ||
| 351 | + param = new ParameterImpl<Long>(ip); | ||
| 352 | + param.setValue(1L); | ||
| 353 | + assertEquals(1L, Whitebox.getInternalState(param, "value")); | ||
| 354 | + verifyAll(); | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + @Test | ||
| 358 | + public void testOthers() { | ||
| 359 | + this.prepareForTestWithKeyFromNameAnnotation(); | ||
| 360 | + | ||
| 361 | + expect(annotated.isAnnotationPresent(SessionScoped.class)).andReturn(false); | ||
| 362 | + expect(annotated.isAnnotationPresent(RequestScoped.class)).andReturn(true); | ||
| 363 | + expect(annotated.isAnnotationPresent(ViewScoped.class)).andReturn(false); | ||
| 364 | + | ||
| 365 | + replayAll(); | ||
| 366 | + param = new ParameterImpl<Long>(ip); | ||
| 367 | + param.setValue(1L); | ||
| 368 | + verifyAll(); | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | +} |