1 23 package com.sun.enterprise.management.config; 24 25 import java.util.Set ; 26 import java.util.Map ; 27 import java.util.Iterator ; 28 29 import javax.management.ObjectName ; 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 46 public final class ManagementRulesConfigTest extends AMXTestBase 47 { 48 private static final String 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 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 <String ,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 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 name, 124 final String eventType, 125 final String 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 name ) 136 { 137 getManagementRulesConfig().removeManagementRuleConfig( name ); 138 } 139 private void 140 testEventConfig( final EventConfig eventConfig ) 141 { 142 assert( eventConfig != null ); 143 144 final String 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 mbeanName = actionConfig.getActionMBeanName(); 160 actionConfig.setActionMBeanName( mbeanName ); 161 } 162 } 163 164 165 public ManagementRuleConfig 166 createAndTestRule( final int id ) 167 throws Exception 168 { 169 final ManagementRulesConfig rules = getManagementRulesConfig(); 170 171 final String 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 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 e ) 197 { 198 } 200 201 try 202 { 203 ruleConfig.createActionConfig( null ); 204 } 205 catch( Exception e ) 206 { 207 } 209 210 return ruleConfig; 211 } 212 213 public void 214 testCreateRule() 215 throws Exception 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 |