KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > ClientServerSetup


1 package org.jacorb.test.common;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2005 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301, USA.
22  */

23
24 import java.util.*;
25
26 import org.omg.CORBA.*;
27 import org.omg.PortableServer.*;
28
29 import junit.framework.*;
30 import junit.extensions.*;
31
32 import org.jacorb.test.common.launch.*;
33
34 /**
35  * A special TestSetup that creates a separate CORBA server process,
36  * and allows JUnit test cases to talk to a CORBA object supplied
37  * by that server.
38  * <p>
39  * A <code>ClientServerSetup</code> should be used together with a
40  * {@link ClientServerTestCase}, which provides an easy way so that
41  * the individual test cases can actually see the setup.
42  * The following example shows how to set this up in the static
43  * <code>suite</code> method:
44  *
45  * <p><blockquote><pre>
46  * public class MyTest extends ClientServerTestCase
47  * {
48  * ...
49  *
50  * public static Test suite()
51  * {
52  * TestSuite suite = new TestSuite ("My CORBA Test");
53  *
54  * // Wrap the setup around the suite, specifying
55  * // the name of the servant class that should be
56  * // instantiated by the server process.
57  *
58  * ClientServerSetup setup =
59  * new ClientServerSetup (suite,
60  * "my.corba.ServerImpl");
61  *
62  * // Add test cases, passing the setup as an
63  * // additional constructor parameter.
64  *
65  * suite.addTest (new MyTest ("testSomething", setup));
66  * ...
67  *
68  * // Return the setup, not the suite!
69  * return setup;
70  * }
71  * }
72  * </pre></blockquote><p>
73  *
74  * The individual test cases can then access the setup in a convenient way.
75  * For details, see {@link ClientServerTestCase}.
76  *
77  * @author Andre Spiegel <spiegel@gnu.org>
78  * @version $Id: ClientServerSetup.java,v 1.21 2005/05/16 17:35:20 andre.spiegel Exp $
79  */

80 public class ClientServerSetup extends TestSetup {
81
82     protected String JavaDoc servantName;
83     protected Process JavaDoc serverProcess;
84     protected StreamListener outListener, errListener;
85     protected org.omg.CORBA.Object JavaDoc serverObject;
86     protected org.omg.CORBA.ORB JavaDoc clientOrb;
87     protected org.omg.PortableServer.POA JavaDoc clientRootPOA;
88
89     private Properties clientOrbProperties = null;
90     private Properties serverOrbProperties = null;
91     
92     private static Comparator comparator = new JacORBVersionComparator();
93
94     /**
95      * Constructs a new ClientServerSetup that is wrapped
96      * around the specified Test. When the test is run,
97      * the setup spawns a server process in which an instance
98      * of the class servantName is created and registered
99      * with the ORB.
100      * @param test The test around which the new setup
101      * should be wrapped.
102      * @param servantName The fully qualified name of the
103      * servant class that should be instantiated in the
104      * server process.
105      */

106     public ClientServerSetup ( Test test, String JavaDoc servantName )
107     {
108         super ( test );
109         this.servantName = servantName;
110         clientOrbProperties = new Properties();
111         clientOrbProperties.put ("org.omg.CORBA.ORBClass",
112                                  "org.jacorb.orb.ORB");
113         clientOrbProperties.put ("org.omg.CORBA.ORBSingletonClass",
114                                  "org.jacorb.orb.ORBSingleton");
115     }
116
117     public ClientServerSetup( Test test,
118                               String JavaDoc servantName,
119                               Properties clientOrbProperties,
120                               Properties serverOrbProperties )
121     {
122         this( test, servantName );
123         if (clientOrbProperties != null)
124             this.clientOrbProperties.putAll (clientOrbProperties);
125         this.serverOrbProperties = serverOrbProperties;
126     }
127
128     public void setUp() throws Exception JavaDoc
129     {
130         clientOrb = ORB.init (new String JavaDoc[0], clientOrbProperties );
131         clientRootPOA = POAHelper.narrow
132                           ( clientOrb.resolve_initial_references( "RootPOA" ) );
133         clientRootPOA.the_POAManager().activate();
134         
135         String JavaDoc serverVersion = System.getProperty ("jacorb.test.server.version",
136                                                    "cvs");
137         String JavaDoc testID = System.getProperty("jacorb.test.id", "");
138         String JavaDoc cs = System.getProperty ("jacorb.test.coverage", "false");
139         boolean coverage = cs.equals("true") || cs.equals("on");
140
141         Properties serverProperties = new Properties();
142         if (serverOrbProperties != null)
143             serverProperties.putAll (serverOrbProperties);
144         serverProperties.put ("jacorb.implname", servantName);
145             
146         JacORBLauncher launcher = JacORBLauncher.getLauncher (serverVersion,
147                                                               coverage);
148
149         if (coverage)
150             serverProperties.put ("emma.coverage.out.file",
151                                   launcher.getJacorbHome()
152                                   + "/test/regression/output/"
153                                   + testID + "/coverage-server.ec");
154         
155         serverProcess = launcher.launch
156         (
157             TestUtils.testHome() + "/classes",
158             serverProperties,
159             getTestServerMain(),
160             new String JavaDoc[] { servantName }
161         );
162         
163         outListener = new StreamListener (serverProcess.getInputStream(),
164                                           "OUT");
165         errListener = new StreamListener (serverProcess.getErrorStream(),
166                                           "ERR");
167         outListener.start();
168         errListener.start();
169         String JavaDoc ior = outListener.getIOR();
170         serverObject = clientOrb.string_to_object(ior);
171     }
172
173     public void tearDown() throws Exception JavaDoc
174     {
175         serverProcess.destroy();
176     }
177
178     public String JavaDoc getTestServerMain()
179     {
180         String JavaDoc serverVersion = System.getProperty ("jacorb.test.server.version",
181                                                    "cvs");
182         if (comparator.compare (serverVersion, "2.2") >= 0)
183             return "org.jacorb.test.common.TestServer";
184         else
185             return "org.jacorb.test.common.TestServer_before_2_2";
186     }
187
188     /**
189      * Gets a reference to the object that was instantiated in the
190      * server process.
191      */

192     public org.omg.CORBA.Object JavaDoc getServerObject()
193     {
194         return serverObject;
195     }
196
197     /**
198      * Gets the client ORB that is used to communicate with the server.
199      */

200     public org.omg.CORBA.ORB JavaDoc getClientOrb()
201     {
202         return clientOrb;
203     }
204
205     /**
206      * Gets the fully qualified name of the servant class that
207      * is instantiated in the server.
208      */

209     public String JavaDoc getServantName()
210     {
211         return servantName;
212     }
213
214     /**
215      * Gets the server process.
216      */

217     public Process JavaDoc getServerProcess()
218     {
219         return serverProcess;
220     }
221
222     public POA getClientRootPOA()
223     {
224         return clientRootPOA;
225     }
226
227 }
228
Popular Tags