KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > bugs > bug344 > TestCase


1 package org.jacorb.test.bugs.bug344;
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 import org.omg.CORBA.Policy JavaDoc;
30
31
32 /**
33  * <code>TestCase</code> tests rapid activation and deactivation of
34  * objects in order to ensure the threading is correct.
35  *
36  * @author <a HREF="mailto:rnc@prismtechnologies.com"></a>
37  * @version 1.0
38  */

39 public class TestCase extends junit.framework.TestCase
40 {
41     /**
42      * <code>orb</code> is used to obtain the root poa.
43      */

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

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

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

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

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

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

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

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

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

205         protected void tearDown ()
206         {
207         }
208     }
209 }
210
Popular Tags