KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > util > GeneratedMonitoringMBeanTest


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
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * GeneratedMonitoringMBeanTest.java
31  *
32  * Created on August 7, 2003, 10:42 AM
33  */

34
35 package com.sun.enterprise.admin.monitor.util;
36
37 import junit.framework.*;
38 import javax.management.j2ee.statistics.Stats JavaDoc;
39 import javax.management.*;
40 import java.util.*;
41
42 /**
43  * Unit Test for testing GeneratedMonitoringMBean that gets created through the process of
44  * transforming a JSR77 Stats object into a Dynamic MBean. The transformation is
45  * helped by the ManagedResourceIntrospector which has its own tests.
46  * @author sg112326
47  */

48 public class GeneratedMonitoringMBeanTest extends TestCase{
49
50     public void testGetAttributeWithNull(){
51         try{
52             Enumeration e = attributes.keys();
53             while(e.hasMoreElements()){
54                 Object JavaDoc n = mbean.getAttribute(null);
55                 try{
56                     assertEquals(Long JavaDoc.class, n.getClass());
57                 }
58                 catch(Error JavaDoc ex){
59                         ex.getLocalizedMessage();
60                         assertEquals(String JavaDoc.class, n.getClass());
61                 }
62             }
63         }
64         catch(Exception JavaDoc ex){
65             assertEquals(NullPointerException JavaDoc.class, ex.getClass());
66         }
67     }
68     
69     public void testGetAttributeWithIncorrectAttrName(){
70         try{
71             Enumeration e = attributes.keys();
72             while(e.hasMoreElements()){
73                 String JavaDoc elem = (String JavaDoc)e.nextElement();
74                 Object JavaDoc n = mbean.getAttribute(elem+"o");
75                 try{
76                     assertEquals(Long JavaDoc.class, n.getClass());
77                 }
78                 catch(Error JavaDoc ex){
79                         ex.getLocalizedMessage();
80                         assertEquals(String JavaDoc.class, n.getClass());
81                 }
82             }
83         }
84         catch(Exception JavaDoc ex){
85             assertEquals(AttributeNotFoundException.class, ex.getClass());
86         }
87     }
88     
89     public void testGetAttributeWithCorrectAttrName(){
90         try{
91             Enumeration e = attributes.keys();
92             while(e.hasMoreElements()){
93                 String JavaDoc elem = (String JavaDoc)e.nextElement();
94                 Object JavaDoc n = mbean.getAttribute(elem);
95                 try{
96                     assertEquals(Long JavaDoc.class, n.getClass());
97                 }
98                 catch(Error JavaDoc ex){
99                         ex.getLocalizedMessage();
100                         assertEquals(String JavaDoc.class, n.getClass());
101                 }
102             }
103         }
104         catch(Exception JavaDoc ex){
105             assertEquals(NullPointerException JavaDoc.class, ex.getClass());
106         }
107     }
108     public void testAtrributes(){
109         Enumeration en = attributes.elements();
110         while(en.hasMoreElements()){
111             String JavaDoc attr = (String JavaDoc)en.nextElement();
112             System.out.println("testing attribute:"+attr);
113             try{ assertEquals("HeapSize", attr.substring(0,attr.indexOf("_")));
114             }catch(Error JavaDoc ex){
115                 try{assertEquals("MaxMemory",attr.substring(0,attr.indexOf("_")));
116                 }catch(Error JavaDoc e){
117                     try{assertEquals("UpTime",attr.substring(0,attr.indexOf("_")));
118                     }catch(Error JavaDoc e1){
119                         assertEquals("AvailableProcessors",attr.substring(0,attr.indexOf("_")));
120                     }
121                 }
122             }
123         }
124     }
125     
126     public void testCreation(){
127         assertNotNull(mbean);
128         assertNotNull(m);
129         assertNotNull(jvm);
130         assertNotNull(attrs);
131     }
132     /** Creates a new instance of GeneratedMonitoringMBeanTest */
133     public GeneratedMonitoringMBeanTest(java.lang.String JavaDoc testName) {
134         super(testName);
135     }
136     GeneratedMonitoringMBeanImpl mbean;
137     S1ASJVMStatsImplMock jvm;
138     MBeanInfo m;
139     MBeanAttributeInfo[] attrs;
140     Hashtable attributes = new Hashtable();
141     
142     protected void setUp() {
143         mbean = new GeneratedMonitoringMBeanImpl();
144         jvm = new S1ASJVMStatsImplMock();
145         m = mbean.introspect(jvm);
146         attrs = m.getAttributes();
147         for(int i=0; i< attrs.length;i++){
148             attributes.put(attrs[i].getName(),attrs[i].getName());
149         }
150     }
151     
152     protected void tearDown() {
153         
154     }
155     
156     public static Test suite() {
157         TestSuite suite = new TestSuite(GeneratedMonitoringMBeanTest.class);
158         return suite;
159     }
160     
161     public static void main(String JavaDoc[] args){
162         junit.textui.TestRunner.run(suite());
163     }
164 }
165
Popular Tags