1 22 package org.objectweb.petals.kernel.registry; 23 24 import java.io.ObjectInputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.net.InetSocketAddress ; 27 import java.net.Socket ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.naming.Binding ; 32 import javax.naming.LinkRef ; 33 import javax.naming.NameClassPair ; 34 35 import junit.framework.TestCase; 36 37 import org.easymock.classextension.EasyMock; 38 import org.objectweb.petals.kernel.registry.mock.MockConfigurationService; 39 import org.objectweb.petals.kernel.registry.msg.request.BindRequest; 40 import org.objectweb.petals.kernel.registry.msg.request.CreateSubcontextRequest; 41 import org.objectweb.petals.kernel.registry.msg.request.DestroySubcontextRequest; 42 import org.objectweb.petals.kernel.registry.msg.request.ListBindingsRequest; 43 import org.objectweb.petals.kernel.registry.msg.request.ListRequest; 44 import org.objectweb.petals.kernel.registry.msg.request.LookupLinkRequest; 45 import org.objectweb.petals.kernel.registry.msg.request.LookupRequest; 46 import org.objectweb.petals.kernel.registry.msg.request.RebindRequest; 47 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest; 48 import org.objectweb.petals.kernel.registry.msg.request.RenameRequest; 49 import org.objectweb.petals.kernel.registry.msg.request.UnbindRequest; 50 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse; 51 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse.ResponseType; 52 import org.objectweb.petals.util.LoggingUtil; 53 import org.objectweb.petals.util.SystemUtil; 54 import org.objectweb.util.monolog.api.Logger; 55 56 61 public class ServerAloneTest extends TestCase { 62 63 private RegistryServer server; 64 65 public void setUp() throws InterruptedException { 66 SystemUtil.setJndiPort("16400"); 67 String groupName = "petals" 68 + System.nanoTime(); 69 server = new RegistryServer(); 70 Logger logger = EasyMock.createMock(Logger.class); 71 server.logger = logger; 72 server.configurationService = new MockConfigurationService(); 73 server.log = EasyMock.createMock(LoggingUtil.class); 74 server.registryUtil.setLog(EasyMock.createMock(LoggingUtil.class)); 75 server.setGroupName(groupName); 76 77 server.startServer(); 78 Thread.sleep(1000); 80 } 81 82 public void tearDown() { 83 server.stopServer(); 84 } 85 86 public void testBind() throws Exception { 87 Socket socket = new Socket (); 88 socket 89 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 90 91 RegistryRequest request = new BindRequest("/", "key", "value", 0, 0); 92 93 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 94 .getOutputStream()); 95 objectOutputStream.writeObject(request); 96 objectOutputStream.flush(); 97 98 ObjectInputStream objectInputStream = new ObjectInputStream (socket 99 .getInputStream()); 100 RegistryResponse response = (RegistryResponse) objectInputStream 101 .readObject(); 102 103 if (!response.getType().equals(ResponseType.ack)) { 104 fail(); 105 } else { 106 assertEquals(server.data.get("/").get("key"), "value"); 107 } 108 } 109 110 @SuppressWarnings ("unchecked") 111 public void testBindInSubcontext() throws Exception { 112 113 testCreateSubcontext(); 114 115 Socket socket = new Socket (); 116 socket 117 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 118 119 RegistryRequest request = new BindRequest("/sub/", "key", "value", 0, 0); 120 121 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 122 .getOutputStream()); 123 objectOutputStream.writeObject(request); 124 objectOutputStream.flush(); 125 126 ObjectInputStream objectInputStream = new ObjectInputStream (socket 127 .getInputStream()); 128 RegistryResponse response = (RegistryResponse) objectInputStream 129 .readObject(); 130 131 if (!response.getType().equals(ResponseType.ack)) { 132 fail(); 133 } else { 134 assertEquals(server.data.get("/sub/").get("key"), "value"); 135 } 136 } 137 138 public void testCreateSubcontext() throws Exception { 139 Socket socket = new Socket (); 140 socket 141 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 142 143 RegistryRequest request = new CreateSubcontextRequest("/", "sub", 0, 0); 144 145 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 146 .getOutputStream()); 147 objectOutputStream.writeObject(request); 148 objectOutputStream.flush(); 149 150 ObjectInputStream objectInputStream = new ObjectInputStream (socket 151 .getInputStream()); 152 RegistryResponse response = (RegistryResponse) objectInputStream 153 .readObject(); 154 155 if (!response.getType().equals(ResponseType.createSubcontext)) { 156 fail(); 157 } else { 158 assertEquals(response.getArg1(), "/sub/"); 159 assertNotNull(server.data.get("/").get("sub")); 160 assertNotNull(server.data.get("/sub/")); 161 } 162 } 163 164 public void testDestroySubcontext() throws Exception { 165 Socket socket = new Socket (); 166 socket 167 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 168 169 Map <String , Object > map = new HashMap <String , Object >(); 170 map.put("sub", new HashMap <String , Object >()); 171 172 server.data.put("/", map); 173 174 Map <String , Object > subMap = new HashMap <String , Object >(); 175 subMap.put("key", "value"); 176 server.data.put("/sub/", subMap); 177 178 RegistryRequest request = new DestroySubcontextRequest("/", "sub", 0, 0); 179 180 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 181 .getOutputStream()); 182 objectOutputStream.writeObject(request); 183 objectOutputStream.flush(); 184 185 ObjectInputStream objectInputStream = new ObjectInputStream (socket 186 .getInputStream()); 187 RegistryResponse response = (RegistryResponse) objectInputStream 188 .readObject(); 189 190 if (!response.getType().equals(ResponseType.ack)) { 191 fail(); 192 } else { 193 assertEquals(server.data.get("/").size(), 0); 194 assertNull(server.getData().get("/sub/")); 195 } 196 } 197 198 public void testList() throws Exception { 199 Socket socket = new Socket (); 200 socket 201 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 202 203 Map <String , Object > sub = new HashMap <String , Object >(); 204 sub.put("key", "value"); 205 server.data.put("/sub/", sub); 206 207 RegistryRequest request = new ListRequest("/", "sub", 0, 0); 208 209 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 210 .getOutputStream()); 211 objectOutputStream.writeObject(request); 212 objectOutputStream.flush(); 213 214 ObjectInputStream objectInputStream = new ObjectInputStream (socket 215 .getInputStream()); 216 RegistryResponse response = (RegistryResponse) objectInputStream 217 .readObject(); 218 219 if (!response.getType().equals(ResponseType.list)) { 220 fail(); 221 } else { 222 assertEquals("key", ((NameClassPair ) ((NameClassPair []) response 223 .getArg1())[0]).getName()); 224 } 225 } 226 227 public void testListBindings() throws Exception { 228 Socket socket = new Socket (); 229 socket 230 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 231 232 Map <String , Object > sub = new HashMap <String , Object >(); 233 sub.put("key", "value"); 234 server.data.put("/sub/", sub); 235 236 RegistryRequest request = new ListBindingsRequest("/", "sub", 0, 0); 237 238 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 239 .getOutputStream()); 240 objectOutputStream.writeObject(request); 241 objectOutputStream.flush(); 242 243 ObjectInputStream objectInputStream = new ObjectInputStream (socket 244 .getInputStream()); 245 RegistryResponse response = (RegistryResponse) objectInputStream 246 .readObject(); 247 248 if (!response.getType().equals(ResponseType.listBindings)) { 249 fail(); 250 } else { 251 assertEquals("key", ((Binding ) ((Binding []) response.getArg1())[0]) 252 .getName()); 253 assertEquals("value", 254 ((Binding ) ((Binding []) response.getArg1())[0]).getObject()); 255 } 256 } 257 258 public void testLookup() throws Exception { 259 Socket socket = new Socket (); 260 socket 261 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 262 Map <String , Object > map = new HashMap <String , Object >(); 263 map.put("key", "value"); 264 server.data.put("/", map); 265 266 RegistryRequest request = new LookupRequest("/", "key", 0, 0); 267 268 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 269 .getOutputStream()); 270 objectOutputStream.writeObject(request); 271 objectOutputStream.flush(); 272 273 ObjectInputStream objectInputStream = new ObjectInputStream (socket 274 .getInputStream()); 275 RegistryResponse response = (RegistryResponse) objectInputStream 276 .readObject(); 277 278 if (!response.getType().equals(ResponseType.lookup)) { 279 fail(); 280 } else { 281 assertEquals(response.getArg1(), "value"); 282 } 283 } 284 285 public void testLookupLink() throws Exception { 286 Socket socket = new Socket (); 287 socket 288 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 289 290 Map <String , Object > map = new HashMap <String , Object >(); 291 map.put("key", "value"); 292 server.data.put("/", map); 293 294 RegistryRequest request = new LookupLinkRequest("/", "key", 0, 0); 295 296 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 297 .getOutputStream()); 298 objectOutputStream.writeObject(request); 299 objectOutputStream.flush(); 300 301 ObjectInputStream objectInputStream = new ObjectInputStream (socket 302 .getInputStream()); 303 RegistryResponse response = (RegistryResponse) objectInputStream 304 .readObject(); 305 306 if (!response.getType().equals(ResponseType.lookupLink)) { 307 fail(); 308 } else { 309 assertEquals(response.getArg1(), "value"); 310 } 311 } 312 313 public void testLookupLinkWithLink() throws Exception { 314 Socket socket = new Socket (); 315 socket 316 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 317 318 Map <String , Object > subMap = new HashMap <String , Object >(); 319 LinkRef linkRef = new LinkRef ("/key"); 320 subMap.put("link", linkRef); 321 322 server.data.put("/sub/", subMap); 323 Map <String , Object > map = new HashMap <String , Object >(); 324 map.put("key", "value"); 325 server.data.put("/", map); 326 327 RegistryRequest request = new LookupLinkRequest("/sub/", "link", 0, 0); 328 329 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 330 .getOutputStream()); 331 objectOutputStream.writeObject(request); 332 objectOutputStream.flush(); 333 334 ObjectInputStream objectInputStream = new ObjectInputStream (socket 335 .getInputStream()); 336 RegistryResponse response = (RegistryResponse) objectInputStream 337 .readObject(); 338 339 if (!response.getType().equals(ResponseType.lookupLink)) { 340 fail(); 341 } else { 342 assertEquals(response.getArg1(), "value"); 343 } 344 } 345 346 public void testRebind() throws Exception { 347 Socket socket = new Socket (); 348 socket 349 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 350 351 Map <String , Object > map = new HashMap <String , Object >(); 352 map.put("key", "value"); 353 server.data.put("/", map); 354 355 RegistryRequest request = new RebindRequest("/", "key", "value1", 0, 0); 356 357 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 358 .getOutputStream()); 359 objectOutputStream.writeObject(request); 360 objectOutputStream.flush(); 361 362 ObjectInputStream objectInputStream = new ObjectInputStream (socket 363 .getInputStream()); 364 RegistryResponse response = (RegistryResponse) objectInputStream 365 .readObject(); 366 367 if (!response.getType().equals(ResponseType.ack)) { 368 fail(); 369 } else { 370 assertEquals(server.data.get("/").get("key"), "value1"); 371 } 372 } 373 374 public void testRename() throws Exception { 375 Socket socket = new Socket (); 376 socket 377 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 378 Map <String , Object > map = new HashMap <String , Object >(); 379 map.put("key", "value"); 380 server.data.put("/", map); 381 382 RegistryRequest request = new RenameRequest("/", "key", "key1", 0, 0); 383 384 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 385 .getOutputStream()); 386 objectOutputStream.writeObject(request); 387 objectOutputStream.flush(); 388 389 ObjectInputStream objectInputStream = new ObjectInputStream (socket 390 .getInputStream()); 391 RegistryResponse response = (RegistryResponse) objectInputStream 392 .readObject(); 393 394 if (!response.getType().equals(ResponseType.ack)) { 395 fail(); 396 } else { 397 assertEquals(server.data.get("/").get("key1"), "value"); 398 } 399 } 400 401 public void testUnbind() throws Exception { 402 Socket socket = new Socket (); 403 socket 404 .connect(new InetSocketAddress (SystemUtil.getHost(), 16400), 1000); 405 406 Map <String , Object > map = new HashMap <String , Object >(); 407 map.put("key", "value"); 408 server.data.put("/", map); 409 410 RegistryRequest request = new UnbindRequest("/", "key", 0, 0); 411 412 ObjectOutputStream objectOutputStream = new ObjectOutputStream (socket 413 .getOutputStream()); 414 objectOutputStream.writeObject(request); 415 objectOutputStream.flush(); 416 417 ObjectInputStream objectInputStream = new ObjectInputStream (socket 418 .getInputStream()); 419 RegistryResponse response = (RegistryResponse) objectInputStream 420 .readObject(); 421 422 if (!response.getType().equals(ResponseType.ack)) { 423 fail(); 424 } else { 425 assertEquals(server.data.get("/").size(), 0); 426 } 427 } 428 429 } 430 | Popular Tags |