From d6266d0c93298c3c044af02d36cd5ae12e227a78 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Tue, 10 Sep 2013 13:40:26 -0300 Subject: [PATCH] Correção nos testes para aderir à nova especificação das interfaces de segurança. --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java | 16 +++++----------- impl/core/src/test/java/security/athentication/error/ErrorAuthenticatorTest.java | 41 ++++++++++++++++++----------------------- impl/core/src/test/java/security/authorization/error/ErrorAuthorizerTest.java | 48 +++++++++++++++++++++--------------------------- 3 files changed, 44 insertions(+), 61 deletions(-) diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java index e686d3c..a81a4e1 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java @@ -38,6 +38,7 @@ package br.gov.frameworkdemoiselle.internal.implementation; import javax.inject.Named; +import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful; import br.gov.frameworkdemoiselle.security.AfterLogoutSuccessful; @@ -45,7 +46,6 @@ import br.gov.frameworkdemoiselle.security.AuthenticationException; import br.gov.frameworkdemoiselle.security.Authenticator; import br.gov.frameworkdemoiselle.security.AuthorizationException; import br.gov.frameworkdemoiselle.security.Authorizer; -import br.gov.frameworkdemoiselle.security.InvalidCredentialsException; import br.gov.frameworkdemoiselle.security.NotLoggedInException; import br.gov.frameworkdemoiselle.security.SecurityContext; import br.gov.frameworkdemoiselle.security.User; @@ -110,7 +110,7 @@ public class SecurityContextImpl implements SecurityContext { try { result = getAuthorizer().hasPermission(resource, operation); - } catch (AuthorizationException cause) { + } catch (DemoiselleException cause) { throw cause; } catch (Exception cause) { @@ -134,7 +134,7 @@ public class SecurityContextImpl implements SecurityContext { try { result = getAuthorizer().hasRole(role); - } catch (AuthorizationException cause) { + } catch (DemoiselleException cause) { throw cause; } catch (Exception cause) { @@ -169,10 +169,7 @@ public class SecurityContextImpl implements SecurityContext { try { getAuthenticator().authenticate(); - } catch (InvalidCredentialsException cause) { - throw cause; - - } catch (AuthenticationException cause) { + } catch (DemoiselleException cause) { throw cause; } catch (Exception cause) { @@ -198,10 +195,7 @@ public class SecurityContextImpl implements SecurityContext { try { getAuthenticator().unauthenticate(); - } catch (InvalidCredentialsException cause) { - throw cause; - - } catch (AuthenticationException cause) { + } catch (DemoiselleException cause) { throw cause; } catch (Exception cause) { diff --git a/impl/core/src/test/java/security/athentication/error/ErrorAuthenticatorTest.java b/impl/core/src/test/java/security/athentication/error/ErrorAuthenticatorTest.java index 7ac8542..bb0e1c4 100644 --- a/impl/core/src/test/java/security/athentication/error/ErrorAuthenticatorTest.java +++ b/impl/core/src/test/java/security/athentication/error/ErrorAuthenticatorTest.java @@ -36,9 +36,10 @@ */ package security.athentication.error; -import javax.inject.Inject; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; -import junit.framework.Assert; +import javax.inject.Inject; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -63,33 +64,27 @@ public class ErrorAuthenticatorTest { deployment.addClass(ErrorAuthenticator.class); return deployment; } - + @Test - public void errorDuringLogin(){ - try{ + public void errorDuringLogin() { + try { context.login(); - Assert.fail("Login deveria disparar exceção de runtime"); - } - catch(AuthenticationException ae){ - Assert.fail("A exceção disparada não foi a esperada"); - } - catch(RuntimeException e){ - //PASS + fail("Login deveria disparar exceção de runtime"); + + } catch (AuthenticationException cause) { + assertEquals(RuntimeException.class, cause.getCause().getClass()); } } - + @Test - public void errorDuringLogout(){ - try{ + public void errorDuringLogout() { + try { context.login(); - Assert.fail("Logout deveria disparar exceção de runtime"); - } - catch(AuthenticationException ae){ - Assert.fail("A exceção disparada não foi a esperada"); - } - catch(RuntimeException e){ - //PASS + fail("Logout deveria disparar exceção de runtime"); + + } catch (AuthenticationException cause) { + assertEquals(RuntimeException.class, cause.getCause().getClass()); } } - + } diff --git a/impl/core/src/test/java/security/authorization/error/ErrorAuthorizerTest.java b/impl/core/src/test/java/security/authorization/error/ErrorAuthorizerTest.java index f017a50..6cb00ee 100644 --- a/impl/core/src/test/java/security/authorization/error/ErrorAuthorizerTest.java +++ b/impl/core/src/test/java/security/authorization/error/ErrorAuthorizerTest.java @@ -36,9 +36,10 @@ */ package security.authorization.error; -import javax.inject.Inject; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; -import junit.framework.Assert; +import javax.inject.Inject; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -51,7 +52,6 @@ import org.junit.runner.RunWith; import security.athentication.custom.CustomAuthenticator; import test.Tests; import br.gov.frameworkdemoiselle.security.AuthorizationException; -import br.gov.frameworkdemoiselle.security.NotLoggedInException; import br.gov.frameworkdemoiselle.security.SecurityContext; import configuration.resource.ConfigurationResourceTest; @@ -68,43 +68,37 @@ public class ErrorAuthorizerTest { deployment.addClass(ErrorAuthorizer.class); return deployment; } - + @Before - public void loginToTest(){ + public void loginToTest() { context.login(); } @Test - public void errorDuringCheckPermission(){ - try{ + public void errorDuringCheckPermission() { + try { context.hasPermission("resource", "operation"); - Assert.fail("Verificar permissão deveria disparar exceção de runtime"); - } - catch(NotLoggedInException ae){ - Assert.fail("A exceção disparada não foi a esperada"); - } - catch(RuntimeException e){ - //PASS + fail("Verificar permissão deveria disparar exceção de runtime"); + + } catch (AuthorizationException cause) { + assertEquals(RuntimeException.class, cause.getCause().getClass()); } } - + @Test - public void errorDuringCheckRole(){ - try{ + public void errorDuringCheckRole() { + try { context.hasRole("role"); - Assert.fail("Verificar papel deveria disparar exceção de runtime"); - } - catch(AuthorizationException ae){ - Assert.fail("A exceção disparada não foi a esperada"); - } - catch(RuntimeException e){ - //PASS + fail("Verificar papel deveria disparar exceção de runtime"); + + } catch (AuthorizationException cause) { + assertEquals(RuntimeException.class, cause.getCause().getClass()); } } - + @After - public void logoutAfterTest(){ + public void logoutAfterTest() { context.logout(); } - + } -- libgit2 0.21.2