KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > ejtools > management > test > ManagedObjectTestCase


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package net.sourceforge.ejtools.management.test;
8
9 import java.util.Collection JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import javax.management.MBeanAttributeInfo JavaDoc;
14 import javax.management.MBeanInfo JavaDoc;
15 import javax.management.MBeanOperationInfo JavaDoc;
16 import javax.management.ObjectName JavaDoc;
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 /**
24  * Description of the Class
25  *
26  * @author letiembl
27  * @created 21 mars 2002
28  */

29 public class ManagedObjectTestCase extends ReportTestCase
30 {
31    /** Description of the Field */
32    protected Collection JavaDoc attributeInfos = new Vector JavaDoc();
33    /** Description of the Field */
34    protected MBeanInfo JavaDoc info = null;
35    /** Description of the Field */
36    protected ObjectName JavaDoc objectName = null;
37    /** Description of the Field */
38    protected Collection JavaDoc operationInfos = new Vector JavaDoc();
39    /** Description of the Field */
40    private static Category logger = Category.getInstance(ManagedObjectTestCase.class);
41
42
43    /**
44     * Constructor for the ManagedObjectTestCase object
45     *
46     * @param objectName Description of the Parameter
47     */

48    public ManagedObjectTestCase(ObjectName JavaDoc objectName)
49    {
50       this(objectName.getCanonicalName(), objectName);
51    }
52
53
54    /**
55     * Constructor for the ConnectivityTest object
56     *
57     * @param name Description of the Parameter
58     * @param objectName Description of the Parameter
59     */

60    public ManagedObjectTestCase(String JavaDoc name, ObjectName JavaDoc 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 it's a managed bean, go ahead...
77
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    /** A unit test for JUnit */
89    public void testAttributes()
90    {
91         // If it's a managed bean, go ahead...
92
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       // Checks mandatory attributes
102
String JavaDoc attributes = JSR77.getRequiredAttributes(objectName);
103       logger.debug("Attributes => " + attributes);
104       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(attributes, ",");
105       while(st.hasMoreTokens()) {
106         String JavaDoc attribute = st.nextToken();
107         vassert(attribute+" attribute found", attribute +" attribute not found", testAttribute(attribute));
108       }
109       }
110    }
111
112    /** A unit test for JUnit */
113    public void testEventProvider()
114    {
115         // If it's a managed bean, go ahead...
116
if(JSR77.hasRequiredKeys(objectName))
117       {
118       logger.debug("testEventProvider...");
119       if (testAttribute("eventProvider"))
120       {
121          boolean isEventProvider = new Boolean JavaDoc("" + 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    /** A unit test for JUnit */
132    public void testStateManageable()
133    {
134         // If it's a managed bean, go ahead...
135
if(JSR77.hasRequiredKeys(objectName))
136       {
137       logger.debug("testStateManageable...");
138       if (testAttribute("stateManageable"))
139       {
140          boolean isStateManageable = new Boolean JavaDoc("" + 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 JavaDoc("" + 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    /** A unit test for JUnit */
162    public void testStatisticsProvider()
163    {
164         // If it's a managed bean, go ahead...
165
if(JSR77.hasRequiredKeys(objectName))
166       {
167       logger.debug("testStatisticsProvider...");
168       if (testAttribute("statisticsProvider"))
169       {
170          boolean isStatisticsProvider = new Boolean JavaDoc("" + 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    /**
181     * Gets the attribute attribute of the ManagedObjectTest object
182     *
183     * @param attribute Description of the Parameter
184     * @return The attribute value
185     */

186    protected Object JavaDoc getAttribute(String JavaDoc attribute)
187    {
188       logger.debug("getAttribute(" + attribute + ")");
189       try
190       {
191          return MEJB.createMEJB().getAttribute(objectName, attribute);
192       }
193       catch (Exception JavaDoc e)
194       {
195       }
196       return null;
197    }
198
199
200    /**
201     * @exception Exception Description of the Exception
202     * @see junit.extensions.jfunc.JFuncTestCase#setUpOnce()
203     */

204    protected void setUp() throws Exception JavaDoc
205    {
206       try
207       {
208          info = MEJB.createMEJB().getMBeanInfo(objectName);
209
210          MBeanAttributeInfo JavaDoc[] 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 JavaDoc[] 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 JavaDoc e)
225       {
226          logger.warn("Error during info fetching..." + e.getMessage());
227       }
228    }
229
230
231    /**
232     * A unit test for JUnit
233     *
234     * @param attribute Description of the Parameter
235     * @return The attribute value
236     */

237    protected boolean testAttribute(String JavaDoc attribute)
238    {
239       logger.debug("testAttribute(" + attribute + ")");
240       return attributeInfos.contains(attribute);
241    }
242
243
244    /**
245     * Gets the operation attribute of the ManagedObjectTest object
246     *
247     * @param operation Description of the Parameter
248     * @return The operation value
249     */

250    protected boolean testOperation(String JavaDoc operation)
251    {
252       logger.debug("testOperation(" + operation + ")");
253       return operationInfos.contains(operation);
254    }
255 }
256
Popular Tags