1 23 package com.sun.enterprise.management.base; 24 25 import java.util.Set ; 26 import java.util.Map ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.logging.Level ; 31 32 import javax.management.ObjectName ; 33 import javax.management.MBeanServerConnection ; 34 import javax.management.AttributeList ; 35 import javax.management.MBeanInfo ; 36 import javax.management.MBeanAttributeInfo ; 37 import javax.management.MBeanOperationInfo ; 38 import javax.management.InstanceNotFoundException ; 39 import javax.management.Notification ; 40 import javax.management.NotificationListener ; 41 import javax.management.AttributeChangeNotification ; 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 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 81 { 82 final NotificationService proxy = create(); 83 84 removeNotificationService( proxy ); 85 } 86 87 public void 88 testGetFromEmpty() 89 throws Exception 90 { 91 final NotificationService proxy = create(); 92 93 assert( proxy.getListeneeSet().size() == 0 ); 94 final Object id = proxy.createBuffer( 10, null); 95 final Map <String ,Object > result = proxy.getBufferNotifications( id, 0 ); 96 final Notification [] notifs = (Notification [])result.get( proxy.NOTIFICATIONS_KEY ); 97 assertEquals( 0, notifs.length ); 98 } 99 100 private void 101 removeNotificationService( final NotificationService service ) 102 throws InstanceNotFoundException 103 { 104 getNotificationServiceMgr().removeNotificationService( service.getName() ); 105 } 106 107 108 private static final class MyListener implements NotificationListener 109 { 110 private final List <Notification > mReceived; 111 112 public MyListener() 113 { 114 mReceived = Collections.synchronizedList( new ArrayList <Notification >() ); 115 } 116 117 public void 118 handleNotification( final Notification notif, final Object 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 e ) 138 { 139 } 140 } 141 142 public void 143 testListen() 144 throws Exception 145 { 146 final NotificationService proxy = create(); 147 148 final QueryMgr queryMgr = getQueryMgr(); 149 final ObjectName objectName = Util.getObjectName( queryMgr ); 150 151 final Object 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 saveLevel = queryMgr.getMBeanLogLevel(); 161 queryMgr.setMBeanLogLevel( Level.INFO ); 162 queryMgr.setMBeanLogLevel( saveLevel ); 163 164 while ( myListener.getCount() < 2 ) 166 { 167 sleep( 20 ); 168 } 169 assert( myListener.getCount() == 2 ); 170 171 Notification [] 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 |