KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > orb > AlternateProfileTest


1 package org.jacorb.test.orb;
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.Properties JavaDoc;
25
26 import junit.framework.*;
27
28 import org.jacorb.orb.ParsedIOR;
29 import org.jacorb.orb.iiop.IIOPProfile;
30
31 import org.omg.CORBA.portable.Delegate JavaDoc;
32 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc;
33 import org.omg.IOP.TaggedProfile JavaDoc;
34
35 import org.jacorb.test.*;
36 import org.jacorb.test.common.*;
37 import org.omg.CORBA.ORB JavaDoc;
38
39 /**
40  * This test is similar to AlternateIIOPAddressTest, but it uses the
41  * special IORInfoExt functions to set up the IORs.
42  *
43  * @jacorb-since 2.2
44  * @author Marc Heide
45  * @version $Id: AlternateProfileTest.java,v 1.3 2005/05/13 13:42:14 andre.spiegel Exp $
46  */

47 public class AlternateProfileTest extends ClientServerTestCase
48 {
49     protected IIOPAddressServer server = null;
50     protected ClientServerSetup _setup = null;
51
52     private static final String JavaDoc CORRECT_HOST = "127.0.0.1";
53     private static final String JavaDoc WRONG_HOST = "10.0.1.223"; //"194.138.122.114"
54
private static final String JavaDoc WRONG_HOST_2 = "10.0.1.223"; //"147.54.135.239"
55

56     private static final int CORRECT_PORT = 50000;
57     private static final int WRONG_PORT = 50001;
58
59     public AlternateProfileTest(String JavaDoc name, ClientServerSetup setup)
60     {
61         super(name, setup);
62         _setup = setup;
63     }
64
65     protected void setUp() throws Exception JavaDoc
66     {
67         server = IIOPAddressServerHelper.narrow(setup.getServerObject());
68     }
69
70     protected void tearDown() throws Exception JavaDoc
71     {
72         // server.clearSocketAddress();
73
server.setIORAddress (CORRECT_HOST, CORRECT_PORT);
74         server.clearAlternateAddresses();
75     }
76
77     public static Test suite()
78     {
79         TestSuite suite = new JacORBTestSuite
80         (
81             "Test TAG_ALTERNATE_IIOP_ADDRESS/IORInfoExt",
82             AlternateProfileTest.class
83         );
84
85         Properties JavaDoc client_props = new Properties JavaDoc();
86         client_props.setProperty ("jacorb.retries", "0");
87         client_props.setProperty ("jacorb.retry_interval", "50");
88         client_props.setProperty ("jacorb.connection.client.pending_reply_timeout", "2000");
89         client_props.setProperty ("jacorb.log.verbosity", "4");
90
91         Properties JavaDoc server_props = new Properties JavaDoc();
92         server_props.setProperty
93             ("org.omg.PortableInterceptor.ORBInitializerClass.IIOPProfileORBInitializer",
94              "org.jacorb.test.orb.IIOPProfileORBInitializer");
95         server_props.setProperty ("OAPort", Integer.toString(CORRECT_PORT));
96
97         ClientServerSetup setup =
98          new ClientServerSetup (suite,
99                                    "org.jacorb.test.orb.IIOPAddressServerImpl",
100                                    client_props,
101                                    server_props);
102
103         suite.addTest (new AlternateProfileTest("test_ping", setup));
104         suite.addTest (new AlternateProfileTest("test_primary_ok", setup));
105         suite.addTest (new AlternateProfileTest("test_primary_wrong_host", setup));
106         suite.addTest (new AlternateProfileTest("test_primary_wrong_port", setup));
107         suite.addTest (new AlternateProfileTest("test_alternate_ok", setup));
108         suite.addTest (new AlternateProfileTest("test_alternate_ok_2", setup));
109         suite.addTest (new AlternateProfileTest("test_alternate_wrong", setup));
110
111         return setup;
112     }
113
114     public void test_ping()
115     {
116         Sample s = server.getObject();
117         testNumberOfIIOPProfiles(1, s);
118         int result = s.ping (17);
119         assertEquals (18, result);
120     }
121
122     public void test_primary_ok()
123     {
124         server.setIORAddress( CORRECT_HOST, CORRECT_PORT );
125         Sample s = server.getObject();
126         testNumberOfIIOPProfiles(1, s);
127         int result = s.ping (77);
128         assertEquals (78, result);
129     }
130
131     public void test_primary_wrong_host()
132     {
133         server.setIORAddress( WRONG_HOST, CORRECT_PORT );
134         Sample s = server.getObject();
135         testNumberOfIIOPProfiles(1, s);
136         try
137         {
138             int result = s.ping (123);
139             fail ("TRANSIENT or TIMEOUT exception expected");
140         }
141         catch (org.omg.CORBA.TRANSIENT JavaDoc ex)
142         {
143             // ok
144
}
145     }
146
147     public void test_primary_wrong_port()
148     {
149         server.setIORAddress( CORRECT_HOST, WRONG_PORT );
150         Sample s = server.getObject();
151         testNumberOfIIOPProfiles(1, s);
152
153         try
154         {
155             int result = s.ping (4);
156             fail ("TRANSIENT exception expected");
157         }
158         catch (org.omg.CORBA.TRANSIENT JavaDoc ex)
159         {
160             // ok
161
}
162     }
163
164     public void test_alternate_ok()
165     {
166         server.setIORAddress( WRONG_HOST, CORRECT_PORT );
167         server.addAlternateAddress( CORRECT_HOST, CORRECT_PORT );
168         Sample s = server.getObject();
169         testNumberOfIIOPProfiles(2, s);
170         testHostAndPortInIIOPProfile(s, 0, WRONG_HOST, CORRECT_PORT);
171         testHostAndPortInIIOPProfile(s, 1, CORRECT_HOST, CORRECT_PORT);
172
173         ORB JavaDoc _myOrb = _setup.getClientOrb();
174         String JavaDoc iorStr = _myOrb.object_to_string(s);
175         System.out.println(iorStr);
176         int result = s.ping (99);
177         assertEquals (100, result);
178     }
179
180     public void test_alternate_ok_2()
181     {
182         server.setIORAddress( WRONG_HOST, CORRECT_PORT );
183         server.addAlternateAddress( WRONG_HOST_2, CORRECT_PORT );
184         server.addAlternateAddress( CORRECT_HOST, CORRECT_PORT );
185         Sample s = server.getObject();
186         testNumberOfIIOPProfiles(3, s);
187         testHostAndPortInIIOPProfile(s, 0, WRONG_HOST, CORRECT_PORT);
188         testHostAndPortInIIOPProfile(s, 1, WRONG_HOST_2, CORRECT_PORT);
189         testHostAndPortInIIOPProfile(s, 2, CORRECT_HOST, CORRECT_PORT);
190         int result = s.ping (187);
191         assertEquals (188, result);
192     }
193
194     public void test_alternate_wrong()
195     {
196         server.setIORAddress( CORRECT_HOST, WRONG_PORT );
197         server.addAlternateAddress( WRONG_HOST, CORRECT_PORT );
198         server.addAlternateAddress( WRONG_HOST_2, WRONG_PORT );
199         server.addAlternateAddress( WRONG_HOST_2, CORRECT_PORT );
200         Sample s = server.getObject();
201         testNumberOfIIOPProfiles(4, s);
202         testHostAndPortInIIOPProfile(s, 0, CORRECT_HOST, WRONG_PORT);
203         testHostAndPortInIIOPProfile(s, 1, WRONG_HOST, CORRECT_PORT);
204         testHostAndPortInIIOPProfile(s, 2, WRONG_HOST_2, WRONG_PORT);
205         testHostAndPortInIIOPProfile(s, 3, WRONG_HOST_2, CORRECT_PORT);
206         try
207         {
208             int result = s.ping (33);
209             fail ("TRANSIENT exception expected");
210         }
211         catch (org.omg.CORBA.TRANSIENT JavaDoc ex)
212         {
213             // ok
214
}
215
216     }
217
218     /**
219      * Tests the number of IOP profiles in given IOR.
220      * @param numberExpected
221      * @param obj
222      */

223     public void testNumberOfIIOPProfiles( int numberExpected, org.omg.CORBA.Object JavaDoc obj )
224     {
225        // try to get ORB delegate to object
226
org.jacorb.orb.Delegate jacOrbDelegate = null;
227        Delegate JavaDoc localObj = ((org.omg.CORBA.portable.ObjectImpl JavaDoc)obj)._get_delegate();
228        jacOrbDelegate = (org.jacorb.orb.Delegate)localObj;
229
230        ParsedIOR pior = jacOrbDelegate.getParsedIOR();
231        org.omg.IOP.IOR JavaDoc ior = jacOrbDelegate.getIOR();
232
233        TaggedProfile JavaDoc[] profiles = ior.profiles;
234        int nrOfIOPProf = 0;
235        for (int i = 0; i < profiles.length; i++)
236        {
237            if (profiles[i].tag == TAG_INTERNET_IOP.value)
238            {
239               nrOfIOPProf++;
240            }
241        }
242        assertEquals(numberExpected, nrOfIOPProf);
243     }
244
245     /**
246      * Tests if given host and port equal values in given IOR.
247      * Since several IOP profiles may be coded in IOR, an position must be specified.
248      * Position must be: 0 <= pos < max_number_of_profiles
249      * @param obj
250      * @param pos
251      * @param host
252      * @param port
253      */

254     public void testHostAndPortInIIOPProfile(org.omg.CORBA.Object JavaDoc obj, int pos, String JavaDoc host, int port)
255     {
256        // try to get ORB delegate to object
257
org.jacorb.orb.Delegate jacOrbDelegate = null;
258        Delegate JavaDoc localObj = ((org.omg.CORBA.portable.ObjectImpl JavaDoc)obj)._get_delegate();
259        jacOrbDelegate = (org.jacorb.orb.Delegate)localObj;
260
261        ParsedIOR pior = jacOrbDelegate.getParsedIOR();
262        org.omg.IOP.IOR JavaDoc ior = jacOrbDelegate.getIOR();
263
264        TaggedProfile JavaDoc[] profiles = ior.profiles;
265        int cnt = pos;
266        boolean found = false;
267        for (int i = 0; i < profiles.length; i++)
268        {
269            if (profiles[i].tag == TAG_INTERNET_IOP.value)
270            {
271               if( cnt == 0 )
272               {
273                   IIOPProfile prof = new IIOPProfile(profiles[i].profile_data);
274                   assertEquals(prof.getAddress().getIP(), host);
275                   assertEquals(prof.getAddress().getPort(), port);
276                   found = true;
277                   break;
278               }
279               else
280               {
281                   cnt--;
282                   continue;
283               }
284            }
285        }
286        assertEquals(true, found);
287     }
288
289     public static void main(String JavaDoc args[])
290     {
291       junit.textui.TestRunner.run(suite());
292     }
293
294 }
295
Popular Tags