Commit 0cc2807702b0cccd2d252fab8087355166f34a23
1 parent
c9c67673
Exists in
master
Refatoração dos testes unitários
Showing
3 changed files
with
1056 additions
and
1056 deletions
Show diff stats
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.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.interceptor; | ||
| 38 | -// | ||
| 39 | -//import static org.easymock.EasyMock.expect; | ||
| 40 | -//import static org.easymock.EasyMock.verify; | ||
| 41 | -//import static org.junit.Assert.assertEquals; | ||
| 42 | -//import static org.junit.Assert.assertNull; | ||
| 43 | -//import static org.junit.Assert.assertTrue; | ||
| 44 | -//import static org.junit.Assert.fail; | ||
| 45 | -//import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 46 | -//import static org.powermock.api.easymock.PowerMock.replay; | ||
| 47 | -//import static org.powermock.api.easymock.PowerMock.replayAll; | ||
| 48 | -//import static org.powermock.api.easymock.PowerMock.verifyAll; | ||
| 49 | -// | ||
| 50 | -//import java.util.Locale; | ||
| 51 | -// | ||
| 52 | -//import javax.interceptor.InvocationContext; | ||
| 53 | -// | ||
| 54 | -//import org.easymock.EasyMock; | ||
| 55 | -//import org.junit.Before; | ||
| 56 | -//import org.junit.Test; | ||
| 57 | -//import org.junit.runner.RunWith; | ||
| 58 | -//import org.powermock.api.easymock.PowerMock; | ||
| 59 | -//import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 60 | -//import org.powermock.modules.junit4.PowerMockRunner; | ||
| 61 | -//import org.slf4j.Logger; | ||
| 62 | -// | ||
| 63 | -//import br.gov.frameworkdemoiselle.DemoiselleException; | ||
| 64 | -//import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | ||
| 65 | -//import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; | ||
| 66 | -//import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 67 | -// | ||
| 68 | -//@RunWith(PowerMockRunner.class) | ||
| 69 | -//@PrepareForTest(CoreBootstrap.class) | ||
| 70 | -//public class ExceptionHandlerInterceptorTest { | ||
| 71 | -// | ||
| 72 | -// private ExceptionHandlerInterceptor interceptor; | ||
| 73 | -// | ||
| 74 | -// private InvocationContext context; | ||
| 75 | -// | ||
| 76 | -// private Logger logger; | ||
| 77 | -// | ||
| 78 | -// private ResourceBundle bundle; | ||
| 79 | -// | ||
| 80 | -// class TestException extends DemoiselleException { | ||
| 81 | -// | ||
| 82 | -// private static final long serialVersionUID = 1L; | ||
| 83 | -// | ||
| 84 | -// public TestException(String message) { | ||
| 85 | -// super(message); | ||
| 86 | -// } | ||
| 87 | -// } | ||
| 88 | -// | ||
| 89 | -// class ClassWithMethodsAnnotatedWithExceptionHandler { | ||
| 90 | -// | ||
| 91 | -// int times = 0; | ||
| 92 | -// | ||
| 93 | -// @ExceptionHandler | ||
| 94 | -// public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 95 | -// times++; | ||
| 96 | -// } | ||
| 97 | -// | ||
| 98 | -// @ExceptionHandler | ||
| 99 | -// public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { | ||
| 100 | -// times++; | ||
| 101 | -// } | ||
| 102 | -// | ||
| 103 | -// } | ||
| 104 | -// | ||
| 105 | -// class ClassWithoutMethodsAnnotatedWithExceptionHandler { | ||
| 106 | -// | ||
| 107 | -// public void methodWithoutExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 108 | -// } | ||
| 109 | -// } | ||
| 110 | -// | ||
| 111 | -// class ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException { | ||
| 112 | -// | ||
| 113 | -// int times = 0; | ||
| 114 | -// | ||
| 115 | -// @ExceptionHandler | ||
| 116 | -// public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 117 | -// times++; | ||
| 118 | -// throw new RuntimeException(); | ||
| 119 | -// } | ||
| 120 | -// } | ||
| 121 | -// | ||
| 122 | -// class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { | ||
| 123 | -// | ||
| 124 | -// @ExceptionHandler | ||
| 125 | -// public void methodWithExceptionHandlerAnotation() { | ||
| 126 | -// } | ||
| 127 | -// | ||
| 128 | -// } | ||
| 129 | -// | ||
| 130 | -// class ClassWithExceptionHandlerMethodThatRethrowException { | ||
| 131 | -// | ||
| 132 | -// int times = 0; | ||
| 133 | -// | ||
| 134 | -// @ExceptionHandler | ||
| 135 | -// public void methodThatRethrowException(TestException cause) { | ||
| 136 | -// times++; | ||
| 137 | -// throw cause; | ||
| 138 | -// } | ||
| 139 | -// | ||
| 140 | -// } | ||
| 141 | -// | ||
| 142 | -// @Before | ||
| 143 | -// public void setUp() throws Exception { | ||
| 144 | -// this.logger = PowerMock.createMock(Logger.class); | ||
| 145 | -// this.bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
| 146 | -// this.logger.info(EasyMock.anyObject(String.class)); | ||
| 147 | -// PowerMock.expectLastCall().anyTimes(); | ||
| 148 | -// replay(this.logger); | ||
| 149 | -// this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); | ||
| 150 | -// this.context = PowerMock.createMock(InvocationContext.class); | ||
| 151 | -// mockStatic(CoreBootstrap.class); | ||
| 152 | -// } | ||
| 153 | -// | ||
| 154 | -// @Test | ||
| 155 | -// public void manageSuccessfully() throws Throwable { | ||
| 156 | -// expect(this.context.proceed()).andReturn(null); | ||
| 157 | -// replay(); | ||
| 158 | -// assertEquals(null, this.interceptor.manage(this.context)); | ||
| 159 | -// verify(); | ||
| 160 | -// } | ||
| 161 | -// | ||
| 162 | -// @Test | ||
| 163 | -// public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { | ||
| 164 | -// ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); | ||
| 165 | -// expect(this.context.getTarget()).andReturn(classWithoutException); | ||
| 166 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 167 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithoutMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 168 | -// replayAll(this.context, ClassWithoutMethodsAnnotatedWithExceptionHandler.class); | ||
| 169 | -// | ||
| 170 | -// try { | ||
| 171 | -// this.interceptor.manage(this.context); | ||
| 172 | -// fail(); | ||
| 173 | -// } catch (DemoiselleException e) { | ||
| 174 | -// assertTrue(true); | ||
| 175 | -// } | ||
| 176 | -// | ||
| 177 | -// verifyAll(); | ||
| 178 | -// } | ||
| 179 | -// | ||
| 180 | -// @Test | ||
| 181 | -// public void manageWithClassThatContainsHandleMethod() throws Exception { | ||
| 182 | -// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 183 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 184 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 185 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 186 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 187 | -// | ||
| 188 | -// assertNull(this.interceptor.manage(this.context)); | ||
| 189 | -// assertEquals(1, classWithException.times); | ||
| 190 | -// verifyAll(); | ||
| 191 | -// } | ||
| 192 | -// | ||
| 193 | -// @Test | ||
| 194 | -// public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { | ||
| 195 | -// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 196 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 197 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 198 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 199 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 200 | -// | ||
| 201 | -// assertNull(this.interceptor.manage(this.context)); | ||
| 202 | -// assertEquals(1, classWithException.times); | ||
| 203 | -// verifyAll(); | ||
| 204 | -// } | ||
| 205 | -// | ||
| 206 | -// @Test | ||
| 207 | -// public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { | ||
| 208 | -// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 209 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 210 | -// expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 211 | -// replay(this.context); | ||
| 212 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 213 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 214 | -// | ||
| 215 | -// try { | ||
| 216 | -// this.interceptor.manage(this.context); | ||
| 217 | -// fail(); | ||
| 218 | -// } catch (TestException e) { | ||
| 219 | -// assertEquals(0, classWithException.times); | ||
| 220 | -// } | ||
| 221 | -// | ||
| 222 | -// verify(); | ||
| 223 | -// } | ||
| 224 | -// | ||
| 225 | -// @Test | ||
| 226 | -// public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { | ||
| 227 | -// ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); | ||
| 228 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 229 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 230 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException.class)) | ||
| 231 | -// .andReturn(true); | ||
| 232 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 233 | -// | ||
| 234 | -// try { | ||
| 235 | -// this.interceptor.manage(this.context); | ||
| 236 | -// fail(); | ||
| 237 | -// } catch (RuntimeException e) { | ||
| 238 | -// assertEquals(1, classWithException.times); | ||
| 239 | -// } | ||
| 240 | -// | ||
| 241 | -// verifyAll(); | ||
| 242 | -// } | ||
| 243 | -// | ||
| 244 | -// @Test | ||
| 245 | -// public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { | ||
| 246 | -// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 247 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 248 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 249 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true) | ||
| 250 | -// .anyTimes(); | ||
| 251 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 252 | -// | ||
| 253 | -// assertNull(this.interceptor.manage(this.context)); | ||
| 254 | -// assertEquals(1, classWithException.times); | ||
| 255 | -// | ||
| 256 | -// this.context = PowerMock.createMock(InvocationContext.class); | ||
| 257 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 258 | -// expect(this.context.proceed()).andThrow(new Exception("")); | ||
| 259 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 260 | -// | ||
| 261 | -// assertNull(this.interceptor.manage(this.context)); | ||
| 262 | -// assertEquals(2, classWithException.times); | ||
| 263 | -// verifyAll(); | ||
| 264 | -// | ||
| 265 | -// } | ||
| 266 | -// | ||
| 267 | -// @Test | ||
| 268 | -// public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { | ||
| 269 | -// ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); | ||
| 270 | -// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 271 | -// expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 272 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)) | ||
| 273 | -// .andReturn(true); | ||
| 274 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 275 | -// | ||
| 276 | -// try { | ||
| 277 | -// this.interceptor.manage(this.context); | ||
| 278 | -// fail(); | ||
| 279 | -// } catch (DemoiselleException e) { | ||
| 280 | -// assertTrue(true); | ||
| 281 | -// } | ||
| 282 | -// | ||
| 283 | -// verifyAll(); | ||
| 284 | -// } | ||
| 285 | -// | ||
| 286 | -// @Test | ||
| 287 | -// public void manageHandlerMethodThatRethrowExpectedException() throws Exception { | ||
| 288 | -// ClassWithExceptionHandlerMethodThatRethrowException testClass = new ClassWithExceptionHandlerMethodThatRethrowException(); | ||
| 289 | -// expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 290 | -// expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 291 | -// expect(CoreBootstrap.isAnnotatedType(ClassWithExceptionHandlerMethodThatRethrowException.class)) | ||
| 292 | -// .andReturn(true); | ||
| 293 | -// replayAll(this.context, CoreBootstrap.class); | ||
| 294 | -// | ||
| 295 | -// try { | ||
| 296 | -// this.interceptor.manage(this.context); | ||
| 297 | -// fail(); | ||
| 298 | -// } catch (TestException e) { | ||
| 299 | -// assertEquals(1, testClass.times); | ||
| 300 | -// } | ||
| 301 | -// | ||
| 302 | -// verifyAll(); | ||
| 303 | -// } | ||
| 304 | -// | ||
| 305 | -// /** | ||
| 306 | -// * Tests an exception handler when the class that contains the method is a proxy. This is the case when using | ||
| 307 | -// * injection. | ||
| 308 | -// * | ||
| 309 | -// * @throws Exception | ||
| 310 | -// */ | ||
| 311 | -// @Test | ||
| 312 | -// public void manageHandlerMethodInsideProxyClass() throws Exception { | ||
| 313 | -// // creates a proxy class | ||
| 314 | -// ClassWithExceptionHandlerMethodThatRethrowException testClass = PowerMock | ||
| 315 | -// .createNicePartialMockForAllMethodsExcept(ClassWithExceptionHandlerMethodThatRethrowException.class, | ||
| 316 | -// "methodThatRethrowException"); | ||
| 317 | -// expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 318 | -// expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 319 | -// expect(CoreBootstrap.isAnnotatedType(testClass.getClass())).andReturn(false); | ||
| 320 | -// | ||
| 321 | -// this.logger = PowerMock.createMock(Logger.class); | ||
| 322 | -// this.logger.info(EasyMock.anyObject(String.class)); | ||
| 323 | -// this.logger.debug(EasyMock.anyObject(String.class)); | ||
| 324 | -// replayAll(testClass, this.context, CoreBootstrap.class, logger); | ||
| 325 | -// | ||
| 326 | -// this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); | ||
| 327 | -// | ||
| 328 | -// try { | ||
| 329 | -// this.interceptor.manage(this.context); | ||
| 330 | -// fail(); | ||
| 331 | -// } catch (TestException e) { | ||
| 332 | -// assertEquals(1, testClass.times); | ||
| 333 | -// } | ||
| 334 | -// } | ||
| 335 | -// | ||
| 336 | -//} | 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.interceptor; | ||
| 38 | + | ||
| 39 | +import static org.easymock.EasyMock.expect; | ||
| 40 | +import static org.easymock.EasyMock.verify; | ||
| 41 | +import static org.junit.Assert.assertEquals; | ||
| 42 | +import static org.junit.Assert.assertNull; | ||
| 43 | +import static org.junit.Assert.assertTrue; | ||
| 44 | +import static org.junit.Assert.fail; | ||
| 45 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 46 | +import static org.powermock.api.easymock.PowerMock.replay; | ||
| 47 | +import static org.powermock.api.easymock.PowerMock.replayAll; | ||
| 48 | +import static org.powermock.api.easymock.PowerMock.verifyAll; | ||
| 49 | + | ||
| 50 | +import java.util.Locale; | ||
| 51 | + | ||
| 52 | +import javax.interceptor.InvocationContext; | ||
| 53 | + | ||
| 54 | +import org.easymock.EasyMock; | ||
| 55 | +import org.junit.Before; | ||
| 56 | +import org.junit.Test; | ||
| 57 | +import org.junit.runner.RunWith; | ||
| 58 | +import org.powermock.api.easymock.PowerMock; | ||
| 59 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 60 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
| 61 | +import org.slf4j.Logger; | ||
| 62 | + | ||
| 63 | +import br.gov.frameworkdemoiselle.DemoiselleException; | ||
| 64 | +import br.gov.frameworkdemoiselle.exception.ExceptionHandler; | ||
| 65 | +import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; | ||
| 66 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 67 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 68 | + | ||
| 69 | +@RunWith(PowerMockRunner.class) | ||
| 70 | +@PrepareForTest(Beans.class) | ||
| 71 | +public class ExceptionHandlerInterceptorTest { | ||
| 72 | + | ||
| 73 | + private ExceptionHandlerInterceptor interceptor; | ||
| 74 | + | ||
| 75 | + private InvocationContext context; | ||
| 76 | + | ||
| 77 | + private Logger logger; | ||
| 78 | + | ||
| 79 | + private CoreBootstrap coreBootstrap; | ||
| 80 | + | ||
| 81 | + class TestException extends DemoiselleException { | ||
| 82 | + | ||
| 83 | + private static final long serialVersionUID = 1L; | ||
| 84 | + | ||
| 85 | + public TestException(String message) { | ||
| 86 | + super(message); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + class ClassWithMethodsAnnotatedWithExceptionHandler { | ||
| 91 | + | ||
| 92 | + int times = 0; | ||
| 93 | + | ||
| 94 | + @ExceptionHandler | ||
| 95 | + public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 96 | + times++; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + @ExceptionHandler | ||
| 100 | + public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { | ||
| 101 | + times++; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + class ClassWithoutMethodsAnnotatedWithExceptionHandler { | ||
| 107 | + | ||
| 108 | + public void methodWithoutExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 109 | + } | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + class ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException { | ||
| 113 | + | ||
| 114 | + int times = 0; | ||
| 115 | + | ||
| 116 | + @ExceptionHandler | ||
| 117 | + public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { | ||
| 118 | + times++; | ||
| 119 | + throw new RuntimeException(); | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { | ||
| 124 | + | ||
| 125 | + @ExceptionHandler | ||
| 126 | + public void methodWithExceptionHandlerAnotation() { | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + class ClassWithExceptionHandlerMethodThatRethrowException { | ||
| 132 | + | ||
| 133 | + int times = 0; | ||
| 134 | + | ||
| 135 | + @ExceptionHandler | ||
| 136 | + public void methodThatRethrowException(TestException cause) { | ||
| 137 | + times++; | ||
| 138 | + throw cause; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + @Before | ||
| 144 | + public void setUp() throws Exception { | ||
| 145 | + | ||
| 146 | + this.interceptor = new ExceptionHandlerInterceptor(); | ||
| 147 | + this.context = PowerMock.createMock(InvocationContext.class); | ||
| 148 | + this.coreBootstrap = PowerMock.createMock(CoreBootstrap.class); | ||
| 149 | + | ||
| 150 | + mockStatic(Beans.class); | ||
| 151 | + expect(Beans.getReference(Locale.class)).andReturn(Locale.getDefault()).anyTimes(); | ||
| 152 | + expect(Beans.getReference(CoreBootstrap.class)).andReturn(this.coreBootstrap).anyTimes(); | ||
| 153 | + | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @Test | ||
| 157 | + public void manageSuccessfully() throws Throwable { | ||
| 158 | + expect(this.context.proceed()).andReturn(null); | ||
| 159 | + replayAll(); | ||
| 160 | + assertEquals(null, this.interceptor.manage(this.context)); | ||
| 161 | + verify(); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + @Test | ||
| 165 | + public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { | ||
| 166 | + ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); | ||
| 167 | + | ||
| 168 | + expect(this.context.getTarget()).andReturn(classWithoutException); | ||
| 169 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 170 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithoutMethodsAnnotatedWithExceptionHandler.class)).andReturn( | ||
| 171 | + true); | ||
| 172 | + | ||
| 173 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 174 | + | ||
| 175 | + try { | ||
| 176 | + this.interceptor.manage(this.context); | ||
| 177 | + fail(); | ||
| 178 | + } catch (DemoiselleException e) { | ||
| 179 | + assertTrue(true); | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + verifyAll(); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + @Test | ||
| 186 | + public void manageWithClassThatContainsHandleMethod() throws Exception { | ||
| 187 | + ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 188 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 189 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 190 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 191 | + | ||
| 192 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 193 | + | ||
| 194 | + assertNull(this.interceptor.manage(this.context)); | ||
| 195 | + assertEquals(1, classWithException.times); | ||
| 196 | + verifyAll(); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + @Test | ||
| 200 | + public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { | ||
| 201 | + ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 202 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 203 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 204 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 205 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 206 | + | ||
| 207 | + assertNull(this.interceptor.manage(this.context)); | ||
| 208 | + assertEquals(1, classWithException.times); | ||
| 209 | + verifyAll(); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @Test | ||
| 213 | + public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { | ||
| 214 | + ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 215 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 216 | + expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 217 | + replay(this.context); | ||
| 218 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | ||
| 219 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 220 | + | ||
| 221 | + try { | ||
| 222 | + this.interceptor.manage(this.context); | ||
| 223 | + fail(); | ||
| 224 | + } catch (TestException e) { | ||
| 225 | + assertEquals(0, classWithException.times); | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + verify(); | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + @Test | ||
| 232 | + public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { | ||
| 233 | + ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); | ||
| 234 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 235 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 236 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException.class)) | ||
| 237 | + .andReturn(true); | ||
| 238 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 239 | + | ||
| 240 | + try { | ||
| 241 | + this.interceptor.manage(this.context); | ||
| 242 | + fail(); | ||
| 243 | + } catch (RuntimeException e) { | ||
| 244 | + assertEquals(1, classWithException.times); | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + verifyAll(); | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + @Test | ||
| 251 | + public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { | ||
| 252 | + ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 253 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 254 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 255 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true) | ||
| 256 | + .anyTimes(); | ||
| 257 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 258 | + | ||
| 259 | + assertNull(this.interceptor.manage(this.context)); | ||
| 260 | + assertEquals(1, classWithException.times); | ||
| 261 | + | ||
| 262 | + this.context = PowerMock.createMock(InvocationContext.class); | ||
| 263 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 264 | + expect(this.context.proceed()).andThrow(new Exception("")); | ||
| 265 | + replayAll(this.context); | ||
| 266 | + | ||
| 267 | + assertNull(this.interceptor.manage(this.context)); | ||
| 268 | + assertEquals(2, classWithException.times); | ||
| 269 | + verifyAll(); | ||
| 270 | + | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + @Test | ||
| 274 | + public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { | ||
| 275 | + ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); | ||
| 276 | + expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 277 | + expect(this.context.proceed()).andThrow(new DemoiselleException("")); | ||
| 278 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)) | ||
| 279 | + .andReturn(true); | ||
| 280 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 281 | + | ||
| 282 | + try { | ||
| 283 | + this.interceptor.manage(this.context); | ||
| 284 | + fail(); | ||
| 285 | + } catch (DemoiselleException e) { | ||
| 286 | + assertTrue(true); | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + verifyAll(); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + @Test | ||
| 293 | + public void manageHandlerMethodThatRethrowExpectedException() throws Exception { | ||
| 294 | + ClassWithExceptionHandlerMethodThatRethrowException testClass = new ClassWithExceptionHandlerMethodThatRethrowException(); | ||
| 295 | + expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 296 | + expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 297 | + expect(this.coreBootstrap.isAnnotatedType(ClassWithExceptionHandlerMethodThatRethrowException.class)) | ||
| 298 | + .andReturn(true); | ||
| 299 | + replayAll(this.context, this.coreBootstrap, Beans.class); | ||
| 300 | + | ||
| 301 | + try { | ||
| 302 | + this.interceptor.manage(this.context); | ||
| 303 | + fail(); | ||
| 304 | + } catch (TestException e) { | ||
| 305 | + assertEquals(1, testClass.times); | ||
| 306 | + } | ||
| 307 | + | ||
| 308 | + verifyAll(); | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + /** | ||
| 312 | + * Tests an exception handler when the class that contains the method is a proxy. This is the case when using | ||
| 313 | + * injection. | ||
| 314 | + * | ||
| 315 | + * @throws Exception | ||
| 316 | + */ | ||
| 317 | + @Test | ||
| 318 | + public void manageHandlerMethodInsideProxyClass() throws Exception { | ||
| 319 | + // creates a proxy class | ||
| 320 | + ClassWithExceptionHandlerMethodThatRethrowException testClass = PowerMock | ||
| 321 | + .createNicePartialMockForAllMethodsExcept(ClassWithExceptionHandlerMethodThatRethrowException.class, | ||
| 322 | + "methodThatRethrowException"); | ||
| 323 | + expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 324 | + expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 325 | + expect(this.coreBootstrap.isAnnotatedType(testClass.getClass())).andReturn(false); | ||
| 326 | + | ||
| 327 | + this.logger = PowerMock.createMock(Logger.class); | ||
| 328 | + this.logger.info(EasyMock.anyObject(String.class)); | ||
| 329 | + this.logger.debug(EasyMock.anyObject(String.class)); | ||
| 330 | + replayAll(testClass, this.context, this.coreBootstrap, logger, Beans.class); | ||
| 331 | + | ||
| 332 | + this.interceptor = new ExceptionHandlerInterceptor(); | ||
| 333 | + | ||
| 334 | + try { | ||
| 335 | + this.interceptor.manage(this.context); | ||
| 336 | + fail(); | ||
| 337 | + } catch (TestException e) { | ||
| 338 | + assertEquals(1, testClass.times); | ||
| 339 | + } | ||
| 340 | + } | ||
| 341 | + | ||
| 342 | +} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptorTest.java
| 1 | -//package br.gov.frameworkdemoiselle.internal.interceptor; | ||
| 2 | -// | ||
| 3 | -//import static org.easymock.EasyMock.createMock; | ||
| 4 | -//import static org.easymock.EasyMock.expect; | ||
| 5 | -//import static org.easymock.EasyMock.expectLastCall; | ||
| 6 | -//import static org.junit.Assert.fail; | ||
| 7 | -//import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 8 | -//import static org.powermock.api.easymock.PowerMock.replay; | ||
| 9 | -// | ||
| 10 | -//import java.util.Locale; | ||
| 11 | -// | ||
| 12 | -//import javax.enterprise.inject.Instance; | ||
| 13 | -//import javax.interceptor.InvocationContext; | ||
| 14 | -// | ||
| 15 | -//import org.junit.Before; | ||
| 16 | -//import org.junit.Test; | ||
| 17 | -//import org.junit.runner.RunWith; | ||
| 18 | -//import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 19 | -//import org.powermock.modules.junit4.PowerMockRunner; | ||
| 20 | -//import org.slf4j.Logger; | ||
| 21 | -// | ||
| 22 | -//import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 23 | -//import br.gov.frameworkdemoiselle.internal.implementation.CoreBundle; | ||
| 24 | -//import br.gov.frameworkdemoiselle.security.NotLoggedInException; | ||
| 25 | -//import br.gov.frameworkdemoiselle.security.RequiredPermission; | ||
| 26 | -//import br.gov.frameworkdemoiselle.security.SecurityContext; | ||
| 27 | -//import br.gov.frameworkdemoiselle.security.SecurityException; | ||
| 28 | -//import br.gov.frameworkdemoiselle.security.User; | ||
| 29 | -//import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 30 | -// | ||
| 31 | -//@RunWith(PowerMockRunner.class) | ||
| 32 | -//@PrepareForTest(CoreBundle.class) | ||
| 33 | -//public class RequiredPermissionInterceptorTest { | ||
| 34 | -// | ||
| 35 | -// private RequiredPermissionInterceptor interceptor; | ||
| 36 | -// | ||
| 37 | -// private InvocationContext ic; | ||
| 38 | -// | ||
| 39 | -// private SecurityContext securityContext; | ||
| 40 | -// | ||
| 41 | -// class UnnamedClass { | ||
| 42 | -// | ||
| 43 | -// @RequiredPermission | ||
| 44 | -// public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 45 | -// } | ||
| 46 | -// | ||
| 47 | -// @RequiredPermission(operation = "insert") | ||
| 48 | -// public void requiredPermissionWithDeclaredOperation() { | ||
| 49 | -// } | ||
| 50 | -// | ||
| 51 | -// @RequiredPermission(resource = "contact") | ||
| 52 | -// public void requiredPermissionWithDeclaredResource() { | ||
| 53 | -// } | ||
| 54 | -// | ||
| 55 | -// @RequiredPermission(resource = "contact", operation = "insert") | ||
| 56 | -// public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 57 | -// } | ||
| 58 | -// } | ||
| 59 | -// | ||
| 60 | -// @Name("contact2") | ||
| 61 | -// class NamedClass { | ||
| 62 | -// | ||
| 63 | -// @RequiredPermission | ||
| 64 | -// public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 65 | -// } | ||
| 66 | -// | ||
| 67 | -// @RequiredPermission(operation = "insert") | ||
| 68 | -// public void requiredPermissionWithDeclaredOperation() { | ||
| 69 | -// } | ||
| 70 | -// | ||
| 71 | -// @RequiredPermission(resource = "contact") | ||
| 72 | -// public void requiredPermissionWithDeclaredResource() { | ||
| 73 | -// } | ||
| 74 | -// | ||
| 75 | -// @RequiredPermission(resource = "contact", operation = "insert") | ||
| 76 | -// public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 77 | -// } | ||
| 78 | -// } | ||
| 79 | -// | ||
| 80 | -// @Name("contact2") | ||
| 81 | -// class NamedClassWithNamedMethods { | ||
| 82 | -// | ||
| 83 | -// @Name("delete") | ||
| 84 | -// @RequiredPermission | ||
| 85 | -// public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 86 | -// } | ||
| 87 | -// | ||
| 88 | -// @Name("delete") | ||
| 89 | -// @RequiredPermission(operation = "insert") | ||
| 90 | -// public void requiredPermissionWithDeclaredOperation() { | ||
| 91 | -// } | ||
| 92 | -// | ||
| 93 | -// @Name("delete") | ||
| 94 | -// @RequiredPermission(resource = "contact") | ||
| 95 | -// public void requiredPermissionWithDeclaredResource() { | ||
| 96 | -// } | ||
| 97 | -// | ||
| 98 | -// @Name("delete") | ||
| 99 | -// @RequiredPermission(resource = "contact", operation = "insert") | ||
| 100 | -// public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 101 | -// } | ||
| 102 | -// } | ||
| 103 | -// | ||
| 104 | -// @RequiredPermission | ||
| 105 | -// class ClassAnnotedWithRequiredPermission { | ||
| 106 | -// | ||
| 107 | -// public void withoutRequiredPermissionAnnotation() { | ||
| 108 | -// } | ||
| 109 | -// | ||
| 110 | -// @RequiredPermission(operation = "insert") | ||
| 111 | -// public void requiredPermissionWithDeclaredOperation() { | ||
| 112 | -// } | ||
| 113 | -// } | ||
| 114 | -// | ||
| 115 | -// @Before | ||
| 116 | -// public void setUp() throws Exception { | ||
| 117 | -// @SuppressWarnings("unchecked") | ||
| 118 | -// Instance<SecurityContext> securityContextInstance = createMock(Instance.class); | ||
| 119 | -// Logger logger = createMock(Logger.class); | ||
| 120 | -// | ||
| 121 | -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
| 122 | -// User user = createMock(User.class); | ||
| 123 | -// | ||
| 124 | -// mockStatic(CoreBundle.class); | ||
| 125 | -// expect(CoreBundle.get()).andReturn(bundle); | ||
| 126 | -// | ||
| 127 | -// this.securityContext = createMock(SecurityContext.class); | ||
| 128 | -// this.ic = createMock(InvocationContext.class); | ||
| 129 | -// | ||
| 130 | -// expect(user.getId()).andReturn("UserName").anyTimes(); | ||
| 131 | -// expect(this.securityContext.getUser()).andReturn(user).anyTimes(); | ||
| 132 | -// expect(securityContextInstance.get()).andReturn(securityContext).anyTimes(); | ||
| 133 | -// expect(this.ic.proceed()).andReturn(null); | ||
| 134 | -// replay(securityContextInstance, user, CoreBundle.class); | ||
| 135 | -// | ||
| 136 | -// this.interceptor = new RequiredPermissionInterceptor(securityContextInstance, bundle, logger); | ||
| 137 | -// } | ||
| 138 | -// | ||
| 139 | -// private void prepareMock(Object target, String methodName, String expectedResource, String expectedOperation, | ||
| 140 | -// boolean hasPermission, boolean isLoggedUser) throws Exception { | ||
| 141 | -// | ||
| 142 | -// expect(this.securityContext.isLoggedIn()).andReturn(isLoggedUser).anyTimes(); | ||
| 143 | -// | ||
| 144 | -// this.securityContext.hasPermission(expectedResource, expectedOperation); | ||
| 145 | -// | ||
| 146 | -// if (isLoggedUser) { | ||
| 147 | -// expectLastCall().andReturn(hasPermission); | ||
| 148 | -// } else { | ||
| 149 | -// expectLastCall().andThrow(new NotLoggedInException("")); | ||
| 150 | -// } | ||
| 151 | -// | ||
| 152 | -// expect(this.ic.getTarget()).andReturn(target).anyTimes(); | ||
| 153 | -// expect(this.ic.getMethod()).andReturn(target.getClass().getMethod(methodName)).anyTimes(); | ||
| 154 | -// replay(this.ic, this.securityContext); | ||
| 155 | -// } | ||
| 156 | -// | ||
| 157 | -// /* Testing UnnamedClass */ | ||
| 158 | -// | ||
| 159 | -// @Test | ||
| 160 | -// public void testManageUnnamedClassAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() throws Exception { | ||
| 161 | -// try { | ||
| 162 | -// Object target = new UnnamedClass(); | ||
| 163 | -// String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 164 | -// String expectedResource = target.getClass().getSimpleName(); | ||
| 165 | -// String expectedOperation = methodName; | ||
| 166 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 167 | -// | ||
| 168 | -// interceptor.manage(this.ic); | ||
| 169 | -// } catch (SecurityException cause) { | ||
| 170 | -// fail(); | ||
| 171 | -// } | ||
| 172 | -// } | ||
| 173 | -// | ||
| 174 | -// @Test | ||
| 175 | -// public void testManageUnnamedClassAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 176 | -// try { | ||
| 177 | -// Object target = new UnnamedClass(); | ||
| 178 | -// String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 179 | -// String expectedResource = target.getClass().getSimpleName(); | ||
| 180 | -// String expectedOperation = "insert"; | ||
| 181 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 182 | -// | ||
| 183 | -// interceptor.manage(this.ic); | ||
| 184 | -// } catch (SecurityException cause) { | ||
| 185 | -// fail(); | ||
| 186 | -// } | ||
| 187 | -// } | ||
| 188 | -// | ||
| 189 | -// @Test | ||
| 190 | -// public void testManageUnnamedClassAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 191 | -// try { | ||
| 192 | -// Object target = new UnnamedClass(); | ||
| 193 | -// String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 194 | -// String expectedResource = "contact"; | ||
| 195 | -// String expectedOperation = methodName; | ||
| 196 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 197 | -// | ||
| 198 | -// interceptor.manage(this.ic); | ||
| 199 | -// } catch (SecurityException cause) { | ||
| 200 | -// fail(); | ||
| 201 | -// } | ||
| 202 | -// } | ||
| 203 | -// | ||
| 204 | -// @Test | ||
| 205 | -// public void testManageUnnamedClassAtRequiredPermissionWithDeclaredResourceAndOperation() throws Exception { | ||
| 206 | -// try { | ||
| 207 | -// Object target = new UnnamedClass(); | ||
| 208 | -// String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 209 | -// String expectedResource = "contact"; | ||
| 210 | -// String expectedOperation = "insert"; | ||
| 211 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 212 | -// | ||
| 213 | -// interceptor.manage(this.ic); | ||
| 214 | -// } catch (SecurityException cause) { | ||
| 215 | -// fail(); | ||
| 216 | -// } | ||
| 217 | -// } | ||
| 218 | -// | ||
| 219 | -// /* Testing NamedClass */ | ||
| 220 | -// | ||
| 221 | -// @Test | ||
| 222 | -// public void testManageNamedClassAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() throws Exception { | ||
| 223 | -// try { | ||
| 224 | -// Object target = new NamedClass(); | ||
| 225 | -// String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 226 | -// String expectedResource = "contact2"; | ||
| 227 | -// String expectedOperation = methodName; | ||
| 228 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 229 | -// | ||
| 230 | -// interceptor.manage(this.ic); | ||
| 231 | -// } catch (SecurityException cause) { | ||
| 232 | -// fail(); | ||
| 233 | -// } | ||
| 234 | -// } | ||
| 235 | -// | ||
| 236 | -// @Test | ||
| 237 | -// public void testManageNamedClassAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 238 | -// try { | ||
| 239 | -// Object target = new NamedClass(); | ||
| 240 | -// String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 241 | -// String expectedResource = "contact2"; | ||
| 242 | -// String expectedOperation = "insert"; | ||
| 243 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 244 | -// | ||
| 245 | -// interceptor.manage(this.ic); | ||
| 246 | -// } catch (SecurityException cause) { | ||
| 247 | -// fail(); | ||
| 248 | -// } | ||
| 249 | -// } | ||
| 250 | -// | ||
| 251 | -// @Test | ||
| 252 | -// public void testManageNamedClassAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 253 | -// try { | ||
| 254 | -// Object target = new NamedClass(); | ||
| 255 | -// String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 256 | -// String expectedResource = "contact"; | ||
| 257 | -// String expectedOperation = methodName; | ||
| 258 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 259 | -// | ||
| 260 | -// interceptor.manage(this.ic); | ||
| 261 | -// } catch (SecurityException cause) { | ||
| 262 | -// fail(); | ||
| 263 | -// } | ||
| 264 | -// } | ||
| 265 | -// | ||
| 266 | -// @Test | ||
| 267 | -// public void testManageNamedClassAtRequiredPermissionWithDeclaredResourceAndOperation() throws Exception { | ||
| 268 | -// try { | ||
| 269 | -// Object target = new NamedClass(); | ||
| 270 | -// String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 271 | -// String expectedResource = "contact"; | ||
| 272 | -// String expectedOperation = "insert"; | ||
| 273 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 274 | -// | ||
| 275 | -// interceptor.manage(this.ic); | ||
| 276 | -// } catch (SecurityException cause) { | ||
| 277 | -// fail(); | ||
| 278 | -// } | ||
| 279 | -// } | ||
| 280 | -// | ||
| 281 | -// /* Testing NamedClassWithNamedMethods */ | ||
| 282 | -// | ||
| 283 | -// @Test | ||
| 284 | -// public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() | ||
| 285 | -// throws Exception { | ||
| 286 | -// try { | ||
| 287 | -// Object target = new NamedClassWithNamedMethods(); | ||
| 288 | -// String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 289 | -// String expectedResource = "contact2"; | ||
| 290 | -// String expectedOperation = "delete"; | ||
| 291 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 292 | -// | ||
| 293 | -// interceptor.manage(this.ic); | ||
| 294 | -// } catch (SecurityException cause) { | ||
| 295 | -// fail(); | ||
| 296 | -// } | ||
| 297 | -// } | ||
| 298 | -// | ||
| 299 | -// @Test | ||
| 300 | -// public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 301 | -// try { | ||
| 302 | -// Object target = new NamedClassWithNamedMethods(); | ||
| 303 | -// String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 304 | -// String expectedResource = "contact2"; | ||
| 305 | -// String expectedOperation = "insert"; | ||
| 306 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 307 | -// | ||
| 308 | -// interceptor.manage(this.ic); | ||
| 309 | -// } catch (SecurityException cause) { | ||
| 310 | -// fail(); | ||
| 311 | -// } | ||
| 312 | -// } | ||
| 313 | -// | ||
| 314 | -// @Test | ||
| 315 | -// public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 316 | -// try { | ||
| 317 | -// Object target = new NamedClassWithNamedMethods(); | ||
| 318 | -// String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 319 | -// String expectedResource = "contact"; | ||
| 320 | -// String expectedOperation = "delete"; | ||
| 321 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 322 | -// | ||
| 323 | -// interceptor.manage(this.ic); | ||
| 324 | -// } catch (SecurityException cause) { | ||
| 325 | -// fail(); | ||
| 326 | -// } | ||
| 327 | -// } | ||
| 328 | -// | ||
| 329 | -// @Test | ||
| 330 | -// public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredResourceAndOperation() | ||
| 331 | -// throws Exception { | ||
| 332 | -// try { | ||
| 333 | -// Object target = new NamedClassWithNamedMethods(); | ||
| 334 | -// String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 335 | -// String expectedResource = "contact"; | ||
| 336 | -// String expectedOperation = "insert"; | ||
| 337 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 338 | -// | ||
| 339 | -// interceptor.manage(this.ic); | ||
| 340 | -// } catch (SecurityException cause) { | ||
| 341 | -// fail(); | ||
| 342 | -// } | ||
| 343 | -// } | ||
| 344 | -// | ||
| 345 | -// /* Testing ClassAnnotedWithRequiredPermission */ | ||
| 346 | -// | ||
| 347 | -// @Test | ||
| 348 | -// public void testManageClassAnnotedWithRequiredPermissionAtWithoutRequiredPermissionAnnotation() throws Exception { | ||
| 349 | -// try { | ||
| 350 | -// Object target = new ClassAnnotedWithRequiredPermission(); | ||
| 351 | -// String methodName = "withoutRequiredPermissionAnnotation"; | ||
| 352 | -// String expectedResource = target.getClass().getSimpleName(); | ||
| 353 | -// String expectedOperation = methodName; | ||
| 354 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 355 | -// | ||
| 356 | -// interceptor.manage(this.ic); | ||
| 357 | -// } catch (SecurityException cause) { | ||
| 358 | -// fail(); | ||
| 359 | -// } | ||
| 360 | -// } | ||
| 361 | -// | ||
| 362 | -// @Test | ||
| 363 | -// public void testManageClassAnnotedWithRequiredPermissionAtRequiredPermissionWithDeclaredOperation() | ||
| 364 | -// throws Exception { | ||
| 365 | -// try { | ||
| 366 | -// Object target = new ClassAnnotedWithRequiredPermission(); | ||
| 367 | -// String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 368 | -// String expectedResource = target.getClass().getSimpleName(); | ||
| 369 | -// String expectedOperation = "insert"; | ||
| 370 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 371 | -// | ||
| 372 | -// interceptor.manage(this.ic); | ||
| 373 | -// } catch (SecurityException cause) { | ||
| 374 | -// fail(); | ||
| 375 | -// } | ||
| 376 | -// } | ||
| 377 | -// | ||
| 378 | -// /* Other tests */ | ||
| 379 | -// | ||
| 380 | -// @Test | ||
| 381 | -// public void testManagePermissionNotAllowed() throws Exception { | ||
| 382 | -// try { | ||
| 383 | -// Object target = new UnnamedClass(); | ||
| 384 | -// String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 385 | -// String expectedResource = "contact"; | ||
| 386 | -// String expectedOperation = "insert"; | ||
| 387 | -// prepareMock(target, methodName, expectedResource, expectedOperation, false, true); | ||
| 388 | -// | ||
| 389 | -// interceptor.manage(this.ic); | ||
| 390 | -// fail(); | ||
| 391 | -// } catch (SecurityException cause) { | ||
| 392 | -// } | ||
| 393 | -// } | ||
| 394 | -// | ||
| 395 | -// @Test | ||
| 396 | -// public void testUserNotLoggedIn() throws Exception { | ||
| 397 | -// try { | ||
| 398 | -// Object target = new UnnamedClass(); | ||
| 399 | -// String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 400 | -// String expectedResource = "contact"; | ||
| 401 | -// String expectedOperation = "insert"; | ||
| 402 | -// prepareMock(target, methodName, expectedResource, expectedOperation, true, false); | ||
| 403 | -// | ||
| 404 | -// interceptor.manage(this.ic); | ||
| 405 | -// fail(); | ||
| 406 | -// } catch (SecurityException cause) { | ||
| 407 | -// } | ||
| 408 | -// } | ||
| 409 | -// | ||
| 410 | -//} | 1 | +package br.gov.frameworkdemoiselle.internal.interceptor; |
| 2 | + | ||
| 3 | +import static org.easymock.EasyMock.createMock; | ||
| 4 | +import static org.easymock.EasyMock.expect; | ||
| 5 | +import static org.easymock.EasyMock.expectLastCall; | ||
| 6 | +import static org.junit.Assert.fail; | ||
| 7 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 8 | +import static org.powermock.api.easymock.PowerMock.replay; | ||
| 9 | + | ||
| 10 | +import java.util.Locale; | ||
| 11 | + | ||
| 12 | +import javax.enterprise.inject.Instance; | ||
| 13 | +import javax.interceptor.InvocationContext; | ||
| 14 | + | ||
| 15 | +import org.junit.Before; | ||
| 16 | +import org.junit.Test; | ||
| 17 | +import org.junit.runner.RunWith; | ||
| 18 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 19 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
| 20 | + | ||
| 21 | +import br.gov.frameworkdemoiselle.annotation.Name; | ||
| 22 | +import br.gov.frameworkdemoiselle.security.NotLoggedInException; | ||
| 23 | +import br.gov.frameworkdemoiselle.security.RequiredPermission; | ||
| 24 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | ||
| 25 | +import br.gov.frameworkdemoiselle.security.SecurityException; | ||
| 26 | +import br.gov.frameworkdemoiselle.security.User; | ||
| 27 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 28 | + | ||
| 29 | +@RunWith(PowerMockRunner.class) | ||
| 30 | +@PrepareForTest(Beans.class) | ||
| 31 | +public class RequiredPermissionInterceptorTest { | ||
| 32 | + | ||
| 33 | + private RequiredPermissionInterceptor interceptor; | ||
| 34 | + | ||
| 35 | + private InvocationContext ic; | ||
| 36 | + | ||
| 37 | + private SecurityContext securityContext; | ||
| 38 | + | ||
| 39 | + class UnnamedClass { | ||
| 40 | + | ||
| 41 | + @RequiredPermission | ||
| 42 | + public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @RequiredPermission(operation = "insert") | ||
| 46 | + public void requiredPermissionWithDeclaredOperation() { | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + @RequiredPermission(resource = "contact") | ||
| 50 | + public void requiredPermissionWithDeclaredResource() { | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @RequiredPermission(resource = "contact", operation = "insert") | ||
| 54 | + public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Name("contact2") | ||
| 59 | + class NamedClass { | ||
| 60 | + | ||
| 61 | + @RequiredPermission | ||
| 62 | + public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @RequiredPermission(operation = "insert") | ||
| 66 | + public void requiredPermissionWithDeclaredOperation() { | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @RequiredPermission(resource = "contact") | ||
| 70 | + public void requiredPermissionWithDeclaredResource() { | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @RequiredPermission(resource = "contact", operation = "insert") | ||
| 74 | + public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Name("contact2") | ||
| 79 | + class NamedClassWithNamedMethods { | ||
| 80 | + | ||
| 81 | + @Name("delete") | ||
| 82 | + @RequiredPermission | ||
| 83 | + public void requiredPermissionWithoutDeclaredResourceAndOperation() { | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Name("delete") | ||
| 87 | + @RequiredPermission(operation = "insert") | ||
| 88 | + public void requiredPermissionWithDeclaredOperation() { | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + @Name("delete") | ||
| 92 | + @RequiredPermission(resource = "contact") | ||
| 93 | + public void requiredPermissionWithDeclaredResource() { | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + @Name("delete") | ||
| 97 | + @RequiredPermission(resource = "contact", operation = "insert") | ||
| 98 | + public void requiredPermissionWithDeclaredResourceAndOperation() { | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @RequiredPermission | ||
| 103 | + class ClassAnnotedWithRequiredPermission { | ||
| 104 | + | ||
| 105 | + public void withoutRequiredPermissionAnnotation() { | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + @RequiredPermission(operation = "insert") | ||
| 109 | + public void requiredPermissionWithDeclaredOperation() { | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + @Before | ||
| 114 | + public void setUp() throws Exception { | ||
| 115 | + @SuppressWarnings("unchecked") | ||
| 116 | + Instance<SecurityContext> securityContextInstance = createMock(Instance.class); | ||
| 117 | + | ||
| 118 | + User user = createMock(User.class); | ||
| 119 | + | ||
| 120 | + this.securityContext = createMock(SecurityContext.class); | ||
| 121 | + this.ic = createMock(InvocationContext.class); | ||
| 122 | + | ||
| 123 | + mockStatic(Beans.class); | ||
| 124 | + expect(Beans.getReference(Locale.class)).andReturn(Locale.getDefault()); | ||
| 125 | + expect(Beans.getReference(SecurityContext.class)).andReturn(this.securityContext); | ||
| 126 | + | ||
| 127 | + expect(user.getId()).andReturn("UserName").anyTimes(); | ||
| 128 | + expect(this.securityContext.getUser()).andReturn(user).anyTimes(); | ||
| 129 | + expect(securityContextInstance.get()).andReturn(securityContext).anyTimes(); | ||
| 130 | + expect(this.ic.proceed()).andReturn(null); | ||
| 131 | + replay(securityContextInstance, user, Beans.class); | ||
| 132 | + | ||
| 133 | + this.interceptor = new RequiredPermissionInterceptor(); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + private void prepareMock(Object target, String methodName, String expectedResource, String expectedOperation, | ||
| 137 | + boolean hasPermission, boolean isLoggedUser) throws Exception { | ||
| 138 | + | ||
| 139 | + expect(this.securityContext.isLoggedIn()).andReturn(isLoggedUser).anyTimes(); | ||
| 140 | + | ||
| 141 | + this.securityContext.hasPermission(expectedResource, expectedOperation); | ||
| 142 | + | ||
| 143 | + if (isLoggedUser) { | ||
| 144 | + expectLastCall().andReturn(hasPermission); | ||
| 145 | + } else { | ||
| 146 | + expectLastCall().andThrow(new NotLoggedInException("")); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + expect(this.ic.getTarget()).andReturn(target).anyTimes(); | ||
| 150 | + expect(this.ic.getMethod()).andReturn(target.getClass().getMethod(methodName)).anyTimes(); | ||
| 151 | + replay(this.ic, this.securityContext); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + /* Testing UnnamedClass */ | ||
| 155 | + | ||
| 156 | + @Test | ||
| 157 | + public void testManageUnnamedClassAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() throws Exception { | ||
| 158 | + try { | ||
| 159 | + Object target = new UnnamedClass(); | ||
| 160 | + String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 161 | + String expectedResource = target.getClass().getSimpleName(); | ||
| 162 | + String expectedOperation = methodName; | ||
| 163 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 164 | + | ||
| 165 | + interceptor.manage(this.ic); | ||
| 166 | + } catch (SecurityException cause) { | ||
| 167 | + fail(); | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + @Test | ||
| 172 | + public void testManageUnnamedClassAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 173 | + try { | ||
| 174 | + Object target = new UnnamedClass(); | ||
| 175 | + String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 176 | + String expectedResource = target.getClass().getSimpleName(); | ||
| 177 | + String expectedOperation = "insert"; | ||
| 178 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 179 | + | ||
| 180 | + interceptor.manage(this.ic); | ||
| 181 | + } catch (SecurityException cause) { | ||
| 182 | + fail(); | ||
| 183 | + } | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + @Test | ||
| 187 | + public void testManageUnnamedClassAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 188 | + try { | ||
| 189 | + Object target = new UnnamedClass(); | ||
| 190 | + String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 191 | + String expectedResource = "contact"; | ||
| 192 | + String expectedOperation = methodName; | ||
| 193 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 194 | + | ||
| 195 | + interceptor.manage(this.ic); | ||
| 196 | + } catch (SecurityException cause) { | ||
| 197 | + fail(); | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Test | ||
| 202 | + public void testManageUnnamedClassAtRequiredPermissionWithDeclaredResourceAndOperation() throws Exception { | ||
| 203 | + try { | ||
| 204 | + Object target = new UnnamedClass(); | ||
| 205 | + String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 206 | + String expectedResource = "contact"; | ||
| 207 | + String expectedOperation = "insert"; | ||
| 208 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 209 | + | ||
| 210 | + interceptor.manage(this.ic); | ||
| 211 | + } catch (SecurityException cause) { | ||
| 212 | + fail(); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + /* Testing NamedClass */ | ||
| 217 | + | ||
| 218 | + @Test | ||
| 219 | + public void testManageNamedClassAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() throws Exception { | ||
| 220 | + try { | ||
| 221 | + Object target = new NamedClass(); | ||
| 222 | + String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 223 | + String expectedResource = "contact2"; | ||
| 224 | + String expectedOperation = methodName; | ||
| 225 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 226 | + | ||
| 227 | + interceptor.manage(this.ic); | ||
| 228 | + } catch (SecurityException cause) { | ||
| 229 | + fail(); | ||
| 230 | + } | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + @Test | ||
| 234 | + public void testManageNamedClassAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 235 | + try { | ||
| 236 | + Object target = new NamedClass(); | ||
| 237 | + String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 238 | + String expectedResource = "contact2"; | ||
| 239 | + String expectedOperation = "insert"; | ||
| 240 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 241 | + | ||
| 242 | + interceptor.manage(this.ic); | ||
| 243 | + } catch (SecurityException cause) { | ||
| 244 | + fail(); | ||
| 245 | + } | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + @Test | ||
| 249 | + public void testManageNamedClassAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 250 | + try { | ||
| 251 | + Object target = new NamedClass(); | ||
| 252 | + String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 253 | + String expectedResource = "contact"; | ||
| 254 | + String expectedOperation = methodName; | ||
| 255 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 256 | + | ||
| 257 | + interceptor.manage(this.ic); | ||
| 258 | + } catch (SecurityException cause) { | ||
| 259 | + fail(); | ||
| 260 | + } | ||
| 261 | + } | ||
| 262 | + | ||
| 263 | + @Test | ||
| 264 | + public void testManageNamedClassAtRequiredPermissionWithDeclaredResourceAndOperation() throws Exception { | ||
| 265 | + try { | ||
| 266 | + Object target = new NamedClass(); | ||
| 267 | + String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 268 | + String expectedResource = "contact"; | ||
| 269 | + String expectedOperation = "insert"; | ||
| 270 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 271 | + | ||
| 272 | + interceptor.manage(this.ic); | ||
| 273 | + } catch (SecurityException cause) { | ||
| 274 | + fail(); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + /* Testing NamedClassWithNamedMethods */ | ||
| 279 | + | ||
| 280 | + @Test | ||
| 281 | + public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithoutDeclaredResourceAndOperationMethod() | ||
| 282 | + throws Exception { | ||
| 283 | + try { | ||
| 284 | + Object target = new NamedClassWithNamedMethods(); | ||
| 285 | + String methodName = "requiredPermissionWithoutDeclaredResourceAndOperation"; | ||
| 286 | + String expectedResource = "contact2"; | ||
| 287 | + String expectedOperation = "delete"; | ||
| 288 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 289 | + | ||
| 290 | + interceptor.manage(this.ic); | ||
| 291 | + } catch (SecurityException cause) { | ||
| 292 | + fail(); | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + @Test | ||
| 297 | + public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredOperationMethod() throws Exception { | ||
| 298 | + try { | ||
| 299 | + Object target = new NamedClassWithNamedMethods(); | ||
| 300 | + String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 301 | + String expectedResource = "contact2"; | ||
| 302 | + String expectedOperation = "insert"; | ||
| 303 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 304 | + | ||
| 305 | + interceptor.manage(this.ic); | ||
| 306 | + } catch (SecurityException cause) { | ||
| 307 | + fail(); | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + @Test | ||
| 312 | + public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredResourceMethod() throws Exception { | ||
| 313 | + try { | ||
| 314 | + Object target = new NamedClassWithNamedMethods(); | ||
| 315 | + String methodName = "requiredPermissionWithDeclaredResource"; | ||
| 316 | + String expectedResource = "contact"; | ||
| 317 | + String expectedOperation = "delete"; | ||
| 318 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 319 | + | ||
| 320 | + interceptor.manage(this.ic); | ||
| 321 | + } catch (SecurityException cause) { | ||
| 322 | + fail(); | ||
| 323 | + } | ||
| 324 | + } | ||
| 325 | + | ||
| 326 | + @Test | ||
| 327 | + public void testManageNamedClassWithNamedMethodsAtRequiredPermissionWithDeclaredResourceAndOperation() | ||
| 328 | + throws Exception { | ||
| 329 | + try { | ||
| 330 | + Object target = new NamedClassWithNamedMethods(); | ||
| 331 | + String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 332 | + String expectedResource = "contact"; | ||
| 333 | + String expectedOperation = "insert"; | ||
| 334 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 335 | + | ||
| 336 | + interceptor.manage(this.ic); | ||
| 337 | + } catch (SecurityException cause) { | ||
| 338 | + fail(); | ||
| 339 | + } | ||
| 340 | + } | ||
| 341 | + | ||
| 342 | + /* Testing ClassAnnotedWithRequiredPermission */ | ||
| 343 | + | ||
| 344 | + @Test | ||
| 345 | + public void testManageClassAnnotedWithRequiredPermissionAtWithoutRequiredPermissionAnnotation() throws Exception { | ||
| 346 | + try { | ||
| 347 | + Object target = new ClassAnnotedWithRequiredPermission(); | ||
| 348 | + String methodName = "withoutRequiredPermissionAnnotation"; | ||
| 349 | + String expectedResource = target.getClass().getSimpleName(); | ||
| 350 | + String expectedOperation = methodName; | ||
| 351 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 352 | + | ||
| 353 | + interceptor.manage(this.ic); | ||
| 354 | + } catch (SecurityException cause) { | ||
| 355 | + fail(); | ||
| 356 | + } | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + @Test | ||
| 360 | + public void testManageClassAnnotedWithRequiredPermissionAtRequiredPermissionWithDeclaredOperation() | ||
| 361 | + throws Exception { | ||
| 362 | + try { | ||
| 363 | + Object target = new ClassAnnotedWithRequiredPermission(); | ||
| 364 | + String methodName = "requiredPermissionWithDeclaredOperation"; | ||
| 365 | + String expectedResource = target.getClass().getSimpleName(); | ||
| 366 | + String expectedOperation = "insert"; | ||
| 367 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, true); | ||
| 368 | + | ||
| 369 | + interceptor.manage(this.ic); | ||
| 370 | + } catch (SecurityException cause) { | ||
| 371 | + fail(); | ||
| 372 | + } | ||
| 373 | + } | ||
| 374 | + | ||
| 375 | + /* Other tests */ | ||
| 376 | + | ||
| 377 | + @Test | ||
| 378 | + public void testManagePermissionNotAllowed() throws Exception { | ||
| 379 | + try { | ||
| 380 | + Object target = new UnnamedClass(); | ||
| 381 | + String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 382 | + String expectedResource = "contact"; | ||
| 383 | + String expectedOperation = "insert"; | ||
| 384 | + prepareMock(target, methodName, expectedResource, expectedOperation, false, true); | ||
| 385 | + | ||
| 386 | + interceptor.manage(this.ic); | ||
| 387 | + fail(); | ||
| 388 | + } catch (SecurityException cause) { | ||
| 389 | + } | ||
| 390 | + } | ||
| 391 | + | ||
| 392 | + @Test | ||
| 393 | + public void testUserNotLoggedIn() throws Exception { | ||
| 394 | + try { | ||
| 395 | + Object target = new UnnamedClass(); | ||
| 396 | + String methodName = "requiredPermissionWithDeclaredResourceAndOperation"; | ||
| 397 | + String expectedResource = "contact"; | ||
| 398 | + String expectedOperation = "insert"; | ||
| 399 | + prepareMock(target, methodName, expectedResource, expectedOperation, true, false); | ||
| 400 | + | ||
| 401 | + interceptor.manage(this.ic); | ||
| 402 | + fail(); | ||
| 403 | + } catch (SecurityException cause) { | ||
| 404 | + } | ||
| 405 | + } | ||
| 406 | + | ||
| 407 | +} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptorTest.java
| 1 | -//package br.gov.frameworkdemoiselle.internal.interceptor; | ||
| 2 | -// | ||
| 3 | -//import static org.easymock.EasyMock.expect; | ||
| 4 | -//import static org.junit.Assert.fail; | ||
| 5 | -//import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 6 | -//import static org.powermock.api.easymock.PowerMock.replay; | ||
| 7 | -// | ||
| 8 | -//import java.util.Locale; | ||
| 9 | -// | ||
| 10 | -//import javax.enterprise.inject.Instance; | ||
| 11 | -//import javax.interceptor.InvocationContext; | ||
| 12 | -// | ||
| 13 | -//import org.easymock.EasyMock; | ||
| 14 | -//import org.junit.Before; | ||
| 15 | -//import org.junit.Test; | ||
| 16 | -//import org.junit.runner.RunWith; | ||
| 17 | -//import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 18 | -//import org.powermock.modules.junit4.PowerMockRunner; | ||
| 19 | -//import org.slf4j.Logger; | ||
| 20 | -// | ||
| 21 | -//import br.gov.frameworkdemoiselle.internal.implementation.CoreBundle; | ||
| 22 | -//import br.gov.frameworkdemoiselle.security.AuthorizationException; | ||
| 23 | -//import br.gov.frameworkdemoiselle.security.NotLoggedInException; | ||
| 24 | -//import br.gov.frameworkdemoiselle.security.RequiredRole; | ||
| 25 | -//import br.gov.frameworkdemoiselle.security.SecurityContext; | ||
| 26 | -//import br.gov.frameworkdemoiselle.security.User; | ||
| 27 | -//import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 28 | -// | ||
| 29 | -//@RunWith(PowerMockRunner.class) | ||
| 30 | -//@PrepareForTest(CoreBundle.class) | ||
| 31 | -//public class RequiredRoleInterceptorTest { | ||
| 32 | -// | ||
| 33 | -// private RequiredRoleInterceptor interceptor; | ||
| 34 | -// | ||
| 35 | -// private InvocationContext ic; | ||
| 36 | -// | ||
| 37 | -// private SecurityContext securityContext; | ||
| 38 | -// | ||
| 39 | -// class ClassNotAnnoted { | ||
| 40 | -// | ||
| 41 | -// @RequiredRole("simpleRoleName") | ||
| 42 | -// public void requiredRoleWithSingleRole() { | ||
| 43 | -// } | ||
| 44 | -// | ||
| 45 | -// @RequiredRole({ "firstRole", "secondRole", "thirdRole", "fourthRole", "fifthRole" }) | ||
| 46 | -// public void requiredRoleWithArrayOfRoles() { | ||
| 47 | -// } | ||
| 48 | -// | ||
| 49 | -// @RequiredRole({ "firstRole, secondRole" }) | ||
| 50 | -// public void requiredRoleWithArrayOfSingleRoleComma() { | ||
| 51 | -// } | ||
| 52 | -// | ||
| 53 | -// @RequiredRole("firstRole, secondRole") | ||
| 54 | -// public void requiredRoleWithSingleRoleComma() { | ||
| 55 | -// } | ||
| 56 | -// | ||
| 57 | -// @RequiredRole("") | ||
| 58 | -// public void requiredRoleWithEmptyValue() { | ||
| 59 | -// } | ||
| 60 | -// | ||
| 61 | -// @RequiredRole({}) | ||
| 62 | -// public void requiredRoleWithEmptyArray() { | ||
| 63 | -// } | ||
| 64 | -// | ||
| 65 | -// @RequiredRole({ "" }) | ||
| 66 | -// public void requiredRoleWithArrayOfEmptyString() { | ||
| 67 | -// } | ||
| 68 | -// | ||
| 69 | -// public void methodNotAnnoted() { | ||
| 70 | -// } | ||
| 71 | -// } | ||
| 72 | -// | ||
| 73 | -// @RequiredRole("classRole") | ||
| 74 | -// class ClassAnnotedWithRequiredRole { | ||
| 75 | -// | ||
| 76 | -// public void withoutRole() { | ||
| 77 | -// } | ||
| 78 | -// | ||
| 79 | -// @RequiredRole("simpleRoleName") | ||
| 80 | -// public void requiredRoleWithSingleRole() { | ||
| 81 | -// } | ||
| 82 | -// } | ||
| 83 | -// | ||
| 84 | -// @Before | ||
| 85 | -// public void setUp() throws Exception { | ||
| 86 | -// | ||
| 87 | -// @SuppressWarnings("unchecked") | ||
| 88 | -// Instance<SecurityContext> securityContextInstance = EasyMock.createMock(Instance.class); | ||
| 89 | -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
| 90 | -// Logger logger = EasyMock.createMock(Logger.class); | ||
| 91 | -// User user = EasyMock.createMock(User.class); | ||
| 92 | -// | ||
| 93 | -// this.securityContext = EasyMock.createMock(SecurityContext.class); | ||
| 94 | -// this.ic = EasyMock.createMock(InvocationContext.class); | ||
| 95 | -// | ||
| 96 | -// mockStatic(CoreBundle.class); | ||
| 97 | -// expect(CoreBundle.get()).andReturn(bundle); | ||
| 98 | -// | ||
| 99 | -// expect(user.getId()).andReturn("UserName").anyTimes(); | ||
| 100 | -// expect(this.securityContext.getUser()).andReturn(user).anyTimes(); | ||
| 101 | -// expect(securityContextInstance.get()).andReturn(securityContext).anyTimes(); | ||
| 102 | -// expect(this.ic.proceed()).andReturn(null); | ||
| 103 | -// replay(securityContextInstance, user, CoreBundle.class); | ||
| 104 | -// | ||
| 105 | -// this.interceptor = new RequiredRoleInterceptor(securityContextInstance, bundle, logger); | ||
| 106 | -// } | ||
| 107 | -// | ||
| 108 | -// private void prepareMock(Object target, String methodName, String[] expectedRoles, boolean hasHole, | ||
| 109 | -// boolean isLoggedUser) throws Exception { | ||
| 110 | -// | ||
| 111 | -// expect(this.securityContext.isLoggedIn()).andReturn(isLoggedUser).anyTimes(); | ||
| 112 | -// | ||
| 113 | -// for (String role : expectedRoles) { | ||
| 114 | -// this.securityContext.hasRole(role); | ||
| 115 | -// if (isLoggedUser) { | ||
| 116 | -// EasyMock.expectLastCall().andReturn(hasHole); | ||
| 117 | -// } else { | ||
| 118 | -// EasyMock.expectLastCall().andThrow(new NotLoggedInException("")); | ||
| 119 | -// } | ||
| 120 | -// } | ||
| 121 | -// | ||
| 122 | -// expect(this.ic.getTarget()).andReturn(target).anyTimes(); | ||
| 123 | -// expect(this.ic.getMethod()).andReturn(target.getClass().getMethod(methodName)).anyTimes(); | ||
| 124 | -// replay(this.ic, this.securityContext); | ||
| 125 | -// } | ||
| 126 | -// | ||
| 127 | -// /* | ||
| 128 | -// * Testing ClassNotAnnoted | ||
| 129 | -// */ | ||
| 130 | -// @Test | ||
| 131 | -// public void testManageClassNotAnnotedAtRequiredRoleWithSingleRole() throws Exception { | ||
| 132 | -// Object target = new ClassNotAnnoted(); | ||
| 133 | -// String methodName = "requiredRoleWithSingleRole"; | ||
| 134 | -// String[] expectedRoles = { "simpleRoleName" }; | ||
| 135 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 136 | -// | ||
| 137 | -// interceptor.manage(this.ic); | ||
| 138 | -// } | ||
| 139 | -// | ||
| 140 | -// @Test | ||
| 141 | -// public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfRoles() throws Exception { | ||
| 142 | -// Object target = new ClassNotAnnoted(); | ||
| 143 | -// String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 144 | -// String[] expectedRoles = { "firstRole", "secondRole", "thirdRole", "fourthRole", "fifthRole" }; | ||
| 145 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 146 | -// | ||
| 147 | -// interceptor.manage(this.ic); | ||
| 148 | -// } | ||
| 149 | -// | ||
| 150 | -// @Test | ||
| 151 | -// public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfSingleRoleComma() throws Exception { | ||
| 152 | -// Object target = new ClassNotAnnoted(); | ||
| 153 | -// String methodName = "requiredRoleWithArrayOfSingleRoleComma"; | ||
| 154 | -// String[] expectedRoles = { "firstRole, secondRole" }; | ||
| 155 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 156 | -// | ||
| 157 | -// interceptor.manage(this.ic); | ||
| 158 | -// } | ||
| 159 | -// | ||
| 160 | -// @Test | ||
| 161 | -// public void testManageClassNotAnnotedAtRequiredRoleWithSingleRoleComma() throws Exception { | ||
| 162 | -// Object target = new ClassNotAnnoted(); | ||
| 163 | -// String methodName = "requiredRoleWithSingleRoleComma"; | ||
| 164 | -// String[] expectedRoles = { "firstRole, secondRole" }; | ||
| 165 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 166 | -// | ||
| 167 | -// interceptor.manage(this.ic); | ||
| 168 | -// } | ||
| 169 | -// | ||
| 170 | -// @Test | ||
| 171 | -// public void testManageClassNotAnnotedAtRequiredRoleWithEmptyValue() throws Exception { | ||
| 172 | -// try { | ||
| 173 | -// Object target = new ClassNotAnnoted(); | ||
| 174 | -// String methodName = "requiredRoleWithEmptyValue"; | ||
| 175 | -// String[] expectedRoles = { "" }; | ||
| 176 | -// prepareMock(target, methodName, expectedRoles, false, true); | ||
| 177 | -// | ||
| 178 | -// interceptor.manage(this.ic); | ||
| 179 | -// fail(); | ||
| 180 | -// } catch (AuthorizationException cause) { | ||
| 181 | -// } | ||
| 182 | -// } | ||
| 183 | -// | ||
| 184 | -// @Test | ||
| 185 | -// public void testManageClassNotAnnotedAtRequiredRoleWithEmptyArray() throws Exception { | ||
| 186 | -// try { | ||
| 187 | -// Object target = new ClassNotAnnoted(); | ||
| 188 | -// String methodName = "requiredRoleWithEmptyArray"; | ||
| 189 | -// String[] expectedRoles = { "" }; | ||
| 190 | -// prepareMock(target, methodName, expectedRoles, false, true); | ||
| 191 | -// | ||
| 192 | -// interceptor.manage(this.ic); | ||
| 193 | -// fail(); | ||
| 194 | -// } catch (AuthorizationException cause) { | ||
| 195 | -// } | ||
| 196 | -// } | ||
| 197 | -// | ||
| 198 | -// @Test | ||
| 199 | -// public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfEmptyString() throws Exception { | ||
| 200 | -// try { | ||
| 201 | -// Object target = new ClassNotAnnoted(); | ||
| 202 | -// String methodName = "requiredRoleWithArrayOfEmptyString"; | ||
| 203 | -// String[] expectedRoles = { "" }; | ||
| 204 | -// prepareMock(target, methodName, expectedRoles, false, true); | ||
| 205 | -// | ||
| 206 | -// interceptor.manage(this.ic); | ||
| 207 | -// fail(); | ||
| 208 | -// } catch (AuthorizationException cause) { | ||
| 209 | -// } | ||
| 210 | -// } | ||
| 211 | -// | ||
| 212 | -// @Test | ||
| 213 | -// public void testManageClassNotAnnotedAtMethodNotAnnoted() throws Exception { | ||
| 214 | -// try { | ||
| 215 | -// Object target = new ClassNotAnnoted(); | ||
| 216 | -// String methodName = "methodNotAnnoted"; | ||
| 217 | -// String[] expectedRoles = { "" }; | ||
| 218 | -// prepareMock(target, methodName, expectedRoles, false, true); | ||
| 219 | -// | ||
| 220 | -// interceptor.manage(this.ic); | ||
| 221 | -// fail(); | ||
| 222 | -// } catch (AuthorizationException cause) { | ||
| 223 | -// } | ||
| 224 | -// } | ||
| 225 | -// | ||
| 226 | -// /* | ||
| 227 | -// * Testing ClassAnnoted | ||
| 228 | -// */ | ||
| 229 | -// @Test | ||
| 230 | -// public void testManageClassAnnotedWithRequiredRoleAtWithoutRole() throws Exception { | ||
| 231 | -// Object target = new ClassAnnotedWithRequiredRole(); | ||
| 232 | -// String methodName = "withoutRole"; | ||
| 233 | -// String[] expectedRoles = { "classRole" }; | ||
| 234 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 235 | -// | ||
| 236 | -// interceptor.manage(this.ic); | ||
| 237 | -// } | ||
| 238 | -// | ||
| 239 | -// @Test | ||
| 240 | -// public void testManageClassAnnotedWithRequiredRoleAtRequiredRoleWithSingleRole() throws Exception { | ||
| 241 | -// Object target = new ClassAnnotedWithRequiredRole(); | ||
| 242 | -// String methodName = "requiredRoleWithSingleRole"; | ||
| 243 | -// String[] expectedRoles = { "simpleRoleName" }; | ||
| 244 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 245 | -// | ||
| 246 | -// interceptor.manage(this.ic); | ||
| 247 | -// } | ||
| 248 | -// | ||
| 249 | -// /* | ||
| 250 | -// * Other tests | ||
| 251 | -// */ | ||
| 252 | -// | ||
| 253 | -// @Test | ||
| 254 | -// public void testDoesNotHaveSingleRole() throws Exception { | ||
| 255 | -// try { | ||
| 256 | -// Object target = new ClassNotAnnoted(); | ||
| 257 | -// String methodName = "requiredRoleWithSingleRole"; | ||
| 258 | -// String[] expectedRoles = { "simpleRoleName" }; | ||
| 259 | -// prepareMock(target, methodName, expectedRoles, false, true); | ||
| 260 | -// | ||
| 261 | -// interceptor.manage(this.ic); | ||
| 262 | -// fail(); | ||
| 263 | -// } catch (AuthorizationException cause) { | ||
| 264 | -// } | ||
| 265 | -// } | ||
| 266 | -// | ||
| 267 | -// @Test | ||
| 268 | -// public void testUserNotLoggedIn() throws Exception { | ||
| 269 | -// try { | ||
| 270 | -// Object target = new ClassNotAnnoted(); | ||
| 271 | -// String methodName = "requiredRoleWithSingleRole"; | ||
| 272 | -// String[] expectedRoles = { "simpleRoleName" }; | ||
| 273 | -// prepareMock(target, methodName, expectedRoles, true, false); | ||
| 274 | -// | ||
| 275 | -// interceptor.manage(this.ic); | ||
| 276 | -// fail(); | ||
| 277 | -// } catch (NotLoggedInException cause) { | ||
| 278 | -// } | ||
| 279 | -// } | ||
| 280 | -// | ||
| 281 | -// @Test | ||
| 282 | -// public void testDoesNotHaveOneOrMoreRolesOfArray() throws Exception { | ||
| 283 | -// Object target = new ClassNotAnnoted(); | ||
| 284 | -// String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 285 | -// String[] expectedRoles = { "thirdRole", "fourthRole", "fifthRole" }; | ||
| 286 | -// | ||
| 287 | -// expect(this.securityContext.hasRole("firstRole")).andReturn(false); | ||
| 288 | -// expect(this.securityContext.hasRole("secondRole")).andReturn(false); | ||
| 289 | -// | ||
| 290 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 291 | -// | ||
| 292 | -// interceptor.manage(this.ic); | ||
| 293 | -// } | ||
| 294 | -// | ||
| 295 | -// @Test | ||
| 296 | -// public void testHasMoreRolesThenArray() throws Exception { | ||
| 297 | -// Object target = new ClassNotAnnoted(); | ||
| 298 | -// String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 299 | -// String[] expectedRoles = { "thirdRole", "fourthRole", "fifthRole" }; | ||
| 300 | -// | ||
| 301 | -// expect(this.securityContext.hasRole("firstRole")).andReturn(false); | ||
| 302 | -// expect(this.securityContext.hasRole("secondRole")).andReturn(false); | ||
| 303 | -// expect(this.securityContext.hasRole("sixthRole")).andReturn(true); | ||
| 304 | -// | ||
| 305 | -// prepareMock(target, methodName, expectedRoles, true, true); | ||
| 306 | -// | ||
| 307 | -// interceptor.manage(this.ic); | ||
| 308 | -// } | ||
| 309 | -// | ||
| 310 | -//} | 1 | +package br.gov.frameworkdemoiselle.internal.interceptor; |
| 2 | + | ||
| 3 | +import static org.easymock.EasyMock.expect; | ||
| 4 | +import static org.junit.Assert.fail; | ||
| 5 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
| 6 | +import static org.powermock.api.easymock.PowerMock.replay; | ||
| 7 | + | ||
| 8 | +import java.util.Locale; | ||
| 9 | + | ||
| 10 | +import javax.enterprise.inject.Instance; | ||
| 11 | +import javax.interceptor.InvocationContext; | ||
| 12 | + | ||
| 13 | +import org.easymock.EasyMock; | ||
| 14 | +import org.junit.Before; | ||
| 15 | +import org.junit.Test; | ||
| 16 | +import org.junit.runner.RunWith; | ||
| 17 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| 18 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
| 19 | + | ||
| 20 | +import br.gov.frameworkdemoiselle.security.AuthorizationException; | ||
| 21 | +import br.gov.frameworkdemoiselle.security.NotLoggedInException; | ||
| 22 | +import br.gov.frameworkdemoiselle.security.RequiredRole; | ||
| 23 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | ||
| 24 | +import br.gov.frameworkdemoiselle.security.User; | ||
| 25 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 26 | + | ||
| 27 | +@RunWith(PowerMockRunner.class) | ||
| 28 | +@PrepareForTest(Beans.class) | ||
| 29 | +public class RequiredRoleInterceptorTest { | ||
| 30 | + | ||
| 31 | + private RequiredRoleInterceptor interceptor; | ||
| 32 | + | ||
| 33 | + private InvocationContext ic; | ||
| 34 | + | ||
| 35 | + private SecurityContext securityContext; | ||
| 36 | + | ||
| 37 | + class ClassNotAnnoted { | ||
| 38 | + | ||
| 39 | + @RequiredRole("simpleRoleName") | ||
| 40 | + public void requiredRoleWithSingleRole() { | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @RequiredRole({ "firstRole", "secondRole", "thirdRole", "fourthRole", "fifthRole" }) | ||
| 44 | + public void requiredRoleWithArrayOfRoles() { | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @RequiredRole({ "firstRole, secondRole" }) | ||
| 48 | + public void requiredRoleWithArrayOfSingleRoleComma() { | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @RequiredRole("firstRole, secondRole") | ||
| 52 | + public void requiredRoleWithSingleRoleComma() { | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @RequiredRole("") | ||
| 56 | + public void requiredRoleWithEmptyValue() { | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @RequiredRole({}) | ||
| 60 | + public void requiredRoleWithEmptyArray() { | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @RequiredRole({ "" }) | ||
| 64 | + public void requiredRoleWithArrayOfEmptyString() { | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public void methodNotAnnoted() { | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @RequiredRole("classRole") | ||
| 72 | + class ClassAnnotedWithRequiredRole { | ||
| 73 | + | ||
| 74 | + public void withoutRole() { | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + @RequiredRole("simpleRoleName") | ||
| 78 | + public void requiredRoleWithSingleRole() { | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + @Before | ||
| 83 | + public void setUp() throws Exception { | ||
| 84 | + | ||
| 85 | + @SuppressWarnings("unchecked") | ||
| 86 | + Instance<SecurityContext> securityContextInstance = EasyMock.createMock(Instance.class); | ||
| 87 | + User user = EasyMock.createMock(User.class); | ||
| 88 | + | ||
| 89 | + this.securityContext = EasyMock.createMock(SecurityContext.class); | ||
| 90 | + this.ic = EasyMock.createMock(InvocationContext.class); | ||
| 91 | + | ||
| 92 | + mockStatic(Beans.class); | ||
| 93 | + expect(Beans.getReference(Locale.class)).andReturn(Locale.getDefault()); | ||
| 94 | + expect(Beans.getReference(SecurityContext.class)).andReturn(this.securityContext); | ||
| 95 | + | ||
| 96 | + expect(user.getId()).andReturn("UserName").anyTimes(); | ||
| 97 | + expect(this.securityContext.getUser()).andReturn(user).anyTimes(); | ||
| 98 | + expect(securityContextInstance.get()).andReturn(securityContext).anyTimes(); | ||
| 99 | + expect(this.ic.proceed()).andReturn(null); | ||
| 100 | + replay(securityContextInstance, user, Beans.class); | ||
| 101 | + | ||
| 102 | + this.interceptor = new RequiredRoleInterceptor(); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + private void prepareMock(Object target, String methodName, String[] expectedRoles, boolean hasHole, | ||
| 106 | + boolean isLoggedUser) throws Exception { | ||
| 107 | + | ||
| 108 | + expect(this.securityContext.isLoggedIn()).andReturn(isLoggedUser).anyTimes(); | ||
| 109 | + | ||
| 110 | + for (String role : expectedRoles) { | ||
| 111 | + this.securityContext.hasRole(role); | ||
| 112 | + if (isLoggedUser) { | ||
| 113 | + EasyMock.expectLastCall().andReturn(hasHole); | ||
| 114 | + } else { | ||
| 115 | + EasyMock.expectLastCall().andThrow(new NotLoggedInException("")); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + expect(this.ic.getTarget()).andReturn(target).anyTimes(); | ||
| 120 | + expect(this.ic.getMethod()).andReturn(target.getClass().getMethod(methodName)).anyTimes(); | ||
| 121 | + replay(this.ic, this.securityContext); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /* | ||
| 125 | + * Testing ClassNotAnnoted | ||
| 126 | + */ | ||
| 127 | + @Test | ||
| 128 | + public void testManageClassNotAnnotedAtRequiredRoleWithSingleRole() throws Exception { | ||
| 129 | + Object target = new ClassNotAnnoted(); | ||
| 130 | + String methodName = "requiredRoleWithSingleRole"; | ||
| 131 | + String[] expectedRoles = { "simpleRoleName" }; | ||
| 132 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 133 | + | ||
| 134 | + interceptor.manage(this.ic); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + @Test | ||
| 138 | + public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfRoles() throws Exception { | ||
| 139 | + Object target = new ClassNotAnnoted(); | ||
| 140 | + String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 141 | + String[] expectedRoles = { "firstRole", "secondRole", "thirdRole", "fourthRole", "fifthRole" }; | ||
| 142 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 143 | + | ||
| 144 | + interceptor.manage(this.ic); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + @Test | ||
| 148 | + public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfSingleRoleComma() throws Exception { | ||
| 149 | + Object target = new ClassNotAnnoted(); | ||
| 150 | + String methodName = "requiredRoleWithArrayOfSingleRoleComma"; | ||
| 151 | + String[] expectedRoles = { "firstRole, secondRole" }; | ||
| 152 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 153 | + | ||
| 154 | + interceptor.manage(this.ic); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + @Test | ||
| 158 | + public void testManageClassNotAnnotedAtRequiredRoleWithSingleRoleComma() throws Exception { | ||
| 159 | + Object target = new ClassNotAnnoted(); | ||
| 160 | + String methodName = "requiredRoleWithSingleRoleComma"; | ||
| 161 | + String[] expectedRoles = { "firstRole, secondRole" }; | ||
| 162 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 163 | + | ||
| 164 | + interceptor.manage(this.ic); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + @Test | ||
| 168 | + public void testManageClassNotAnnotedAtRequiredRoleWithEmptyValue() throws Exception { | ||
| 169 | + try { | ||
| 170 | + Object target = new ClassNotAnnoted(); | ||
| 171 | + String methodName = "requiredRoleWithEmptyValue"; | ||
| 172 | + String[] expectedRoles = { "" }; | ||
| 173 | + prepareMock(target, methodName, expectedRoles, false, true); | ||
| 174 | + | ||
| 175 | + interceptor.manage(this.ic); | ||
| 176 | + fail(); | ||
| 177 | + } catch (AuthorizationException cause) { | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + @Test | ||
| 182 | + public void testManageClassNotAnnotedAtRequiredRoleWithEmptyArray() throws Exception { | ||
| 183 | + try { | ||
| 184 | + Object target = new ClassNotAnnoted(); | ||
| 185 | + String methodName = "requiredRoleWithEmptyArray"; | ||
| 186 | + String[] expectedRoles = { "" }; | ||
| 187 | + prepareMock(target, methodName, expectedRoles, false, true); | ||
| 188 | + | ||
| 189 | + interceptor.manage(this.ic); | ||
| 190 | + fail(); | ||
| 191 | + } catch (AuthorizationException cause) { | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + @Test | ||
| 196 | + public void testManageClassNotAnnotedAtRequiredRoleWithArrayOfEmptyString() throws Exception { | ||
| 197 | + try { | ||
| 198 | + Object target = new ClassNotAnnoted(); | ||
| 199 | + String methodName = "requiredRoleWithArrayOfEmptyString"; | ||
| 200 | + String[] expectedRoles = { "" }; | ||
| 201 | + prepareMock(target, methodName, expectedRoles, false, true); | ||
| 202 | + | ||
| 203 | + interceptor.manage(this.ic); | ||
| 204 | + fail(); | ||
| 205 | + } catch (AuthorizationException cause) { | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + @Test | ||
| 210 | + public void testManageClassNotAnnotedAtMethodNotAnnoted() throws Exception { | ||
| 211 | + try { | ||
| 212 | + Object target = new ClassNotAnnoted(); | ||
| 213 | + String methodName = "methodNotAnnoted"; | ||
| 214 | + String[] expectedRoles = { "" }; | ||
| 215 | + prepareMock(target, methodName, expectedRoles, false, true); | ||
| 216 | + | ||
| 217 | + interceptor.manage(this.ic); | ||
| 218 | + fail(); | ||
| 219 | + } catch (AuthorizationException cause) { | ||
| 220 | + } | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + /* | ||
| 224 | + * Testing ClassAnnoted | ||
| 225 | + */ | ||
| 226 | + @Test | ||
| 227 | + public void testManageClassAnnotedWithRequiredRoleAtWithoutRole() throws Exception { | ||
| 228 | + Object target = new ClassAnnotedWithRequiredRole(); | ||
| 229 | + String methodName = "withoutRole"; | ||
| 230 | + String[] expectedRoles = { "classRole" }; | ||
| 231 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 232 | + | ||
| 233 | + interceptor.manage(this.ic); | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + @Test | ||
| 237 | + public void testManageClassAnnotedWithRequiredRoleAtRequiredRoleWithSingleRole() throws Exception { | ||
| 238 | + Object target = new ClassAnnotedWithRequiredRole(); | ||
| 239 | + String methodName = "requiredRoleWithSingleRole"; | ||
| 240 | + String[] expectedRoles = { "simpleRoleName" }; | ||
| 241 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 242 | + | ||
| 243 | + interceptor.manage(this.ic); | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + /* | ||
| 247 | + * Other tests | ||
| 248 | + */ | ||
| 249 | + | ||
| 250 | + @Test | ||
| 251 | + public void testDoesNotHaveSingleRole() throws Exception { | ||
| 252 | + try { | ||
| 253 | + Object target = new ClassNotAnnoted(); | ||
| 254 | + String methodName = "requiredRoleWithSingleRole"; | ||
| 255 | + String[] expectedRoles = { "simpleRoleName" }; | ||
| 256 | + prepareMock(target, methodName, expectedRoles, false, true); | ||
| 257 | + | ||
| 258 | + interceptor.manage(this.ic); | ||
| 259 | + fail(); | ||
| 260 | + } catch (AuthorizationException cause) { | ||
| 261 | + } | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + @Test | ||
| 265 | + public void testUserNotLoggedIn() throws Exception { | ||
| 266 | + try { | ||
| 267 | + Object target = new ClassNotAnnoted(); | ||
| 268 | + String methodName = "requiredRoleWithSingleRole"; | ||
| 269 | + String[] expectedRoles = { "simpleRoleName" }; | ||
| 270 | + prepareMock(target, methodName, expectedRoles, true, false); | ||
| 271 | + | ||
| 272 | + interceptor.manage(this.ic); | ||
| 273 | + fail(); | ||
| 274 | + } catch (NotLoggedInException cause) { | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + @Test | ||
| 279 | + public void testDoesNotHaveOneOrMoreRolesOfArray() throws Exception { | ||
| 280 | + Object target = new ClassNotAnnoted(); | ||
| 281 | + String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 282 | + String[] expectedRoles = { "thirdRole", "fourthRole", "fifthRole" }; | ||
| 283 | + | ||
| 284 | + expect(this.securityContext.hasRole("firstRole")).andReturn(false); | ||
| 285 | + expect(this.securityContext.hasRole("secondRole")).andReturn(false); | ||
| 286 | + | ||
| 287 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 288 | + | ||
| 289 | + interceptor.manage(this.ic); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + @Test | ||
| 293 | + public void testHasMoreRolesThenArray() throws Exception { | ||
| 294 | + Object target = new ClassNotAnnoted(); | ||
| 295 | + String methodName = "requiredRoleWithArrayOfRoles"; | ||
| 296 | + String[] expectedRoles = { "thirdRole", "fourthRole", "fifthRole" }; | ||
| 297 | + | ||
| 298 | + expect(this.securityContext.hasRole("firstRole")).andReturn(false); | ||
| 299 | + expect(this.securityContext.hasRole("secondRole")).andReturn(false); | ||
| 300 | + expect(this.securityContext.hasRole("sixthRole")).andReturn(true); | ||
| 301 | + | ||
| 302 | + prepareMock(target, methodName, expectedRoles, true, true); | ||
| 303 | + | ||
| 304 | + interceptor.manage(this.ic); | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | +} |