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 | 171 | try { |
| 172 | 172 | method.invoke(object, param); |
| 173 | 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 | 182 | method.setAccessible(accessible); |
| 178 | 183 | } |
| 179 | - | |
| 184 | + | |
| 180 | 185 | @AroundInvoke |
| 181 | 186 | public Object manage(final InvocationContext ic) throws Exception { |
| 182 | 187 | Object result = null; | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java
| ... | ... | @@ -75,11 +75,11 @@ public class ExceptionHandlerInterceptorTest { |
| 75 | 75 | |
| 76 | 76 | private ResourceBundle bundle; |
| 77 | 77 | |
| 78 | - class TesteException extends DemoiselleException { | |
| 78 | + class TestException extends DemoiselleException { | |
| 79 | 79 | |
| 80 | 80 | private static final long serialVersionUID = 1L; |
| 81 | 81 | |
| 82 | - public TesteException(String message) { | |
| 82 | + public TestException(String message) { | |
| 83 | 83 | super(message); |
| 84 | 84 | } |
| 85 | 85 | } |
| ... | ... | @@ -97,7 +97,7 @@ public class ExceptionHandlerInterceptorTest { |
| 97 | 97 | public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { |
| 98 | 98 | times++; |
| 99 | 99 | } |
| 100 | - | |
| 100 | + | |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | class ClassWithoutMethodsAnnotatedWithExceptionHandler { |
| ... | ... | @@ -116,7 +116,7 @@ public class ExceptionHandlerInterceptorTest { |
| 116 | 116 | throw new RuntimeException(); |
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | - | |
| 119 | + | |
| 120 | 120 | class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { |
| 121 | 121 | |
| 122 | 122 | @ExceptionHandler |
| ... | ... | @@ -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 | 140 | @Before |
| 129 | 141 | public void setUp() throws Exception { |
| 130 | 142 | this.logger = PowerMock.createMock(Logger.class); |
| ... | ... | @@ -138,7 +150,7 @@ public class ExceptionHandlerInterceptorTest { |
| 138 | 150 | } |
| 139 | 151 | |
| 140 | 152 | @Test |
| 141 | - public void testManageSucessyfull() throws Throwable { | |
| 153 | + public void manageSuccessfully() throws Throwable { | |
| 142 | 154 | expect(this.context.proceed()).andReturn(null); |
| 143 | 155 | replay(); |
| 144 | 156 | assertEquals(null, this.interceptor.manage(this.context)); |
| ... | ... | @@ -146,7 +158,7 @@ public class ExceptionHandlerInterceptorTest { |
| 146 | 158 | } |
| 147 | 159 | |
| 148 | 160 | @Test |
| 149 | - public void testManageWithClassThatDoNotContainMethodAnnotatedWithHandleException() throws Throwable { | |
| 161 | + public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { | |
| 150 | 162 | ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); |
| 151 | 163 | expect(this.context.getTarget()).andReturn(classWithoutException); |
| 152 | 164 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| ... | ... | @@ -164,7 +176,7 @@ public class ExceptionHandlerInterceptorTest { |
| 164 | 176 | } |
| 165 | 177 | |
| 166 | 178 | @Test |
| 167 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleException() throws Throwable { | |
| 179 | + public void manageWithClassThatContainsHandleMethod() throws Exception { | |
| 168 | 180 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 169 | 181 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 170 | 182 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| ... | ... | @@ -177,8 +189,7 @@ public class ExceptionHandlerInterceptorTest { |
| 177 | 189 | } |
| 178 | 190 | |
| 179 | 191 | @Test |
| 180 | - public void testManageWithClassThatContainTwoMethodsAnnotatedWithHandleExceptionButOnlyOneIsCalled() | |
| 181 | - throws Throwable { | |
| 192 | + public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { | |
| 182 | 193 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 183 | 194 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 184 | 195 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| ... | ... | @@ -191,44 +202,26 @@ public class ExceptionHandlerInterceptorTest { |
| 191 | 202 | } |
| 192 | 203 | |
| 193 | 204 | @Test |
| 194 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleParentException() throws Throwable { | |
| 205 | + public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { | |
| 195 | 206 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 196 | 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 | 210 | expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); |
| 199 | 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 | 213 | try { |
| 220 | 214 | this.interceptor.manage(this.context); |
| 221 | 215 | fail(); |
| 222 | - } catch (TesteException e) { | |
| 223 | - assertTrue(true); | |
| 216 | + } catch (TestException e) { | |
| 224 | 217 | assertEquals(0, classWithException.times); |
| 225 | 218 | } |
| 219 | + | |
| 226 | 220 | verify(); |
| 227 | 221 | } |
| 228 | 222 | |
| 229 | 223 | @Test |
| 230 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleExceptionButMethodCouldNotBeCalled() | |
| 231 | - throws Throwable { | |
| 224 | + public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { | |
| 232 | 225 | ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); |
| 233 | 226 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 234 | 227 | expect(this.context.proceed()).andThrow(new DemoiselleException("")); |
| ... | ... | @@ -239,25 +232,25 @@ public class ExceptionHandlerInterceptorTest { |
| 239 | 232 | try { |
| 240 | 233 | this.interceptor.manage(this.context); |
| 241 | 234 | fail(); |
| 242 | - } catch (Exception e) { | |
| 235 | + } catch (RuntimeException e) { | |
| 243 | 236 | assertEquals(1, classWithException.times); |
| 244 | - assertTrue(true); | |
| 245 | 237 | } |
| 246 | 238 | |
| 247 | 239 | verifyAll(); |
| 248 | 240 | } |
| 249 | - | |
| 241 | + | |
| 250 | 242 | @Test |
| 251 | - public void testManageWithClassThatContainMethodsAnnotatedWithHandleExceptionAndIsInvokedTwice() throws Throwable { | |
| 243 | + public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { | |
| 252 | 244 | ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); |
| 253 | 245 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 254 | 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 | 249 | replayAll(this.context, CoreBootstrap.class); |
| 257 | 250 | |
| 258 | 251 | assertNull(this.interceptor.manage(this.context)); |
| 259 | 252 | assertEquals(1, classWithException.times); |
| 260 | - | |
| 253 | + | |
| 261 | 254 | this.context = PowerMock.createMock(InvocationContext.class); |
| 262 | 255 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 263 | 256 | expect(this.context.proceed()).andThrow(new Exception("")); |
| ... | ... | @@ -266,15 +259,16 @@ public class ExceptionHandlerInterceptorTest { |
| 266 | 259 | assertNull(this.interceptor.manage(this.context)); |
| 267 | 260 | assertEquals(2, classWithException.times); |
| 268 | 261 | verifyAll(); |
| 269 | - | |
| 262 | + | |
| 270 | 263 | } |
| 271 | - | |
| 264 | + | |
| 272 | 265 | @Test |
| 273 | - public void testManageWithClassThatContainMethodAnnotatedWithHandleExceptionWithoutParameter() throws Throwable { | |
| 266 | + public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { | |
| 274 | 267 | ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); |
| 275 | 268 | expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); |
| 276 | 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 | 272 | replayAll(this.context, CoreBootstrap.class); |
| 279 | 273 | |
| 280 | 274 | try { |
| ... | ... | @@ -283,8 +277,58 @@ public class ExceptionHandlerInterceptorTest { |
| 283 | 277 | } catch (DemoiselleException e) { |
| 284 | 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 | 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 | } | ... | ... |