KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > stats > GenericStatsImplTest


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 package com.sun.enterprise.admin.monitor.stats;
29
30 import javax.management.j2ee.statistics.*;
31 import com.sun.enterprise.admin.monitor.stats.*;
32 import java.util.*;
33 import junit.framework.*;
34
35 /** Tests the class GenericStatsImplTescc.
36  * No need to import the class being tested, as the package name is the same.
37  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
38  * @version $Revision: 1.2 $
39  */

40 public class GenericStatsImplTest extends TestCase {
41     
42     public void testCorrectClassNameConstructor() {
43         try {
44             final String JavaDoc c = "javax.management.j2ee.statistics.EJBStats";
45             final EJBStats provider = createEjbStatsProvider();
46             final Stats stats = new GenericStatsImpl(c, provider);
47             assertNotNull("Object is not null", stats);
48         }
49         catch (Exception JavaDoc e) {
50             // It should never get here as this should never be exceptional
51
}
52     }
53     
54     public void testInCorrectClassNameConstructor() {
55         try {
56             final String JavaDoc invalid = "javax.management.j2ee.statistics.UnknownStats";
57             final EJBStats provider = createEjbStatsProvider();
58             final Stats stats = new GenericStatsImpl(invalid, provider);
59             fail("How can javax.management.j2ee.statistics.UnknownStats be impl????");
60         }
61         catch (Exception JavaDoc e) {
62             //We always should get this exception -- so this test should PASS.
63
}
64     }
65     
66     public void testInvalidInterface() {
67         try {
68             final String JavaDoc invalid = "java.io.Serializable";
69             final EJBStats provider = createEjbStatsProvider();
70             final Stats stats = new GenericStatsImpl(invalid, provider);
71             fail("We have not committed to java.io.Serializable as it is not a Stats interface");
72         }
73         catch (Exception JavaDoc e) {
74             //We always should get this exception -- so this test should PASS.
75
}
76     }
77     
78     public void testGetStatisticNamesWithEjbStats() {
79         try {
80             final String JavaDoc c = "javax.management.j2ee.statistics.EJBStats";
81             final EJBStats provider = createEjbStatsProvider();
82             final Stats stats = new GenericStatsImpl(c, provider);
83             final String JavaDoc[] s1 = stats.getStatisticNames();
84             final String JavaDoc[] s2 = new String JavaDoc[]{"CreateCount", "RemoveCount"};
85             final List ls1 = Arrays.asList(s1);
86             final List ls2 = Arrays.asList(s2);
87             assertTrue(ls1.containsAll(ls2)); //order is unimportant
88
assertEquals(s1.length, s2.length);
89         }
90         catch (Exception JavaDoc e) {
91             // It should never get here as this should never be exceptional
92
}
93     }
94     
95     public void testGetAStatisticWithEjbStats() {
96         try {
97             final String JavaDoc c = "javax.management.j2ee.statistics.EJBStats";
98             final EJBStats provider = createEjbStatsProvider();
99             final Stats stats = new GenericStatsImpl(c, provider);
100             final String JavaDoc[] names = new String JavaDoc[]{"CreateCount", "RemoveCount"};
101             final CountStatistic c1 = new CountStatisticImpl(10, names[0], "", "Beans Created", 10, 10);
102             final CountStatistic c2 = new CountStatisticImpl(5, names[1], "", "Beans Removed", 10, 10);
103
104             /* tests for equality */
105             final CountStatistic cc = (CountStatistic)stats.getStatistic(names[0]);
106             final CountStatistic rc = (CountStatistic)stats.getStatistic(names[1]);
107             assertEquals(c1.getCount(), cc.getCount());
108             assertEquals(c1.getName(), cc.getName());
109
110             final CountStatistic u = (CountStatistic)stats.getStatistic(names[1]);
111             assertEquals(c2.getCount(), rc.getCount());
112             assertEquals(c2.getName(), rc.getName());
113
114             /* tests for inequality */
115             final CountStatistic c3 = new CountStatisticImpl(1111, names[0], "", "Beans Created", 10, 10);
116             final CountStatistic c4 = new CountStatisticImpl(444, names[1], "", "Beans Removed", 10, 10);
117             
118             assertTrue(c3.getCount() != cc.getCount());
119             assertTrue(c4.getCount() != rc.getCount());
120         }
121         catch (Exception JavaDoc e) {
122             // It should never get here as this should never be exceptional
123
}
124     }
125     public GenericStatsImplTest(java.lang.String JavaDoc testName) {
126         super(testName);
127     }
128     protected void setUp() {
129     }
130     
131     protected void tearDown() {
132     }
133     private EJBStats createEjbStatsProvider() {
134         return new EjbStatsImpl();
135     }
136     
137     private static class EjbStatsImpl implements EJBStats, java.io.Serializable JavaDoc {
138         final Map m = new HashMap();
139         final String JavaDoc[] names = new String JavaDoc[]{"CreateCount", "RemoveCount"};
140         final CountStatistic c = new CountStatisticImpl(10, names[0], "", "Beans Created", 10, 10);
141         final CountStatistic r = new CountStatisticImpl(5, names[1], "", "Beans Removed", 10, 10);
142         EjbStatsImpl() {
143             m.put(names[0], c);
144             m.put(names[1], r);
145         }
146         
147         public CountStatistic getCreateCount() {
148             return ( c );
149         }
150         
151         public CountStatistic getRemoveCount() {
152             return ( r );
153         }
154         
155         public Statistic getStatistic(String JavaDoc str) {
156             return ( (Statistic) m.get(str) );
157         }
158         
159         public String JavaDoc[] getStatisticNames() {
160             return ( names );
161         }
162         
163         public Statistic[] getStatistics() {
164             return ( (Statistic[]) m.values().toArray() );
165         }
166     }
167     
168     public static Test suite() {
169         TestSuite suite = new TestSuite(GenericStatsImplTest.class);
170         return suite;
171     }
172     
173     public static void main(String JavaDoc args[]){
174         junit.textui.TestRunner.run(suite());
175         //junicc.swingui.TestRunner.run(suite());
176
}
177     
178 }
179
Popular Tags