KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import java.io.IOException JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33
34 import com.sun.appserv.management.config.SystemPropertiesAccess;
35 import com.sun.appserv.management.base.AMX;
36 import com.sun.appserv.management.base.Util;
37
38
39 import com.sun.enterprise.management.AMXTestBase;
40 import com.sun.enterprise.management.Capabilities;
41
42 /**
43  */

44 public final class SystemPropertiesAccessTest extends AMXTestBase
45 {
46         public
47     SystemPropertiesAccessTest( )
48     {
49     }
50     
51         private Set JavaDoc<ObjectName JavaDoc>
52     getAll()
53         throws Exception JavaDoc
54     {
55         final Set JavaDoc<ObjectName JavaDoc> objectNames =
56             getQueryMgr().queryInterfaceObjectNameSet(
57                 SystemPropertiesAccess.class.getName(), null);
58         
59         return( objectNames );
60     }
61     
62     
63         private void
64     checkPropertiesGet( final SystemPropertiesAccess props )
65     {
66         final Map JavaDoc<String JavaDoc,String JavaDoc> all = props.getSystemProperties();
67         
68         final String JavaDoc[] propNames = props.getSystemPropertyNames();
69         
70         for( final String JavaDoc name : propNames )
71         {
72             assert( props.existsSystemProperty( name ) );
73             final String JavaDoc value = props.getSystemPropertyValue( name );
74         }
75     }
76     
77         private void
78     testPropertiesSetToSameValue( final SystemPropertiesAccess props )
79     {
80         final String JavaDoc[] propNames = props.getSystemPropertyNames();
81         
82         // get each property, set it to the same value, the verify
83
// it's the same.
84
for( int i = 0; i < propNames.length; ++i )
85         {
86             final String JavaDoc propName = propNames[ i ];
87             
88             final String JavaDoc value = props.getSystemPropertyValue( propName );
89             props.setSystemPropertyValue( propName, value );
90             
91             assert( props.getSystemPropertyValue( propName ).equals( value ) );
92         }
93     }
94     
95         private void
96     testCreateEmptySystemProperty( final SystemPropertiesAccess props )
97     {
98         final String JavaDoc NAME = "test.empty";
99         
100         props.createSystemProperty( NAME, "" );
101         assert( props.existsSystemProperty( NAME ) );
102         props.removeSystemProperty( NAME );
103         assert( ! props.existsSystemProperty( NAME ) );
104     }
105     
106         private void
107     testSystemPropertiesCreateRemove( final SystemPropertiesAccess props )
108     {
109         final String JavaDoc[] propNames = props.getSystemPropertyNames();
110         
111         // add some properties, then delete them
112
final int numToAdd = 1;
113         final long now = System.currentTimeMillis();
114         for( int i = 0; i < numToAdd; ++i )
115         {
116             final String JavaDoc testName = "__junittest_" + i + now;
117             
118             if ( props.existsSystemProperty( testName ) )
119             {
120                 failure( "test property already exists: " + testName );
121             }
122             
123             props.createSystemProperty( testName, "value_" + i );
124             assert( props.existsSystemProperty( testName ) );
125         }
126         final int numProps = props.getSystemPropertyNames().length;
127         
128         if ( numProps != numToAdd + propNames.length )
129         {
130             failure( "expecting " + numProps + " have " + numToAdd + propNames.length );
131         }
132         
133         
134         // remove the ones we added
135
for( int i = 0; i < numToAdd; ++i )
136         {
137             final String JavaDoc testName = "__junittest_" + i + now;
138             
139             props.removeSystemProperty( testName );
140             assert( ! props.existsSystemProperty( testName ) );
141         }
142         
143         assert( props.getSystemPropertyNames().length == propNames.length );
144         
145     }
146     
147         public synchronized void
148     checkGetProperties( final ObjectName JavaDoc src )
149         throws Exception JavaDoc
150     {
151         final AMX proxy = getProxy( src, AMX.class);
152         
153         if ( ! (proxy instanceof SystemPropertiesAccess) )
154         {
155             throw new IllegalArgumentException JavaDoc(
156                 "MBean does not implement SystemPropertiesAccess: " + quote( src ) );
157         }
158         
159         final SystemPropertiesAccess props = (SystemPropertiesAccess)proxy;
160         checkPropertiesGet( props );
161     }
162     
163         public void
164     checkSetPropertiesSetToSameValue( final ObjectName JavaDoc src )
165         throws Exception JavaDoc
166     {
167         final SystemPropertiesAccess props = getProxy( src, SystemPropertiesAccess.class );
168         
169         testPropertiesSetToSameValue( props );
170     }
171     
172     
173         public void
174     checkCreateRemove( final ObjectName JavaDoc src )
175         throws Exception JavaDoc
176     {
177         final SystemPropertiesAccess props =
178             getProxy( src, SystemPropertiesAccess.class );
179         
180         testSystemPropertiesCreateRemove( props );
181     }
182
183         public synchronized void
184     testPropertiesGet()
185         throws Exception JavaDoc
186     {
187         final Set JavaDoc<ObjectName JavaDoc> all = getAll();
188         
189         testAll( all, "checkGetProperties" );
190     }
191     
192         public synchronized void
193     testPropertiesSetToSameValue()
194         throws Exception JavaDoc
195     {
196         final Set JavaDoc<ObjectName JavaDoc> all = getAll();
197         
198         testAll( all, "checkSetPropertiesSetToSameValue" );
199     }
200     
201     
202         public synchronized void
203     testCreateRemove()
204         throws Exception JavaDoc
205     {
206         if ( checkNotOffline( "testCreateRemove" ) )
207         {
208             final Set JavaDoc<ObjectName JavaDoc> all = getAll();
209             testAll( all, "checkCreateRemove" );
210         }
211     }
212     
213 }
214
215
216
Popular Tags