KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
28
29 import javax.management.ObjectName JavaDoc;
30
31 import com.sun.appserv.management.config.NamedConfigElement;
32 import com.sun.appserv.management.config.DomainConfig;
33
34 import com.sun.enterprise.management.AMXTestBase;
35 import com.sun.enterprise.management.Capabilities;
36 import com.sun.appserv.management.base.AMX;
37 import com.sun.appserv.management.config.*;
38
39
40 import com.sun.appserv.management.util.misc.StringUtil;
41 import com.sun.appserv.management.base.Util;
42
43
44 /**
45  */

46 public final class ManagementRulesConfigTest extends AMXTestBase
47 {
48     //private static final String ACTION_MBEAN_NAME =
49
//"user:type=rule-action,name=" + CustomMBeanConfigTest.getDefaultInstanceName();
50
private static final String JavaDoc ACTION_MBEAN_NAME = "com.foo.Bar";
51     
52         public
53     ManagementRulesConfigTest()
54     {
55         if ( checkNotOffline( "ensureDefaultInstance" ) )
56         {
57             ensureDefaultInstance( getConfigConfig() );
58         }
59     }
60     
61         public static String JavaDoc
62     getDefaultInstanceName()
63     {
64         return getDefaultInstanceName( "ManagementRulesConfigTest" );
65     }
66     
67         public static ManagementRuleConfig
68     ensureDefaultInstance( final ConfigConfig config )
69     {
70         final ManagementRulesConfig rules = getManagementRulesConfig(config);
71         
72         ManagementRuleConfig result =
73             rules.getManagementRuleConfigMap().get( getDefaultInstanceName() );
74         
75         if ( result == null )
76         {
77             result = createRule( rules, getDefaultInstanceName(), EventTypeValues.TRACE, ACTION_MBEAN_NAME );
78         }
79         
80         return result;
81     }
82     
83         private static ManagementRulesConfig
84     getManagementRulesConfig( final ConfigConfig config )
85     {
86         return config.getManagementRulesConfig();
87     }
88     
89     
90         private ManagementRulesConfig
91     getManagementRulesConfig( )
92     {
93         return getManagementRulesConfig( getConfigConfig() );
94     }
95     
96         public void
97     testGet()
98     {
99         final ManagementRulesConfig rulesConfig = getManagementRulesConfig();
100         
101         if ( rulesConfig != null )
102         {
103             final Map JavaDoc<String JavaDoc,ManagementRuleConfig> ruleConfigs =
104                 rulesConfig.getManagementRuleConfigMap();
105             assert( ruleConfigs != null );
106         }
107         else
108         {
109             warning( "testGet: ManagementRulesConfig is null...skipping test" );
110         }
111     }
112     
113     
114         private String JavaDoc
115     createName( final int i )
116     {
117         return "rule" + i;
118     }
119     
120         private static ManagementRuleConfig
121     createRule(
122         final ManagementRulesConfig rules,
123         final String JavaDoc name,
124         final String JavaDoc eventType,
125         final String JavaDoc actionMBeanName )
126     {
127         final ManagementRuleConfig rule =
128             rules.createManagementRuleConfig(
129                 name, eventType, actionMBeanName, null );
130         
131         return rule;
132     }
133     
134         private void
135     removeRule( final String JavaDoc name )
136     {
137         getManagementRulesConfig().removeManagementRuleConfig( name );
138     }
139         private void
140     testEventConfig( final EventConfig eventConfig )
141     {
142         assert( eventConfig != null );
143         
144         final String JavaDoc eventType = eventConfig.getType();
145         eventConfig.setType( eventType );
146         eventConfig.setDescription( "test description" );
147     }
148     
149         private void
150     testRuleConfig( final ManagementRuleConfig ruleConfig )
151     {
152         final EventConfig eventConfig = ruleConfig.getEventConfig();
153         assert( eventConfig != null );
154         testEventConfig( eventConfig );
155         
156         final ActionConfig actionConfig = ruleConfig.getActionConfig();
157         if ( actionConfig != null )
158         {
159             final String JavaDoc mbeanName = actionConfig.getActionMBeanName();
160             actionConfig.setActionMBeanName( mbeanName );
161         }
162     }
163     
164     
165         public ManagementRuleConfig
166     createAndTestRule( final int id )
167         throws Exception JavaDoc
168     {
169         final ManagementRulesConfig rules = getManagementRulesConfig();
170         
171         final String JavaDoc name = createName( id );
172         
173         if ( rules.getManagementRuleConfigMap().get( name ) != null )
174         {
175             removeRule( name );
176             warning( "Removed left over ManagementRuleConfig: " + StringUtil.quote( name ) );
177         }
178         
179         String JavaDoc actionMBeanName = ((id%2) ==0) ? ACTION_MBEAN_NAME : null;
180         
181         ManagementRuleConfig ruleConfig =
182             createRule( rules, name, EventTypeValues.TRACE, actionMBeanName );
183         
184         testRuleConfig( ruleConfig );
185         ActionConfig actionConfig = ruleConfig.getActionConfig();
186         if ( actionConfig == null )
187         {
188             actionConfig = ruleConfig.createActionConfig( ACTION_MBEAN_NAME );
189             testRuleConfig( ruleConfig );
190         }
191         
192         try
193         {
194             ruleConfig.createActionConfig( ACTION_MBEAN_NAME );
195         }
196         catch( Exception JavaDoc e )
197         {
198             // good, we expect to be here
199
}
200         
201         try
202         {
203             ruleConfig.createActionConfig( null );
204         }
205         catch( Exception JavaDoc e )
206         {
207             // good, we expect to be here
208
}
209         
210         return ruleConfig;
211     }
212     
213         public void
214     testCreateRule()
215         throws Exception JavaDoc
216     {
217         final ManagementRulesConfig rulesConfig = getManagementRulesConfig();
218         if ( rulesConfig == null )
219         {
220             warning( "testCreateRule: ManagementRulesConfig is null...skipping test" );
221             return;
222         }
223         
224         final int NUM = 6;
225         final ManagementRuleConfig[] ruleConfigs = new ManagementRuleConfig[ NUM ];
226         
227         try
228         {
229             for( int i = 0; i < NUM; ++i )
230             {
231                 ruleConfigs[ i ] = createAndTestRule( i );
232             }
233         }
234         finally
235         {
236             for( int i = 0; i < NUM; ++i )
237             {
238                 if ( ruleConfigs[ i ] != null )
239                 {
240                     removeRule( ruleConfigs[ i ].getName() );
241                 }
242             }
243         }
244     }
245     
246     
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
Popular Tags