KickJava   Java API By Example, From Geeks To Geeks.

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


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  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/tests/com/sun/enterprise/management/base/NotificationEmitterServiceTest.java,v 1.4 2006/03/09 20:30:51 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/09 20:30:51 $
28  */

29 package com.sun.enterprise.management.base;
30
31 import javax.management.Notification JavaDoc;
32 import javax.management.NotificationListener JavaDoc;
33
34 import com.sun.appserv.management.base.NotificationEmitterService;
35 import com.sun.appserv.management.base.NotificationEmitterServiceKeys;
36 import com.sun.appserv.management.base.Util;
37 import com.sun.appserv.management.util.jmx.NotificationBuilder;
38
39
40 import com.sun.enterprise.management.AMXTestBase;
41 import com.sun.enterprise.management.Capabilities;
42
43
44 /**
45  */

46 public final class NotificationEmitterServiceTest extends AMXTestBase
47 {
48         public
49     NotificationEmitterServiceTest( )
50     {
51     }
52         public static Capabilities
53     getCapabilities()
54     {
55         return getOfflineCapableCapabilities( true );
56     }
57
58         public NotificationEmitterService
59     getNotificationEmitterService()
60     {
61         return getDomainRoot().getDomainNotificationEmitterService();
62     }
63     
64         public void
65     testGet()
66     {
67         assert getNotificationEmitterService() != null;
68     }
69     
70     
71     private final static class testEmitListener implements NotificationListener JavaDoc
72     {
73         static final String JavaDoc TEST_TYPE = "unittests.testEmitListener";
74         private Notification JavaDoc mNotification;
75         private int mNumHeard;
76         
77         public testEmitListener()
78         {
79             mNumHeard = 0;
80             mNotification = null;
81         }
82         
83             public void
84         handleNotification( final Notification JavaDoc notif, final Object JavaDoc handback )
85         {
86             mNotification = notif;
87             ++mNumHeard;
88         }
89         
90         public Notification JavaDoc getLast() { return mNotification; }
91         public int getNumHeard() { return mNumHeard; }
92         public void clear() { mNumHeard = 0; mNotification = null; }
93     }
94
95     private static final String JavaDoc TEST_SOURCE = "NotificationEmitterServiceTest";
96     private static final String JavaDoc TEST_MESSAGE = "Message";
97     private static final String JavaDoc TEST_KEY = "TestKey";
98     private static final String JavaDoc TEST_VALUE = "test value";
99     
100         public void
101     testEmit()
102     {
103         final NotificationEmitterService nes = getNotificationEmitterService();
104         
105         final NotificationBuilder builder =
106             new NotificationBuilder( testEmitListener.TEST_TYPE, TEST_SOURCE );
107         
108         final testEmitListener listener = new testEmitListener();
109         nes.addNotificationListener( listener, null, null);
110         final Notification JavaDoc notif = builder.buildNew( TEST_MESSAGE);
111         builder.putMapData( notif, TEST_KEY, TEST_VALUE );
112         
113         // call emitNotification() and verify it was emitted
114
nes.emitNotification( notif );
115         while( listener.getLast() == null )
116         {
117             // wait...
118
mySleep( 20 );
119         }
120         final Notification JavaDoc retrieved = listener.getLast();
121         assert( retrieved.getType().equals( notif.getType() ) );
122         assert( Util.getAMXNotificationValue( retrieved, TEST_KEY, String JavaDoc.class).equals( TEST_VALUE ) );
123         assert( retrieved.getSource().equals( TEST_SOURCE ) );
124         assert( retrieved.getMessage().equals( TEST_MESSAGE ) );
125         
126         // now emit many Notifications.
127
listener.clear();
128         long start = now();
129         final int ITER = 200;
130         for( int i = 0; i < ITER; ++i)
131         {
132             final Notification JavaDoc temp = builder.buildNew( TEST_MESSAGE);
133             builder.putMapData( notif, TEST_KEY, TEST_VALUE );
134             nes.emitNotification( temp );
135         }
136         printElapsedIter( "Emitted Notifications", start, ITER );
137         start = now();
138         while( listener.getNumHeard() < ITER )
139         {
140             mySleep( 10 );
141         }
142         printElapsedIter( "After sending, received emitted Notifications", start, ITER );
143     }
144 }
145
146
147
148
149
150
151
152
153
154
155
Popular Tags