Commit 37a4099c9cbb0d78b58404f7e16297a443b7d0b5
1 parent
dff9ad80
Exists in
master
Mantis 0000752: Correção da exceção retornada pelo interceptador de
ExceptionHandler
Showing
2 changed files
with
95 additions
and
46 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java
| @@ -171,12 +171,17 @@ public class ExceptionHandlerInterceptor implements Serializable { | @@ -171,12 +171,17 @@ public class ExceptionHandlerInterceptor implements Serializable { | ||
| 171 | try { | 171 | try { |
| 172 | method.invoke(object, param); | 172 | method.invoke(object, param); |
| 173 | } catch (InvocationTargetException cause) { | 173 | } catch (InvocationTargetException cause) { |
| 174 | - throw new DemoiselleException(cause.getTargetException()); | 174 | + Throwable targetTrowable = cause.getTargetException(); |
| 175 | + if (targetTrowable instanceof Exception) { | ||
| 176 | + throw (Exception) targetTrowable; | ||
| 177 | + } else { | ||
| 178 | + throw new Exception(targetTrowable); | ||
| 179 | + } | ||
| 175 | } | 180 | } |
| 176 | 181 | ||
| 177 | method.setAccessible(accessible); | 182 | method.setAccessible(accessible); |
| 178 | } | 183 | } |
| 179 | - | 184 | + |
| 180 | @AroundInvoke | 185 | @AroundInvoke |
| 181 | public Object manage(final InvocationContext ic) throws Exception { | 186 | public Object manage(final InvocationContext ic) throws Exception { |
| 182 | Object result = null; | 187 | Object result = null; |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java
| @@ -75,11 +75,11 @@ public class ExceptionHandlerInterceptorTest { | @@ -75,11 +75,11 @@ public class ExceptionHandlerInterceptorTest { | ||
| 75 | 75 | ||
| 76 | private ResourceBundle bundle; | 76 | private ResourceBundle bundle; |
| 77 | 77 | ||
| 78 | - class TesteException extends DemoiselleException { | 78 | + class TestException extends DemoiselleException { |
| 79 | 79 | ||
| 80 | private static final long serialVersionUID = 1L; | 80 | private static final long serialVersionUID = 1L; |
| 81 | 81 | ||
| 82 | - public TesteException(String message) { | 82 | + public TestException(String message) { |
| 83 | super(message); | 83 | super(message); |
| 84 | } | 84 | } |
| 85 | } | 85 | } |
| @@ -97,7 +97,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -97,7 +97,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 97 | public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { | 97 | public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { |
| 98 | times++; | 98 | times++; |
| 99 | } | 99 | } |
| 100 | - | 100 | + |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | class ClassWithoutMethodsAnnotatedWithExceptionHandler { | 103 | class ClassWithoutMethodsAnnotatedWithExceptionHandler { |
| @@ -116,7 +116,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -116,7 +116,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 116 | throw new RuntimeException(); | 116 | throw new RuntimeException(); |
| 117 | } | 117 | } |
| 118 | } | 118 | } |
| 119 | - | 119 | + |
| 120 | class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { | 120 | class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { |
| 121 | 121 | ||
| 122 | @ExceptionHandler | 122 | @ExceptionHandler |
| @@ -125,6 +125,18 @@ public class ExceptionHandlerInterceptorTest { | @@ -125,6 +125,18 @@ public class ExceptionHandlerInterceptorTest { | ||
| 125 | 125 | ||
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | + class ClassWithExceptionHandlerMethodThatRethrowException { | ||
| 129 | + | ||
| 130 | + int times = 0; | ||
| 131 | + | ||
| 132 | + @ExceptionHandler | ||
| 133 | + public void methodThatRethrowException(TestException cause) { | ||
| 134 | + times++; | ||
| 135 | + throw cause; | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + } | ||
| 139 | + | ||
| 128 | @Before | 140 | @Before |
| 129 | public void setUp() throws Exception { | 141 | public void setUp() throws Exception { |
| 130 | this.logger = PowerMock.createMock(Logger.class); | 142 | this.logger = PowerMock.createMock(Logger.class); |
| @@ -138,7 +150,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -138,7 +150,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 138 | } | 150 | } |
| 139 | 151 | ||
| 140 | @Test | 152 | @Test |
| 141 | - public void testManageSucessyfull() throws Throwable { | 153 | + public void manageSuccessfully() throws Throwable { |
| 142 | expect(this.context.proceed()).andReturn(null); | 154 | expect(this.context.proceed()).andReturn(null); |
| 143 | replay(); | 155 | replay(); |
| 144 | assertEquals(null, this.interceptor.manage(this.context)); | 156 | assertEquals(null, this.interceptor.manage(this.context)); |
| @@ -146,7 +158,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -146,7 +158,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 146 | } | 158 | } |
| 147 | 159 | ||
| 148 | @Test | 160 | @Test |
| 149 | - public void testManageWithClassThatDoNotContainMethodAnnotatedWithHandleException() throws Throwable { | 161 | + public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { |
| 150 | ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); | 162 | ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); |
| 151 | expect(this.context.getTarget()).andReturn(classWithoutException); | 163 | expect(this.context.getTarget()).andReturn(classWithoutException); |
| 152 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 164 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| @@ -164,7 +176,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -164,7 +176,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 164 | } | 176 | } |
| 165 | 177 | ||
| 166 | @Test | 178 | @Test |
| 167 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleException() throws Throwable { | 179 | + public void manageWithClassThatContainsHandleMethod() throws Exception { |
| 168 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | 180 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 169 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 181 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 170 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 182 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| @@ -177,8 +189,7 @@ public class ExceptionHandlerInterceptorTest { | @@ -177,8 +189,7 @@ public class ExceptionHandlerInterceptorTest { | ||
| 177 | } | 189 | } |
| 178 | 190 | ||
| 179 | @Test | 191 | @Test |
| 180 | - public void testManageWithClassThatContainTwoMethodsAnnotatedWithHandleExceptionButOnlyOneIsCalled() | ||
| 181 | - throws Throwable { | 192 | + public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { |
| 182 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | 193 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 183 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 194 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 184 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 195 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| @@ -191,44 +202,26 @@ public class ExceptionHandlerInterceptorTest { | @@ -191,44 +202,26 @@ public class ExceptionHandlerInterceptorTest { | ||
| 191 | } | 202 | } |
| 192 | 203 | ||
| 193 | @Test | 204 | @Test |
| 194 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleParentException() throws Throwable { | 205 | + public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { |
| 195 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | 206 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 196 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 207 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 197 | - expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 208 | + expect(this.context.proceed()).andThrow(new TestException("")); |
| 209 | + replay(this.context); | ||
| 198 | expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); | 210 | expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); |
| 199 | replayAll(this.context, CoreBootstrap.class); | 211 | replayAll(this.context, CoreBootstrap.class); |
| 200 | 212 | ||
| 201 | - assertNull(this.interceptor.manage(this.context)); | ||
| 202 | - assertEquals(1, classWithException.times); | ||
| 203 | - verifyAll(); | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - @Test | ||
| 207 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleExceptionButCauseIsDiferent() throws Throwable { | ||
| 208 | - ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | ||
| 209 | - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | ||
| 210 | - expect(this.context.proceed()).andThrow(new TesteException("")); | ||
| 211 | - replay(this.context); | ||
| 212 | - this.logger = PowerMock.createMock(Logger.class); | ||
| 213 | - this.logger.info(EasyMock.anyObject(String.class)); | ||
| 214 | - this.logger.debug(EasyMock.anyObject(String.class)); | ||
| 215 | - replay(this.logger); | ||
| 216 | - | ||
| 217 | - this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); | ||
| 218 | - | ||
| 219 | try { | 213 | try { |
| 220 | this.interceptor.manage(this.context); | 214 | this.interceptor.manage(this.context); |
| 221 | fail(); | 215 | fail(); |
| 222 | - } catch (TesteException e) { | ||
| 223 | - assertTrue(true); | 216 | + } catch (TestException e) { |
| 224 | assertEquals(0, classWithException.times); | 217 | assertEquals(0, classWithException.times); |
| 225 | } | 218 | } |
| 219 | + | ||
| 226 | verify(); | 220 | verify(); |
| 227 | } | 221 | } |
| 228 | 222 | ||
| 229 | @Test | 223 | @Test |
| 230 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleExceptionButMethodCouldNotBeCalled() | ||
| 231 | - throws Throwable { | 224 | + public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { |
| 232 | ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); | 225 | ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); |
| 233 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 226 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 234 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 227 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| @@ -239,25 +232,25 @@ public class ExceptionHandlerInterceptorTest { | @@ -239,25 +232,25 @@ public class ExceptionHandlerInterceptorTest { | ||
| 239 | try { | 232 | try { |
| 240 | this.interceptor.manage(this.context); | 233 | this.interceptor.manage(this.context); |
| 241 | fail(); | 234 | fail(); |
| 242 | - } catch (Exception e) { | 235 | + } catch (RuntimeException e) { |
| 243 | assertEquals(1, classWithException.times); | 236 | assertEquals(1, classWithException.times); |
| 244 | - assertTrue(true); | ||
| 245 | } | 237 | } |
| 246 | 238 | ||
| 247 | verifyAll(); | 239 | verifyAll(); |
| 248 | } | 240 | } |
| 249 | - | 241 | + |
| 250 | @Test | 242 | @Test |
| 251 | - public void testManageWithClassThatContainMethodsAnnotatedWithHandleExceptionAndIsInvokedTwice() throws Throwable { | 243 | + public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { |
| 252 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); | 244 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 253 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 245 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 254 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 246 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| 255 | - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true).anyTimes(); | 247 | + expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true) |
| 248 | + .anyTimes(); | ||
| 256 | replayAll(this.context, CoreBootstrap.class); | 249 | replayAll(this.context, CoreBootstrap.class); |
| 257 | 250 | ||
| 258 | assertNull(this.interceptor.manage(this.context)); | 251 | assertNull(this.interceptor.manage(this.context)); |
| 259 | assertEquals(1, classWithException.times); | 252 | assertEquals(1, classWithException.times); |
| 260 | - | 253 | + |
| 261 | this.context = PowerMock.createMock(InvocationContext.class); | 254 | this.context = PowerMock.createMock(InvocationContext.class); |
| 262 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 255 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 263 | expect(this.context.proceed()).andThrow(new Exception("")); | 256 | expect(this.context.proceed()).andThrow(new Exception("")); |
| @@ -266,15 +259,16 @@ public class ExceptionHandlerInterceptorTest { | @@ -266,15 +259,16 @@ public class ExceptionHandlerInterceptorTest { | ||
| 266 | assertNull(this.interceptor.manage(this.context)); | 259 | assertNull(this.interceptor.manage(this.context)); |
| 267 | assertEquals(2, classWithException.times); | 260 | assertEquals(2, classWithException.times); |
| 268 | verifyAll(); | 261 | verifyAll(); |
| 269 | - | 262 | + |
| 270 | } | 263 | } |
| 271 | - | 264 | + |
| 272 | @Test | 265 | @Test |
| 273 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleExceptionWithoutParameter() throws Throwable { | 266 | + public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { |
| 274 | ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); | 267 | ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); |
| 275 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); | 268 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 276 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); | 269 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| 277 | - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)).andReturn(true); | 270 | + expect(CoreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)) |
| 271 | + .andReturn(true); | ||
| 278 | replayAll(this.context, CoreBootstrap.class); | 272 | replayAll(this.context, CoreBootstrap.class); |
| 279 | 273 | ||
| 280 | try { | 274 | try { |
| @@ -283,8 +277,58 @@ public class ExceptionHandlerInterceptorTest { | @@ -283,8 +277,58 @@ public class ExceptionHandlerInterceptorTest { | ||
| 283 | } catch (DemoiselleException e) { | 277 | } catch (DemoiselleException e) { |
| 284 | assertTrue(true); | 278 | assertTrue(true); |
| 285 | } | 279 | } |
| 286 | - | 280 | + |
| 281 | + verifyAll(); | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + @Test | ||
| 285 | + public void manageHandlerMethodThatRethrowExpectedException() throws Exception { | ||
| 286 | + ClassWithExceptionHandlerMethodThatRethrowException testClass = new ClassWithExceptionHandlerMethodThatRethrowException(); | ||
| 287 | + expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 288 | + expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 289 | + expect(CoreBootstrap.isAnnotatedType(ClassWithExceptionHandlerMethodThatRethrowException.class)) | ||
| 290 | + .andReturn(true); | ||
| 291 | + replayAll(this.context, CoreBootstrap.class); | ||
| 292 | + | ||
| 293 | + try { | ||
| 294 | + this.interceptor.manage(this.context); | ||
| 295 | + fail(); | ||
| 296 | + } catch (TestException e) { | ||
| 297 | + assertEquals(1, testClass.times); | ||
| 298 | + } | ||
| 299 | + | ||
| 287 | verifyAll(); | 300 | verifyAll(); |
| 288 | } | 301 | } |
| 289 | 302 | ||
| 303 | + /** | ||
| 304 | + * Tests an exception handler when the class that contains the method is a proxy. This is the case when using | ||
| 305 | + * injection. | ||
| 306 | + * | ||
| 307 | + * @throws Exception | ||
| 308 | + */ | ||
| 309 | + @Test | ||
| 310 | + public void manageHandlerMethodInsideProxyClass() throws Exception { | ||
| 311 | + // creates a proxy class | ||
| 312 | + ClassWithExceptionHandlerMethodThatRethrowException testClass = PowerMock | ||
| 313 | + .createNicePartialMockForAllMethodsExcept(ClassWithExceptionHandlerMethodThatRethrowException.class, | ||
| 314 | + "methodThatRethrowException"); | ||
| 315 | + expect(this.context.getTarget()).andReturn(testClass).anyTimes(); | ||
| 316 | + expect(this.context.proceed()).andThrow(new TestException("")); | ||
| 317 | + expect(CoreBootstrap.isAnnotatedType(testClass.getClass())).andReturn(false); | ||
| 318 | + | ||
| 319 | + this.logger = PowerMock.createMock(Logger.class); | ||
| 320 | + this.logger.info(EasyMock.anyObject(String.class)); | ||
| 321 | + this.logger.debug(EasyMock.anyObject(String.class)); | ||
| 322 | + replayAll(testClass, this.context, CoreBootstrap.class, logger); | ||
| 323 | + | ||
| 324 | + this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); | ||
| 325 | + | ||
| 326 | + try { | ||
| 327 | + this.interceptor.manage(this.context); | ||
| 328 | + fail(); | ||
| 329 | + } catch (TestException e) { | ||
| 330 | + assertEquals(1, testClass.times); | ||
| 331 | + } | ||
| 332 | + } | ||
| 333 | + | ||
| 290 | } | 334 | } |