KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > poa > POAActivateTest


1 package org.jacorb.test.poa;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2001 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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import junit.framework.*;
24 import junit.extensions.TestSetup;
25 import org.jacorb.test.common.ORBSetup;
26 import org.jacorb.test.orb.BasicServerImpl;
27 import org.omg.PortableServer.POA JavaDoc;
28 import org.omg.PortableServer.POAHelper JavaDoc;
29
30 import org.omg.PortableServer.*;
31 import org.omg.PortableServer.POAPackage.*;
32 import org.omg.CORBA.*;
33
34 /**
35  * <code>POAActivateTest</code> tests rapid activation and deactivation of
36  * objects in order to ensure the threading is correct.
37  *
38  * @author <a HREF="mailto:rnc@prismtechnologies.com"></a>
39  * @version 1.0
40  */

41 public class POAActivateTest extends TestCase
42 {
43     /**
44      * <code>orb</code> is used to obtain the root poa.
45      */

46     private static org.omg.CORBA.ORB JavaDoc orb = null;
47
48
49     /**
50      * <code>POAActivateTest</code> constructor - for JUnit.
51      *
52      * @param name a <code>String</code> value
53      */

54     public POAActivateTest (String JavaDoc name)
55     {
56         super (name);
57     }
58
59
60     /**
61      * <code>suite</code> lists the tests for Junit to run.
62      *
63      * @return a <code>Test</code> value
64      */

65     public static Test suite ()
66     {
67         TestSuite suite = new TestSuite ("POA Activation/Deactivation Tests");
68         Setup setup = new Setup( suite );
69         ORBSetup osetup = new ORBSetup( setup );
70
71         suite.addTest (new POAActivateTest ("testActivateDeactivate1"));
72         suite.addTest (new POAActivateTest ("testActivateDeactivate2"));
73         suite.addTest (new POAActivateTest ("testActivateDeactivate3"));
74
75         return osetup;
76     }
77
78
79     /**
80      * <code>testActivateDeactivate1</code> tests activating an object without
81      * ID (i.e. using a new one each time) and using servant_to_id to obtain
82      * the ID to deactivate_the_object.
83      */

84     public void testActivateDeactivate1 ()
85     {
86         try
87         {
88             POA JavaDoc poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
89
90             poa.the_POAManager().activate();
91
92             BasicServerImpl soi = new BasicServerImpl();
93
94             for (int count=0;count<100;count++)
95             {
96 // System.out.println("Iteration #"+count+" - activating object");
97
poa.activate_object( soi);
98 // System.out.println("Iteration #"+count+" - deactivating object");
99
poa.deactivate_object(poa.servant_to_id(soi));
100             }
101         }
102         catch( Exception JavaDoc e )
103         {
104             fail( "unexpected exception: " + e );
105         }
106     }
107
108
109     /**
110      * <code>testActivateDeactivate2</code> tests activating and deactivating
111      * the object using the same ID.
112      */

113     public void testActivateDeactivate2 ()
114     {
115         try
116         {
117             POA JavaDoc poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
118
119             poa.the_POAManager().activate();
120
121             BasicServerImpl soi = new BasicServerImpl();
122
123             // This will activate it so do deactivate first
124
byte []id = poa.servant_to_id( soi );
125
126             for (int count=0;count<100;count++)
127             {
128                 poa.deactivate_object(id);
129                 poa.activate_object_with_id(id, soi);
130             }
131         }
132         catch( Exception JavaDoc e )
133         {
134             fail( "unexpected exception: " + e );
135         }
136     }
137
138
139     /**
140      * <code>testActivateDeactivate3</code> tests activating an object using a POA policy
141      * of MULTIPLE_ID.
142      */

143     public void testActivateDeactivate3 ()
144     {
145         try
146         {
147             POA JavaDoc rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
148
149             // create POA
150
Policy policies[] = new Policy[3];
151             policies[0] = rootPoa.create_id_assignment_policy(
152                 org.omg.PortableServer.IdAssignmentPolicyValue.SYSTEM_ID);
153             policies[1] = rootPoa.create_id_uniqueness_policy(
154                 org.omg.PortableServer.IdUniquenessPolicyValue.MULTIPLE_ID);
155             policies[2] = rootPoa.create_servant_retention_policy(
156                 org.omg.PortableServer.ServantRetentionPolicyValue.RETAIN);
157
158             POA JavaDoc poa = rootPoa.create_POA("system_id", rootPoa.the_POAManager(), policies);
159
160             BasicServerImpl soi = new BasicServerImpl();
161
162             byte [] id = poa.activate_object(soi);
163
164             for (int count=0;count<100;count++)
165             {
166                 poa.deactivate_object(id);
167                 poa.activate_object_with_id( id, soi);
168            }
169         }
170         catch( Exception JavaDoc e )
171         {
172             e.printStackTrace();
173             fail( "unexpected exception: " + e );
174         }
175     }
176
177
178
179
180     /**
181      * <code>Setup</code> is an inner class to initialize the ORB.
182      */

183     private static class Setup extends TestSetup
184     {
185         /**
186          * Creates a new <code>Setup</code> instance.
187          *
188          * @param test a <code>Test</code> value
189          */

190         public Setup (Test test)
191         {
192             super (test);
193         }
194
195         /**
196          * <code>setUp</code> sets the orb variable.
197          */

198         protected void setUp ()
199         {
200             org.omg.CORBA.Object JavaDoc obj = null;
201
202             orb = ORBSetup.getORB ();
203         }
204
205         /**
206          * <code>tearDown</code> does nothing for this test.
207          */

208         protected void tearDown ()
209         {
210         }
211     }
212 }
213
Popular Tags