1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanOperationInfo ; 31 32 import com.sun.appserv.management.config.DomainConfig; 33 import com.sun.appserv.management.util.jmx.JMXUtil; 34 35 37 public final class CoverageInfoTest extends junit.framework.TestCase 38 { 39 public 40 CoverageInfoTest() 41 { 42 } 43 44 45 public CoverageInfoImpl 46 create( final MBeanInfo info ) 47 { 48 return new CoverageInfoImpl( info ); 49 } 50 51 52 public void 53 testCreate() 54 { 55 CoverageInfoImpl impl = create( null ); 56 57 try { impl.getNumReadableAttributes(); assert( false ); } catch( Exception e ) {} 59 try { impl.getNumWriteableAttributes(); assert( false ); } catch( Exception e ) {} 60 try { impl.getNumWriteableAttributes(); assert( false ); } catch( Exception e ) {} 61 62 impl = create( getTestMBeanInfo() ); 63 assert( impl.getReadableAttributes() != null ); 64 assert( impl.getWriteableAttributes() != null ); 65 assert( impl.getOperations() != null ); 66 assert impl.getNumReadableAttributes() != 0; 67 assert impl.getNumWriteableAttributes() != 0; 68 assert impl.getNumOperations() != 0; 69 assert impl.getAttributesRead().size() == 0; 70 assert impl.getAttributesWritten().size() == 0; 71 assert impl.getOperationsInvoked().size() == 0; 72 } 73 74 private MBeanInfo 75 getTestMBeanInfo() 76 { 77 return MBeanInfoConverter.getInstance().convert( DomainConfig.class, null ); 78 } 79 80 public void 81 testAttributes() 82 { 83 final MBeanInfo mbeanInfo = getTestMBeanInfo(); 84 85 final CoverageInfoImpl impl = create( mbeanInfo ); 86 assert( mbeanInfo == impl.getMBeanInfo() ); 87 88 final MBeanAttributeInfo [] attrInfos = mbeanInfo.getAttributes(); 89 90 for( final MBeanAttributeInfo attrInfo : attrInfos ) 91 { 92 final String name = attrInfo.getName(); 93 94 if ( attrInfo.isReadable() ) 95 { 96 assert( impl.getReadableAttributes().contains( name ) ); 97 impl.attributeWasRead( name ); 98 assert( impl.getAttributesRead().contains( name ) ); 99 assert( ! impl.getAttributesNotRead().contains( name ) ); 100 } 101 102 if ( attrInfo.isWritable() ) 103 { 104 assert( impl.getWriteableAttributes().contains( name ) ); 105 impl.attributeWasWritten( name ); 106 assert( impl.getAttributesWritten().contains( name ) ); 107 assert( ! impl.getAttributesNotWritten().contains( name ) ); 108 } 109 } 110 111 assert( impl.getAttributesNotRead().size() == 0 ); 112 assert( impl.getAttributeReadCoverage() == 100 ); 113 assert( impl.getAttributeGetFailures().size() == 0 ); 114 115 assert( impl.getAttributesNotWritten().size() == 0 ); 116 assert( impl.getAttributeWriteCoverage() == 100 ); 117 assert( impl.getAttributeSetFailures().size() == 0 ); 118 119 120 final String BOGUS = "bogus"; 121 impl.attributeWasRead( BOGUS ); 122 impl.attributeWasWritten( BOGUS ); 123 124 assert( impl.getUnknownAttributes().keySet().contains( BOGUS ) ); 125 assert( impl.getUnknownAttributes().keySet().size() == 1 ); 126 assert( impl.getAttributeGetFailures().size() == 0 ); 127 assert( impl.getAttributeSetFailures().size() == 0 ); 128 129 final MBeanAttributeInfo attr = attrInfos[ 0 ]; 130 impl.attributeGetFailure( attr.getName() ); 131 impl.attributeGetFailure( attr.getName() ); 132 assert( impl.getAttributeGetFailures().size() == 1 ); 133 impl.attributeSetFailure( attr.getName() ); 134 impl.attributeSetFailure( attr.getName() ); 135 assert( impl.getAttributeSetFailures().size() == 1 ); 136 } 137 138 139 public void 140 testOperations() 141 { 142 final MBeanInfo mbeanInfo = getTestMBeanInfo(); 143 final MBeanOperationInfo [] operationInfos = mbeanInfo.getOperations(); 144 145 final CoverageInfoImpl impl = create( mbeanInfo ); 146 assert( mbeanInfo == impl.getMBeanInfo() ); 147 148 151 for( final MBeanOperationInfo operationInfo : operationInfos ) 152 { 153 final String name = operationInfo.getName(); 154 final String [] sig = JMXUtil.getSignature( operationInfo.getSignature() ); 155 156 impl.operationWasInvoked( name, sig ); 157 } 158 159 assert( impl.getOperationCoverage() == 100 ) : 160 "Expected coverage of 100%, got " + impl.getOperationCoverage(); 161 162 assert( impl.getUnknownOperations().size() == 0 ); 163 assert( impl.getOperationsNotInvoked().size() == 0 ); 164 assert( impl.getInvocationFailures().size() == 0 ); 165 impl.toString( true ); 166 impl.toString( false ); 167 168 169 172 final Set <String > invoked = impl.getOperationsInvoked(); 173 impl.clear(); 174 for( final String op : invoked ) 175 { 176 impl.markAsInvoked( op ); 177 } 178 179 assert( impl.getOperationCoverage() == 100 ) : 180 "Expected coverage of 100%, got " + impl.getOperationCoverage(); 181 assert( impl.getUnknownOperations().size() == 0 ); 182 assert( impl.getOperationsNotInvoked().size() == 0 ); 183 assert( impl.getInvocationFailures().size() == 0 ); 184 185 final String DUMMY_OPERATION = "dummyOperationName"; 186 impl.operationWasInvoked( DUMMY_OPERATION, null ); 187 impl.operationWasInvoked( DUMMY_OPERATION, null ); 188 assert( impl.getUnknownOperations().size() == 1 ); 189 impl.toString( true ); 190 impl.toString( false ); 191 192 193 impl.clear(); 196 for( final MBeanOperationInfo operationInfo : operationInfos ) 197 { 198 final String name = operationInfo.getName(); 199 final String [] sig = JMXUtil.getSignature( operationInfo.getSignature() ); 200 201 impl.operationWasInvoked( name, sig ); 202 impl.operationFailed( name, sig ); 203 } 204 assert( impl.getOperationCoverage() == 100 ) : 205 "Expected coverage of 100%, got " + impl.getOperationCoverage(); 206 assert( impl.getUnknownOperations().size() == 0 ); 207 assert( impl.getOperationsNotInvoked().size() == 0 ); 208 assert( impl.getOperationsInvoked().size() == operationInfos.length ); 209 assert( impl.getInvocationFailures().size() == operationInfos.length ); 210 impl.toString( true ); 211 impl.toString( false ); 212 213 214 try 217 { 218 impl.operationFailed( "foo", null ); 219 assert( false ) : "expected failure when calling operationFailed()"; 220 } 221 catch( IllegalArgumentException e ) 222 { 223 } 224 impl.toString( true ); 225 impl.toString( false ); 226 impl.toString(); 227 } 228 } 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | Popular Tags |