1 package org.jacorb.test.orb; 2 3 23 24 import java.util.Properties ; 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 ; 32 import org.omg.IOP.TAG_INTERNET_IOP ; 33 import org.omg.IOP.TaggedProfile ; 34 35 import org.jacorb.test.*; 36 import org.jacorb.test.common.*; 37 import org.omg.CORBA.ORB ; 38 39 47 public class AlternateProfileTest extends ClientServerTestCase 48 { 49 protected IIOPAddressServer server = null; 50 protected ClientServerSetup _setup = null; 51 52 private static final String CORRECT_HOST = "127.0.0.1"; 53 private static final String WRONG_HOST = "10.0.1.223"; private static final String WRONG_HOST_2 = "10.0.1.223"; 56 private static final int CORRECT_PORT = 50000; 57 private static final int WRONG_PORT = 50001; 58 59 public AlternateProfileTest(String name, ClientServerSetup setup) 60 { 61 super(name, setup); 62 _setup = setup; 63 } 64 65 protected void setUp() throws Exception 66 { 67 server = IIOPAddressServerHelper.narrow(setup.getServerObject()); 68 } 69 70 protected void tearDown() throws Exception 71 { 72 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 client_props = new Properties (); 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 server_props = new Properties (); 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 ex) 142 { 143 } 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 ex) 159 { 160 } 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 _myOrb = _setup.getClientOrb(); 174 String 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 ex) 212 { 213 } 215 216 } 217 218 223 public void testNumberOfIIOPProfiles( int numberExpected, org.omg.CORBA.Object obj ) 224 { 225 org.jacorb.orb.Delegate jacOrbDelegate = null; 227 Delegate localObj = ((org.omg.CORBA.portable.ObjectImpl )obj)._get_delegate(); 228 jacOrbDelegate = (org.jacorb.orb.Delegate)localObj; 229 230 ParsedIOR pior = jacOrbDelegate.getParsedIOR(); 231 org.omg.IOP.IOR ior = jacOrbDelegate.getIOR(); 232 233 TaggedProfile [] 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 254 public void testHostAndPortInIIOPProfile(org.omg.CORBA.Object obj, int pos, String host, int port) 255 { 256 org.jacorb.orb.Delegate jacOrbDelegate = null; 258 Delegate localObj = ((org.omg.CORBA.portable.ObjectImpl )obj)._get_delegate(); 259 jacOrbDelegate = (org.jacorb.orb.Delegate)localObj; 260 261 ParsedIOR pior = jacOrbDelegate.getParsedIOR(); 262 org.omg.IOP.IOR ior = jacOrbDelegate.getIOR(); 263 264 TaggedProfile [] 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 args[]) 290 { 291 junit.textui.TestRunner.run(suite()); 292 } 293 294 } 295 | Popular Tags |