KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > NotificationServiceTest


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.base;
24
25 import java.util.Set JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.logging.Level JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MBeanServerConnection JavaDoc;
34 import javax.management.AttributeList JavaDoc;
35 import javax.management.MBeanInfo JavaDoc;
36 import javax.management.MBeanAttributeInfo JavaDoc;
37 import javax.management.MBeanOperationInfo JavaDoc;
38 import javax.management.InstanceNotFoundException JavaDoc;
39 import javax.management.Notification JavaDoc;
40 import javax.management.NotificationListener JavaDoc;
41 import javax.management.AttributeChangeNotification JavaDoc;
42
43 import com.sun.appserv.management.base.AMX;
44 import com.sun.appserv.management.base.Util;
45 import com.sun.appserv.management.base.NotificationServiceMgr;
46 import com.sun.appserv.management.base.NotificationService;
47 import com.sun.appserv.management.helper.NotificationServiceHelper;
48 import com.sun.appserv.management.base.QueryMgr;
49 import com.sun.appserv.management.client.ProxyFactory;
50
51
52 import com.sun.enterprise.management.AMXTestBase;
53 import com.sun.enterprise.management.Capabilities;
54
55
56 /**
57  */

58 public final class NotificationServiceTest extends AMXTestBase
59 {
60         public
61     NotificationServiceTest( )
62     {
63     }
64         public static Capabilities
65     getCapabilities()
66     {
67         return getOfflineCapableCapabilities( true );
68     }
69
70         public NotificationService
71     create()
72     {
73         final NotificationServiceMgr proxy = getNotificationServiceMgr();
74         
75         return( proxy.createNotificationService( "test", 512 ) );
76     }
77
78         public void
79     testCreate()
80         throws Exception JavaDoc
81     {
82         final NotificationService proxy = create();
83         
84         removeNotificationService( proxy );
85     }
86
87         public void
88     testGetFromEmpty()
89         throws Exception JavaDoc
90     {
91         final NotificationService proxy = create();
92         
93         assert( proxy.getListeneeSet().size() == 0 );
94         final Object JavaDoc id = proxy.createBuffer( 10, null);
95         final Map JavaDoc<String JavaDoc,Object JavaDoc> result = proxy.getBufferNotifications( id, 0 );
96         final Notification JavaDoc[] notifs = (Notification JavaDoc[])result.get( proxy.NOTIFICATIONS_KEY );
97         assertEquals( 0, notifs.length );
98     }
99     
100         private void
101     removeNotificationService( final NotificationService service )
102         throws InstanceNotFoundException JavaDoc
103     {
104         getNotificationServiceMgr().removeNotificationService( service.getName() );
105     }
106
107
108     private static final class MyListener implements NotificationListener JavaDoc
109     {
110         private final List JavaDoc<Notification JavaDoc> mReceived;
111         
112         public MyListener()
113         {
114             mReceived = Collections.synchronizedList( new ArrayList JavaDoc<Notification JavaDoc>() );
115         }
116         
117             public void
118         handleNotification( final Notification JavaDoc notif, final Object JavaDoc handback )
119         {
120             mReceived.add( notif );
121         }
122         
123             public int
124         getCount()
125         {
126             return( mReceived.size() );
127         }
128     }
129     
130         private static void
131     sleep( int duration )
132     {
133         try
134         {
135             Thread.sleep( duration );
136         }
137         catch( InterruptedException JavaDoc e )
138         {
139         }
140     }
141     
142         public void
143     testListen()
144         throws Exception JavaDoc
145     {
146         final NotificationService proxy = create();
147     
148         final QueryMgr queryMgr = getQueryMgr();
149         final ObjectName JavaDoc objectName = Util.getObjectName( queryMgr );
150         
151         final Object JavaDoc id = proxy.createBuffer( 10, null);
152         final NotificationServiceHelper helper = new NotificationServiceHelper( proxy, id);
153         proxy.listenTo( objectName, null );
154         assert( proxy.getListeneeSet().size() == 1 );
155         assert( Util.getObjectName( (Util.asAMX(proxy.getListeneeSet().iterator().next())) ).equals( objectName ) );
156         
157         final MyListener myListener = new MyListener();
158         proxy.addNotificationListener( myListener, null, null );
159          
160         final Level JavaDoc saveLevel = queryMgr.getMBeanLogLevel();
161         queryMgr.setMBeanLogLevel( Level.INFO );
162         queryMgr.setMBeanLogLevel( saveLevel );
163         
164         // delivery may be asynchronous; wait until done
165
while ( myListener.getCount() < 2 )
166         {
167             sleep( 20 );
168         }
169         assert( myListener.getCount() == 2 );
170         
171         Notification JavaDoc[] notifs = helper.getNotifications();
172         
173         assertEquals( 2, notifs.length );
174         assert( notifs[ 0 ].getType().equals( AttributeChangeNotification.ATTRIBUTE_CHANGE ) );
175         assert( notifs[ 1 ].getType().equals( AttributeChangeNotification.ATTRIBUTE_CHANGE ) );
176         notifs = helper.getNotifications();
177         assert( notifs.length == 0 );
178         
179         
180         proxy.dontListenTo( objectName );
181         assert( proxy.getListeneeSet().size() == 0 );
182         
183         removeNotificationService( proxy );
184     }
185
186 }
187
188
189
Popular Tags