1 package org.jacorb.test.naming; 2 3 9 10 import org.omg.CosNaming.*; 11 import org.omg.CosNaming.NamingContextPackage.AlreadyBound ; 12 13 import junit.framework.*; 14 import junit.extensions.TestSetup; 15 16 import org.jacorb.test.common.ORBSetup; 17 18 public class ContextTest extends TestCase 19 { 20 private static org.omg.CORBA.ORB orb = null; 21 private static NamingContextExt rootContext = null; 22 private static NameComponent[] firstName, secondName, thirdName , failureName; 23 24 private static class Setup extends TestSetup 25 { 26 public Setup (Test test) 27 { 28 super (test); 29 } 30 31 protected void setUp () 32 { 33 org.omg.CORBA.Object obj = null; 34 35 orb = ORBSetup.getORB (); 36 try 37 { 38 obj = orb.resolve_initial_references ("NameService"); 39 } 40 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 41 { 42 fail ("Failed to resolve NameService: " + ex); 43 } 44 try 45 { 46 rootContext = 47 NamingContextExtHelper.narrow( orb.resolve_initial_references("NameService")); 48 } 49 catch (Throwable ex) 50 { 51 fail ("Failed to narrow to NamingContext: " + ex); 52 } 53 firstName = new NameComponent[1]; 54 firstName[0] = new NameComponent("first","context"); 55 56 secondName = new NameComponent[1]; 57 secondName[0] = new NameComponent("second","context"); 58 59 thirdName = new NameComponent[1]; 60 thirdName[0] = new NameComponent("third","context"); 61 62 failureName = new NameComponent[2]; 63 failureName[0] = secondName[0]; 64 failureName[1] = thirdName[0]; 65 } 66 67 68 protected void tearDown () 69 { 70 } 71 72 } 73 74 75 76 77 public ContextTest (String name) 78 { 79 super (name); 80 } 81 82 83 public static Test suite() 84 { 85 TestSuite suite = new TestSuite("Naming Context Tests"); 86 Setup setup = new Setup( suite ); 87 ORBSetup osetup = new ORBSetup( setup ); 88 89 suite.addTest (new ContextTest("testCreateContextSuccess")); 90 suite.addTest (new ContextTest("testCreateContextFailure")); 91 suite.addTest (new ContextTest("testUnbindContext")); 92 93 return osetup; 94 } 95 96 public void testUnbindContext() 97 { 98 try 99 { 100 rootContext.unbind( failureName ); 101 rootContext.unbind( secondName ); 102 rootContext.unbind( firstName ); 103 } 104 catch( Exception e) 105 { 106 fail("Exception " + e); 107 } 108 109 } 110 111 112 public void testCreateContextFailure() 113 { 114 116 try 117 { 118 NamingContextExt failContext = 119 NamingContextExtHelper.narrow(rootContext.bind_new_context( failureName )); 120 121 fail("NamingContext was expected to be already bound!"); 122 123 } 124 catch( AlreadyBound e ) 125 { 126 assertTrue ("We should get here!", true ); 127 } 128 catch( Exception e ) 129 { 130 fail("Expected AlreadyBound exception, but got " + e); 131 } 132 } 133 134 137 138 public void testCreateContextSuccess() 139 { 140 141 142 143 try 144 { 145 NamingContextExt firstsubContext = 146 NamingContextExtHelper.narrow( rootContext.bind_new_context( firstName ) ); 147 } 148 catch (Exception ex) 149 { 150 fail ("Failed to bind first name: " + ex); 151 } 152 153 org.omg.CORBA.Object obj = null; 154 try 155 { 156 obj = rootContext.resolve( firstName ); 157 } 158 catch (Exception ex) 159 { 160 fail ("Failed to resolve first name: " + ex); 161 } 162 163 164 165 try 166 { 167 NamingContextExt secondsubContext = 168 NamingContextExtHelper.narrow( rootContext.bind_new_context( secondName )); 169 170 NamingContextExt subsubContext = 171 NamingContextExtHelper.narrow( secondsubContext.bind_new_context( thirdName )); 172 173 } 174 catch (Exception e) 175 { 176 fail( "Exception " + e ); 177 } 178 179 180 } 181 } 182 183 184 185 | Popular Tags |