1 7 package net.sourceforge.ejtools.management.test; 8 9 import java.util.Collection ; 10 import java.util.StringTokenizer ; 11 import java.util.Vector ; 12 13 import javax.management.MBeanAttributeInfo ; 14 import javax.management.MBeanInfo ; 15 import javax.management.MBeanOperationInfo ; 16 import javax.management.ObjectName ; 17 18 import net.sourceforge.ejtools.management.util.JSR77; 19 import net.sourceforge.ejtools.management.util.MEJB; 20 21 import org.apache.log4j.Category; 22 23 29 public class ManagedObjectTestCase extends ReportTestCase 30 { 31 32 protected Collection attributeInfos = new Vector (); 33 34 protected MBeanInfo info = null; 35 36 protected ObjectName objectName = null; 37 38 protected Collection operationInfos = new Vector (); 39 40 private static Category logger = Category.getInstance(ManagedObjectTestCase.class); 41 42 43 48 public ManagedObjectTestCase(ObjectName objectName) 49 { 50 this(objectName.getCanonicalName(), objectName); 51 } 52 53 54 60 public ManagedObjectTestCase(String name, ObjectName objectName) 61 { 62 super(name); 63 super.setFatal(false); 64 this.objectName = objectName; 65 66 } 67 68 public void testObjectName() { 69 this.addInfo("mbean.name", objectName.getCanonicalName()); 70 71 logger.debug("hasRequiredKeys..."); 72 vassert("Required keys found", "Required keys not found", JSR77.hasRequiredKeys(objectName)); 73 74 this.addInfo("mbean.managed", "" + JSR77.hasRequiredKeys(objectName)); 75 76 if(JSR77.hasRequiredKeys(objectName)) 78 { 79 this.addInfo("mbean.name", JSR77.extractName(objectName)); 80 this.addInfo("mbean.j2eeType", JSR77.extractJ2EEType(objectName)); 81 this.addInfo("mbean.generic", "" + JSR77.isGenericManagedObject(objectName)); 82 83 logger.debug("hasRequiredParentTypes..."); 84 vassert("Required parent keys found", "Required parent keys not found", JSR77.hasRequiredParentTypes(objectName)); 85 } 86 } 87 88 89 public void testAttributes() 90 { 91 if(JSR77.hasRequiredKeys(objectName)) 93 { 94 logger.debug("testAttributes..."); 95 96 vassert("objectName attribute found", "objectName attribute not found", testAttribute("objectName")); 97 vassert("eventProvider attribute found", "eventProvider attribute not found", testAttribute("eventProvider")); 98 vassert("stateManageable attribute found", "stateManageable attribute not found", testAttribute("stateManageable")); 99 vassert("statisticsProvider attribute found", "statisticsProvider attribute not found", testAttribute("statisticsProvider")); 100 101 String attributes = JSR77.getRequiredAttributes(objectName); 103 logger.debug("Attributes => " + attributes); 104 StringTokenizer st = new StringTokenizer (attributes, ","); 105 while(st.hasMoreTokens()) { 106 String attribute = st.nextToken(); 107 vassert(attribute+" attribute found", attribute +" attribute not found", testAttribute(attribute)); 108 } 109 } 110 } 111 112 113 public void testEventProvider() 114 { 115 if(JSR77.hasRequiredKeys(objectName)) 117 { 118 logger.debug("testEventProvider..."); 119 if (testAttribute("eventProvider")) 120 { 121 boolean isEventProvider = new Boolean ("" + getAttribute("eventProvider")).booleanValue(); 122 if (isEventProvider) 123 { 124 vassert("eventTypes attribute found", "EventTypes attribute not found", testAttribute("eventTypes")); 125 } 126 } 127 } 128 } 129 130 131 132 public void testStateManageable() 133 { 134 if(JSR77.hasRequiredKeys(objectName)) 136 { 137 logger.debug("testStateManageable..."); 138 if (testAttribute("stateManageable")) 139 { 140 boolean isStateManageable = new Boolean ("" + getAttribute("stateManageable")).booleanValue(); 141 if (isStateManageable) 142 { 143 vassert("startTime attribute found", "startTime attribute not found", testAttribute("startTime")); 144 vassert("state attribute found", "state attribute not found", testAttribute("state")); 145 146 if (testAttribute("state")) 147 { 148 int state = new Integer ("" + getAttribute("state")).intValue(); 149 vassert("state attribute in range", "state attribute not in range", (state >= 0) && (state <= 4)); 150 } 151 152 vassert("start operation found", "start operation not found", testOperation("start")); 153 vassert("startRecursive operation found", "startRecursive operation not found", testOperation("startRecursive")); 154 vassert("stop operation found", "stop operation not found", testOperation("stop")); 155 } 156 } 157 } 158 } 159 160 161 162 public void testStatisticsProvider() 163 { 164 if(JSR77.hasRequiredKeys(objectName)) 166 { 167 logger.debug("testStatisticsProvider..."); 168 if (testAttribute("statisticsProvider")) 169 { 170 boolean isStatisticsProvider = new Boolean ("" + getAttribute("statisticsProvider")).booleanValue(); 171 if (isStatisticsProvider) 172 { 173 vassert("stats attribute found", "stats attribute not found", testAttribute("stats")); 174 } 175 } 176 } 177 } 178 179 180 186 protected Object getAttribute(String attribute) 187 { 188 logger.debug("getAttribute(" + attribute + ")"); 189 try 190 { 191 return MEJB.createMEJB().getAttribute(objectName, attribute); 192 } 193 catch (Exception e) 194 { 195 } 196 return null; 197 } 198 199 200 204 protected void setUp() throws Exception 205 { 206 try 207 { 208 info = MEJB.createMEJB().getMBeanInfo(objectName); 209 210 MBeanAttributeInfo [] mbai = info.getAttributes(); 211 for (int i = 0; i < mbai.length; i++) 212 { 213 attributeInfos.add(mbai[i].getName()); 214 logger.debug("Attribute <" + mbai[i].getName() + ">"); 215 } 216 217 MBeanOperationInfo [] mboi = info.getOperations(); 218 for (int i = 0; i < mboi.length; i++) 219 { 220 operationInfos.add(mboi[i].getName()); 221 logger.debug("Operation <" + mboi[i].getName() + ">"); 222 } 223 } 224 catch (Exception e) 225 { 226 logger.warn("Error during info fetching..." + e.getMessage()); 227 } 228 } 229 230 231 237 protected boolean testAttribute(String attribute) 238 { 239 logger.debug("testAttribute(" + attribute + ")"); 240 return attributeInfos.contains(attribute); 241 } 242 243 244 250 protected boolean testOperation(String operation) 251 { 252 logger.debug("testOperation(" + operation + ")"); 253 return operationInfos.contains(operation); 254 } 255 } 256 | Popular Tags |