Commit 74ae7e0c964056214685af582e61a20c63fed77b
1 parent
79d89477
Exists in
master
Implementação de testes de bootstrap e notification do módulo de
gerenciamento e monitoração (antigo demoiselle-management).
Showing
14 changed files
with
453 additions
and
200 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ManagementBootstrap.java
| ... | ... | @@ -11,12 +11,12 @@ import javax.enterprise.inject.spi.AfterDeploymentValidation; |
| 11 | 11 | import javax.enterprise.inject.spi.AnnotatedType; |
| 12 | 12 | import javax.enterprise.inject.spi.Bean; |
| 13 | 13 | import javax.enterprise.inject.spi.BeanManager; |
| 14 | +import javax.enterprise.inject.spi.BeforeShutdown; | |
| 14 | 15 | import javax.enterprise.inject.spi.Extension; |
| 15 | 16 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 16 | 17 | |
| 17 | 18 | import br.gov.frameworkdemoiselle.internal.context.ContextManager; |
| 18 | 19 | import br.gov.frameworkdemoiselle.internal.context.ManagedContext; |
| 19 | -import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | |
| 20 | 20 | import br.gov.frameworkdemoiselle.management.annotation.Managed; |
| 21 | 21 | import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; |
| 22 | 22 | import br.gov.frameworkdemoiselle.management.internal.ManagedType; |
| ... | ... | @@ -60,11 +60,13 @@ public class ManagementBootstrap implements Extension { |
| 60 | 60 | monitoringManager.initialize(managementExtensionCache); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - public void unregisterAvailableManagedTypes(@Observes final AfterShutdownProccess event) { | |
| 63 | + public void unregisterAvailableManagedTypes(@Observes final BeforeShutdown event) { | |
| 64 | 64 | |
| 65 | 65 | MonitoringManager manager = Beans.getReference(MonitoringManager.class); |
| 66 | 66 | manager.shutdown(managementExtensionCache); |
| 67 | - | |
| 67 | + | |
| 68 | + managementExtensionCache.clear(); | |
| 69 | + types.clear(); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/MonitoringManager.java
| ... | ... | @@ -13,6 +13,7 @@ import javax.management.ReflectionException; |
| 13 | 13 | import org.slf4j.Logger; |
| 14 | 14 | |
| 15 | 15 | import br.gov.frameworkdemoiselle.DemoiselleException; |
| 16 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 16 | 17 | import br.gov.frameworkdemoiselle.internal.context.ContextManager; |
| 17 | 18 | import br.gov.frameworkdemoiselle.internal.context.ManagedContext; |
| 18 | 19 | import br.gov.frameworkdemoiselle.management.annotation.Managed; |
| ... | ... | @@ -38,12 +39,14 @@ public class MonitoringManager { |
| 38 | 39 | private Logger logger; |
| 39 | 40 | |
| 40 | 41 | @Inject |
| 42 | + @Name("demoiselle-core-bundle") | |
| 41 | 43 | private ResourceBundle bundle; |
| 42 | 44 | |
| 43 | 45 | private final List<ManagedType> managedTypes = new ArrayList<ManagedType>(); |
| 44 | 46 | |
| 45 | 47 | public void addManagedType(ManagedType managedType) { |
| 46 | 48 | managedTypes.add(managedType); |
| 49 | + logger.debug(bundle.getString("management-debug-registering-managed-type",managedType.getType().getCanonicalName())); | |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | /** |
| ... | ... | @@ -189,8 +192,13 @@ public class MonitoringManager { |
| 189 | 192 | //Manda uma notificação de mudança de atributo |
| 190 | 193 | NotificationManager notificationManager = Beans.getReference(NotificationManager.class); |
| 191 | 194 | Class<? extends Object> attributeType = newValue!=null ? newValue.getClass() : null; |
| 192 | - AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString(""), propertyName, attributeType, oldValue, newValue); | |
| 193 | - notificationManager.sendAttributeChangedMessage(notification); | |
| 195 | + | |
| 196 | + AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString("management-notification-attribute-changed",propertyName,managedType.getType().getCanonicalName()) | |
| 197 | + , propertyName | |
| 198 | + , attributeType | |
| 199 | + , oldValue | |
| 200 | + , newValue); | |
| 201 | + notificationManager.sendNotification(notification); | |
| 194 | 202 | |
| 195 | 203 | } catch (Exception e) { |
| 196 | 204 | throw new DemoiselleException(bundle.getString( |
| ... | ... | @@ -233,6 +241,8 @@ public class MonitoringManager { |
| 233 | 241 | ManagementExtension monitoringExtension = Beans.getReference(monitoringExtensionClass); |
| 234 | 242 | |
| 235 | 243 | monitoringExtension.shutdown(this.getManagedTypes()); |
| 244 | + | |
| 245 | + logger.debug( bundle.getString("management-debug-removing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); | |
| 236 | 246 | |
| 237 | 247 | } |
| 238 | 248 | |
| ... | ... | @@ -246,6 +256,8 @@ public class MonitoringManager { |
| 246 | 256 | .getReference(monitoringExtensionClass); |
| 247 | 257 | |
| 248 | 258 | monitoringExtension.initialize(this.getManagedTypes()); |
| 259 | + | |
| 260 | + logger.debug( bundle.getString("management-debug-processing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); | |
| 249 | 261 | |
| 250 | 262 | } |
| 251 | 263 | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/NotificationManager.java
| ... | ... | @@ -5,6 +5,7 @@ import java.io.Serializable; |
| 5 | 5 | import javax.enterprise.context.ApplicationScoped; |
| 6 | 6 | import javax.enterprise.event.Event; |
| 7 | 7 | import javax.enterprise.event.Observes; |
| 8 | +import javax.enterprise.util.AnnotationLiteral; | |
| 8 | 9 | import javax.inject.Inject; |
| 9 | 10 | |
| 10 | 11 | import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; |
| ... | ... | @@ -48,16 +49,32 @@ public class NotificationManager implements Serializable{ |
| 48 | 49 | * @param notification The notification to send |
| 49 | 50 | */ |
| 50 | 51 | public void sendNotification(Notification notification) { |
| 51 | - genericNotificationEvent.fire(new NotificationEvent(notification)); | |
| 52 | + if (! AttributeChangeNotification.class.isInstance(notification) ){ | |
| 53 | + getGenericNotificationEvent().fire(new NotificationEvent(notification)); | |
| 54 | + } | |
| 55 | + else{ | |
| 56 | + getAttributeChangeNotificationEvent().fire(new NotificationEvent(notification)); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + @SuppressWarnings("unchecked") | |
| 61 | + private Event<NotificationEvent> getGenericNotificationEvent() { | |
| 62 | + if (genericNotificationEvent==null){ | |
| 63 | + genericNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral<Generic>() {}); | |
| 64 | + } | |
| 65 | + | |
| 66 | + return genericNotificationEvent; | |
| 52 | 67 | } |
| 53 | 68 | |
| 54 | - /** | |
| 55 | - * Sends a notification comunicating about a change of value for an attribute. | |
| 56 | - * | |
| 57 | - * @param notification Special notification communicating a change of value in an attribute. | |
| 58 | - * | |
| 59 | - */ | |
| 60 | - public void sendAttributeChangedMessage(AttributeChangeNotification notification){ | |
| 61 | - attributeChangeNotificationEvent.fire(new NotificationEvent(notification)); | |
| 69 | + @SuppressWarnings("unchecked") | |
| 70 | + private Event<NotificationEvent> getAttributeChangeNotificationEvent() { | |
| 71 | + if (attributeChangeNotificationEvent==null){ | |
| 72 | + attributeChangeNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral<AttributeChange>() {}); | |
| 73 | + } | |
| 74 | + | |
| 75 | + return attributeChangeNotificationEvent; | |
| 62 | 76 | } |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 63 | 80 | } | ... | ... |
impl/core/src/main/resources/demoiselle-core-bundle.properties
| ... | ... | @@ -98,15 +98,19 @@ user-has-role=Usu\u00E1rio {0} possui a(s) role(s)\: {1} |
| 98 | 98 | |
| 99 | 99 | 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 | 100 | |
| 101 | -management-null-class-defined=A classe gerenciada informada n\u00E3o pode ser nula. | |
| 102 | -management-no-annotation-found=Classe {0} precisa ser anotada com @Managed. | |
| 103 | -management-invalid-property-no-getter-setter=Falha ao inicializar classe gerenciada {0}, n\u00E3o foi encontrado um m\u00E9todo get ou m\u00E9todo set para a propriedade {1}. | |
| 104 | -management-invalid-property-as-operation=Falha ao inicializar classe gerenciada {0}, n\u00E3o \u00E9 poss\u00EDvel declarar uma propriedade cujo m\u00E9todo get ou set \u00E9 uma opera\u00E7\u00E3o. | |
| 105 | -management-introspection-error=Erro ao ler atributos da classe gerenciada {0}. | |
| 106 | -management-type-not-found=A classe gerenciada informada n\u00E3o existe: {0}. | |
| 107 | -management-invoke-error=Erro ao tentar invocar a opera\u00E7\u00E3o "{0}" da classe gerenciada, a opera\u00E7\u00E3o n\u00E3o foi encontrada. | |
| 108 | -management-debug-acessing-property=Acessando propriedade {0} da classe gerenciada {1}. | |
| 109 | -management-debug-setting-property=Definindo novo valor para propriedade {0} da classe gerenciada {1}. | |
| 110 | -management-debug-invoking-operation=Invocando opera\u00E7\u00E3o {0} da classe gerenciada {1}. | |
| 111 | -management-debug-starting-custom-context=Levantando contexto {0} para executar comando na classe gerenciada {1}. | |
| 112 | -management-debug-stoping-custom-context=Desligando contexto {0} para classe gerenciada {1}. | |
| 101 | +management-notification-attribute-changed=O atributo [{0}] da classe gerenciada [{1}] foi alterado | |
| 102 | +management-null-class-defined=A classe gerenciada informada n\u00E3o pode ser nula | |
| 103 | +management-no-annotation-found=Classe {0} precisa ser anotada com @Managed | |
| 104 | +management-invalid-property-no-getter-setter=Falha ao inicializar classe gerenciada {0}, n\u00E3o foi encontrado um m\u00E9todo get ou m\u00E9todo set para a propriedade {1} | |
| 105 | +management-invalid-property-as-operation=Falha ao inicializar classe gerenciada {0}, n\u00E3o \u00E9 poss\u00EDvel declarar uma propriedade cujo m\u00E9todo get ou set \u00E9 uma opera\u00E7\u00E3o | |
| 106 | +management-introspection-error=Erro ao ler atributos da classe gerenciada {0} | |
| 107 | +management-type-not-found=A classe gerenciada informada n\u00E3o existe\: {0} | |
| 108 | +management-invoke-error=Erro ao tentar invocar a opera\u00E7\u00E3o "{0}" da classe gerenciada, a opera\u00E7\u00E3o n\u00E3o foi encontrada | |
| 109 | +management-debug-acessing-property=Acessando propriedade {0} da classe gerenciada {1} | |
| 110 | +management-debug-setting-property=Definindo novo valor para propriedade {0} da classe gerenciada {1} | |
| 111 | +management-debug-invoking-operation=Invocando opera\u00E7\u00E3o {0} da classe gerenciada {1} | |
| 112 | +management-debug-starting-custom-context=Levantando contexto {0} para executar comando na classe gerenciada {1} | |
| 113 | +management-debug-stoping-custom-context=Desligando contexto {0} para classe gerenciada {1} | |
| 114 | +management-debug-registering-managed-type=Registrando classe gerenciada [{0}] | |
| 115 | +management-debug-processing-management-extension=Processando extens\u00E3o de gerenciamento [{0}] | |
| 116 | +management-debug-removing-management-extension=Desativando extens\u00E3o de gerenciamento [{0}] | |
| 113 | 117 | \ No newline at end of file | ... | ... |
impl/core/src/test/java/management/DummyManagedClass.java
| ... | ... | @@ -1,74 +0,0 @@ |
| 1 | -package management; | |
| 2 | - | |
| 3 | -import java.util.UUID; | |
| 4 | - | |
| 5 | -import br.gov.frameworkdemoiselle.management.annotation.Managed; | |
| 6 | -import br.gov.frameworkdemoiselle.management.annotation.Operation; | |
| 7 | -import br.gov.frameworkdemoiselle.management.annotation.Property; | |
| 8 | -import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues; | |
| 9 | -import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues.ValueType; | |
| 10 | - | |
| 11 | -@Managed | |
| 12 | -public class DummyManagedClass { | |
| 13 | - | |
| 14 | - @Property | |
| 15 | - @AllowedValues(allows={"f","m","F","M"},valueType=ValueType.INTEGER) | |
| 16 | - private Integer id; | |
| 17 | - | |
| 18 | - @Property | |
| 19 | - private String uuid; | |
| 20 | - | |
| 21 | - @Property | |
| 22 | - private String writeOnlyProperty; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas uma letra. | |
| 26 | - */ | |
| 27 | - @Property | |
| 28 | - private Integer a; | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas letras maiúsculas. | |
| 32 | - */ | |
| 33 | - @Property | |
| 34 | - private Integer MAIUSCULO; | |
| 35 | - | |
| 36 | - public Integer getId() { | |
| 37 | - return id; | |
| 38 | - } | |
| 39 | - | |
| 40 | - public void setId(Integer id) { | |
| 41 | - this.id = id; | |
| 42 | - } | |
| 43 | - | |
| 44 | - public String getUuid() { | |
| 45 | - return uuid; | |
| 46 | - } | |
| 47 | - | |
| 48 | - public void setWriteOnlyProperty(String newValue){ | |
| 49 | - this.writeOnlyProperty = newValue; | |
| 50 | - } | |
| 51 | - | |
| 52 | - public Integer getA() { | |
| 53 | - return a; | |
| 54 | - } | |
| 55 | - | |
| 56 | - public void setA(Integer a) { | |
| 57 | - this.a = a; | |
| 58 | - } | |
| 59 | - | |
| 60 | - public Integer getMAIUSCULO() { | |
| 61 | - return MAIUSCULO; | |
| 62 | - } | |
| 63 | - | |
| 64 | - | |
| 65 | - public void setMAIUSCULO(Integer mAIUSCULO) { | |
| 66 | - MAIUSCULO = mAIUSCULO; | |
| 67 | - } | |
| 68 | - | |
| 69 | - @Operation(description="Generates a random UUID") | |
| 70 | - public String generateUUID(){ | |
| 71 | - this.uuid = UUID.randomUUID().toString(); | |
| 72 | - return this.uuid; | |
| 73 | - } | |
| 74 | -} |
impl/core/src/test/java/management/DummyManagementExtension.java
| ... | ... | @@ -1,29 +0,0 @@ |
| 1 | -package management; | |
| 2 | - | |
| 3 | -import java.util.List; | |
| 4 | - | |
| 5 | -import javax.inject.Inject; | |
| 6 | - | |
| 7 | -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; | |
| 8 | -import br.gov.frameworkdemoiselle.management.internal.ManagedType; | |
| 9 | - | |
| 10 | -public class DummyManagementExtension implements ManagementExtension { | |
| 11 | - | |
| 12 | - @Inject | |
| 13 | - private ManagedClassStore store; | |
| 14 | - | |
| 15 | - @Override | |
| 16 | - public void initialize(List<ManagedType> managedTypes) { | |
| 17 | - // Armazena os beans managed detectados neste store, | |
| 18 | - // para depois serem testados. | |
| 19 | - store.setManagedTypes(managedTypes); | |
| 20 | - } | |
| 21 | - | |
| 22 | - @Override | |
| 23 | - public void shutdown(List<ManagedType> managedTypes) { | |
| 24 | - // Limpa o store, depois o teste verificará se | |
| 25 | - // o processo de shutdown rodou e limpou o store. | |
| 26 | - store.setManagedTypes(null); | |
| 27 | - } | |
| 28 | - | |
| 29 | -} |
impl/core/src/test/java/management/ManagedClassStore.java
| 1 | 1 | package management; |
| 2 | 2 | |
| 3 | +import java.util.ArrayList; | |
| 4 | +import java.util.Collection; | |
| 3 | 5 | import java.util.List; |
| 4 | 6 | |
| 5 | 7 | import javax.enterprise.context.ApplicationScoped; |
| ... | ... | @@ -9,15 +11,15 @@ import br.gov.frameworkdemoiselle.management.internal.ManagedType; |
| 9 | 11 | @ApplicationScoped |
| 10 | 12 | public class ManagedClassStore { |
| 11 | 13 | |
| 12 | - private List<ManagedType> managedTypes = null; | |
| 14 | + private List<ManagedType> managedTypes = new ArrayList<ManagedType>(); | |
| 13 | 15 | |
| 14 | 16 | |
| 15 | 17 | public List<ManagedType> getManagedTypes() { |
| 16 | 18 | return managedTypes; |
| 17 | 19 | } |
| 18 | 20 | |
| 19 | - public void setManagedTypes(List<ManagedType> managedTypes) { | |
| 20 | - this.managedTypes = managedTypes; | |
| 21 | + public void addManagedTypes(Collection<ManagedType> managedTypes){ | |
| 22 | + this.managedTypes.addAll(managedTypes); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | 25 | } | ... | ... |
impl/core/src/test/java/management/ManagementBootstrapTest.java
| ... | ... | @@ -1,68 +0,0 @@ |
| 1 | -package management; | |
| 2 | - | |
| 3 | -import javax.inject.Inject; | |
| 4 | - | |
| 5 | -import org.jboss.arquillian.container.test.api.Deployment; | |
| 6 | -import org.jboss.arquillian.junit.Arquillian; | |
| 7 | -import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
| 8 | -import org.junit.Assert; | |
| 9 | -import org.junit.Test; | |
| 10 | -import org.junit.runner.RunWith; | |
| 11 | - | |
| 12 | -import test.Tests; | |
| 13 | -import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | |
| 14 | -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; | |
| 15 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 16 | - | |
| 17 | - | |
| 18 | -@RunWith(Arquillian.class) | |
| 19 | -public class ManagementBootstrapTest { | |
| 20 | - | |
| 21 | - @Inject | |
| 22 | - private ManagedClassStore store; | |
| 23 | - | |
| 24 | - @Deployment | |
| 25 | - public static JavaArchive createDeployment() { | |
| 26 | - JavaArchive deployment = Tests.createDeployment(ManagementBootstrapTest.class); | |
| 27 | - | |
| 28 | - /*deployment | |
| 29 | - .addClass(ManagedClassStore.class) | |
| 30 | - .addClass(DummyManagedClass.class) | |
| 31 | - .addClass(DummyManagementExtension.class);*/ | |
| 32 | - | |
| 33 | - return deployment; | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Test if a a management extension (a library that implements {@link ManagementExtension}) is correctly detected. | |
| 38 | - */ | |
| 39 | - @Test | |
| 40 | - public void testManagementExtensionRegistration(){ | |
| 41 | - | |
| 42 | - //"store" é application scoped e é usado pelo DummyManagementExtension para | |
| 43 | - //armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, | |
| 44 | - //ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. | |
| 45 | - Assert.assertNotNull(store.getManagedTypes()); | |
| 46 | - Assert.assertEquals(1, store.getManagedTypes().size()); | |
| 47 | - | |
| 48 | - } | |
| 49 | - | |
| 50 | - /** | |
| 51 | - * Test if a a management extension's (a library that implements {@link ManagementExtension}) shutdown | |
| 52 | - * method is correctly called upon application shutdown. | |
| 53 | - */ | |
| 54 | - @Test | |
| 55 | - public void testManagementExtensionShutdown(){ | |
| 56 | - | |
| 57 | - //"store" é application scoped e é usado pelo DummyManagementExtension para | |
| 58 | - //armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, | |
| 59 | - //ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. | |
| 60 | - //Nós então disparamos o evento de shutdown onde ele deverá limpar o store. | |
| 61 | - Assert.assertNotNull(store.getManagedTypes()); | |
| 62 | - Assert.assertEquals(1, store.getManagedTypes().size()); | |
| 63 | - | |
| 64 | - Beans.getBeanManager().fireEvent(new AfterShutdownProccess() {}); | |
| 65 | - Assert.assertNull(store.getManagedTypes()); | |
| 66 | - } | |
| 67 | - | |
| 68 | -} |
impl/core/src/test/java/management/ManagementBootstrapTestCase.java
0 → 100644
| ... | ... | @@ -0,0 +1,124 @@ |
| 1 | +package management; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +import management.testclasses.DummyManagedClass; | |
| 7 | +import management.testclasses.DummyManagedClassPropertyError; | |
| 8 | +import management.testclasses.DummyManagementExtension; | |
| 9 | + | |
| 10 | +import org.jboss.arquillian.container.test.api.Deployer; | |
| 11 | +import org.jboss.arquillian.container.test.api.Deployment; | |
| 12 | +import org.jboss.arquillian.junit.Arquillian; | |
| 13 | +import org.jboss.arquillian.test.api.ArquillianResource; | |
| 14 | +import org.jboss.shrinkwrap.api.ShrinkWrap; | |
| 15 | +import org.jboss.shrinkwrap.api.asset.FileAsset; | |
| 16 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
| 17 | +import org.junit.Assert; | |
| 18 | +import org.junit.Test; | |
| 19 | +import org.junit.runner.RunWith; | |
| 20 | + | |
| 21 | +import test.LocaleProducer; | |
| 22 | +import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; | |
| 23 | +import br.gov.frameworkdemoiselle.management.internal.ManagedType; | |
| 24 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 25 | + | |
| 26 | +@RunWith(Arquillian.class) | |
| 27 | +public class ManagementBootstrapTestCase { | |
| 28 | + | |
| 29 | + @ArquillianResource | |
| 30 | + private Deployer deployer; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Deployment to test normal deployment behaviour | |
| 34 | + * | |
| 35 | + */ | |
| 36 | + @Deployment(name = "default",managed=false,testable=false) | |
| 37 | + public static JavaArchive createDeployment() { | |
| 38 | + return ShrinkWrap | |
| 39 | + .create(JavaArchive.class) | |
| 40 | + .addClass(LocaleProducer.class) | |
| 41 | + .addPackages(true, "br") | |
| 42 | + .addAsResource(new FileAsset(new File("src/test/resources/test/beans.xml")), "beans.xml") | |
| 43 | + .addAsManifestResource( | |
| 44 | + new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"), | |
| 45 | + "services/javax.enterprise.inject.spi.Extension") | |
| 46 | + .addPackages(false, ManagementBootstrapTestCase.class.getPackage()) | |
| 47 | + .addClasses(DummyManagementExtension.class,DummyManagedClass.class); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Deployment containing a malformed managed class. Tests using this deployment will | |
| 52 | + * check if deployment fails (it has to). | |
| 53 | + * | |
| 54 | + */ | |
| 55 | + @Deployment(name = "wrong_annotation",managed=false,testable=false) | |
| 56 | + public static JavaArchive createWrongAnnotationDeployment() { | |
| 57 | + return ShrinkWrap | |
| 58 | + .create(JavaArchive.class) | |
| 59 | + .addClass(LocaleProducer.class) | |
| 60 | + .addPackages(true, "br") | |
| 61 | + .addAsResource(new FileAsset(new File("src/test/resources/test/beans.xml")), "beans.xml") | |
| 62 | + .addAsManifestResource( | |
| 63 | + new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"), | |
| 64 | + "services/javax.enterprise.inject.spi.Extension") | |
| 65 | + .addPackages(false, ManagementBootstrapTestCase.class.getPackage()) | |
| 66 | + .addClasses(DummyManagementExtension.class,DummyManagedClassPropertyError.class); | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Test if a a management extension (a library that implements {@link ManagementExtension}) is correctly detected. | |
| 71 | + */ | |
| 72 | + @Test | |
| 73 | + public void testManagementExtensionRegistration() { | |
| 74 | + deployer.deploy("default"); | |
| 75 | + | |
| 76 | + // "store" é application scoped e é usado pelo DummyManagementExtension para | |
| 77 | + // armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, | |
| 78 | + // ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. | |
| 79 | + ManagedClassStore store = Beans.getReference(ManagedClassStore.class); | |
| 80 | + | |
| 81 | + Assert.assertEquals(1, store.getManagedTypes().size()); | |
| 82 | + | |
| 83 | + deployer.undeploy("default"); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Test if a a management extension's shutdown method is | |
| 88 | + * correctly called upon application shutdown. | |
| 89 | + */ | |
| 90 | + @Test | |
| 91 | + public void testManagementExtensionShutdown() { | |
| 92 | + deployer.deploy("default"); | |
| 93 | + | |
| 94 | + // "store" é application scoped e é usado pelo DummyManagementExtension para | |
| 95 | + // armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, | |
| 96 | + // ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. | |
| 97 | + // Nós então disparamos o evento de shutdown onde ele deverá limpar o store. | |
| 98 | + ManagedClassStore store = Beans.getReference(ManagedClassStore.class); | |
| 99 | + | |
| 100 | + //Detecta se a classe anotada foi detectada | |
| 101 | + List<ManagedType> managedTypes = store.getManagedTypes(); | |
| 102 | + Assert.assertEquals(1, managedTypes.size()); | |
| 103 | + | |
| 104 | + deployer.undeploy("default"); | |
| 105 | + | |
| 106 | + //Após o "undeploy", o ciclo de vida precisa ter removido a classe gerenciada da lista. | |
| 107 | + Assert.assertEquals(0, managedTypes.size()); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Test | |
| 111 | + public void testWrongAnnotation(){ | |
| 112 | + | |
| 113 | + try{ | |
| 114 | + deployer.deploy("wrong_annotation"); | |
| 115 | + | |
| 116 | + //O processo de deploy precisa falhar, pois temos uma classe anotada com falhas. | |
| 117 | + Assert.fail(); | |
| 118 | + } | |
| 119 | + catch(Exception e){ | |
| 120 | + deployer.undeploy("wrong_annotation"); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | +} | ... | ... |
impl/core/src/test/java/management/NotificationTestCase.java
0 → 100644
| ... | ... | @@ -0,0 +1,100 @@ |
| 1 | +package management; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | + | |
| 5 | +import javax.inject.Inject; | |
| 6 | + | |
| 7 | +import junit.framework.Assert; | |
| 8 | +import management.testclasses.DummyManagedClass; | |
| 9 | +import management.testclasses.DummyNotificationListener; | |
| 10 | + | |
| 11 | +import org.jboss.arquillian.container.test.api.Deployment; | |
| 12 | +import org.jboss.arquillian.junit.Arquillian; | |
| 13 | +import org.jboss.shrinkwrap.api.ShrinkWrap; | |
| 14 | +import org.jboss.shrinkwrap.api.asset.FileAsset; | |
| 15 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
| 16 | +import org.junit.Test; | |
| 17 | +import org.junit.runner.RunWith; | |
| 18 | + | |
| 19 | +import test.LocaleProducer; | |
| 20 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 21 | +import br.gov.frameworkdemoiselle.management.internal.ManagedType; | |
| 22 | +import br.gov.frameworkdemoiselle.management.internal.MonitoringManager; | |
| 23 | +import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; | |
| 24 | +import br.gov.frameworkdemoiselle.management.notification.Notification; | |
| 25 | +import br.gov.frameworkdemoiselle.management.notification.NotificationManager; | |
| 26 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 27 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Test the {@link NotificationManager} with a dummy extension | |
| 31 | + * to check if notifications are correctly propagated | |
| 32 | + * | |
| 33 | + * @author serpro | |
| 34 | + * | |
| 35 | + */ | |
| 36 | +@RunWith(Arquillian.class) | |
| 37 | +public class NotificationTestCase { | |
| 38 | + | |
| 39 | + @Inject | |
| 40 | + private NotificationManager manager; | |
| 41 | + | |
| 42 | + @Inject | |
| 43 | + @Name("demoiselle-core-bundle") | |
| 44 | + private ResourceBundle bundle; | |
| 45 | + | |
| 46 | + @Deployment | |
| 47 | + public static JavaArchive createDeployment() { | |
| 48 | + return ShrinkWrap | |
| 49 | + .create(JavaArchive.class) | |
| 50 | + .addClass(LocaleProducer.class) | |
| 51 | + .addPackages(true, "br") | |
| 52 | + .addAsResource(new FileAsset(new File("src/test/resources/test/beans.xml")), "beans.xml") | |
| 53 | + .addAsManifestResource( | |
| 54 | + new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"), | |
| 55 | + "services/javax.enterprise.inject.spi.Extension") | |
| 56 | + .addPackages(false, ManagementBootstrapTestCase.class.getPackage()) | |
| 57 | + .addClasses(DummyNotificationListener.class,DummyManagedClass.class); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Test sending a normal notification | |
| 62 | + */ | |
| 63 | + @Test | |
| 64 | + public void testSendGenericNotification(){ | |
| 65 | + manager.sendNotification(new Notification("Test Message")); | |
| 66 | + DummyNotificationListener listener = Beans.getReference(DummyNotificationListener.class); | |
| 67 | + Assert.assertEquals("Test Message", listener.getMessage()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Test sending a notification of change in attribute | |
| 72 | + */ | |
| 73 | + @Test | |
| 74 | + public void testSendAttributeChangeNotification(){ | |
| 75 | + manager.sendNotification(new AttributeChangeNotification("Test Message", "attribute", String.class, "old", "new")); | |
| 76 | + DummyNotificationListener listener = Beans.getReference(DummyNotificationListener.class); | |
| 77 | + Assert.assertEquals("Test Message - attribute", listener.getMessage()); | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Test if notifications are automatically sent when an attribute from a managed | |
| 82 | + * class change values | |
| 83 | + */ | |
| 84 | + @Test | |
| 85 | + public void testNotifyChangeManagedClass(){ | |
| 86 | + MonitoringManager manager = Beans.getReference(MonitoringManager.class); | |
| 87 | + | |
| 88 | + for (ManagedType type : manager.getManagedTypes()){ | |
| 89 | + if (type.getType().equals(DummyManagedClass.class)){ | |
| 90 | + manager.setProperty(type, "id", new Integer(10)); | |
| 91 | + break; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + DummyNotificationListener listener = Beans.getReference(DummyNotificationListener.class); | |
| 96 | + Assert.assertEquals( bundle.getString("management-notification-attribute-changed","id",DummyManagedClass.class.getCanonicalName()) + " - id" | |
| 97 | + , listener.getMessage()); | |
| 98 | + } | |
| 99 | + | |
| 100 | +} | ... | ... |
impl/core/src/test/java/management/testclasses/DummyManagedClass.java
0 → 100644
| ... | ... | @@ -0,0 +1,74 @@ |
| 1 | +package management.testclasses; | |
| 2 | + | |
| 3 | +import java.util.UUID; | |
| 4 | + | |
| 5 | +import br.gov.frameworkdemoiselle.management.annotation.Managed; | |
| 6 | +import br.gov.frameworkdemoiselle.management.annotation.Operation; | |
| 7 | +import br.gov.frameworkdemoiselle.management.annotation.Property; | |
| 8 | +import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues; | |
| 9 | +import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues.ValueType; | |
| 10 | + | |
| 11 | +@Managed | |
| 12 | +public class DummyManagedClass { | |
| 13 | + | |
| 14 | + @Property | |
| 15 | + @AllowedValues(allows={"f","m","F","M"},valueType=ValueType.INTEGER) | |
| 16 | + private Integer id; | |
| 17 | + | |
| 18 | + @Property | |
| 19 | + private String uuid; | |
| 20 | + | |
| 21 | + @Property | |
| 22 | + private String writeOnlyProperty; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas uma letra. | |
| 26 | + */ | |
| 27 | + @Property | |
| 28 | + private Integer a; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas letras maiúsculas. | |
| 32 | + */ | |
| 33 | + @Property | |
| 34 | + private Integer MAIUSCULO; | |
| 35 | + | |
| 36 | + public Integer getId() { | |
| 37 | + return id; | |
| 38 | + } | |
| 39 | + | |
| 40 | + public void setId(Integer id) { | |
| 41 | + this.id = id; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public String getUuid() { | |
| 45 | + return uuid; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public void setWriteOnlyProperty(String newValue){ | |
| 49 | + this.writeOnlyProperty = newValue; | |
| 50 | + } | |
| 51 | + | |
| 52 | + public Integer getA() { | |
| 53 | + return a; | |
| 54 | + } | |
| 55 | + | |
| 56 | + public void setA(Integer a) { | |
| 57 | + this.a = a; | |
| 58 | + } | |
| 59 | + | |
| 60 | + public Integer getMAIUSCULO() { | |
| 61 | + return MAIUSCULO; | |
| 62 | + } | |
| 63 | + | |
| 64 | + | |
| 65 | + public void setMAIUSCULO(Integer mAIUSCULO) { | |
| 66 | + MAIUSCULO = mAIUSCULO; | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Operation(description="Generates a random UUID") | |
| 70 | + public String generateUUID(){ | |
| 71 | + this.uuid = UUID.randomUUID().toString(); | |
| 72 | + return this.uuid; | |
| 73 | + } | |
| 74 | +} | ... | ... |
impl/core/src/test/java/management/testclasses/DummyManagedClassPropertyError.java
0 → 100644
| ... | ... | @@ -0,0 +1,23 @@ |
| 1 | +package management.testclasses; | |
| 2 | + | |
| 3 | +import br.gov.frameworkdemoiselle.management.annotation.Managed; | |
| 4 | +import br.gov.frameworkdemoiselle.management.annotation.Property; | |
| 5 | + | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * | |
| 9 | + * Used in tests to detect if the bootstrap detects wrong annotations | |
| 10 | + * | |
| 11 | + * @author serpro | |
| 12 | + * | |
| 13 | + */ | |
| 14 | +@Managed | |
| 15 | +public class DummyManagedClassPropertyError { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Property with no setter or getter | |
| 19 | + */ | |
| 20 | + @Property | |
| 21 | + private Long property; | |
| 22 | + | |
| 23 | +} | ... | ... |
impl/core/src/test/java/management/testclasses/DummyManagementExtension.java
0 → 100644
| ... | ... | @@ -0,0 +1,31 @@ |
| 1 | +package management.testclasses; | |
| 2 | + | |
| 3 | +import java.util.List; | |
| 4 | + | |
| 5 | +import javax.inject.Inject; | |
| 6 | + | |
| 7 | +import management.ManagedClassStore; | |
| 8 | + | |
| 9 | +import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; | |
| 10 | +import br.gov.frameworkdemoiselle.management.internal.ManagedType; | |
| 11 | + | |
| 12 | +public class DummyManagementExtension implements ManagementExtension { | |
| 13 | + | |
| 14 | + @Inject | |
| 15 | + private ManagedClassStore store; | |
| 16 | + | |
| 17 | + @Override | |
| 18 | + public void initialize(List<ManagedType> managedTypes) { | |
| 19 | + // Armazena os beans managed detectados neste store, | |
| 20 | + // para depois serem testados. | |
| 21 | + store.addManagedTypes(managedTypes); | |
| 22 | + } | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + public void shutdown(List<ManagedType> managedTypes) { | |
| 26 | + // Limpa o store, depois o teste verificará se | |
| 27 | + // o processo de shutdown rodou e limpou o store. | |
| 28 | + store.getManagedTypes().clear(); | |
| 29 | + } | |
| 30 | + | |
| 31 | +} | ... | ... |
impl/core/src/test/java/management/testclasses/DummyNotificationListener.java
0 → 100644
| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | +package management.testclasses; | |
| 2 | + | |
| 3 | +import javax.enterprise.context.ApplicationScoped; | |
| 4 | +import javax.enterprise.event.Observes; | |
| 5 | + | |
| 6 | +import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; | |
| 7 | +import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.AttributeChange; | |
| 8 | +import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.Generic; | |
| 9 | +import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; | |
| 10 | +import br.gov.frameworkdemoiselle.management.notification.NotificationManager; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Dummy class to test receiving of notifications sent by the {@link NotificationManager} | |
| 14 | + * | |
| 15 | + * @author serpro | |
| 16 | + * | |
| 17 | + */ | |
| 18 | +@ApplicationScoped | |
| 19 | +public class DummyNotificationListener { | |
| 20 | + | |
| 21 | + private String message = null; | |
| 22 | + | |
| 23 | + public void listenNotification(@Observes @Generic NotificationEvent event){ | |
| 24 | + message = event.getNotification().getMessage().toString(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + public void listenAttributeChangeNotification(@Observes @AttributeChange NotificationEvent event){ | |
| 28 | + AttributeChangeNotification notification = (AttributeChangeNotification)event.getNotification(); | |
| 29 | + message = notification.getMessage().toString() + " - " + notification.getAttributeName(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + public String getMessage() { | |
| 33 | + return message; | |
| 34 | + } | |
| 35 | +} | ... | ... |