KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > config > CustomMBeanConfigTest


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.config;
24
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.JMException JavaDoc;
32
33
34 import com.sun.enterprise.management.AMXTestBase;
35 import com.sun.enterprise.management.Capabilities;
36
37 import com.sun.appserv.management.base.Util;
38 import com.sun.appserv.management.base.QueryMgr;
39
40 import com.sun.appserv.management.config.CustomMBeanConfig;
41 import com.sun.appserv.management.config.DomainConfig;
42 import com.sun.appserv.management.config.PropertiesAccess;
43
44 import com.sun.enterprise.management.support.TestDummy;
45 import com.sun.enterprise.management.support.TestDummyMBean;
46
47 import com.sun.appserv.management.helper.RefHelper;
48
49 import com.sun.appserv.management.util.misc.CollectionUtil;
50
51 /**
52  */

53 public final class CustomMBeanConfigTest extends AMXTestBase
54 {
55     // built-into server already
56
private static final String JavaDoc IMPL_CLASSNAME = TestDummy.class.getName();
57         
58     private static final String JavaDoc TEST_NAME_BASE = "custom";
59     private static final String JavaDoc TEST_TYPE = "CustomMBeanConfigTest";
60     
61         public
62     CustomMBeanConfigTest ()
63     {
64         if ( checkNotOffline( "ensureDefaultInstance" ) )
65         {
66             ensureDefaultInstance( getDomainConfig() );
67         }
68     }
69     
70        public static String JavaDoc
71     getDefaultInstanceName()
72     {
73         return getDefaultInstanceName( "CustomMBeanConfig" );
74     }
75     
76         public static CustomMBeanConfig
77     ensureDefaultInstance( final DomainConfig domainConfig )
78     {
79         CustomMBeanConfig result =
80             domainConfig.getCustomMBeanConfigMap().get( getDefaultInstanceName() );
81         
82         if ( result == null )
83         {
84             result = createInstance(
85                 domainConfig,
86                 getDefaultInstanceName(),
87                 createProps() );
88         }
89         
90         return result;
91     }
92     
93         public static CustomMBeanConfig
94     createInstance(
95         final DomainConfig domainConfig,
96         final String JavaDoc name,
97         final Map JavaDoc<String JavaDoc,String JavaDoc> optional )
98     {
99         final CustomMBeanConfig custom =
100             domainConfig.createCustomMBeanConfig( name, IMPL_CLASSNAME,
101             createObjectName( name ),
102             false,
103             optional );
104         
105         return custom;
106     }
107     
108     
109         public Map JavaDoc<String JavaDoc,CustomMBeanConfig>
110     getCustomMBeanConfigs()
111     {
112         return getDomainConfig().getCustomMBeanConfigMap();
113     }
114     
115         private void
116     _testGetAll()
117     {
118         final Map JavaDoc<String JavaDoc,CustomMBeanConfig> all = getCustomMBeanConfigs();
119         assert( all != null );
120     }
121     
122         private void
123     sanityCheck( final CustomMBeanConfig config )
124     {
125         final String JavaDoc objectName = config.getObjectNameInConfig();
126         
127         final String JavaDoc implClassname = config.getImplClassname();
128     }
129
130         private synchronized void
131     _testAttrs()
132     {
133         final Map JavaDoc<String JavaDoc,CustomMBeanConfig> all = getCustomMBeanConfigs();
134         
135         if ( all.size() != 0 )
136         {
137             // everything is already tested generically, but we'll
138
// do some basic sanity checks here
139
for( final CustomMBeanConfig config : all.values() )
140             {
141                 sanityCheck( config );
142             }
143         }
144         else
145         {
146             warning( "CustomMBeanConfigTest: No custom MBeans to test" );
147         }
148     }
149     
150
151     /**
152         Create some dummy properties for creating a CustomMBeanConfig
153      */

154         private static Map JavaDoc<String JavaDoc,String JavaDoc>
155     createProps()
156     {
157         final String JavaDoc PRP = PropertiesAccess.PROPERTY_PREFIX; // shorten
158

159         final Map JavaDoc<String JavaDoc,String JavaDoc> optional = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
160         
161         // these must be available in test MBean
162
optional.put( PRP + "Attr1", "hello" );
163         optional.put( PRP + "Attr2", "world" );
164         
165         return optional;
166     }
167     
168         private static String JavaDoc
169     createObjectName( final String JavaDoc name )
170     {
171         return CustomMBeanConfig.JMX_DOMAIN + ":name=" + name +
172             ",type=" + TEST_TYPE;
173     }
174     
175         public synchronized CustomMBeanConfig
176     create(
177         final DomainConfig domainConfig,
178         final String JavaDoc name,
179         final Map JavaDoc<String JavaDoc,String JavaDoc> optional )
180     {
181         return createInstance( domainConfig, name, optional );
182     }
183     
184         public synchronized void
185     verifyPropsAdded(
186         final CustomMBeanConfig config,
187         final Map JavaDoc<String JavaDoc,String JavaDoc> props )
188     {
189         for( final String JavaDoc key : props.keySet() )
190         {
191             if ( key.startsWith( PropertiesAccess.PROPERTY_PREFIX ) )
192             {
193                 final String JavaDoc specifiedValue = props.get( key ).toString();
194                 final String JavaDoc propName = key.substring(
195                     PropertiesAccess.PROPERTY_PREFIX.length(), key.length());
196                 
197                 final String JavaDoc actualValue = config.getPropertyValue( propName );
198                 assert( specifiedValue.equals( actualValue ) );
199             }
200         }
201     }
202
203
204         private void
205     removeCustomMBean( final String JavaDoc name )
206     {
207         getDomainConfig().removeCustomMBeanConfig( name );
208     }
209     
210         private Set JavaDoc<ObjectName JavaDoc>
211     getRegisteredCustoms()
212     {
213         final QueryMgr queryMgr = getQueryMgr();
214         final Set JavaDoc<ObjectName JavaDoc> mbeans =
215             queryMgr.queryPatternObjectNameSet( CustomMBeanConfig.JMX_DOMAIN, "type=" + TEST_TYPE );
216         
217         return mbeans;
218     }
219     
220         private void
221     unregisterAnyTestMBeans()
222     {
223         final Set JavaDoc<ObjectName JavaDoc> customs = getRegisteredCustoms();
224         for( final ObjectName JavaDoc objectName : customs )
225         {
226             if ( TEST_TYPE.equals( objectName.getKeyProperty( "type" ) ) )
227             {
228                 try
229                 {
230                     getMBeanServerConnection().unregisterMBean( objectName );
231                     printVerbose( "unregistered: " + objectName );
232                 }
233                 catch( Exception JavaDoc e )
234                 {
235                 }
236             }
237         }
238     }
239     
240         public synchronized void
241     testCreateRemove()
242     {
243         if ( ! checkNotOffline( "testCreateRemove" ) )
244         {
245             return;
246         }
247         
248         final DomainConfig domainConfig = getDomainConfig();
249         
250         final Map JavaDoc<String JavaDoc,String JavaDoc> optional = createProps();
251         
252         final Set JavaDoc<CustomMBeanConfig> created = new HashSet JavaDoc<CustomMBeanConfig>();
253         
254         final Map JavaDoc<String JavaDoc,CustomMBeanConfig> existing =
255             getDomainConfig().getCustomMBeanConfigMap();
256         
257         unregisterAnyTestMBeans();
258         final Set JavaDoc<ObjectName JavaDoc> customsBefore = getRegisteredCustoms();
259         if ( customsBefore.size() != 0 )
260         {
261             printVerbose( "custom MBeans already registered:\n" +
262                 CollectionUtil.toString( customsBefore, "\n" ) );
263         }
264         
265         final int NUM = 3;
266         for( int i = 0; i < NUM; ++i )
267         {
268             final String JavaDoc testName = TEST_NAME_BASE + i;
269             
270             if ( existing.containsKey( testName ) )
271             {
272                 RefHelper.removeAllRefsTo( existing.get( testName ), true );
273                 // leftover from a failed test...
274
removeCustomMBean( testName );
275             }
276             
277             final CustomMBeanConfig config =
278                 create( domainConfig, TEST_NAME_BASE + i, optional );
279             //printVerbose( "created: " + Util.getObjectName( config ) );
280

281             assert( getCustomMBeanConfigs().get( config.getName() ) == config );
282             
283             created.add( config );
284             sanityCheck( config );
285             
286             verifyPropsAdded( config, optional );
287         }
288         
289         _testGetAll();
290         _testAttrs();
291         
292         for( final CustomMBeanConfig config : created )
293         {
294             //printVerbose( "removing: " + Util.getObjectName( config ) );
295
final String JavaDoc name = config.getName();
296             removeCustomMBean( name );
297             
298             assert( getCustomMBeanConfigs().get( name ) == null );
299         }
300         
301         _testGetAll();
302         
303         mySleep( 100 );
304         final Set JavaDoc<ObjectName JavaDoc> customsAfter = getRegisteredCustoms();
305         customsAfter.removeAll( customsBefore );
306         if ( customsAfter.size() != 0 )
307         {
308             warning( "after removing custom MBeans, " +
309                 "they are still registered (not an AMX bug):\n" +
310                 CollectionUtil.toString( customsAfter, "\n" ) );
311         }
312         unregisterAnyTestMBeans();
313     }
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
Popular Tags