KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import com.sun.appserv.management.config.AMXConfig;
33 import com.sun.appserv.management.config.AuthRealmConfig;
34 import com.sun.appserv.management.config.SecurityServiceConfig;
35 import static com.sun.appserv.management.config.AuthRealmConfig.*;
36
37 import com.sun.appserv.management.base.Container;
38 import com.sun.appserv.management.base.XTypes;
39 import com.sun.appserv.management.base.Util;
40 import com.sun.appserv.management.util.misc.GSetUtil;
41 import com.sun.appserv.management.util.misc.CollectionUtil;
42
43
44 /**
45  */

46 public final class AuthRealmConfigTest extends ConfigMgrTestBase
47 {
48         public
49     AuthRealmConfigTest()
50     {
51         if ( checkNotOffline( "ensureDefaultInstance" ) )
52         {
53             ensureDefaultInstance( getConfigConfig().getSecurityServiceConfig() );
54         }
55     }
56     
57     private static boolean WARNED_TESTABLE = false;
58     
59     private static final String JavaDoc TEMPLATE_PREFIX = "${";
60     
61         private synchronized Map JavaDoc<String JavaDoc,AuthRealmConfig>
62     getTestableAuthRealms()
63     {
64         final Map JavaDoc<String JavaDoc,AuthRealmConfig> m =
65             getConfigConfig().getSecurityServiceConfig().getAuthRealmConfigMap();
66         
67         final Map JavaDoc<String JavaDoc,AuthRealmConfig> std = new HashMap JavaDoc<String JavaDoc,AuthRealmConfig>();
68         final List JavaDoc<String JavaDoc> warnings = new ArrayList JavaDoc<String JavaDoc>();
69         for( final String JavaDoc name : m.keySet() )
70         {
71             final AuthRealmConfig c = m.get( name );
72             
73             if ( AuthRealmConfig.DEFAULT_REALM_CLASSNAME.equals( c.getClassname() ) )
74             {
75                 try
76                 {
77                     final String JavaDoc file = c.getPropertyValue( "file" );
78                     if ( file == null )
79                     {
80                         warnings.add( "Realm " + name +
81                             " does not have a 'file' property (test skipped)" );
82                     }
83                     else if ( file.indexOf( TEMPLATE_PREFIX ) >= 0 )
84                     {
85                         warnings.add( "Realm " + name +
86                             " uses a ${...} name, not yet supported (test skipped)" );
87                     }
88                     else
89                     {
90                         std.put( c.getName(), c );
91                     }
92                 }
93                 catch( Exception JavaDoc e )
94                 {
95                 }
96             }
97         }
98         
99         if ( ! WARNED_TESTABLE )
100         {
101             WARNED_TESTABLE = true;
102             warning( NEWLINE + CollectionUtil.toString( warnings, NEWLINE ) + NEWLINE +
103             "Realms which WILL be tested: {" + CollectionUtil.toString( m.keySet() ) + "}");
104         }
105         
106         return std;
107     }
108     
109         public synchronized void
110     testGetters()
111     {
112         final Map JavaDoc<String JavaDoc,AuthRealmConfig> arcMap = getTestableAuthRealms();
113         
114         for( final AuthRealmConfig ar : arcMap.values() )
115         {
116             ar.getName();
117             final String JavaDoc classname = ar.getClassname();
118             if ( classname != null )
119             {
120                 ar.setClassname( classname );
121             }
122         }
123     }
124     
125         private boolean
126     userExists( final AuthRealmConfig config, final String JavaDoc user )
127     {
128         return GSetUtil.newStringSet( (String JavaDoc[])config.getUserNames() ).contains( user );
129     }
130     
131         public synchronized void
132     testAddRemoveUpdateUser()
133         throws Exception JavaDoc
134     {
135         final Map JavaDoc<String JavaDoc,AuthRealmConfig> arcMap = getTestableAuthRealms();
136         
137         final String JavaDoc USER = "test";
138         
139         final Set JavaDoc<AuthRealmConfig> failures = new HashSet JavaDoc<AuthRealmConfig>();
140         
141         for( final AuthRealmConfig ar : arcMap.values() )
142         {
143             //printVerbose( "TESTING: " + ar.getName() );
144

145             try
146             {
147                 ar.getUserNames();
148                 
149                 if ( userExists( ar, USER ) )
150                 {
151                     ar.removeUser( USER );
152                 }
153                 
154                 ar.addUser( USER, "foo-pwd", null );
155                 assert( userExists( ar, USER ) );
156                 ar.updateUser( USER, "foo-pwd2", null );
157                 assert( userExists( ar, USER ) );
158                 ar.removeUser( USER );
159                 assert( ! userExists( ar, USER ) );
160                 //printVerbose( "SUCCESS testing: " + ar.getName() );
161
}
162             catch( Exception JavaDoc e )
163             {
164                 trace( "" );
165                 trace( "" );
166                 trace( "FAILURE FOR: " + ar.getName() );
167                 //e.printStackTrace();
168
failures.add( ar );
169             }
170         }
171         
172         if ( failures.size() != 0 )
173         {
174             final Set JavaDoc<String JavaDoc> names = Util.getNames( failures );
175             
176             warning( "testAddRemoveUpdateUser failed on the following realms: " +
177                 CollectionUtil.toString( names ) );
178             assert( false );
179         }
180         
181     }
182
183         public synchronized void
184     testGetGroupNames()
185     {
186         final Map JavaDoc<String JavaDoc,AuthRealmConfig> arcMap = getTestableAuthRealms();
187         
188         for( final AuthRealmConfig ar : arcMap.values() )
189         {
190             ar.getGroupNames();
191         }
192     }
193
194         public synchronized void
195     testGetUserGroupNames()
196     {
197         final Map JavaDoc<String JavaDoc,AuthRealmConfig> arcMap = getTestableAuthRealms();
198         
199         for( final AuthRealmConfig ar : arcMap.values() )
200         {
201             final String JavaDoc[] users = ar.getUserNames();
202             for( final String JavaDoc user : users )
203             {
204                 ar.getUserGroupNames( user );
205             }
206         }
207     }
208     
209     
210         public static String JavaDoc
211     getDefaultInstanceName()
212     {
213         return getDefaultInstanceName( "AuthRealmConfig" );
214     }
215     
216         public static AuthRealmConfig
217     ensureDefaultInstance( final SecurityServiceConfig securityServiceConfig )
218     {
219         AuthRealmConfig result =
220             securityServiceConfig.getAuthRealmConfigMap().get( getDefaultInstanceName() );
221         
222         if ( result == null )
223         {
224             result = createInstance( securityServiceConfig,
225                 getDefaultInstanceName(),
226                 DEFAULT_REALM_CLASSNAME,
227                 KEY_FILE_PREFIX + "default-instance-test" );
228         }
229         
230         return result;
231     }
232     
233         public static AuthRealmConfig
234     createInstance(
235         final SecurityServiceConfig securityServiceConfig,
236         final String JavaDoc name,
237         final String JavaDoc classname,
238         final String JavaDoc keyFile )
239     {
240         final Map JavaDoc<String JavaDoc,String JavaDoc> options = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
241         
242         options.put( KEY_FILE_PROPERTY_KEY, keyFile );
243         options.put( JAAS_CONTEXT_PROPERTY_KEY, "dummy-jaas-context-value" );
244         
245         return securityServiceConfig.createAuthRealmConfig( name, classname, options);
246     }
247     
248     
249     
250         protected Container
251     getProgenyContainer()
252     {
253         return getConfigConfig().getSecurityServiceConfig();
254     }
255
256         protected String JavaDoc
257     getProgenyJ2EEType()
258     {
259         return XTypes.AUTH_REALM_CONFIG;
260     }
261
262
263         protected void
264     removeProgeny( final String JavaDoc name )
265     {
266         getConfigConfig().getSecurityServiceConfig().removeAuthRealmConfig( name );
267     }
268     
269     
270         protected final AMXConfig
271     createProgeny(final String JavaDoc name, final Map JavaDoc<String JavaDoc,String JavaDoc> options )
272     {
273         if ( name.indexOf( "Illegal" ) >= 0 )
274         {
275             // MBean doesn't detect illegal parameters; anything is allowed
276
// for the realm.
277
throw new IllegalArgumentException JavaDoc();
278         }
279         
280         return createInstance( getConfigConfig().getSecurityServiceConfig(),
281             name, DEFAULT_REALM_CLASSNAME, name + "-keyfile" );
282     }
283 }
284
285
286
Popular Tags