KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29
30 import com.sun.appserv.management.config.PropertiesAccess;
31 import com.sun.appserv.management.base.AMX;
32 import com.sun.appserv.management.base.Util;
33 import com.sun.appserv.management.base.XTypes;
34
35 import com.sun.appserv.management.config.ResourceConfig;
36
37 import com.sun.appserv.management.util.misc.GSetUtil;
38
39
40 import com.sun.enterprise.management.AMXTestBase;
41 import com.sun.enterprise.management.Capabilities;
42 import com.sun.enterprise.management.TestUtil;
43
44 /**
45  */

46 public final class PropertiesAccessTest extends AMXTestBase
47 {
48         public
49     PropertiesAccessTest( )
50     {
51     }
52     
53         private Set JavaDoc<ObjectName JavaDoc>
54     getAllImplementorsOfProperties()
55         throws Exception JavaDoc
56     {
57         final Set JavaDoc<AMX> amxs = getQueryMgr().queryInterfaceSet(
58             PropertiesAccess.class.getName(), null);
59         
60         return( TestUtil.newSortedSet( Util.toObjectNames( amxs ) ) );
61             
62     }
63     
64     
65         private void
66     testCreateEmptyProperty( final PropertiesAccess props )
67     {
68         final String JavaDoc NAME = "test.empty";
69         
70         props.createProperty( NAME, "" );
71         assert( props.existsProperty( NAME ) );
72         props.removeProperty( NAME );
73         assert( ! props.existsProperty( NAME ) );
74     }
75     
76         private void
77     testPropertiesGet( final PropertiesAccess props )
78     {
79         final String JavaDoc[] propNames = props.getPropertyNames();
80         
81         for( final String JavaDoc name : propNames )
82         {
83             assert( props.existsProperty( name ) );
84             final String JavaDoc value = props.getPropertyValue( name );
85         }
86     }
87     
88         private void
89     testPropertiesSetToSameValue( final PropertiesAccess props )
90     {
91         final String JavaDoc[] propNames = props.getPropertyNames();
92         
93         // get each property, set it to the same value, the verify
94
// it's the same.
95
for( int i = 0; i < propNames.length; ++i )
96         {
97             final String JavaDoc propName = propNames[ i ];
98             
99             assert( props.existsProperty( propName ) );
100             final String JavaDoc value = props.getPropertyValue( propName );
101             props.setPropertyValue( propName, value );
102             
103             assert( props.getPropertyValue( propName ).equals( value ) );
104         }
105     }
106     
107     /**
108         Adding or removing test properties to these types does not
109         cause any side effects. Plus, there is no need to test
110         every MBean.
111      */

112     private static final Set JavaDoc<String JavaDoc> TEST_CREATE_REMOVE_TYPES =
113         GSetUtil.newUnmodifiableStringSet(
114             XTypes.DOMAIN_CONFIG,
115             XTypes.CONFIG_CONFIG,
116             XTypes.PROFILER_CONFIG,
117             XTypes.STANDALONE_SERVER_CONFIG,
118             XTypes.CLUSTERED_SERVER_CONFIG,
119             XTypes.ORB_CONFIG,
120             XTypes.MODULE_MONITORING_LEVELS_CONFIG,
121             XTypes.NODE_AGENT_CONFIG
122             );
123         
124         private void
125     testPropertiesCreateRemove( final PropertiesAccess props )
126     {
127         final String JavaDoc[] propNames = props.getPropertyNames();
128
129         final AMX amx = Util.asAMX( props );
130         final String JavaDoc j2eeType = amx.getJ2EEType();
131         if ( ! TEST_CREATE_REMOVE_TYPES.contains( j2eeType ) )
132         {
133             return;
134         }
135
136         // add some properties, then delete them
137
final int numToAdd = 1;
138         final long now = System.currentTimeMillis();
139         for( int i = 0; i < numToAdd; ++i )
140         {
141             final String JavaDoc testName = "__junittest_" + i + now;
142             
143             if ( props.existsProperty( testName ) )
144             {
145                 failure( "test property already exists: " + testName );
146             }
147             
148             props.createProperty( testName, "value_" + i );
149             assert( props.existsProperty( testName ) );
150         }
151         final int numProps = props.getPropertyNames().length;
152         
153         if ( numProps != numToAdd + propNames.length )
154         {
155             failure( "expecting " + numProps + " have " + numToAdd + propNames.length );
156         }
157         
158         
159         // remove the ones we added
160
for( int i = 0; i < numToAdd; ++i )
161         {
162             final String JavaDoc testName = "__junittest_" + i + now;
163             
164             props.removeProperty( testName );
165             assert( ! props.existsProperty( testName ) );
166         }
167         
168         assert( props.getPropertyNames().length == propNames.length );
169     }
170     
171         public void
172     checkGetProperties( final ObjectName JavaDoc src )
173         throws Exception JavaDoc
174     {
175         final AMX proxy = getProxy( src );
176         
177         if ( ! (proxy instanceof PropertiesAccess) )
178         {
179             throw new IllegalArgumentException JavaDoc(
180                 "MBean does not implement PropertiesAccess: " + quote( src ) );
181         }
182         
183         final PropertiesAccess props = (PropertiesAccess)proxy;
184         testPropertiesGet( props );
185     }
186     
187         public void
188     checkSetPropertiesSetToSameValue( final ObjectName JavaDoc src )
189         throws Exception JavaDoc
190     {
191         final PropertiesAccess props = (PropertiesAccess)getProxy( src );
192         
193         testPropertiesSetToSameValue( props );
194     }
195     
196     
197         public void
198     checkCreateRemove( final ObjectName JavaDoc src )
199         throws Exception JavaDoc
200     {
201         final PropertiesAccess props = (PropertiesAccess)getProxy( src );
202         
203         testPropertiesCreateRemove( props );
204     }
205
206         public synchronized void
207     testPropertiesGet()
208         throws Exception JavaDoc
209     {
210         final Set JavaDoc<ObjectName JavaDoc> all = getAllImplementorsOfProperties();
211         
212         testAll( all, "checkGetProperties" );
213     }
214     
215         public synchronized void
216     testPropertiesSetToSameValue()
217         throws Exception JavaDoc
218     {
219         final Set JavaDoc<ObjectName JavaDoc> all = getAllImplementorsOfProperties();
220         
221         testAll( all, "checkSetPropertiesSetToSameValue" );
222     }
223     
224     
225         public synchronized void
226     testPropertiesCreateRemove()
227         throws Exception JavaDoc
228     {
229         if ( checkNotOffline( "testPropertiesCreateRemove" ) )
230         {
231             final Set JavaDoc<ObjectName JavaDoc> all = getAllImplementorsOfProperties();
232                 
233             testAll( all, "checkCreateRemove" );
234         }
235     }
236     
237 }
238
239
240
Popular Tags