KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > registry > spi > ValueListMapTest


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  * $Id: ValueListMapTest.java,v 1.3 2005/12/25 03:43:53 tcfujii Exp $
26  * $Date: 2005/12/25 03:43:53 $
27  * $Revision: 1.3 $
28  */

29 package com.sun.enterprise.admin.monitor.registry.spi;
30
31 import javax.management.j2ee.statistics.*;
32 import com.sun.enterprise.admin.monitor.registry.*;
33 import com.sun.enterprise.admin.monitor.stats.*;
34 import java.util.*;
35 import junit.framework.*;
36
37 /**
38  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
39  * @since $Revision: 1.3 $
40  */

41 public class ValueListMapTest extends TestCase {
42     
43     /* Write your unit tests here as methods ending in "Test". */
44     private MonitoringLevelListener[] jvml;
45     private MonitoringLevelListener[] ejbl;
46     public void testCreateMap() {
47         final Map m = new ValueListMap();
48     }
49     public void testInvalidPut() {
50         try {
51             final Map m = new ValueListMap(); //value with listener class.
52
final String JavaDoc key = "key1";
53             final String JavaDoc val = "Invalid Value"; //attempt to put a value with String class.
54
m.put(key, val);
55             fail ("Should have thrown IllegalArgumentException - Failed Test");
56         }
57         catch(IllegalArgumentException JavaDoc e) {}
58     }
59     public void testJvmListenerAdd() {
60         final Map m = new ValueListMap();
61         m.put(MonitoredObjectType.JVM, jvml[0]);
62         m.put(MonitoredObjectType.JVM, jvml[1]);
63         final Map n = (Map) m.get(MonitoredObjectType.JVM); //I know it is a map
64
assertEquals(n.keySet().size(), 2);
65     }
66     public void testJvmListenerAddRemove() {
67         final Map m = new ValueListMap();
68         m.put(MonitoredObjectType.JVM, jvml[0]);
69         m.put(MonitoredObjectType.JVM, jvml[1]);
70         m.remove(jvml[1]);
71         final Map n = (Map) m.get(MonitoredObjectType.JVM); //I know it is a map
72
assertEquals(n.keySet().size(), 1);
73     }
74     
75     public void testRemoveJvmType() {
76         final Map m = new ValueListMap();
77         m.put(MonitoredObjectType.JVM, jvml[0]);
78         m.put(MonitoredObjectType.JVM, jvml[1]);
79         final Collection removed = (Collection)m.remove(MonitoredObjectType.JVM);
80         assertEquals(m.get(MonitoredObjectType.JVM), null);
81         assertEquals(removed.size(), 2);
82         final Iterator it = removed.iterator();
83         while (it.hasNext()) {
84             final Object JavaDoc n = it.next();
85             assertTrue(n == jvml[0] || n == jvml[1]); //order is not known
86
}
87     }
88     
89     public void testAddOneListenerForTwoTypes() {
90         final Map m = new ValueListMap();
91         m.put(MonitoredObjectType.JVM, jvml[0]);
92         m.put(MonitoredObjectType.EJB, jvml[0]);
93         //now there should be two keys created
94
assertEquals(m.keySet().size(), 2);
95         //removed Collection should also contain 2 elements.
96
final Collection removed = (Collection) m.remove(jvml[0]); //It is a collection
97
assertEquals(removed.size(), 2);
98         final Iterator it = removed.iterator();
99         while (it.hasNext()) {
100             assertSame(jvml[0], it.next());
101         }
102     }
103     public ValueListMapTest(java.lang.String JavaDoc testName) {
104         super(testName);
105         createListeners();
106     }
107     protected void setUp() {
108     }
109     
110     protected void tearDown() {
111     }
112     
113     private void createListeners() {
114         final int nj = 2;
115         jvml = new MonitoringLevelListener[nj];
116         jvml[0] = new JvmListener();
117         jvml[1] = new JvmListener();
118         final int ne = 3;
119         ejbl = new MonitoringLevelListener[ne];
120         ejbl[0] = new EjbListener();
121         ejbl[1] = new EjbListener();
122         ejbl[2] = new EjbListener();
123     }
124     
125     private static class JvmListener implements MonitoringLevelListener {
126         JvmListener() {
127         }
128         public void changeLevel(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType type) {
129             System.out.println("from = " + from + " to = " + to + " type = " + type);
130         }
131         
132         public void changeLevel(MonitoringLevel from, MonitoringLevel to, javax.management.j2ee.statistics.Stats JavaDoc handback) {
133             //@deprecated
134
}
135         public void setLevel(MonitoringLevel level) {
136             //@deprecated
137
}
138     }
139     private static class EjbListener implements MonitoringLevelListener {
140         EjbListener() {
141         }
142         public void changeLevel(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType type) {
143             System.out.println("from = " + from + " to = " + to + " type = " + type);
144         }
145         
146         public void changeLevel(MonitoringLevel from, MonitoringLevel to, javax.management.j2ee.statistics.Stats JavaDoc handback) {
147             //@deprecated
148
}
149         public void setLevel(MonitoringLevel level) {
150             //@deprecated
151
}
152     }
153     private void nyi() {
154         fail("Not yet implemented");
155     }
156     
157     public static Test suite(){
158         TestSuite suite = new TestSuite(ValueListMapTest.class);
159         return suite;
160     }
161     
162     public static void main(String JavaDoc args[]){
163         junit.textui.TestRunner.run(suite());
164         //junit.swingui.TestRunner.run(suite());
165
}
166     
167 }
168
Popular Tags