From 20203de8cb9eff8fdb2d668ad09a0e0184a251ee Mon Sep 17 00:00:00 2001 From: Ednara Oliveira Date: Tue, 13 Aug 2013 15:50:15 -0300 Subject: [PATCH] Lançamento de eventos do startup e shutdown nos testes do demoiselle-jmx --- impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java | 20 ++++++++++++++++---- impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------- 2 files changed, 96 insertions(+), 69 deletions(-) diff --git a/impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java b/impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java index 20f5f31..35980ef 100644 --- a/impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java +++ b/impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java @@ -52,12 +52,15 @@ import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.FileAsset; -import org.jboss.shrinkwrap.api.exporter.ZipExporter; import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; import br.gov.frameworkdemoiselle.util.Beans; @RunWith(Arquillian.class) @@ -73,12 +76,21 @@ public class DynamicMBeanProxyTestCase { .addPackages(false, DynamicMBeanProxyTestCase.class.getPackage()) .addClasses(LocaleProducer.class, ManagedTestClass.class); - mainDeployment.as(ZipExporter.class).exportTo( - new File("/home/81986912515/myPackage.jar"), true); - return mainDeployment; } + @BeforeClass + public static void fireEventStartupProccess() { + Beans.getBeanManager().fireEvent(new AfterStartupProccess() { + }); + } + + @AfterClass + public static void fireEventShutdownProccess() { + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() { + }); + } + /** * Testa se o bootstrap está corretamente carregando e registrando classes anotadas com {@link Managed} como MBeans. */ diff --git a/impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java b/impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java index 89b408f..55f7a4b 100644 --- a/impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java +++ b/impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java @@ -55,11 +55,15 @@ import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.FileAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import br.gov.frameworkdemoiselle.jmx.configuration.JMXConfig; import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; import br.gov.frameworkdemoiselle.management.GenericNotification; import br.gov.frameworkdemoiselle.management.NotificationManager; @@ -67,7 +71,7 @@ import br.gov.frameworkdemoiselle.util.Beans; @RunWith(Arquillian.class) public class NotificationBroadcasterTestCase { - + @Deployment public static JavaArchive createDeployment() { JavaArchive mainDeployment = ShrinkWrap.create(JavaArchive.class); @@ -80,129 +84,140 @@ public class NotificationBroadcasterTestCase { return mainDeployment; } - + + @BeforeClass + public static void fireEventStartupProccess() { + Beans.getBeanManager().fireEvent(new AfterStartupProccess() { + }); + } + + @AfterClass + public static void fireEventShutdownProccess() { + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() { + }); + } + /** * Testa o envio de uma mensagem para clientes conectados */ @Test - public void sendMessageTest(){ + public void sendMessageTest() { JMXConfig config = Beans.getReference(JMXConfig.class); - - //Este será o lado cliente. Este manager é usado para enviar notificações a partir do código da aplicação + + // Este será o lado cliente. Este manager é usado para enviar notificações a partir do código da aplicação NotificationManager notificationManager = Beans.getReference(NotificationManager.class); - - //Obtém o servidor MBean onde anexaremos um listener para a notificação + + // Obtém o servidor MBean onde anexaremos um listener para a notificação MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - - //Aqui obtemos o MBean de notificações já registrado pelo bootstrap + + // Aqui obtemos o MBean de notificações já registrado pelo bootstrap StringBuffer notificationMBeanName = new StringBuffer() - .append( config.getNotificationDomain()!=null ? config.getNotificationDomain() : "br.gov.frameworkdemoiselle.jmx" ) - .append(":name=") - .append( config.getNotificationMBeanName()); - + .append(config.getNotificationDomain() != null ? config.getNotificationDomain() + : "br.gov.frameworkdemoiselle.jmx").append(":name=").append(config.getNotificationMBeanName()); + ObjectName name = null; try { name = new ObjectName(notificationMBeanName.toString()); } catch (MalformedObjectNameException e) { Assert.fail(); } - - //StringBuffer vazio + + // StringBuffer vazio StringBuffer notificationBuffer = new StringBuffer(); - - //Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar - //a mensagem enviada em "notificationBuffer" + + // Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar + // a mensagem enviada em "notificationBuffer" NotificationListener listener = new TestNotificationListener(notificationBuffer); - + try { - //Anexa o listener no servidor MBean - server.addNotificationListener(name,listener,null,null); + // Anexa o listener no servidor MBean + server.addNotificationListener(name, listener, null, null); } catch (InstanceNotFoundException e) { Assert.fail(); } - - //Manda a notificação pelo Demoiselle + + // Manda a notificação pelo Demoiselle GenericNotification n = new GenericNotification("Notification test successful"); notificationManager.sendNotification(n); - - //Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu - //o StringBuffer com nossa mensagem. + + // Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu + // o StringBuffer com nossa mensagem. Assert.assertEquals("Notification test successful", notificationBuffer.toString()); - + } - + /** * Testa o envio de uma mensagem de mudança de atributo para clientes conectados */ @Test - public void sendAttributeChangedMessageTest(){ + public void sendAttributeChangedMessageTest() { JMXConfig config = Beans.getReference(JMXConfig.class); - - //Obtém o servidor MBean onde anexaremos um listener para a notificação + + // Obtém o servidor MBean onde anexaremos um listener para a notificação MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - + NotificationManager notificationManager = Beans.getReference(NotificationManager.class); MBeanManager mBeanManager = Beans.getReference(MBeanManager.class); - - //Aqui obtemos o MBean de notificações já registrado pelo bootstrap + + // Aqui obtemos o MBean de notificações já registrado pelo bootstrap StringBuffer notificationMBeanName = new StringBuffer() - .append( config.getNotificationDomain()!=null ? config.getNotificationDomain() : "br.gov.frameworkdemoiselle.jmx" ) - .append(":name=") - .append( config.getNotificationMBeanName()); + .append(config.getNotificationDomain() != null ? config.getNotificationDomain() + : "br.gov.frameworkdemoiselle.jmx").append(":name=").append(config.getNotificationMBeanName()); ObjectInstance instance = mBeanManager.findMBeanInstance(notificationMBeanName.toString()); - - //StringBuffer vazio + + // StringBuffer vazio StringBuffer notificationBuffer = new StringBuffer(); - - //Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar - //a mensagem enviada em "notificationBuffer" + + // Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar + // a mensagem enviada em "notificationBuffer" NotificationListener listener = new TestNotificationListener(notificationBuffer); - + try { - //Anexa o listener no servidor MBean - server.addNotificationListener(instance.getObjectName(),listener,null,null); + // Anexa o listener no servidor MBean + server.addNotificationListener(instance.getObjectName(), listener, null, null); } catch (InstanceNotFoundException e) { Assert.fail(); } - - //Manda a notificação pelo Demoiselle - AttributeChangeNotification notification = new AttributeChangeNotification("Attribute Changed","name",String.class,"Demoiselle 1","Demoiselle 2"); + + // Manda a notificação pelo Demoiselle + AttributeChangeNotification notification = new AttributeChangeNotification("Attribute Changed", "name", + String.class, "Demoiselle 1", "Demoiselle 2"); notificationManager.sendNotification(notification); - - //Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu - //o StringBuffer com nossa mensagem. + + // Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu + // o StringBuffer com nossa mensagem. Assert.assertEquals("Attribute Changed: name = Demoiselle 2", notificationBuffer.toString()); - + } - + /** - * Implementação do {@link NotificationListener} do Java que vai por qualquer notificação recebida em um StringBuffer. + * Implementação do {@link NotificationListener} do Java que vai por qualquer notificação recebida em um + * StringBuffer. * * @author serpro - * */ class TestNotificationListener implements NotificationListener { - + StringBuffer message; - - public TestNotificationListener(StringBuffer testMessage){ + + public TestNotificationListener(StringBuffer testMessage) { this.message = testMessage; } @Override public void handleNotification(javax.management.Notification notification, Object handback) { - if (message!=null){ + if (message != null) { message.append(notification.getMessage()); - - if (notification instanceof javax.management.AttributeChangeNotification){ + + if (notification instanceof javax.management.AttributeChangeNotification) { message.append(": ") - .append( ((javax.management.AttributeChangeNotification)notification).getAttributeName() ) - .append(" = ") - .append(((javax.management.AttributeChangeNotification)notification).getNewValue()); + .append(((javax.management.AttributeChangeNotification) notification).getAttributeName()) + .append(" = ") + .append(((javax.management.AttributeChangeNotification) notification).getNewValue()); } } } - + } } -- libgit2 0.21.2