KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > util > jmx > NotificationListenerBaseTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.util.jmx;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.management.*;
31
32 import com.sun.appserv.management.util.jmx.NotificationListenerBase;
33 import com.sun.appserv.management.util.jmx.MBeanRegistrationListener;
34 import com.sun.appserv.management.util.jmx.JMXUtil;
35
36
37 public class NotificationListenerBaseTest extends JMXTestBase
38 {
39     private MBeanServer mServer;
40     
41     private static final String JavaDoc TEST = "t";
42     
43         public
44     NotificationListenerBaseTest()
45     {
46         mServer = null;
47     }
48     
49         protected ObjectName
50     registerMBean( final Object JavaDoc mbean, final ObjectName objectName )
51         throws InstanceAlreadyExistsException,
52         NotCompliantMBeanException, MBeanRegistrationException
53     {
54         return mServer.registerMBean( mbean, objectName ).getObjectName();
55     }
56     
57         public void
58     setUp() throws Exception JavaDoc
59     {
60         mServer = MBeanServerFactory.newMBeanServer( "test" );
61     }
62     
63         public void
64     tearDown()
65         throws Exception JavaDoc
66     {
67         mServer = null;
68     }
69     
70     private static final class MyListener extends NotificationListenerBase
71     {
72             public
73         MyListener(
74             final MBeanServerConnection conn,
75             final ObjectName pattern )
76             throws InstanceNotFoundException, IOException JavaDoc
77         {
78             super( conn, pattern, null );
79         }
80         
81             public
82         MyListener(
83             final MBeanServerConnection conn,
84             final ObjectName pattern,
85             final NotificationFilter filter )
86             throws InstanceNotFoundException, IOException JavaDoc
87         {
88             super( conn, pattern, filter );
89         }
90         
91             public void
92         handleNotification( final Notification notif, final Object JavaDoc handback)
93         {
94         }
95     }
96     
97     
98         private MyListener
99     create( final String JavaDoc pattern )
100         throws Exception JavaDoc
101     {
102         final MyListener listener =
103             new MyListener( mServer, JMXUtil.newObjectName( pattern ), null );
104         
105         return listener;
106     }
107     
108         public void
109     testCreate()
110         throws Exception JavaDoc
111     {
112         final MyListener b = create( "*:*" );
113         assert( b.getListenees().size() != 0 );
114         b.getMBeanServerConnection();
115         b.cleanup();
116         assert( b.getListenees().size() == 0 );
117     }
118     
119     public interface DummyMBean extends NotificationEmitter
120     {
121         int getDummy();
122         void setDummy( int value );
123     }
124     
125     public static final class Dummy
126         extends NotificationBroadcasterSupport
127         implements DummyMBean
128     {
129         private int mDummy;
130         
131         public int getDummy() { return mDummy; }
132         public void setDummy( int value ) { mDummy = value; }
133     }
134     
135     static private final String JavaDoc DUMMY_OBJECT_NAME_PREFIX = TEST + ":type=dummy";
136     
137     
138         private ObjectName
139     createObjectName(
140         final String JavaDoc domain,
141         final int id,
142         final String JavaDoc name )
143     {
144        String JavaDoc s = domain + ":" +
145             "name=" + name +
146             ",id=" + id +
147             (",category=" + ((id % 2) == 0 ? "even":"odd"));
148             
149        return JMXUtil.newObjectName( s );
150     }
151     
152         Set JavaDoc<ObjectName>
153     registerDummies(
154         final int count,
155         final String JavaDoc name )
156         throws Exception JavaDoc
157     {
158         return registerDummies( TEST, count, name );
159     }
160     
161         Set JavaDoc<ObjectName>
162     registerDummies(
163         final String JavaDoc domain,
164         final int count,
165         final String JavaDoc name )
166         throws Exception JavaDoc
167     {
168         final Set JavaDoc<ObjectName> s = new HashSet JavaDoc<ObjectName>();
169         
170         for( int i = 0; i < count; ++i )
171         {
172             final Dummy d = new Dummy();
173             d.setDummy( i );
174             
175             final ObjectName on = createObjectName( domain, i, name );
176             final ObjectName objectName = registerMBean( d, on );
177             
178             s.add( objectName );
179         }
180         
181         return s;
182     }
183         
184         public void
185     testGetListenees()
186         throws Exception JavaDoc
187     {
188         final int NUM = 10;
189         final String JavaDoc NAME = "bbb";
190         
191         registerDummies( NUM, NAME );
192         
193         final MyListener odd = create( TEST + ":category=odd,*" );
194         assert( odd.getListenees().size() == NUM / 2 );
195         for( final ObjectName item : odd.getListenees() )
196         {
197             assert( item.getKeyProperty( "category" ).equals( "odd" ) );
198         }
199         assert( odd.isAlive() );
200         
201         final MyListener even = create( TEST + ":category=even,*" );
202         assert( even.getListenees().size() == NUM / 2 );
203         for( final ObjectName item : even.getListenees() )
204         {
205             assert( item.getKeyProperty( "category" ).equals( "even" ) );
206         }
207         assert( even.isAlive() );
208         
209         final MyListener all = create( TEST + ":name=bbb,*" );
210         assert( all.getListenees().size() == NUM );
211         for( final ObjectName item : all.getListenees() )
212         {
213             assert( item.getKeyProperty( "name" ).equals( NAME ) );
214         }
215         assert( all.isAlive() );
216     }
217     
218     
219     private final class MyRegListener extends MBeanRegistrationListener
220     {
221         public int mRegCount = 0;
222         public int mUnregCount = 0;
223         
224             public
225         MyRegListener( final ObjectName constrain )
226             throws InstanceNotFoundException, IOException JavaDoc
227         {
228             super( mServer, constrain );
229         }
230         
231             protected void
232         mbeanRegistered( final ObjectName objectName )
233         {
234             ++mRegCount;
235         }
236         
237             protected void
238         mbeanUnregistered( final ObjectName objectName )
239         {
240             ++mUnregCount;
241         }
242     }
243     
244         private void
245     testListenForRegistration(
246         final String JavaDoc domain,
247         final String JavaDoc name,
248         final int count,
249         final String JavaDoc constrainStr)
250         throws Exception JavaDoc
251     {
252         final ObjectName constrain = (constrainStr == null) ? null :
253             JMXUtil.newObjectName( constrainStr );
254         
255         final MyRegListener listener = new MyRegListener( constrain );
256         assert( listener.getListenees().size() == 1 );
257         
258         final Set JavaDoc<ObjectName> s = registerDummies( domain, count, name );
259         assert( listener.mRegCount == count );
260         
261         for( final ObjectName objectName : s )
262         {
263             mServer.unregisterMBean( objectName );
264         }
265         assert( listener.mUnregCount == count );
266         listener.cleanup();
267         assert( listener.getListenees().size() == 0 );
268         
269     }
270     
271     
272     static private final String JavaDoc REG_DOMAIN = "LFR";
273     static private final int REG_COUNT = 1000;
274     
275         public void
276     testListenForRegistration1()
277         throws Exception JavaDoc
278     {
279         final String JavaDoc NAME = "zzz";
280         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null );
281         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*");
282         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*");
283         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME);
284     }
285     
286        public void
287     testListenForRegistration2()
288         throws Exception JavaDoc
289     {
290         final String JavaDoc NAME = "zzzz";
291         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null );
292         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*");
293         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*");
294         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME);
295     }
296     
297        public void
298     testListenForRegistration3()
299         throws Exception JavaDoc
300     {
301         final String JavaDoc NAME = "zzzzz";
302         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null );
303         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*");
304         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*");
305         testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME);
306     }
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Popular Tags