KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > jmx > server > mx4j > test > JrmpRemotingTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.jmx.server.mx4j.test;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.naming.InitialContext JavaDoc;
25
26 import org.sape.carbon.core.component.Lookup;
27
28 import junit.extensions.ActiveTestSuite;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import mx4j.connector.RemoteMBeanServer;
34 import mx4j.connector.rmi.RMIConnector;
35 import mx4j.connector.rmi.jrmp.JRMPConnector;
36
37 /**
38  * This is a quick and dirty test to connect to a local instance of the
39  * JRMP connector "admin service". This admin server is first started by
40  * making a reference to its component and then its connected to via a simple
41  * RMI adaptor over the standard JRMP port 1099 on localhost.
42  *
43  * Copyright 2002 Sapient
44  * @since carbon 1.0
45  * @author Greg Hinkle, May 2002
46  * @version $Revision: 1.5 $($Author: ghinkl $ / $Date: 2003/10/06 18:32:56 $)
47  */

48 public class JrmpRemotingTest extends TestCase {
49
50     public JrmpRemotingTest(String JavaDoc name) {
51         super(name);
52     }
53
54     /*
55      * write your test methods here following these examples:
56      *
57      * public void testFunction1() {
58      * test something
59      * }
60      *
61      * public void testFunction2() {
62      * test something else
63      * }
64      */

65
66
67     public void testConnect() throws Exception JavaDoc {
68         // Do a local load of the JRMP Admin Server Service
69
Lookup.getInstance().fetchComponent("/manage/RemoteAdminServer");
70
71         // Create an RMIConnector, passing it the JNDI name of the JRMP Adaptor
72
String JavaDoc jndiName = "jrmp";
73
74         Hashtable JavaDoc props = new Hashtable JavaDoc();
75         props.put(
76             InitialContext.INITIAL_CONTEXT_FACTORY,
77             "com.sun.jndi.rmi.registry.RegistryContextFactory");
78         props.put(
79             InitialContext.PROVIDER_URL,
80             "rmi://localhost:1099");
81
82         RMIConnector connector = new JRMPConnector(); //new RMIConnector(jndiName, props);
83
connector.connect(jndiName,props);
84         // Use the connector as it is an MBeanServer
85

86         String JavaDoc remoteHostName = connector.getRemoteHostName();
87
88
89 // System.out.println("Remote server is: " + remoteHostName);
90
RemoteMBeanServer server = connector.getRemoteMBeanServer();
91
92         Set JavaDoc mbeans = server.queryMBeans(null,null);
93         Iterator JavaDoc iter = mbeans.iterator();
94         while (iter.hasNext()) {
95             // need to call the toString method to exercise the bean
96
iter.next().toString();
97 // System.out.println("Bean: " + iter.next());
98

99         }
100
101     }
102
103
104     /**
105      * Method called by jUnit to get all the tests in this test case.
106      * @return Test the suite of tests in this test case
107      */

108     public static Test suite() {
109         TestSuite masterSuite = new TestSuite("JrmpRemotingTest");
110
111         // add single threaded tests
112
Test singleThreadedTests = getSingleThreadedTests();
113         if(singleThreadedTests != null) {
114             masterSuite.addTest(singleThreadedTests);
115         }
116
117         // add multi threaded tests
118
Test multiThreadedTests = getMultiThreadedTests();
119         if(multiThreadedTests != null) {
120             masterSuite.addTest(multiThreadedTests);
121         }
122
123         return masterSuite;
124     }
125
126     /**
127      * This method is used within the suite method to get all of the single
128      * threaded tests.
129      *
130      * Add all your single threaded tests in this method with a line like:
131      * suite.addTest(new JrmpRemotingTester("testFunction1"));
132      *
133      * @return Test the suite of single threaded tests in this test case
134      */

135     private static Test getSingleThreadedTests() {
136         TestSuite suite = new TestSuite();
137         /*
138          * add your tests here following these examples:
139          *
140          * suite.addTest(new JrmpRemotingTester("testFunction1"));
141          * suite.addTest(new JrmpRemotingTester("testFunction2"));
142          */

143         suite.addTest(new JrmpRemotingTest("testConnect"));
144         return suite;
145     }
146
147     /**
148      * This method is used within the suite method to get all of the multi
149      * threaded tests.
150      *
151      * Add all your multi threaded tests in this method with a line like:
152      * addTest(suite, "testFunction1", 5);
153      *
154      * @return Test the suite of multi-threaded tests in this test case
155      */

156     private static Test getMultiThreadedTests() {
157         TestSuite suite = new ActiveTestSuite();
158         /*
159          * add your tests here following these examples:
160          *
161          * addTest(suite, "testFunction1", 5);
162          * addTest(suite, "testFunction2", 10);
163          */

164         return suite;
165     }
166
167     /**
168      * This method will add the give test to the give suite the specified
169      * number of times. This is best used for multi-threaded tests where
170      * suite is an instance of ActiveTestSuite and you want to run the same
171      * test in multiple threads.
172      *
173      * @param suite the suite to add the test to.
174      * @param testName the name of the test to add.
175      * @param number the number of times to add the test to the suite
176      */

177     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
178         for(int count=0; count<number; count++) {
179             suite.addTest(new JrmpRemotingTest(testName));
180         }
181     }
182 }
183
Popular Tags