### Question:
RmiServerPublisher { public static int publish_service(Class<?> cls, Object obj) throws RmiCommException { return publish_service(cls, obj, 0); }  static int publish_service(Class<?> cls, Object obj); static int publish_service(Class<?> cls, Object obj, int port); static void shutdown_all();  }### Answer:
@Test(expected = IllegalArgumentException.class) public void publish_null_object() throws Exception { RmiServerPublisher.publish_service(TService.class, null); RmiServerPublisher.publish_service(null, new TService()); }
@Test(expected = IllegalArgumentException.class) public void publish_null_interface() throws Exception { RmiServerPublisher.publish_service(null, new TService()); }
@Test public void publish_respects_communication_ports() throws Exception { int cnt = 5; for (int i = 0; i < cnt; i++) { TService ts = new TService(); ts.m_val = i; String pp = RmiCommunicationPorts.class.getName(); int rnd = 14000 + RandomUtils.nextInt(2000); System.setProperty(pp + ".min-port", "" + rnd); System.setProperty(pp + ".max-port", "" + (rnd + 5)); int p = RmiServerPublisher.publish_service(TServiceI.class, ts); assertEquals(rnd, p); Client c = new Client("localhost", rnd); Object obj = c.lookup(TServiceI.class); assertNotNull(obj); assertTrue(obj instanceof TServiceI); } }
@Test public void publish_on_specific_port() throws Exception { final int iterationCount = 100; final int minAllowOk = 80; int done = 0; for (int i = 0; i < iterationCount; i++) { int p = 1025 + RandomUtils.nextInt(10000); try (Socket s = new Socket()) { s.connect(new InetSocketAddress(InetAddress.getLocalHost(), p), 50); continue; } catch (ConnectException|SocketTimeoutException e) { } done++; TService ts = new TService(); int pp = RmiServerPublisher.publish_service(TServiceI.class, ts, p); assertEquals(p, pp); try (Socket s = new Socket("localhost", p)) { } } assertTrue(done >= minAllowOk); }