KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > j2ee > management > Abstract77Test


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.j2ee.management;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Collections JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27
28 import junit.framework.TestCase;
29 import org.apache.geronimo.gbean.GBeanData;
30 import org.apache.geronimo.gbean.GBeanInfo;
31 import org.apache.geronimo.gbean.AbstractName;
32 import org.apache.geronimo.gbean.ReferencePatterns;
33 import org.apache.geronimo.gbean.AbstractNameQuery;
34 import org.apache.geronimo.j2ee.management.impl.J2EEDomainImpl;
35 import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl;
36 import org.apache.geronimo.j2ee.management.impl.JVMImpl;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.kernel.KernelFactory;
39 import org.apache.geronimo.kernel.Kernel;
40 import org.apache.geronimo.kernel.repository.Artifact;
41 import org.apache.geronimo.system.serverinfo.BasicServerInfo;
42 import org.apache.geronimo.system.plugin.PluginInstaller;
43 import org.apache.geronimo.system.plugin.PluginRepositoryList;
44 import org.apache.geronimo.management.J2EEResource;
45 import org.apache.geronimo.management.EJBModule;
46 import org.apache.geronimo.management.AppClientModule;
47 import org.apache.geronimo.management.geronimo.WebModule;
48 import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
49 import org.apache.geronimo.management.geronimo.J2EEApplication;
50 import org.apache.geronimo.management.geronimo.J2EEServer;
51 import org.apache.geronimo.management.geronimo.JVM;
52
53 /**
54  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
55  */

