Commit db5111de396939ba2432b81093f274d6c11eb3bb
Exists in
master
Merge branch '2.3' of ssh://git@github.com/demoiselle/framework.git into 2.3
Showing
42 changed files
with
1366 additions
and
2582 deletions
Show diff stats
example/.gitignore
example/initializer/.gitignore
example/initializer/pom.xml
| ... | ... | @@ -1,74 +0,0 @@ |
| 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 | -<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | |
| 38 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| 39 | - | |
| 40 | - <modelVersion>4.0.0</modelVersion> | |
| 41 | - | |
| 42 | - <artifactId>demoiselle-initializer-example</artifactId> | |
| 43 | - <packaging>jar</packaging> | |
| 44 | - | |
| 45 | - <name></name> | |
| 46 | - <description></description> | |
| 47 | - <url></url> | |
| 48 | - | |
| 49 | - <parent> | |
| 50 | - <groupId>br.gov.frameworkdemoiselle</groupId> | |
| 51 | - <artifactId>demoiselle-minimal-parent</artifactId> | |
| 52 | - <version>2.3.0-RC1-SNAPSHOT</version> | |
| 53 | - <relativePath>../../parent/minimal/pom.xml</relativePath> | |
| 54 | - </parent> | |
| 55 | - | |
| 56 | - <dependencies> | |
| 57 | - <dependency> | |
| 58 | - <groupId>org.slf4j</groupId> | |
| 59 | - <artifactId>slf4j-log4j12</artifactId> | |
| 60 | - <scope>test</scope> | |
| 61 | - </dependency> | |
| 62 | - </dependencies> | |
| 63 | - | |
| 64 | - <repositories> | |
| 65 | - <repository> | |
| 66 | - <id>demoiselle.sourceforge.net-release</id> | |
| 67 | - <url>http://demoiselle.sourceforge.net/repository/release</url> | |
| 68 | - </repository> | |
| 69 | - </repositories> | |
| 70 | - | |
| 71 | - <properties> | |
| 72 | - <demoiselle.junit.version>2.3.0-RC1-SNAPSHOT</demoiselle.junit.version> | |
| 73 | - </properties> | |
| 74 | -</project> |
example/initializer/src/main/java/example/Hello.java
| ... | ... | @@ -1,86 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import static br.gov.frameworkdemoiselle.annotation.Priority.MAX_PRIORITY; | |
| 40 | -import static br.gov.frameworkdemoiselle.annotation.Priority.MIN_PRIORITY; | |
| 41 | - | |
| 42 | -import java.util.ArrayList; | |
| 43 | -import java.util.List; | |
| 44 | - | |
| 45 | -import javax.enterprise.context.ApplicationScoped; | |
| 46 | - | |
| 47 | -import br.gov.frameworkdemoiselle.annotation.Priority; | |
| 48 | -import br.gov.frameworkdemoiselle.lifecycle.Startup; | |
| 49 | -import br.gov.frameworkdemoiselle.lifecycle.Shutdown; | |
| 50 | - | |
| 51 | -@ApplicationScoped | |
| 52 | -public class Hello { | |
| 53 | - | |
| 54 | - private List<String> list = new ArrayList<String>(); | |
| 55 | - | |
| 56 | - public List<String> getList() { | |
| 57 | - return list; | |
| 58 | - } | |
| 59 | - | |
| 60 | - public void say() { | |
| 61 | - list.add("Hello World"); | |
| 62 | - } | |
| 63 | - | |
| 64 | - @Startup | |
| 65 | - public void load() { | |
| 66 | - list.add("Startup: Priority Not Defined"); | |
| 67 | - } | |
| 68 | - | |
| 69 | - @Startup | |
| 70 | - @Priority(MIN_PRIORITY) | |
| 71 | - public void loadWithMinPriority() { | |
| 72 | - list.add("Startup: Min Priority"); | |
| 73 | - } | |
| 74 | - | |
| 75 | - @Shutdown | |
| 76 | - @Priority(1) | |
| 77 | - public void unload() { | |
| 78 | - list.add("Shutdown: Priority 1"); | |
| 79 | - } | |
| 80 | - | |
| 81 | - @Shutdown | |
| 82 | - @Priority(MAX_PRIORITY) | |
| 83 | - public void unloadWithMaxPriority() { | |
| 84 | - list.add("Shutdown: Max Priority"); | |
| 85 | - } | |
| 86 | -} |
example/initializer/src/main/resources/META-INF/beans.xml
| ... | ... | @@ -1,40 +0,0 @@ |
| 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 | -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 38 | - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
| 39 | - | |
| 40 | -</beans> |
example/initializer/src/main/resources/demoiselle.properties
| ... | ... | @@ -1,34 +0,0 @@ |
| 1 | -# Demoiselle Framework | |
| 2 | -# Copyright (C) 2010 SERPRO | |
| 3 | -# ---------------------------------------------------------------------------- | |
| 4 | -# This file is part of Demoiselle Framework. | |
| 5 | -# | |
| 6 | -# Demoiselle Framework is free software; you can redistribute it and/or | |
| 7 | -# modify it under the terms of the GNU Lesser General Public License version 3 | |
| 8 | -# as published by the Free Software Foundation. | |
| 9 | -# | |
| 10 | -# This program is distributed in the hope that it will be useful, | |
| 11 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | -# GNU General Public License for more details. | |
| 14 | -# | |
| 15 | -# You should have received a copy of the GNU Lesser General Public License version 3 | |
| 16 | -# along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 17 | -# or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 18 | -# Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | -# ---------------------------------------------------------------------------- | |
| 20 | -# Este arquivo é parte do Framework Demoiselle. | |
| 21 | -# | |
| 22 | -# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 23 | -# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 24 | -# do Software Livre (FSF). | |
| 25 | -# | |
| 26 | -# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 27 | -# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 28 | -# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 29 | -# para maiores detalhes. | |
| 30 | -# | |
| 31 | -# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 32 | -# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 33 | -# ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 34 | -# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 35 | 0 | \ No newline at end of file |
example/initializer/src/test/java/example/InitializerAdvancedTest.java
| ... | ... | @@ -1,90 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import static junit.framework.Assert.assertEquals; | |
| 40 | - | |
| 41 | -import java.util.ArrayList; | |
| 42 | -import java.util.List; | |
| 43 | - | |
| 44 | -import org.junit.AfterClass; | |
| 45 | -import org.junit.BeforeClass; | |
| 46 | -import org.junit.Test; | |
| 47 | -import org.junit.runner.RunWith; | |
| 48 | - | |
| 49 | -import br.gov.frameworkdemoiselle.junit.DemoiselleRunner; | |
| 50 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 51 | - | |
| 52 | -@RunWith(DemoiselleRunner.class) | |
| 53 | -public class InitializerAdvancedTest { | |
| 54 | - | |
| 55 | - private static Hello hello = Beans.getReference(Hello.class); | |
| 56 | - | |
| 57 | - private static List<String> expected = new ArrayList<String>(); | |
| 58 | - | |
| 59 | - @BeforeClass | |
| 60 | - public static void beforeClass() { | |
| 61 | - expected.add("Startup: Priority Not Defined"); | |
| 62 | - expected.add("Startup: Min Priority"); | |
| 63 | - | |
| 64 | - assertEquals(expected, hello.getList()); | |
| 65 | - } | |
| 66 | - | |
| 67 | - @Test | |
| 68 | - public void enqueueingAfterStartup() { | |
| 69 | - hello.say(); | |
| 70 | - expected.add("Hello World"); | |
| 71 | - | |
| 72 | - assertEquals(expected, hello.getList()); | |
| 73 | - } | |
| 74 | - | |
| 75 | - @Test | |
| 76 | - public void enqueueingAfterStartupAgain() { | |
| 77 | - hello.say(); | |
| 78 | - expected.add("Hello World"); | |
| 79 | - | |
| 80 | - assertEquals(expected, hello.getList()); | |
| 81 | - } | |
| 82 | - | |
| 83 | - @AfterClass | |
| 84 | - public static void afterClass() { | |
| 85 | - expected.add("Shutdown: Max Priority"); | |
| 86 | - expected.add("Shutdown: Priority 1"); | |
| 87 | - | |
| 88 | - assertEquals(expected, hello.getList()); | |
| 89 | - } | |
| 90 | -} |
example/initializer/src/test/java/example/InitializerSimpleTest.java
| ... | ... | @@ -1,69 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import static junit.framework.Assert.assertEquals; | |
| 40 | - | |
| 41 | -import org.junit.AfterClass; | |
| 42 | -import org.junit.BeforeClass; | |
| 43 | -import org.junit.Test; | |
| 44 | -import org.junit.runner.RunWith; | |
| 45 | - | |
| 46 | -import br.gov.frameworkdemoiselle.junit.DemoiselleRunner; | |
| 47 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 48 | - | |
| 49 | -@RunWith(DemoiselleRunner.class) | |
| 50 | -public class InitializerSimpleTest { | |
| 51 | - | |
| 52 | - private static Hello hello = Beans.getReference(Hello.class); | |
| 53 | - | |
| 54 | - @BeforeClass | |
| 55 | - public static void beforeClass() { | |
| 56 | - assertEquals(2, hello.getList().size()); | |
| 57 | - } | |
| 58 | - | |
| 59 | - @Test | |
| 60 | - public void enqueueingAfterStartup() { | |
| 61 | - hello.say(); | |
| 62 | - assertEquals(3, hello.getList().size()); | |
| 63 | - } | |
| 64 | - | |
| 65 | - @AfterClass | |
| 66 | - public static void afterClass() { | |
| 67 | - assertEquals(5, hello.getList().size()); | |
| 68 | - } | |
| 69 | -} |
example/initializer/src/test/resources/META-INF/beans.xml
| ... | ... | @@ -1,47 +0,0 @@ |
| 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 | -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
| 38 | - | |
| 39 | - <interceptors> | |
| 40 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.ExceptionHandlerInterceptor</class> | |
| 41 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredPermissionInterceptor</class> | |
| 42 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredRoleInterceptor</class> | |
| 43 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor</class> | |
| 44 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.ConfigurationInterceptor</class> | |
| 45 | - </interceptors> | |
| 46 | - | |
| 47 | -</beans> | |
| 48 | 0 | \ No newline at end of file |
example/initializer/src/test/resources/log4j.properties
| ... | ... | @@ -1,39 +0,0 @@ |
| 1 | -# Demoiselle Framework | |
| 2 | -# Copyright (C) 2010 SERPRO | |
| 3 | -# ---------------------------------------------------------------------------- | |
| 4 | -# This file is part of Demoiselle Framework. | |
| 5 | -# | |
| 6 | -# Demoiselle Framework is free software; you can redistribute it and/or | |
| 7 | -# modify it under the terms of the GNU Lesser General Public License version 3 | |
| 8 | -# as published by the Free Software Foundation. | |
| 9 | -# | |
| 10 | -# This program is distributed in the hope that it will be useful, | |
| 11 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | -# GNU General Public License for more details. | |
| 14 | -# | |
| 15 | -# You should have received a copy of the GNU Lesser General Public License version 3 | |
| 16 | -# along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 17 | -# or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 18 | -# Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | -# ---------------------------------------------------------------------------- | |
| 20 | -# Este arquivo é parte do Framework Demoiselle. | |
| 21 | -# | |
| 22 | -# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 23 | -# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 24 | -# do Software Livre (FSF). | |
| 25 | -# | |
| 26 | -# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 27 | -# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 28 | -# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 29 | -# para maiores detalhes. | |
| 30 | -# | |
| 31 | -# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 32 | -# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 33 | -# ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 34 | -# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 35 | - | |
| 36 | -log4j.rootLogger=INFO, A1 | |
| 37 | -log4j.appender.A1=org.apache.log4j.ConsoleAppender | |
| 38 | -log4j.appender.A1.layout=org.apache.log4j.PatternLayout | |
| 39 | -log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %c - %m%n |
example/pom.xml
| ... | ... | @@ -1,89 +0,0 @@ |
| 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 | -<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | |
| 38 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| 39 | - | |
| 40 | - <modelVersion>4.0.0</modelVersion> | |
| 41 | - | |
| 42 | - <groupId>br.gov.frameworkdemoiselle</groupId> | |
| 43 | - <artifactId>demoiselle-framework-example-build</artifactId> | |
| 44 | - <version>2.3.0-RC1-SNAPSHOT</version> | |
| 45 | - <packaging>pom</packaging> | |
| 46 | - | |
| 47 | - <name>Examples Build Aggregator</name> | |
| 48 | - <description>Demoiselle Examples Build Aggregator</description> | |
| 49 | - | |
| 50 | - <modules> | |
| 51 | - <module>initializer</module> | |
| 52 | - <module>security</module> | |
| 53 | - </modules> | |
| 54 | - | |
| 55 | - <build> | |
| 56 | - <plugins> | |
| 57 | - <plugin> | |
| 58 | - <groupId>org.codehaus.mojo</groupId> | |
| 59 | - <artifactId>wagon-maven-plugin</artifactId> | |
| 60 | - <configuration> | |
| 61 | - <url>.</url> | |
| 62 | - <skip>true</skip> | |
| 63 | - </configuration> | |
| 64 | - </plugin> | |
| 65 | - <plugin> | |
| 66 | - <groupId>org.apache.maven.plugins</groupId> | |
| 67 | - <artifactId>maven-deploy-plugin</artifactId> | |
| 68 | - <configuration> | |
| 69 | - <skip>true</skip> | |
| 70 | - </configuration> | |
| 71 | - </plugin> | |
| 72 | - </plugins> | |
| 73 | - </build> | |
| 74 | - | |
| 75 | - <distributionManagement> | |
| 76 | - <repository> | |
| 77 | - <id /> | |
| 78 | - <url /> | |
| 79 | - </repository> | |
| 80 | - <snapshotRepository> | |
| 81 | - <id /> | |
| 82 | - <url /> | |
| 83 | - </snapshotRepository> | |
| 84 | - <site> | |
| 85 | - <id /> | |
| 86 | - <url>file://${project.build.directory}/tmp</url> | |
| 87 | - </site> | |
| 88 | - </distributionManagement> | |
| 89 | -</project> |
example/security/.gitignore
example/security/pom.xml
| ... | ... | @@ -1,74 +0,0 @@ |
| 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 | -<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | |
| 38 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| 39 | - | |
| 40 | - <modelVersion>4.0.0</modelVersion> | |
| 41 | - | |
| 42 | - <artifactId>demoiselle-security-example</artifactId> | |
| 43 | - <packaging>jar</packaging> | |
| 44 | - | |
| 45 | - <name></name> | |
| 46 | - <description></description> | |
| 47 | - <url></url> | |
| 48 | - | |
| 49 | - <parent> | |
| 50 | - <groupId>br.gov.frameworkdemoiselle</groupId> | |
| 51 | - <artifactId>demoiselle-minimal-parent</artifactId> | |
| 52 | - <version>2.3.0-RC1-SNAPSHOT</version> | |
| 53 | - <relativePath>../../parent/minimal/pom.xml</relativePath> | |
| 54 | - </parent> | |
| 55 | - | |
| 56 | - <dependencies> | |
| 57 | - <dependency> | |
| 58 | - <groupId>org.slf4j</groupId> | |
| 59 | - <artifactId>slf4j-log4j12</artifactId> | |
| 60 | - <scope>test</scope> | |
| 61 | - </dependency> | |
| 62 | - </dependencies> | |
| 63 | - | |
| 64 | - <repositories> | |
| 65 | - <repository> | |
| 66 | - <id>demoiselle.sourceforge.net-release</id> | |
| 67 | - <url>http://demoiselle.sourceforge.net/repository/release</url> | |
| 68 | - </repository> | |
| 69 | - </repositories> | |
| 70 | - | |
| 71 | - <properties> | |
| 72 | - <demoiselle.junit.version>2.3.0-RC1-SNAPSHOT</demoiselle.junit.version> | |
| 73 | - </properties> | |
| 74 | -</project> | |
| 75 | 0 | \ No newline at end of file |
example/security/src/main/java/example/Hello.java
| ... | ... | @@ -1,64 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import br.gov.frameworkdemoiselle.security.RequiredPermission; | |
| 40 | -import br.gov.frameworkdemoiselle.security.RequiredRole; | |
| 41 | - | |
| 42 | -public class Hello { | |
| 43 | - | |
| 44 | - @RequiredPermission(resource = "Hello") | |
| 45 | - public void say1() { | |
| 46 | - System.out.println("Hello 1"); | |
| 47 | - } | |
| 48 | - | |
| 49 | - @RequiredPermission(resource = "World", operation = "scream") | |
| 50 | - public void say2() { | |
| 51 | - System.out.println("Hello 2"); | |
| 52 | - } | |
| 53 | - | |
| 54 | - @RequiredRole("admin") | |
| 55 | - public void say3() { | |
| 56 | - System.out.println("Hello 3"); | |
| 57 | - } | |
| 58 | - | |
| 59 | - @RequiredRole("jedi") | |
| 60 | - @RequiredPermission(resource = "World", operation = "say4") | |
| 61 | - public void say4() { | |
| 62 | - System.out.println("Hello 4"); | |
| 63 | - } | |
| 64 | -} |
example/security/src/main/java/example/MyAuthenticator.java
| ... | ... | @@ -1,81 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import javax.enterprise.context.SessionScoped; | |
| 40 | -import javax.inject.Inject; | |
| 41 | - | |
| 42 | -import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 43 | -import br.gov.frameworkdemoiselle.security.User; | |
| 44 | - | |
| 45 | -@SessionScoped | |
| 46 | -public class MyAuthenticator implements Authenticator { | |
| 47 | - | |
| 48 | - private static final long serialVersionUID = 1L; | |
| 49 | - | |
| 50 | - @Inject | |
| 51 | - private MyCredentials credentials; | |
| 52 | - | |
| 53 | - private User user; | |
| 54 | - | |
| 55 | - @Override | |
| 56 | - public boolean authenticate() { | |
| 57 | - boolean authenticated = false; | |
| 58 | - | |
| 59 | - String username = credentials.getUsername(); | |
| 60 | - String password = credentials.getPassword(); | |
| 61 | - | |
| 62 | - if ("santos.dumont".equals(username) && "secret".equals(password)) { | |
| 63 | - user = new MyUser(credentials.getUsername()); | |
| 64 | - user.setAttribute("roles", credentials.getRoles()); | |
| 65 | - | |
| 66 | - authenticated = true; | |
| 67 | - } | |
| 68 | - | |
| 69 | - return authenticated; | |
| 70 | - } | |
| 71 | - | |
| 72 | - @Override | |
| 73 | - public User getUser() { | |
| 74 | - return user; | |
| 75 | - } | |
| 76 | - | |
| 77 | - @Override | |
| 78 | - public void unAuthenticate() { | |
| 79 | - user = null; | |
| 80 | - } | |
| 81 | -} |
example/security/src/main/java/example/MyAuthorizer.java
| ... | ... | @@ -1,71 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import java.util.List; | |
| 40 | - | |
| 41 | -import javax.inject.Inject; | |
| 42 | - | |
| 43 | -import br.gov.frameworkdemoiselle.security.Authorizer; | |
| 44 | -import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 45 | - | |
| 46 | -public class MyAuthorizer implements Authorizer { | |
| 47 | - | |
| 48 | - private static final long serialVersionUID = 1L; | |
| 49 | - | |
| 50 | - @Inject | |
| 51 | - private SecurityContext securityContext; | |
| 52 | - | |
| 53 | - @Override | |
| 54 | - @SuppressWarnings("unchecked") | |
| 55 | - public boolean hasRole(String role) { | |
| 56 | - List<String> roles = (List<String>) securityContext.getUser().getAttribute("roles"); | |
| 57 | - | |
| 58 | - return roles.contains(role); | |
| 59 | - } | |
| 60 | - | |
| 61 | - @Override | |
| 62 | - public boolean hasPermission(String resource, String operation) { | |
| 63 | - boolean permitted = false; | |
| 64 | - | |
| 65 | - if (resource.equals("Hello") && operation.equals("say1")) { | |
| 66 | - permitted = true; | |
| 67 | - } | |
| 68 | - | |
| 69 | - return permitted; | |
| 70 | - } | |
| 71 | -} |
example/security/src/main/java/example/MyCredentials.java
| ... | ... | @@ -1,79 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import java.io.Serializable; | |
| 40 | -import java.util.ArrayList; | |
| 41 | -import java.util.List; | |
| 42 | - | |
| 43 | -import javax.enterprise.context.RequestScoped; | |
| 44 | - | |
| 45 | -@RequestScoped | |
| 46 | -public class MyCredentials implements Serializable { | |
| 47 | - | |
| 48 | - private static final long serialVersionUID = 1L; | |
| 49 | - | |
| 50 | - private String username; | |
| 51 | - | |
| 52 | - private String password; | |
| 53 | - | |
| 54 | - private List<String> roles = new ArrayList<String>(); | |
| 55 | - | |
| 56 | - public String getUsername() { | |
| 57 | - return username; | |
| 58 | - } | |
| 59 | - | |
| 60 | - public void setUsername(String username) { | |
| 61 | - this.username = username; | |
| 62 | - } | |
| 63 | - | |
| 64 | - public String getPassword() { | |
| 65 | - return password; | |
| 66 | - } | |
| 67 | - | |
| 68 | - public void setPassword(String password) { | |
| 69 | - this.password = password; | |
| 70 | - } | |
| 71 | - | |
| 72 | - public List<String> getRoles() { | |
| 73 | - return roles; | |
| 74 | - } | |
| 75 | - | |
| 76 | - public void addRole(String role) { | |
| 77 | - this.roles.add(role); | |
| 78 | - } | |
| 79 | -} |
example/security/src/main/java/example/MyUser.java
| ... | ... | @@ -1,70 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import java.util.HashMap; | |
| 40 | -import java.util.Map; | |
| 41 | - | |
| 42 | -import br.gov.frameworkdemoiselle.security.User; | |
| 43 | - | |
| 44 | -public class MyUser implements User { | |
| 45 | - | |
| 46 | - private static final long serialVersionUID = 1L; | |
| 47 | - | |
| 48 | - private final String username; | |
| 49 | - | |
| 50 | - private Map<String, Object> attrs = new HashMap<String, Object>(); | |
| 51 | - | |
| 52 | - public MyUser(String username) { | |
| 53 | - this.username = username; | |
| 54 | - } | |
| 55 | - | |
| 56 | - @Override | |
| 57 | - public String getId() { | |
| 58 | - return this.username; | |
| 59 | - } | |
| 60 | - | |
| 61 | - @Override | |
| 62 | - public Object getAttribute(Object key) { | |
| 63 | - return this.attrs.get(key); | |
| 64 | - } | |
| 65 | - | |
| 66 | - @Override | |
| 67 | - public void setAttribute(Object key, Object value) { | |
| 68 | - this.attrs.put((String) key, value); | |
| 69 | - } | |
| 70 | -} |
example/security/src/main/resources/META-INF/beans.xml
| ... | ... | @@ -1,39 +0,0 @@ |
| 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 | -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
| 38 | - | |
| 39 | -</beans> |
example/security/src/main/resources/demoiselle.properties
| ... | ... | @@ -1,34 +0,0 @@ |
| 1 | -# Demoiselle Framework | |
| 2 | -# Copyright (C) 2010 SERPRO | |
| 3 | -# ---------------------------------------------------------------------------- | |
| 4 | -# This file is part of Demoiselle Framework. | |
| 5 | -# | |
| 6 | -# Demoiselle Framework is free software; you can redistribute it and/or | |
| 7 | -# modify it under the terms of the GNU Lesser General Public License version 3 | |
| 8 | -# as published by the Free Software Foundation. | |
| 9 | -# | |
| 10 | -# This program is distributed in the hope that it will be useful, | |
| 11 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | -# GNU General Public License for more details. | |
| 14 | -# | |
| 15 | -# You should have received a copy of the GNU Lesser General Public License version 3 | |
| 16 | -# along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 17 | -# or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 18 | -# Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | -# ---------------------------------------------------------------------------- | |
| 20 | -# Este arquivo é parte do Framework Demoiselle. | |
| 21 | -# | |
| 22 | -# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 23 | -# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 24 | -# do Software Livre (FSF). | |
| 25 | -# | |
| 26 | -# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 27 | -# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 28 | -# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 29 | -# para maiores detalhes. | |
| 30 | -# | |
| 31 | -# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 32 | -# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 33 | -# ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 34 | -# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
example/security/src/test/java/example/AunthenticationTest.java
| ... | ... | @@ -1,83 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import static junit.framework.Assert.assertFalse; | |
| 40 | -import static junit.framework.Assert.assertTrue; | |
| 41 | - | |
| 42 | -import javax.inject.Inject; | |
| 43 | - | |
| 44 | -import org.junit.Test; | |
| 45 | -import org.junit.runner.RunWith; | |
| 46 | - | |
| 47 | -import br.gov.frameworkdemoiselle.junit.DemoiselleRunner; | |
| 48 | -import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 49 | - | |
| 50 | -@RunWith(DemoiselleRunner.class) | |
| 51 | -public class AunthenticationTest { | |
| 52 | - | |
| 53 | - @Inject | |
| 54 | - private MyCredentials credential; | |
| 55 | - | |
| 56 | - @Inject | |
| 57 | - private SecurityContext securityContext; | |
| 58 | - | |
| 59 | - @Test | |
| 60 | - public void isNotLogged() { | |
| 61 | - assertFalse(securityContext.isLoggedIn()); | |
| 62 | - } | |
| 63 | - | |
| 64 | - @Test | |
| 65 | - public void loginFailed() { | |
| 66 | - credential.setUsername("santos.dumont"); | |
| 67 | - credential.setPassword("123"); | |
| 68 | - securityContext.login(); | |
| 69 | - | |
| 70 | - assertFalse(securityContext.isLoggedIn()); | |
| 71 | - } | |
| 72 | - | |
| 73 | - @Test | |
| 74 | - public void loginSuccessful() { | |
| 75 | - credential.setUsername("santos.dumont"); | |
| 76 | - credential.setPassword("secret"); | |
| 77 | - securityContext.login(); | |
| 78 | - | |
| 79 | - assertTrue(securityContext.isLoggedIn()); | |
| 80 | - | |
| 81 | - securityContext.logout(); | |
| 82 | - } | |
| 83 | -} |
example/security/src/test/java/example/AunthorizationTest.java
| ... | ... | @@ -1,99 +0,0 @@ |
| 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 example; | |
| 38 | - | |
| 39 | -import static junit.framework.Assert.fail; | |
| 40 | - | |
| 41 | -import javax.inject.Inject; | |
| 42 | - | |
| 43 | -import org.junit.After; | |
| 44 | -import org.junit.Before; | |
| 45 | -import org.junit.Test; | |
| 46 | -import org.junit.runner.RunWith; | |
| 47 | - | |
| 48 | -import br.gov.frameworkdemoiselle.junit.DemoiselleRunner; | |
| 49 | -import br.gov.frameworkdemoiselle.security.AuthorizationException; | |
| 50 | -import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 51 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 52 | - | |
| 53 | -@RunWith(DemoiselleRunner.class) | |
| 54 | -public class AunthorizationTest { | |
| 55 | - | |
| 56 | - @Inject | |
| 57 | - private Hello hello; | |
| 58 | - | |
| 59 | - @Before | |
| 60 | - public void before() { | |
| 61 | - MyCredentials myCredentials = Beans.getReference(MyCredentials.class); | |
| 62 | - | |
| 63 | - myCredentials.setUsername("santos.dumont"); | |
| 64 | - myCredentials.setPassword("secret"); | |
| 65 | - myCredentials.addRole("admin"); | |
| 66 | - myCredentials.addRole("jedi"); | |
| 67 | - | |
| 68 | - SecurityContext securityContext = Beans.getReference(SecurityContext.class); | |
| 69 | - securityContext.login(); | |
| 70 | - } | |
| 71 | - | |
| 72 | - @After | |
| 73 | - public void after() { | |
| 74 | - SecurityContext securityContext = Beans.getReference(SecurityContext.class); | |
| 75 | - securityContext.logout(); | |
| 76 | - } | |
| 77 | - | |
| 78 | - @Test | |
| 79 | - public void accessSuccessfulSaying1() { | |
| 80 | - hello.say1(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - @Test(expected = AuthorizationException.class) | |
| 84 | - public void accessFailedSaying2() { | |
| 85 | - hello.say2(); | |
| 86 | - fail(); | |
| 87 | - } | |
| 88 | - | |
| 89 | - @Test | |
| 90 | - public void accessFailedSaying3() { | |
| 91 | - hello.say3(); | |
| 92 | - } | |
| 93 | - | |
| 94 | - @Test(expected = AuthorizationException.class) | |
| 95 | - public void accessFailedSaying4() { | |
| 96 | - hello.say4(); | |
| 97 | - fail(); | |
| 98 | - } | |
| 99 | -} |
example/security/src/test/resources/META-INF/beans.xml
| ... | ... | @@ -1,47 +0,0 @@ |
| 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 | -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
| 38 | - | |
| 39 | - <interceptors> | |
| 40 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.ExceptionHandlerInterceptor</class> | |
| 41 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredPermissionInterceptor</class> | |
| 42 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredRoleInterceptor</class> | |
| 43 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor</class> | |
| 44 | - <class>br.gov.frameworkdemoiselle.internal.interceptor.ConfigurationInterceptor</class> | |
| 45 | - </interceptors> | |
| 46 | - | |
| 47 | -</beans> |
example/security/src/test/resources/log4j.properties
| ... | ... | @@ -1,39 +0,0 @@ |
| 1 | -# Demoiselle Framework | |
| 2 | -# Copyright (C) 2010 SERPRO | |
| 3 | -# ---------------------------------------------------------------------------- | |
| 4 | -# This file is part of Demoiselle Framework. | |
| 5 | -# | |
| 6 | -# Demoiselle Framework is free software; you can redistribute it and/or | |
| 7 | -# modify it under the terms of the GNU Lesser General Public License version 3 | |
| 8 | -# as published by the Free Software Foundation. | |
| 9 | -# | |
| 10 | -# This program is distributed in the hope that it will be useful, | |
| 11 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | -# GNU General Public License for more details. | |
| 14 | -# | |
| 15 | -# You should have received a copy of the GNU Lesser General Public License version 3 | |
| 16 | -# along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 17 | -# or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 18 | -# Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | -# ---------------------------------------------------------------------------- | |
| 20 | -# Este arquivo é parte do Framework Demoiselle. | |
| 21 | -# | |
| 22 | -# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 23 | -# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 24 | -# do Software Livre (FSF). | |
| 25 | -# | |
| 26 | -# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 27 | -# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 28 | -# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 29 | -# para maiores detalhes. | |
| 30 | -# | |
| 31 | -# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 32 | -# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 33 | -# ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 34 | -# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 35 | - | |
| 36 | -log4j.rootLogger=INFO, A1 | |
| 37 | -log4j.appender.A1=org.apache.log4j.ConsoleAppender | |
| 38 | -log4j.appender.A1.layout=org.apache.log4j.PatternLayout | |
| 39 | -log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %c - %m%n |
impl/core/src/main/java/br/gov/frameworkdemoiselle/configuration/ConfigurationException.java
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
| ... | ... | @@ -93,7 +93,7 @@ public class ConfigurationLoader implements Serializable { |
| 93 | 93 | public void load(Object object) throws ConfigurationException { |
| 94 | 94 | Class<?> config = object.getClass(); |
| 95 | 95 | CoreBootstrap bootstrap = Beans.getReference(CoreBootstrap.class); |
| 96 | - | |
| 96 | + | |
| 97 | 97 | if (!bootstrap.isAnnotatedType(config)) { |
| 98 | 98 | config = config.getSuperclass(); |
| 99 | 99 | getLogger().debug(getBundle().getString("proxy-detected", config, config.getClass().getSuperclass())); |
| ... | ... | @@ -112,11 +112,13 @@ public class ConfigurationLoader implements Serializable { |
| 112 | 112 | ConfigType type = clazz.getAnnotation(Configuration.class).type(); |
| 113 | 113 | org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); |
| 114 | 114 | |
| 115 | - String key = getKey(field, clazz, config); | |
| 116 | - Object value = getValue(key, field, config); | |
| 115 | + if (config != null) { | |
| 116 | + String key = getKey(field, clazz, config); | |
| 117 | + Object value = getValue(key, field, config); | |
| 117 | 118 | |
| 118 | - validate(field, key, value, resource); | |
| 119 | - setValue(field, key, object, value); | |
| 119 | + validate(field, key, value, resource); | |
| 120 | + setValue(field, key, object, value); | |
| 121 | + } | |
| 120 | 122 | } |
| 121 | 123 | } |
| 122 | 124 | |
| ... | ... | @@ -223,12 +225,20 @@ public class ConfigurationLoader implements Serializable { |
| 223 | 225 | |
| 224 | 226 | case PROPERTIES: |
| 225 | 227 | url = getResourceAsURL(resource + ".properties"); |
| 226 | - result = new DataConfiguration(new PropertiesConfiguration(url)); | |
| 228 | + | |
| 229 | + if (url != null) { | |
| 230 | + result = new DataConfiguration(new PropertiesConfiguration(url)); | |
| 231 | + } | |
| 232 | + | |
| 227 | 233 | break; |
| 228 | 234 | |
| 229 | 235 | case XML: |
| 230 | 236 | url = getResourceAsURL(resource + ".xml"); |
| 231 | - result = new DataConfiguration(new XMLConfiguration(url)); | |
| 237 | + | |
| 238 | + if (url != null) { | |
| 239 | + result = new DataConfiguration(new XMLConfiguration(url)); | |
| 240 | + } | |
| 241 | + | |
| 232 | 242 | break; |
| 233 | 243 | |
| 234 | 244 | default: |
| ... | ... | @@ -416,7 +426,7 @@ public class ConfigurationLoader implements Serializable { |
| 416 | 426 | |
| 417 | 427 | public static URL getResourceAsURL(final String resource) throws FileNotFoundException { |
| 418 | 428 | ClassLoader classLoader = getClassLoaderForResource(resource); |
| 419 | - return classLoader.getResource(resource); | |
| 429 | + return classLoader != null ? classLoader.getResource(resource) : null; | |
| 420 | 430 | } |
| 421 | 431 | |
| 422 | 432 | private static ResourceBundle getBundle() { | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/SecurityConfig.java
| ... | ... | @@ -1,82 +0,0 @@ |
| 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.configuration; | |
| 38 | - | |
| 39 | -import java.io.Serializable; | |
| 40 | - | |
| 41 | -import br.gov.frameworkdemoiselle.configuration.Configuration; | |
| 42 | -import br.gov.frameworkdemoiselle.internal.implementation.DefaultAuthenticator; | |
| 43 | -import br.gov.frameworkdemoiselle.internal.implementation.DefaultAuthorizer; | |
| 44 | -import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 45 | -import br.gov.frameworkdemoiselle.security.Authorizer; | |
| 46 | - | |
| 47 | -/** | |
| 48 | - * A <code>SecurityConfig</code> object is responsible for specifying which security configurations should be used for a | |
| 49 | - * particular application. | |
| 50 | - * | |
| 51 | - * @author SERPRO | |
| 52 | - */ | |
| 53 | -@Configuration(prefix = "frameworkdemoiselle.security") | |
| 54 | -public class SecurityConfig implements Serializable { | |
| 55 | - | |
| 56 | - private static final long serialVersionUID = 1L; | |
| 57 | - | |
| 58 | - private boolean enabled = true; | |
| 59 | - | |
| 60 | - private Class<? extends Authenticator> authenticatorClass = DefaultAuthenticator.class; | |
| 61 | - | |
| 62 | - private Class<? extends Authorizer> authorizerClass = DefaultAuthorizer.class; | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Tells whether or not the security is enabled for the current application. This value could be defined in the | |
| 66 | - * <b>demoiselle.properties</b> file, using the key <i>frameworkdemoiselle.security.enabled</i>. | |
| 67 | - * | |
| 68 | - * @return the value defined for the key <i>frameworkdemoiselle.security.enabled</i> in the | |
| 69 | - * <b>demoiselle.properties</b> file. If there is no value defined, returns the default value <tt>true</tt> | |
| 70 | - */ | |
| 71 | - public boolean isEnabled() { | |
| 72 | - return enabled; | |
| 73 | - } | |
| 74 | - | |
| 75 | - public Class<? extends Authenticator> getAuthenticatorClass() { | |
| 76 | - return authenticatorClass; | |
| 77 | - } | |
| 78 | - | |
| 79 | - public Class<? extends Authorizer> getAuthorizerClass() { | |
| 80 | - return authorizerClass; | |
| 81 | - } | |
| 82 | -} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/SecurityConfigImpl.java
0 → 100644
| ... | ... | @@ -0,0 +1,110 @@ |
| 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.configuration; | |
| 38 | + | |
| 39 | +import java.io.Serializable; | |
| 40 | + | |
| 41 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | |
| 42 | +import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 43 | +import br.gov.frameworkdemoiselle.security.Authorizer; | |
| 44 | +import br.gov.frameworkdemoiselle.security.SecurityConfig; | |
| 45 | + | |
| 46 | +@Configuration(prefix = "frameworkdemoiselle.security") | |
| 47 | +public class SecurityConfigImpl implements Serializable, SecurityConfig { | |
| 48 | + | |
| 49 | + private static final long serialVersionUID = 1L; | |
| 50 | + | |
| 51 | + private boolean enabled = true; | |
| 52 | + | |
| 53 | + private Class<? extends Authenticator> authenticatorClass; | |
| 54 | + | |
| 55 | + private Class<? extends Authorizer> authorizerClass; | |
| 56 | + | |
| 57 | + /* | |
| 58 | + * (non-Javadoc) | |
| 59 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#isEnabled() | |
| 60 | + */ | |
| 61 | + @Override | |
| 62 | + public boolean isEnabled() { | |
| 63 | + return this.enabled; | |
| 64 | + } | |
| 65 | + | |
| 66 | + /* | |
| 67 | + * (non-Javadoc) | |
| 68 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#setEnabled(boolean) | |
| 69 | + */ | |
| 70 | + @Override | |
| 71 | + public void setEnabled(boolean enabled) { | |
| 72 | + this.enabled = enabled; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /* | |
| 76 | + * (non-Javadoc) | |
| 77 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#getAuthenticatorClass() | |
| 78 | + */ | |
| 79 | + @Override | |
| 80 | + public Class<? extends Authenticator> getAuthenticatorClass() { | |
| 81 | + return this.authenticatorClass; | |
| 82 | + } | |
| 83 | + | |
| 84 | + /* | |
| 85 | + * (non-Javadoc) | |
| 86 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#setAuthenticatorClass(java.lang.Class) | |
| 87 | + */ | |
| 88 | + @Override | |
| 89 | + public void setAuthenticatorClass(Class<? extends Authenticator> authenticatorClass) { | |
| 90 | + this.authenticatorClass = authenticatorClass; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /* | |
| 94 | + * (non-Javadoc) | |
| 95 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#getAuthorizerClass() | |
| 96 | + */ | |
| 97 | + @Override | |
| 98 | + public Class<? extends Authorizer> getAuthorizerClass() { | |
| 99 | + return this.authorizerClass; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /* | |
| 103 | + * (non-Javadoc) | |
| 104 | + * @see br.gov.frameworkdemoiselle.security.SecurityConfig#setAuthorizerClass(java.lang.Class) | |
| 105 | + */ | |
| 106 | + @Override | |
| 107 | + public void setAuthorizerClass(Class<? extends Authorizer> authorizerClass) { | |
| 108 | + this.authorizerClass = authorizerClass; | |
| 109 | + } | |
| 110 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/TransactionConfig.java
| ... | ... | @@ -38,8 +38,8 @@ package br.gov.frameworkdemoiselle.internal.configuration; |
| 38 | 38 | |
| 39 | 39 | import java.io.Serializable; |
| 40 | 40 | |
| 41 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 41 | 42 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
| 42 | -import br.gov.frameworkdemoiselle.internal.implementation.DefaultTransaction; | |
| 43 | 43 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 44 | 44 | |
| 45 | 45 | @Configuration(prefix = "frameworkdemoiselle.transaction") |
| ... | ... | @@ -47,7 +47,8 @@ public class TransactionConfig implements Serializable { |
| 47 | 47 | |
| 48 | 48 | private static final long serialVersionUID = 1L; |
| 49 | 49 | |
| 50 | - private Class<? extends Transaction> transactionClass = DefaultTransaction.class; | |
| 50 | + @Name("class") | |
| 51 | + private Class<? extends Transaction> transactionClass; | |
| 51 | 52 | |
| 52 | 53 | public Class<? extends Transaction> getTransactionClass() { |
| 53 | 54 | return transactionClass; | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultAuthorizer.java
| ... | ... | @@ -41,8 +41,7 @@ import br.gov.frameworkdemoiselle.DemoiselleException; |
| 41 | 41 | import br.gov.frameworkdemoiselle.annotation.Priority; |
| 42 | 42 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| 43 | 43 | import br.gov.frameworkdemoiselle.security.Authorizer; |
| 44 | -import br.gov.frameworkdemoiselle.security.RequiredPermission; | |
| 45 | -import br.gov.frameworkdemoiselle.security.RequiredRole; | |
| 44 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | |
| 46 | 45 | import br.gov.frameworkdemoiselle.util.ResourceBundle; |
| 47 | 46 | |
| 48 | 47 | /** |
| ... | ... | @@ -60,14 +59,17 @@ public class DefaultAuthorizer implements Authorizer { |
| 60 | 59 | |
| 61 | 60 | @Override |
| 62 | 61 | public boolean hasRole(String role) { |
| 63 | - throw new DemoiselleException(getBundle().getString("authorizer-not-defined", | |
| 64 | - RequiredRole.class.getSimpleName())); | |
| 62 | + throw getException(); | |
| 65 | 63 | } |
| 66 | 64 | |
| 67 | 65 | @Override |
| 68 | 66 | public boolean hasPermission(String resource, String operation) { |
| 69 | - throw new DemoiselleException(getBundle().getString("authorizer-not-defined", | |
| 70 | - RequiredPermission.class.getSimpleName())); | |
| 67 | + throw getException(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + private DemoiselleException getException() { | |
| 71 | + return new DemoiselleException(getBundle().getString("authorizer-not-defined", | |
| 72 | + SecurityContext.class.getSimpleName())); | |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | private static ResourceBundle getBundle() { | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java
| ... | ... | @@ -40,13 +40,14 @@ import javax.inject.Named; |
| 40 | 40 | |
| 41 | 41 | import br.gov.frameworkdemoiselle.internal.bootstrap.AuthenticatorBootstrap; |
| 42 | 42 | import br.gov.frameworkdemoiselle.internal.bootstrap.AuthorizerBootstrap; |
| 43 | -import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; | |
| 43 | +import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfigImpl; | |
| 44 | 44 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| 45 | 45 | import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful; |
| 46 | 46 | import br.gov.frameworkdemoiselle.security.AfterLogoutSuccessful; |
| 47 | 47 | import br.gov.frameworkdemoiselle.security.Authenticator; |
| 48 | 48 | import br.gov.frameworkdemoiselle.security.Authorizer; |
| 49 | 49 | import br.gov.frameworkdemoiselle.security.NotLoggedInException; |
| 50 | +import br.gov.frameworkdemoiselle.security.SecurityConfig; | |
| 50 | 51 | import br.gov.frameworkdemoiselle.security.SecurityContext; |
| 51 | 52 | import br.gov.frameworkdemoiselle.security.User; |
| 52 | 53 | import br.gov.frameworkdemoiselle.util.Beans; |
| ... | ... | @@ -70,7 +71,10 @@ public class SecurityContextImpl implements SecurityContext { |
| 70 | 71 | if (this.authenticator == null) { |
| 71 | 72 | AuthenticatorBootstrap bootstrap = Beans.getReference(AuthenticatorBootstrap.class); |
| 72 | 73 | Class<? extends Authenticator> clazz = getConfig().getAuthenticatorClass(); |
| 73 | - clazz = StrategySelector.getClass(clazz, bootstrap.getCache()); | |
| 74 | + | |
| 75 | + if (clazz == null) { | |
| 76 | + clazz = StrategySelector.getClass(Authenticator.class, bootstrap.getCache()); | |
| 77 | + } | |
| 74 | 78 | |
| 75 | 79 | this.authenticator = Beans.getReference(clazz); |
| 76 | 80 | } |
| ... | ... | @@ -82,7 +86,10 @@ public class SecurityContextImpl implements SecurityContext { |
| 82 | 86 | if (this.authorizer == null) { |
| 83 | 87 | AuthorizerBootstrap bootstrap = Beans.getReference(AuthorizerBootstrap.class); |
| 84 | 88 | Class<? extends Authorizer> clazz = getConfig().getAuthorizerClass(); |
| 85 | - clazz = StrategySelector.getClass(clazz, bootstrap.getCache()); | |
| 89 | + | |
| 90 | + if (clazz == null) { | |
| 91 | + clazz = StrategySelector.getClass(Authorizer.class, bootstrap.getCache()); | |
| 92 | + } | |
| 86 | 93 | |
| 87 | 94 | this.authorizer = Beans.getReference(clazz); |
| 88 | 95 | } |
| ... | ... | @@ -192,7 +199,7 @@ public class SecurityContextImpl implements SecurityContext { |
| 192 | 199 | } |
| 193 | 200 | |
| 194 | 201 | private SecurityConfig getConfig() { |
| 195 | - return Beans.getReference(SecurityConfig.class); | |
| 202 | + return Beans.getReference(SecurityConfigImpl.class); | |
| 196 | 203 | } |
| 197 | 204 | |
| 198 | 205 | private void checkLoggedIn() throws NotLoggedInException { | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java
| ... | ... | @@ -39,9 +39,13 @@ package br.gov.frameworkdemoiselle.internal.implementation; |
| 39 | 39 | import static br.gov.frameworkdemoiselle.annotation.Priority.MIN_PRIORITY; |
| 40 | 40 | |
| 41 | 41 | import java.io.Serializable; |
| 42 | +import java.util.ArrayList; | |
| 42 | 43 | import java.util.List; |
| 43 | 44 | |
| 44 | 45 | import br.gov.frameworkdemoiselle.annotation.Priority; |
| 46 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
| 47 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | |
| 48 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
| 45 | 49 | |
| 46 | 50 | public final class StrategySelector implements Serializable { |
| 47 | 51 | |
| ... | ... | @@ -55,51 +59,67 @@ public final class StrategySelector implements Serializable { |
| 55 | 59 | |
| 56 | 60 | private static final long serialVersionUID = 1L; |
| 57 | 61 | |
| 62 | + private static ResourceBundle bundle; | |
| 63 | + | |
| 58 | 64 | private StrategySelector() { |
| 59 | 65 | } |
| 60 | 66 | |
| 61 | - public static <T> Class<? extends T> getClass(Class<? extends T> configClass/* , Class<T> defaultClass */, | |
| 62 | - List<Class<? extends T>> optionalClasses) { | |
| 63 | - Class<? extends T> result = configClass; | |
| 64 | - | |
| 65 | - if (configClass == getDefaultClass(optionalClasses)) { | |
| 66 | - result = getPriorityReference(optionalClasses); | |
| 67 | + private static ResourceBundle getBundle() { | |
| 68 | + if (bundle == null) { | |
| 69 | + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); | |
| 67 | 70 | } |
| 68 | 71 | |
| 69 | - return result; | |
| 72 | + return bundle; | |
| 70 | 73 | } |
| 71 | 74 | |
| 72 | - public static <T> Class<? extends T> getDefaultClass(List<Class<? extends T>> optionalClasses) { | |
| 73 | - Class<? extends T> result = null; | |
| 74 | - | |
| 75 | - for (Class<? extends T> optionalClass : optionalClasses) { | |
| 76 | - Priority priority = optionalClass.getAnnotation(Priority.class); | |
| 75 | + public static <T> Class<? extends T> getClass(Class<T> type, List<Class<? extends T>> options) | |
| 76 | + throws ConfigurationException { | |
| 77 | + Class<? extends T> selected = null; | |
| 77 | 78 | |
| 78 | - if (priority != null && priority.value() == CORE_PRIORITY) { | |
| 79 | - result = optionalClass; | |
| 80 | - break; | |
| 79 | + for (Class<? extends T> option : options) { | |
| 80 | + if (selected == null || getPriority(option) < getPriority(selected)) { | |
| 81 | + selected = option; | |
| 81 | 82 | } |
| 82 | 83 | } |
| 83 | 84 | |
| 84 | - return result; | |
| 85 | + checkForAmbiguity(type, selected, options); | |
| 86 | + | |
| 87 | + return selected; | |
| 85 | 88 | } |
| 86 | 89 | |
| 87 | - /* | |
| 88 | - * public static <T> T getReference(String configKey, Class<T> strategyType, Class<T> defaultType, List<Class<T>> | |
| 89 | - * options) { T result = getExplicitReference(configKey, strategyType, defaultType); if (result.getClass() == | |
| 90 | - * defaultType) { result = getPriorityReference(options); } return result; } | |
| 91 | - */ | |
| 90 | + private static <T> void checkForAmbiguity(Class<T> type, Class<? extends T> selected, | |
| 91 | + List<Class<? extends T>> options) throws ConfigurationException { | |
| 92 | + int selectedPriority = getPriority(selected); | |
| 92 | 93 | |
| 93 | - public static <T> Class<? extends T> getPriorityReference(List<Class<? extends T>> options) { | |
| 94 | - Class<? extends T> selected = null; | |
| 94 | + List<Class<? extends T>> ambiguous = new ArrayList<Class<? extends T>>(); | |
| 95 | 95 | |
| 96 | 96 | for (Class<? extends T> option : options) { |
| 97 | - if (selected == null || getPriority(option) < getPriority(selected)) { | |
| 98 | - selected = option; | |
| 97 | + if (selected != option && selectedPriority == getPriority(option)) { | |
| 98 | + ambiguous.add(option); | |
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - return selected; | |
| 102 | + if (!ambiguous.isEmpty()) { | |
| 103 | + ambiguous.add(selected); | |
| 104 | + | |
| 105 | + String message = getExceptionMessage(type, ambiguous); | |
| 106 | + throw new ConfigurationException(message); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 110 | + private static <T> String getExceptionMessage(Class<T> type, List<Class<? extends T>> ambiguous) { | |
| 111 | + StringBuffer classes = new StringBuffer(); | |
| 112 | + | |
| 113 | + int i = 0; | |
| 114 | + for (Class<? extends T> clazz : ambiguous) { | |
| 115 | + if (i++ != 0) { | |
| 116 | + classes.append(", "); | |
| 117 | + } | |
| 118 | + | |
| 119 | + classes.append(clazz.getCanonicalName()); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return getBundle().getString("ambiguous-strategy-resolution", type.getCanonicalName(), classes.toString()); | |
| 103 | 123 | } |
| 104 | 124 | |
| 105 | 125 | private static <T> int getPriority(Class<T> type) { |
| ... | ... | @@ -113,28 +133,40 @@ public final class StrategySelector implements Serializable { |
| 113 | 133 | return result; |
| 114 | 134 | } |
| 115 | 135 | |
| 116 | - /* | |
| 117 | - * public static <T> T getExplicitReference(String configKey, Class<T> strategyType, Class<T> defaultType) { | |
| 118 | - * Class<T> selectedType = loadSelected(configKey, strategyType, defaultType); return | |
| 119 | - * Beans.getReference(selectedType); } | |
| 120 | - */ | |
| 121 | - | |
| 122 | - /* | |
| 123 | - * @SuppressWarnings("unchecked") private static <T> Class<T> loadSelected(String configKey, Class<T> strategyType, | |
| 124 | - * Class<T> defaultType) { ResourceBundle bundle = ResourceBundleProducer.create("demoiselle-core-bundle", | |
| 125 | - * Beans.getReference(Locale.class)); Class<T> result = null; String canonicalName = null; String typeName = | |
| 126 | - * strategyType.getSimpleName().toLowerCase(); String key = null; try { URL url = | |
| 127 | - * ConfigurationLoader.getResourceAsURL("demoiselle.properties"); Configuration config = new | |
| 128 | - * PropertiesConfiguration(url); canonicalName = config.getString(configKey, defaultType.getCanonicalName()); | |
| 129 | - * ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(canonicalName); if (classLoader == null) { | |
| 130 | - * classLoader = Thread.currentThread().getContextClassLoader(); } result = (Class<T>) Class.forName(canonicalName, | |
| 131 | - * false, classLoader); result.asSubclass(strategyType); } catch | |
| 132 | - * (org.apache.commons.configuration.ConfigurationException cause) { throw new | |
| 133 | - * ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); } catch | |
| 134 | - * (ClassNotFoundException cause) { key = Strings.getString("{0}-class-not-found", typeName); throw new | |
| 135 | - * ConfigurationException(bundle.getString(key, canonicalName)); } catch (FileNotFoundException e) { throw new | |
| 136 | - * ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); } catch (ClassCastException | |
| 137 | - * cause) { key = Strings.getString("{0}-class-must-be-of-type", typeName); throw new | |
| 138 | - * ConfigurationException(bundle.getString(key, canonicalName, strategyType)); } return result; } | |
| 139 | - */ | |
| 136 | + // public static <T> T getExplicitReference(String configKey, Class<T> strategyType, Class<T> defaultType) { | |
| 137 | + // Class<T> selectedType = loadSelected(configKey, strategyType, defaultType); | |
| 138 | + // return Beans.getReference(selectedType); | |
| 139 | + // } | |
| 140 | + // | |
| 141 | + // @SuppressWarnings("unchecked") | |
| 142 | + // private static <T> Class<T> loadSelected(String configKey, Class<T> strategyType, Class<T> defaultType) { | |
| 143 | + // ResourceBundle bundle = ResourceBundleProducer.create("demoiselle-core-bundle", | |
| 144 | + // Beans.getReference(Locale.class)); | |
| 145 | + // Class<T> result = null; | |
| 146 | + // String canonicalName = null; | |
| 147 | + // String typeName = strategyType.getSimpleName().toLowerCase(); | |
| 148 | + // String key = null; | |
| 149 | + // try { | |
| 150 | + // URL url = ConfigurationLoader.getResourceAsURL("demoiselle.properties"); | |
| 151 | + // Configuration config = new PropertiesConfiguration(url); | |
| 152 | + // canonicalName = config.getString(configKey, defaultType.getCanonicalName()); | |
| 153 | + // ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(canonicalName); | |
| 154 | + // if (classLoader == null) { | |
| 155 | + // classLoader = Thread.currentThread().getContextClassLoader(); | |
| 156 | + // } | |
| 157 | + // result = (Class<T>) Class.forName(canonicalName, false, classLoader); | |
| 158 | + // result.asSubclass(strategyType); | |
| 159 | + // } catch (org.apache.commons.configuration.ConfigurationException cause) { | |
| 160 | + // throw new ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); | |
| 161 | + // } catch (ClassNotFoundException cause) { | |
| 162 | + // key = Strings.getString("{0}-class-not-found", typeName); | |
| 163 | + // throw new ConfigurationException(bundle.getString(key, canonicalName)); | |
| 164 | + // } catch (FileNotFoundException e) { | |
| 165 | + // throw new ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); | |
| 166 | + // } catch (ClassCastException cause) { | |
| 167 | + // key = Strings.getString("{0}-class-must-be-of-type", typeName); | |
| 168 | + // throw new ConfigurationException(bundle.getString(key, canonicalName, strategyType)); | |
| 169 | + // } | |
| 170 | + // return result; | |
| 171 | + // } | |
| 140 | 172 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/TransactionContextImpl.java
| ... | ... | @@ -60,7 +60,10 @@ public class TransactionContextImpl implements TransactionContext { |
| 60 | 60 | if (this.transaction == null) { |
| 61 | 61 | TransactionBootstrap bootstrap = Beans.getReference(TransactionBootstrap.class); |
| 62 | 62 | Class<? extends Transaction> clazz = getConfig().getTransactionClass(); |
| 63 | - clazz = StrategySelector.getClass(clazz, bootstrap.getCache()); | |
| 63 | + | |
| 64 | + if (clazz == null) { | |
| 65 | + clazz = StrategySelector.getClass(Transaction.class, bootstrap.getCache()); | |
| 66 | + } | |
| 64 | 67 | |
| 65 | 68 | this.transaction = Beans.getReference(clazz); |
| 66 | 69 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/SecurityConfig.java
0 → 100644
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package br.gov.frameworkdemoiselle.security; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * A <code>SecurityConfig</code> object is responsible for specifying which security configurations should be used for a | |
| 5 | + * particular application. | |
| 6 | + * | |
| 7 | + * @author SERPRO | |
| 8 | + */ | |
| 9 | +public interface SecurityConfig { | |
| 10 | + | |
| 11 | + /** | |
| 12 | + * Tells whether or not the security is enabled for the current application. This value could be defined in the | |
| 13 | + * <b>demoiselle.properties</b> file, using the key <i>frameworkdemoiselle.security.enabled</i>. | |
| 14 | + * | |
| 15 | + * @return the value defined for the key <i>frameworkdemoiselle.security.enabled</i> in the | |
| 16 | + * <b>demoiselle.properties</b> file. If there is no value defined, returns the default value <tt>true</tt> | |
| 17 | + */ | |
| 18 | + boolean isEnabled(); | |
| 19 | + | |
| 20 | + void setEnabled(boolean enabled); | |
| 21 | + | |
| 22 | + Class<? extends Authenticator> getAuthenticatorClass(); | |
| 23 | + | |
| 24 | + void setAuthenticatorClass(Class<? extends Authenticator> authenticatorClass); | |
| 25 | + | |
| 26 | + Class<? extends Authorizer> getAuthorizerClass(); | |
| 27 | + | |
| 28 | + void setAuthorizerClass(Class<? extends Authorizer> authorizerClass); | |
| 29 | +} | ... | ... |
impl/core/src/main/resources/demoiselle-core-bundle.properties
| ... | ... | @@ -34,6 +34,7 @@ |
| 34 | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 35 | 35 | |
| 36 | 36 | engine-on=Ligando os motores do Demoiselle ${project.version} |
| 37 | +ambiguous-strategy-resolution=Foi detectada ambiguidade da interface "{0}" com as seguintes implementa\u00E7\u00F5es\: "{1}". Para resolver o conflito, defina explicitamente a implementa\u00E7\u00E3o no demoiselle.properties. | |
| 37 | 38 | bean-not-found=Voc\u00EA est\u00E1 tentando obter um objeto n\u00E3o reconhecido pelo CDI via Beans.getReference({0}) |
| 38 | 39 | more-than-one-exceptionhandler-defined-for-same-class=Foi definido mais de um m\u00E9todo na classe {0} para tratar a exce\u00E7\u00E3o {1} |
| 39 | 40 | handling-exception=Tratando a exce\u00E7\u00E3o {0} |
| ... | ... | @@ -71,8 +72,6 @@ error-creating-new-instance-for=Error creating a new instance for "{0}" |
| 71 | 72 | executed-successfully=\ {0} execultado com sucesso |
| 72 | 73 | must-declare-one-single-parameter=Voc\u00EA deve declarar um par\u00E2metro \u00FAnico em {0} |
| 73 | 74 | loading-default-transaction-manager=Carregando o gerenciador de transa\u00E7\u00E3o padr\u00E3o {0} |
| 74 | -transaction-class-not-found=A classe de transa\u00E7\u00E3o "{0}" informada no demoiselle.properties n\u00E3o foi encontrada no classpath. | |
| 75 | -transaction-class-must-be-of-type=A classe de transa\u00E7\u00E3o "{0}" informada no demoiselle.properties deve ser do tipo "{1}" | |
| 76 | 75 | results-count-greater-page-size=Quantidade de resultados {0} \u00E9 maior que o tamanho da p\u00E1gina {1} |
| 77 | 76 | page-result=Resultado paginado [p\u00E1gina\={0}, total de resultados\={1}] |
| 78 | 77 | page=P\u00E1gina [n\u00FAmero\={0}, tamanho\={1}] |
| ... | ... | @@ -88,8 +87,6 @@ access-allowed=O usu\u00E1rio "{0}" acessou o recurso "{2}" com a a\u00E7\u00E3o |
| 88 | 87 | access-denied=O usu\u00E1rio "{0}" n\u00E3o possui permiss\u00E3o para executar a a\u00E7\u00E3o "{1}" no recurso "{2}" |
| 89 | 88 | access-denied-ui=Voc\u00EA n\u00E3o est\u00E1 autorizado a executar a a\u00E7\u00E3o {1} no recurso {0} |
| 90 | 89 | authorizer-not-defined=Nenhuma regra de resolu\u00E7\u00E3o de permiss\u00F5es foi definida. Para utilizar @{0} \u00E9 preciso definir a propriedade frameworkdemoiselle.security.authorizer.class como regra de resolu\u00E7\u00E3o de permiss\u00F5es desejada no arquivo demoiselle.properties. |
| 91 | -authorizer-class-not-found=A classe de resolu\u00E7\u00E3o de permiss\u00F5es "{0}" informada no demoiselle.properties n\u00E3o foi encontrada no classpath. | |
| 92 | -authorizer-class-must-be-of-type=A classe de resolu\u00E7\u00E3o de permiss\u00F5es "{0}" informada no demoiselle.properties deve ser do tipo "{1}" | |
| 93 | 90 | user-not-authenticated=Usu\u00E1rio n\u00E3o autenticado |
| 94 | 91 | has-role-verification=Verificando se o usu\u00E1rio {0} possui a(s) role(s)\: {1} |
| 95 | 92 | does-not-have-role=Usu\u00E1rio {0} n\u00E3o possui a(s) role(s)\: {1} |
| ... | ... | @@ -97,5 +94,3 @@ does-not-have-role-ui=Para acessar este recurso \u00E9 necess\u00E1rio ser {0} |
| 97 | 94 | user-has-role=Usu\u00E1rio {0} possui a(s) role(s)\: {1} |
| 98 | 95 | |
| 99 | 96 | authenticator-not-defined=Nenhum mecanismo de autentica\u00E7\u00E3o foi definido. Para utilizar {0} \u00E9 preciso definir a propriedade frameworkdemoiselle.security.authenticator.class como mecanismo de autentica\u00E7\u00E3o desejado no arquivo demoiselle.properties. |
| 100 | -authenticator-class-not-found=A classe de autentica\u00E7\u00E3o "{0}" informada no demoiselle.properties n\u00E3o foi encontrada no classpath. | |
| 101 | -authenticator-class-must-be-of-type=A classe de autentica\u00E7\u00E3o "{0}" informada no demoiselle.properties deve ser do tipo "{1}" | |
| 102 | 97 | \ No newline at end of file | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderTest.java
| ... | ... | @@ -44,6 +44,7 @@ import static org.junit.Assert.fail; |
| 44 | 44 | import static org.powermock.api.easymock.PowerMock.mockStatic; |
| 45 | 45 | |
| 46 | 46 | import java.util.Locale; |
| 47 | +import java.util.Properties; | |
| 47 | 48 | |
| 48 | 49 | import javax.validation.constraints.NotNull; |
| 49 | 50 | |
| ... | ... | @@ -72,6 +73,20 @@ public class ConfigurationLoaderTest { |
| 72 | 73 | private CoreBootstrap coreBootstrap; |
| 73 | 74 | |
| 74 | 75 | @Configuration |
| 76 | + public class ConfigurationSuccessfulPropertiesWithClassField { | |
| 77 | + | |
| 78 | + protected Class classe; | |
| 79 | + | |
| 80 | + } | |
| 81 | + | |
| 82 | + @Configuration | |
| 83 | + public class ConfigurationSuccessfulPropertiesWithPropertiesField { | |
| 84 | + | |
| 85 | + protected Properties properties; | |
| 86 | + | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Configuration | |
| 75 | 90 | public class ConfigurationSuccessfulProperties { |
| 76 | 91 | |
| 77 | 92 | @Name("frameworkdemoiselle.configurationtest.nameConfiguration") |
| ... | ... | @@ -295,6 +310,27 @@ public class ConfigurationLoaderTest { |
| 295 | 310 | } |
| 296 | 311 | |
| 297 | 312 | @Test |
| 313 | + public void testConfigurationSuccessfulPropertiesWithClassField() { | |
| 314 | + ConfigurationSuccessfulPropertiesWithClassField config = new ConfigurationSuccessfulPropertiesWithClassField(); | |
| 315 | + expect(coreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | |
| 316 | + PowerMock.replayAll(CoreBootstrap.class,Beans.class); | |
| 317 | + configurationLoader.load(config); | |
| 318 | + assertEquals(ConfigurationLoaderTest.class,config.classe); | |
| 319 | + } | |
| 320 | + | |
| 321 | + @Test | |
| 322 | + public void testConfigurationSuccessfulPropertiesWithPropertiesField() { | |
| 323 | + ConfigurationSuccessfulPropertiesWithPropertiesField config = new ConfigurationSuccessfulPropertiesWithPropertiesField(); | |
| 324 | + expect(coreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | |
| 325 | + PowerMock.replayAll(CoreBootstrap.class,Beans.class); | |
| 326 | + configurationLoader.load(config); | |
| 327 | + assertEquals("teste1",config.properties.getProperty("1")); | |
| 328 | + assertEquals("teste2",config.properties.getProperty("2")); | |
| 329 | + assertTrue(config.properties.containsKey("1")); | |
| 330 | + assertTrue(config.properties.containsKey("2")); | |
| 331 | + } | |
| 332 | + | |
| 333 | + @Test | |
| 298 | 334 | public void testConfigurationSuccessfulPropertiesPossibleConventions() { |
| 299 | 335 | ConfigurationSuccessfulProperties config = new ConfigurationSuccessfulProperties(); |
| 300 | 336 | expect(coreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/SecurityConfigTest.java
| ... | ... | @@ -5,13 +5,16 @@ import static org.junit.Assert.assertEquals; |
| 5 | 5 | import org.junit.Before; |
| 6 | 6 | import org.junit.Test; |
| 7 | 7 | |
| 8 | +import br.gov.frameworkdemoiselle.security.SecurityConfig; | |
| 9 | + | |
| 10 | + | |
| 8 | 11 | public class SecurityConfigTest { |
| 9 | 12 | |
| 10 | 13 | private SecurityConfig config; |
| 11 | 14 | |
| 12 | 15 | @Before |
| 13 | 16 | public void setUp() throws Exception { |
| 14 | - this.config = new SecurityConfig(); | |
| 17 | + this.config = new SecurityConfigImpl(); | |
| 15 | 18 | } |
| 16 | 19 | |
| 17 | 20 | @Test | ... | ... |
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 | +} | ... | ... |
impl/core/src/test/resources/demoiselle.properties
| ... | ... | @@ -75,4 +75,7 @@ convention_underline=Convention Underline |
| 75 | 75 | convention.dot=Convention Dot |
| 76 | 76 | conventionalllowercase=All LowerCase |
| 77 | 77 | CONVENTIONALLUPPERCASE=ALL UPPERCASE |
| 78 | -complexObject=null | |
| 79 | 78 | \ No newline at end of file |
| 79 | +complexObject=null | |
| 80 | +properties.1=teste1 | |
| 81 | +properties.2=teste2 | |
| 82 | +classe=br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoaderTest | |
| 80 | 83 | \ No newline at end of file | ... | ... |