KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.management.Attribute JavaDoc;
31
32
33 import com.sun.appserv.management.base.DottedNames;
34 import com.sun.appserv.management.config.ConfigDottedNames;
35 import com.sun.appserv.management.monitor.MonitoringDottedNames;
36 import com.sun.appserv.management.util.stringifier.SmartStringifier;
37
38
39 import com.sun.enterprise.management.AMXTestBase;
40 import com.sun.enterprise.management.Capabilities;
41
42 /**
43  */

44 public final class DottedNamesTest extends AMXTestBase
45 {
46         public
47     DottedNamesTest( )
48     {
49     }
50     
51         private void
52     checkAttribute( final Attribute JavaDoc attr )
53     {
54         assert( attr != null );
55         
56         final Object JavaDoc value = attr.getValue();
57         if ( value instanceof Attribute JavaDoc )
58         {
59             warning( "Is value of " + attr.getName() + " really another Attribute? => " +
60                 toString( value ) );
61         }
62     }
63     
64         private void
65     checkResultsFromWildGet(
66         final Object JavaDoc[] results )
67     {
68         for( int i = 0; i < results.length; ++i )
69         {
70             final Object JavaDoc result = results[ i ];
71             
72             if ( result == null )
73             {
74                 warning( "null result from dottedNameGet( \"*\" )" );
75             }
76             else if ( ! (result instanceof Attribute JavaDoc) )
77             {
78                 warning( "non-Attribute result from dottedNameGet( \"*\" ): " + result );
79             }
80             else
81             {
82                 // it's an Attribute
83
final Attribute JavaDoc attr = (Attribute JavaDoc)result;
84                 checkAttribute( (Attribute JavaDoc)result );
85             }
86         }
87     }
88     
89         private void
90     checkResultsFromGet(
91         final String JavaDoc[] names,
92         final Object JavaDoc[] results )
93     {
94         for( int i = 0; i < results.length; ++i )
95         {
96             final Object JavaDoc result = results[ i ];
97             
98             if ( result == null )
99             {
100                 warning( "Dotted name has null result: " + names[ i ] );
101             }
102             else if ( ! (result instanceof Attribute JavaDoc) )
103             {
104                 warning( "Dotted name " + names[ i ] + " could not be obtained: " + result );
105             }
106         }
107     }
108         
109         private String JavaDoc[]
110     getAllNames( final DottedNames dottedNames )
111     {
112         final Attribute JavaDoc[] attrs = (Attribute JavaDoc[])dottedNames.dottedNameGet( "*" );
113         final String JavaDoc[] names = new String JavaDoc[ attrs.length ];
114         for( int i = 0; i < names.length; ++i )
115         {
116             names[ i ] = attrs[ i ].getName();
117         }
118         
119         return( names );
120     }
121     
122     
123         public void
124     testGetAllConfigDottedNames()
125     {
126         final long start = now();
127         final ConfigDottedNames dottedNames = getDomainRoot().getConfigDottedNames();
128         
129         final String JavaDoc[] names = getAllNames( dottedNames );
130         
131         final Object JavaDoc[] results = dottedNames.dottedNameGet( names );
132         
133         checkResultsFromGet( names, results );
134         printElapsed( "testGetAllConfigDottedNames", start );
135     }
136     
137         public void
138     testGetAllMonitoringDottedNames()
139     {
140         if ( checkNotOffline( "testMonitoringRefresh" ) )
141         {
142             final MonitoringDottedNames dottedNames = getDomainRoot().getMonitoringDottedNames();
143             final long start = now();
144             final String JavaDoc[] names = getAllNames( dottedNames );
145             
146             final Object JavaDoc[] results = dottedNames.dottedNameGet( names );
147             
148             checkResultsFromGet( names, results );
149             printElapsed( "testGetAllMonitoringDottedNames", start );
150         }
151     }
152     
153         public void
154     testWildGetAllConfigDottedNames()
155     {
156         final long start = now();
157         final ConfigDottedNames dottedNames = getDomainRoot().getConfigDottedNames();
158         
159         final Attribute JavaDoc[] results = (Attribute JavaDoc[])dottedNames.dottedNameGet( "*" );
160         checkResultsFromWildGet( results );
161         printElapsed( "testWildGetAllConfigDottedNames", start );
162     }
163     
164     
165         public void
166     testWildGetAllMonitoringDottedNames()
167     {
168         if ( checkNotOffline( "testMonitoringRefresh" ) )
169         {
170             final long start = now();
171             final MonitoringDottedNames dottedNames = getDomainRoot().getMonitoringDottedNames();
172             final Attribute JavaDoc[] results = (Attribute JavaDoc[])dottedNames.dottedNameGet( "*" );
173             checkResultsFromWildGet( results );
174             printElapsed( "testWildGetAllMonitoringDottedNames", start );
175         }
176     }
177     
178     /**
179         Test that we can set (change) a dotted name.
180      */

181         public void
182     testConfigDottedNameSet()
183     {
184         final long start = now();
185         
186         final ConfigDottedNames dottedNames = getDomainRoot().getConfigDottedNames();
187         
188         final String JavaDoc target = "domain.locale";
189         final Object JavaDoc result = dottedNames.dottedNameGet( target );
190         
191         final Attribute JavaDoc localeAttr = (Attribute JavaDoc)dottedNames.dottedNameGet( target );
192         checkAttribute( localeAttr );
193         
194         final String JavaDoc locale = (String JavaDoc)localeAttr.getValue();
195         
196         // set to a new value
197
Object JavaDoc[] results = dottedNames.dottedNameSet( new String JavaDoc[] { target + "=dummy_locale" } );
198         assert( results.length == 1 );
199         checkAttribute( (Attribute JavaDoc)results[ 0 ] );
200         
201         // change back to previous value
202
final String JavaDoc restoreString = target + "=" + (locale == null ? "" : locale);
203         results = dottedNames.dottedNameSet( new String JavaDoc[] { restoreString } );
204         
205         final Attribute JavaDoc finalAttr = (Attribute JavaDoc)dottedNames.dottedNameGet( target );
206         assert( (finalAttr.getValue() == null && localeAttr.getValue() == null) ||
207             finalAttr.getValue().equals( localeAttr.getValue() ) );
208         printElapsed( "testConfigDottedNameSet", start );
209     }
210     
211         public void
212     testConfigRefresh()
213     {
214         final long start = now();
215         getDomainRoot().getConfigDottedNames().refresh();
216         printElapsed( "testConfigRefresh", start );
217     }
218     
219         public void
220     testMonitoringRefresh()
221     {
222         if ( checkNotOffline( "testMonitoringRefresh" ) )
223         {
224             final MonitoringDottedNames dottedNames = getDomainRoot().getMonitoringDottedNames();
225         
226             final long start = now();
227             dottedNames.refresh();
228             printElapsed( "testMonitoringRefresh", start );
229         }
230     }
231     
232         private int
233     testList( final DottedNames dottedNames, final String JavaDoc dottedName )
234     {
235         final Object JavaDoc[] results = dottedNames.dottedNameList( new String JavaDoc[] { dottedName } );
236         
237         //trace( dottedName + ": " + toString( results ) );
238
for( int i = 0; i < results.length; ++i )
239         {
240             testList( dottedNames, (String JavaDoc)results[ i ] );
241         }
242
243         return( results.length );
244     }
245     
246         public void
247     testRecursiveConfigDottedNameList()
248     {
249         final long start = now();
250         final ConfigDottedNames dottedNames = getDomainRoot().getConfigDottedNames();
251         
252         final int numFound = testList( dottedNames, "domain" );
253         assert( numFound >= 4 ); // should be at least 4.
254
printElapsed( "testRecursiveConfigDottedNameList", start );
255     }
256     
257         public void
258     testRecursiveMonitoringDottedNameList()
259     {
260         if ( checkNotOffline( "testRecursiveMonitoringDottedNameList" ) )
261         {
262             final MonitoringDottedNames dottedNames = getDomainRoot().getMonitoringDottedNames();
263         
264             final long start = now();
265             
266             final int numFound = testList( dottedNames, "server" );
267             assert( numFound >= 4 ); // should be at least 4.\
268

269             testList( dottedNames, "*" );
270             
271             printElapsed( "testRecursiveMonitoringDottedNameList", start );
272         }
273     }
274 }
275
276
277
278
279
280
281
282
283
284
285
286
Popular Tags