56 public abstract class Abstract77Test extends TestCase {
57     protected static final GBeanData SERVER_INFO_DATA = buildGBeanData(new String JavaDoc[] {"role"}, new String JavaDoc[] {"ServerInfo"}, BasicServerInfo.getGBeanInfo());
58
59     protected static final String JavaDoc DOMAIN = "geronimo.test";
60     protected static final GBeanData DOMAIN_DATA = buildGBeanData(new String JavaDoc[] {"j2eeType", "name"}, new String JavaDoc[] {"J2EEDomain", DOMAIN}, J2EEDomainImpl.GBEAN_INFO);
61     protected static final GBeanData SERVER_DATA = buildGBeanData(new String JavaDoc[] {"j2eeType", "name"}, new String JavaDoc[] {"J2EEServer", "test"}, J2EEServerImpl.GBEAN_INFO);
62     protected static final GBeanData JVM_DATA = buildGBeanData(new String JavaDoc[] {"j2eeType", "J2EEServer", "name"}, new String JavaDoc[] {"JVM", "test", "JVM"}, JVMImpl.GBEAN_INFO);
63
64     protected Kernel kernel;
65
66     private static GBeanData buildGBeanData(String JavaDoc[] key, String JavaDoc[] value, GBeanInfo info) {
67         AbstractName abstractName = buildAbstractName(key, value);
68         return new GBeanData(abstractName, info);
69     }
70
71     private static AbstractName buildAbstractName(String JavaDoc[] key, String JavaDoc value[]) {
72         Hashtable JavaDoc names = new Hashtable JavaDoc();
73         for (int i = 0; i < key.length; i++) {
74             String JavaDoc k = key[i];
75             String JavaDoc v = value[i];
76             names.put(k, v);
77         }
78
79         ObjectName JavaDoc objectName;
80         try {
81             objectName = new ObjectName JavaDoc(DOMAIN, names);
82         } catch (MalformedObjectNameException JavaDoc e) {
83             throw new IllegalArgumentException JavaDoc("Malformed ObjectName: " + DOMAIN + ":" + names);
84         }
85         return new AbstractName(new Artifact("test", "foo", "1", "car"), names, objectName);
86     }
87
88     protected void setUp() throws Exception JavaDoc {
89         super.setUp();
90         kernel = KernelFactory.newInstance().createKernel(DOMAIN);
91         kernel.boot();
92
93         ClassLoader JavaDoc classLoader = getClass().getClassLoader();
94
95         // server info
96
SERVER_INFO_DATA.setAttribute("baseDirectory", System.getProperty("java.io.tmpdir"));
97         kernel.loadGBean(SERVER_INFO_DATA, classLoader);
98
99         // domain
100
DOMAIN_DATA.setReferencePatterns("Servers", new ReferencePatterns(new AbstractNameQuery(J2EEServer.class.getName())));
101         kernel.loadGBean(DOMAIN_DATA, classLoader);
102
103         // server
104
SERVER_DATA.setReferencePattern("ServerInfo", SERVER_INFO_DATA.getAbstractName());
105         SERVER_DATA.setReferencePatterns("JVMs", new ReferencePatterns(new AbstractNameQuery(JVM.class.getName())));
106         LinkedHashSet JavaDoc resourcePatterns = new LinkedHashSet JavaDoc();
107         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JAVA_MAIL_RESOURCE), J2EEResource.class.getName()));
108         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JCA_CONNECTION_FACTORY), J2EEResource.class.getName()));
109         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JDBC_RESOURCE), J2EEResource.class.getName()));
110         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JMS_RESOURCE), J2EEResource.class.getName()));
111         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JNDI_RESOURCE), J2EEResource.class.getName()));
112         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JTA_RESOURCE), J2EEResource.class.getName()));
113         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.RMI_IIOP_RESOURCE), J2EEResource.class.getName()));
114         resourcePatterns.add(new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.URL_RESOURCE), J2EEResource.class.getName()));
115         SERVER_DATA.setReferencePatterns("Resources", resourcePatterns);
116         SERVER_DATA.setReferencePatterns("Applications", new ReferencePatterns(new AbstractNameQuery(J2EEApplication.class.getName())));
117         SERVER_DATA.setReferencePatterns("AppClientModules", new ReferencePatterns(new AbstractNameQuery(AppClientModule.class.getName())));
118         SERVER_DATA.setReferencePatterns("EJBModules", new ReferencePatterns(new AbstractNameQuery(EJBModule.class.getName())));
119         SERVER_DATA.setReferencePatterns("ResourceAdapterModules", new ReferencePatterns(new AbstractNameQuery(ResourceAdapterModule.class.getName())));
120         SERVER_DATA.setReferencePatterns("WebModules", new ReferencePatterns(new AbstractNameQuery(WebModule.class.getName())));
121         // Can't test, there are none of these available
122
//SERVER_DATA.setReferencePatterns("PluginInstaller", new ReferencePatterns(new AbstractNameQuery(PluginInstaller.class.getName())));
123
SERVER_DATA.setReferencePatterns("PluginRepoLists", new ReferencePatterns(new AbstractNameQuery(PluginRepositoryList.class.getName())));
124         kernel.loadGBean(SERVER_DATA, classLoader);
125
126         // JVM
127
kernel.loadGBean(JVM_DATA, classLoader);
128
129         // start um
130
kernel.startGBean(SERVER_INFO_DATA.getAbstractName());
131         kernel.startGBean(DOMAIN_DATA.getAbstractName());
132         kernel.startGBean(SERVER_DATA.getAbstractName());
133         kernel.startGBean(JVM_DATA.getAbstractName());
134     }
135
136     protected void tearDown() throws Exception JavaDoc {
137         super.tearDown();
138         kernel.stopGBean(JVM_DATA.getAbstractName());
139         kernel.stopGBean(SERVER_DATA.getAbstractName());
140         kernel.stopGBean(DOMAIN_DATA.getAbstractName());
141         kernel.stopGBean(SERVER_INFO_DATA.getAbstractName());
142         kernel.unloadGBean(JVM_DATA.getAbstractName());
143         kernel.unloadGBean(SERVER_DATA.getAbstractName());
144         kernel.unloadGBean(DOMAIN_DATA.getAbstractName());
145         kernel.unloadGBean(SERVER_INFO_DATA.getAbstractName());
146         kernel.shutdown();
147         kernel = null;
148     }
149
150     protected void assertObjectNamesEqual(String JavaDoc[] expected, String JavaDoc[] test) throws Exception JavaDoc {
151         Set JavaDoc expectedSet = new HashSet JavaDoc(expected.length);
152         for (int i = 0; i < expected.length; i++) {
153             expectedSet.add(new ObjectName JavaDoc(expected[i]));
154         }
155         Set JavaDoc testSet = new HashSet JavaDoc(test.length);
156         for (int i = 0; i < test.length; i++) {
157             testSet.add(new ObjectName JavaDoc(test[i]));
158         }
159         assertEquals(expectedSet, testSet);
160     }
161 }
162
Popular Tags