Commit 20203de8cb9eff8fdb2d668ad09a0e0184a251ee
1 parent
ab7f1823
Exists in
master
Lançamento de eventos do startup e shutdown nos testes do demoiselle-jmx
Showing
2 changed files
with
96 additions
and
69 deletions
Show diff stats
impl/extension/jmx/src/test/java/management/tests/internal/DynamicMBeanProxyTestCase.java
@@ -52,12 +52,15 @@ import org.jboss.arquillian.container.test.api.Deployment; | @@ -52,12 +52,15 @@ import org.jboss.arquillian.container.test.api.Deployment; | ||
52 | import org.jboss.arquillian.junit.Arquillian; | 52 | import org.jboss.arquillian.junit.Arquillian; |
53 | import org.jboss.shrinkwrap.api.ShrinkWrap; | 53 | import org.jboss.shrinkwrap.api.ShrinkWrap; |
54 | import org.jboss.shrinkwrap.api.asset.FileAsset; | 54 | import org.jboss.shrinkwrap.api.asset.FileAsset; |
55 | -import org.jboss.shrinkwrap.api.exporter.ZipExporter; | ||
56 | import org.jboss.shrinkwrap.api.spec.JavaArchive; | 55 | import org.jboss.shrinkwrap.api.spec.JavaArchive; |
56 | +import org.junit.AfterClass; | ||
57 | +import org.junit.BeforeClass; | ||
57 | import org.junit.Test; | 58 | import org.junit.Test; |
58 | import org.junit.runner.RunWith; | 59 | import org.junit.runner.RunWith; |
59 | 60 | ||
60 | import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; | 61 | import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; |
62 | +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | ||
63 | +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; | ||
61 | import br.gov.frameworkdemoiselle.util.Beans; | 64 | import br.gov.frameworkdemoiselle.util.Beans; |
62 | 65 | ||
63 | @RunWith(Arquillian.class) | 66 | @RunWith(Arquillian.class) |
@@ -73,12 +76,21 @@ public class DynamicMBeanProxyTestCase { | @@ -73,12 +76,21 @@ public class DynamicMBeanProxyTestCase { | ||
73 | .addPackages(false, DynamicMBeanProxyTestCase.class.getPackage()) | 76 | .addPackages(false, DynamicMBeanProxyTestCase.class.getPackage()) |
74 | .addClasses(LocaleProducer.class, ManagedTestClass.class); | 77 | .addClasses(LocaleProducer.class, ManagedTestClass.class); |
75 | 78 | ||
76 | - mainDeployment.as(ZipExporter.class).exportTo( | ||
77 | - new File("/home/81986912515/myPackage.jar"), true); | ||
78 | - | ||
79 | return mainDeployment; | 79 | return mainDeployment; |
80 | } | 80 | } |
81 | 81 | ||
82 | + @BeforeClass | ||
83 | + public static void fireEventStartupProccess() { | ||
84 | + Beans.getBeanManager().fireEvent(new AfterStartupProccess() { | ||
85 | + }); | ||
86 | + } | ||
87 | + | ||
88 | + @AfterClass | ||
89 | + public static void fireEventShutdownProccess() { | ||
90 | + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() { | ||
91 | + }); | ||
92 | + } | ||
93 | + | ||
82 | /** | 94 | /** |
83 | * Testa se o bootstrap está corretamente carregando e registrando classes anotadas com {@link Managed} como MBeans. | 95 | * Testa se o bootstrap está corretamente carregando e registrando classes anotadas com {@link Managed} como MBeans. |
84 | */ | 96 | */ |
impl/extension/jmx/src/test/java/management/tests/internal/NotificationBroadcasterTestCase.java
@@ -55,11 +55,15 @@ import org.jboss.arquillian.junit.Arquillian; | @@ -55,11 +55,15 @@ import org.jboss.arquillian.junit.Arquillian; | ||
55 | import org.jboss.shrinkwrap.api.ShrinkWrap; | 55 | import org.jboss.shrinkwrap.api.ShrinkWrap; |
56 | import org.jboss.shrinkwrap.api.asset.FileAsset; | 56 | import org.jboss.shrinkwrap.api.asset.FileAsset; |
57 | import org.jboss.shrinkwrap.api.spec.JavaArchive; | 57 | import org.jboss.shrinkwrap.api.spec.JavaArchive; |
58 | +import org.junit.AfterClass; | ||
59 | +import org.junit.BeforeClass; | ||
58 | import org.junit.Test; | 60 | import org.junit.Test; |
59 | import org.junit.runner.RunWith; | 61 | import org.junit.runner.RunWith; |
60 | 62 | ||
61 | import br.gov.frameworkdemoiselle.jmx.configuration.JMXConfig; | 63 | import br.gov.frameworkdemoiselle.jmx.configuration.JMXConfig; |
62 | import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; | 64 | import br.gov.frameworkdemoiselle.jmx.internal.MBeanManager; |
65 | +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; | ||
66 | +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; | ||
63 | import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; | 67 | import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; |
64 | import br.gov.frameworkdemoiselle.management.GenericNotification; | 68 | import br.gov.frameworkdemoiselle.management.GenericNotification; |
65 | import br.gov.frameworkdemoiselle.management.NotificationManager; | 69 | import br.gov.frameworkdemoiselle.management.NotificationManager; |
@@ -67,7 +71,7 @@ import br.gov.frameworkdemoiselle.util.Beans; | @@ -67,7 +71,7 @@ import br.gov.frameworkdemoiselle.util.Beans; | ||
67 | 71 | ||
68 | @RunWith(Arquillian.class) | 72 | @RunWith(Arquillian.class) |
69 | public class NotificationBroadcasterTestCase { | 73 | public class NotificationBroadcasterTestCase { |
70 | - | 74 | + |
71 | @Deployment | 75 | @Deployment |
72 | public static JavaArchive createDeployment() { | 76 | public static JavaArchive createDeployment() { |
73 | JavaArchive mainDeployment = ShrinkWrap.create(JavaArchive.class); | 77 | JavaArchive mainDeployment = ShrinkWrap.create(JavaArchive.class); |
@@ -80,129 +84,140 @@ public class NotificationBroadcasterTestCase { | @@ -80,129 +84,140 @@ public class NotificationBroadcasterTestCase { | ||
80 | 84 | ||
81 | return mainDeployment; | 85 | return mainDeployment; |
82 | } | 86 | } |
83 | - | 87 | + |
88 | + @BeforeClass | ||
89 | + public static void fireEventStartupProccess() { | ||
90 | + Beans.getBeanManager().fireEvent(new AfterStartupProccess() { | ||
91 | + }); | ||
92 | + } | ||
93 | + | ||
94 | + @AfterClass | ||
95 | + public static void fireEventShutdownProccess() { | ||
96 | + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() { | ||
97 | + }); | ||
98 | + } | ||
99 | + | ||
84 | /** | 100 | /** |
85 | * Testa o envio de uma mensagem para clientes conectados | 101 | * Testa o envio de uma mensagem para clientes conectados |
86 | */ | 102 | */ |
87 | @Test | 103 | @Test |
88 | - public void sendMessageTest(){ | 104 | + public void sendMessageTest() { |
89 | JMXConfig config = Beans.getReference(JMXConfig.class); | 105 | JMXConfig config = Beans.getReference(JMXConfig.class); |
90 | - | ||
91 | - //Este será o lado cliente. Este manager é usado para enviar notificações a partir do código da aplicação | 106 | + |
107 | + // Este será o lado cliente. Este manager é usado para enviar notificações a partir do código da aplicação | ||
92 | NotificationManager notificationManager = Beans.getReference(NotificationManager.class); | 108 | NotificationManager notificationManager = Beans.getReference(NotificationManager.class); |
93 | - | ||
94 | - //Obtém o servidor MBean onde anexaremos um listener para a notificação | 109 | + |
110 | + // Obtém o servidor MBean onde anexaremos um listener para a notificação | ||
95 | MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | 111 | MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
96 | - | ||
97 | - //Aqui obtemos o MBean de notificações já registrado pelo bootstrap | 112 | + |
113 | + // Aqui obtemos o MBean de notificações já registrado pelo bootstrap | ||
98 | StringBuffer notificationMBeanName = new StringBuffer() | 114 | StringBuffer notificationMBeanName = new StringBuffer() |
99 | - .append( config.getNotificationDomain()!=null ? config.getNotificationDomain() : "br.gov.frameworkdemoiselle.jmx" ) | ||
100 | - .append(":name=") | ||
101 | - .append( config.getNotificationMBeanName()); | ||
102 | - | 115 | + .append(config.getNotificationDomain() != null ? config.getNotificationDomain() |
116 | + : "br.gov.frameworkdemoiselle.jmx").append(":name=").append(config.getNotificationMBeanName()); | ||
117 | + | ||
103 | ObjectName name = null; | 118 | ObjectName name = null; |
104 | try { | 119 | try { |
105 | name = new ObjectName(notificationMBeanName.toString()); | 120 | name = new ObjectName(notificationMBeanName.toString()); |
106 | } catch (MalformedObjectNameException e) { | 121 | } catch (MalformedObjectNameException e) { |
107 | Assert.fail(); | 122 | Assert.fail(); |
108 | } | 123 | } |
109 | - | ||
110 | - //StringBuffer vazio | 124 | + |
125 | + // StringBuffer vazio | ||
111 | StringBuffer notificationBuffer = new StringBuffer(); | 126 | StringBuffer notificationBuffer = new StringBuffer(); |
112 | - | ||
113 | - //Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar | ||
114 | - //a mensagem enviada em "notificationBuffer" | 127 | + |
128 | + // Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar | ||
129 | + // a mensagem enviada em "notificationBuffer" | ||
115 | NotificationListener listener = new TestNotificationListener(notificationBuffer); | 130 | NotificationListener listener = new TestNotificationListener(notificationBuffer); |
116 | - | 131 | + |
117 | try { | 132 | try { |
118 | - //Anexa o listener no servidor MBean | ||
119 | - server.addNotificationListener(name,listener,null,null); | 133 | + // Anexa o listener no servidor MBean |
134 | + server.addNotificationListener(name, listener, null, null); | ||
120 | } catch (InstanceNotFoundException e) { | 135 | } catch (InstanceNotFoundException e) { |
121 | Assert.fail(); | 136 | Assert.fail(); |
122 | } | 137 | } |
123 | - | ||
124 | - //Manda a notificação pelo Demoiselle | 138 | + |
139 | + // Manda a notificação pelo Demoiselle | ||
125 | GenericNotification n = new GenericNotification("Notification test successful"); | 140 | GenericNotification n = new GenericNotification("Notification test successful"); |
126 | notificationManager.sendNotification(n); | 141 | notificationManager.sendNotification(n); |
127 | - | ||
128 | - //Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu | ||
129 | - //o StringBuffer com nossa mensagem. | 142 | + |
143 | + // Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu | ||
144 | + // o StringBuffer com nossa mensagem. | ||
130 | Assert.assertEquals("Notification test successful", notificationBuffer.toString()); | 145 | Assert.assertEquals("Notification test successful", notificationBuffer.toString()); |
131 | - | 146 | + |
132 | } | 147 | } |
133 | - | 148 | + |
134 | /** | 149 | /** |
135 | * Testa o envio de uma mensagem de mudança de atributo para clientes conectados | 150 | * Testa o envio de uma mensagem de mudança de atributo para clientes conectados |
136 | */ | 151 | */ |
137 | @Test | 152 | @Test |
138 | - public void sendAttributeChangedMessageTest(){ | 153 | + public void sendAttributeChangedMessageTest() { |
139 | JMXConfig config = Beans.getReference(JMXConfig.class); | 154 | JMXConfig config = Beans.getReference(JMXConfig.class); |
140 | - | ||
141 | - //Obtém o servidor MBean onde anexaremos um listener para a notificação | 155 | + |
156 | + // Obtém o servidor MBean onde anexaremos um listener para a notificação | ||
142 | MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | 157 | MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
143 | - | 158 | + |
144 | NotificationManager notificationManager = Beans.getReference(NotificationManager.class); | 159 | NotificationManager notificationManager = Beans.getReference(NotificationManager.class); |
145 | MBeanManager mBeanManager = Beans.getReference(MBeanManager.class); | 160 | MBeanManager mBeanManager = Beans.getReference(MBeanManager.class); |
146 | - | ||
147 | - //Aqui obtemos o MBean de notificações já registrado pelo bootstrap | 161 | + |
162 | + // Aqui obtemos o MBean de notificações já registrado pelo bootstrap | ||
148 | StringBuffer notificationMBeanName = new StringBuffer() | 163 | StringBuffer notificationMBeanName = new StringBuffer() |
149 | - .append( config.getNotificationDomain()!=null ? config.getNotificationDomain() : "br.gov.frameworkdemoiselle.jmx" ) | ||
150 | - .append(":name=") | ||
151 | - .append( config.getNotificationMBeanName()); | 164 | + .append(config.getNotificationDomain() != null ? config.getNotificationDomain() |
165 | + : "br.gov.frameworkdemoiselle.jmx").append(":name=").append(config.getNotificationMBeanName()); | ||
152 | ObjectInstance instance = mBeanManager.findMBeanInstance(notificationMBeanName.toString()); | 166 | ObjectInstance instance = mBeanManager.findMBeanInstance(notificationMBeanName.toString()); |
153 | - | ||
154 | - //StringBuffer vazio | 167 | + |
168 | + // StringBuffer vazio | ||
155 | StringBuffer notificationBuffer = new StringBuffer(); | 169 | StringBuffer notificationBuffer = new StringBuffer(); |
156 | - | ||
157 | - //Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar | ||
158 | - //a mensagem enviada em "notificationBuffer" | 170 | + |
171 | + // Este notification listener será chamado quando o Demoiselle enviar a notificação e vai colocar | ||
172 | + // a mensagem enviada em "notificationBuffer" | ||
159 | NotificationListener listener = new TestNotificationListener(notificationBuffer); | 173 | NotificationListener listener = new TestNotificationListener(notificationBuffer); |
160 | - | 174 | + |
161 | try { | 175 | try { |
162 | - //Anexa o listener no servidor MBean | ||
163 | - server.addNotificationListener(instance.getObjectName(),listener,null,null); | 176 | + // Anexa o listener no servidor MBean |
177 | + server.addNotificationListener(instance.getObjectName(), listener, null, null); | ||
164 | } catch (InstanceNotFoundException e) { | 178 | } catch (InstanceNotFoundException e) { |
165 | Assert.fail(); | 179 | Assert.fail(); |
166 | } | 180 | } |
167 | - | ||
168 | - //Manda a notificação pelo Demoiselle | ||
169 | - AttributeChangeNotification notification = new AttributeChangeNotification("Attribute Changed","name",String.class,"Demoiselle 1","Demoiselle 2"); | 181 | + |
182 | + // Manda a notificação pelo Demoiselle | ||
183 | + AttributeChangeNotification notification = new AttributeChangeNotification("Attribute Changed", "name", | ||
184 | + String.class, "Demoiselle 1", "Demoiselle 2"); | ||
170 | notificationManager.sendNotification(notification); | 185 | notificationManager.sendNotification(notification); |
171 | - | ||
172 | - //Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu | ||
173 | - //o StringBuffer com nossa mensagem. | 186 | + |
187 | + // Se o componente funcionou, o Demoiselle propagou a notificação para o servidor MBean e o listener preencheu | ||
188 | + // o StringBuffer com nossa mensagem. | ||
174 | Assert.assertEquals("Attribute Changed: name = Demoiselle 2", notificationBuffer.toString()); | 189 | Assert.assertEquals("Attribute Changed: name = Demoiselle 2", notificationBuffer.toString()); |
175 | - | 190 | + |
176 | } | 191 | } |
177 | - | 192 | + |
178 | /** | 193 | /** |
179 | - * Implementação do {@link NotificationListener} do Java que vai por qualquer notificação recebida em um StringBuffer. | 194 | + * Implementação do {@link NotificationListener} do Java que vai por qualquer notificação recebida em um |
195 | + * StringBuffer. | ||
180 | * | 196 | * |
181 | * @author serpro | 197 | * @author serpro |
182 | - * | ||
183 | */ | 198 | */ |
184 | class TestNotificationListener implements NotificationListener { | 199 | class TestNotificationListener implements NotificationListener { |
185 | - | 200 | + |
186 | StringBuffer message; | 201 | StringBuffer message; |
187 | - | ||
188 | - public TestNotificationListener(StringBuffer testMessage){ | 202 | + |
203 | + public TestNotificationListener(StringBuffer testMessage) { | ||
189 | this.message = testMessage; | 204 | this.message = testMessage; |
190 | } | 205 | } |
191 | 206 | ||
192 | @Override | 207 | @Override |
193 | public void handleNotification(javax.management.Notification notification, Object handback) { | 208 | public void handleNotification(javax.management.Notification notification, Object handback) { |
194 | - if (message!=null){ | 209 | + if (message != null) { |
195 | message.append(notification.getMessage()); | 210 | message.append(notification.getMessage()); |
196 | - | ||
197 | - if (notification instanceof javax.management.AttributeChangeNotification){ | 211 | + |
212 | + if (notification instanceof javax.management.AttributeChangeNotification) { | ||
198 | message.append(": ") | 213 | message.append(": ") |
199 | - .append( ((javax.management.AttributeChangeNotification)notification).getAttributeName() ) | ||
200 | - .append(" = ") | ||
201 | - .append(((javax.management.AttributeChangeNotification)notification).getNewValue()); | 214 | + .append(((javax.management.AttributeChangeNotification) notification).getAttributeName()) |
215 | + .append(" = ") | ||
216 | + .append(((javax.management.AttributeChangeNotification) notification).getNewValue()); | ||
202 | } | 217 | } |
203 | } | 218 | } |
204 | } | 219 | } |
205 | - | 220 | + |
206 | } | 221 | } |
207 | 222 | ||
208 | } | 223 | } |