1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24 import java.rmi.Naming;
25 import java.rmi.NotBoundException;
26 import java.rmi.RemoteException;
27 import java.rmi.registry.LocateRegistry;
28 import java.rmi.server.RMIClientSocketFactory;
29 import java.rmi.server.RMIServerSocketFactory;
31 import org.apache.lucene.util.LuceneTestCase;
32 import org.junit.AfterClass;
35 * Base class for remote tests.
37 * Call {@link #startServer(Searchable)} in a {@link #BeforeClass} annotated method
38 * to start the server.
39 * Call {@link #lookupRemote} to get a RemoteSearchable.
41 public abstract class RemoteTestCase extends LuceneTestCase {
42 private static int port;
44 public static void startServer(Searchable searchable) throws Exception {
46 // use our own factories for testing, so we can bind to an ephemeral port.
47 RMIClientSocketFactory clientFactory = new RMIClientSocketFactory() {
48 public Socket createSocket(String host, int port) throws IOException {
49 return new Socket(host, port);
52 class TestRMIServerSocketFactory implements RMIServerSocketFactory {
54 public ServerSocket createServerSocket(int port) throws IOException {
55 return (socket = new ServerSocket(port));
58 TestRMIServerSocketFactory serverFactory = new TestRMIServerSocketFactory();
60 LocateRegistry.createRegistry(0, clientFactory, serverFactory);
61 RemoteSearchable impl = new RemoteSearchable(searchable);
62 port = serverFactory.socket.getLocalPort();
63 Naming.rebind("//localhost:" + port + "/Searchable", impl);
67 public static void stopServer() {
69 Naming.unbind("//localhost:" + port + "/Searchable");
70 } catch (RemoteException e) {
71 } catch (MalformedURLException e) {
72 } catch (NotBoundException e) {
76 public static Searchable lookupRemote() throws Exception {
77 return (Searchable)Naming.lookup("//localhost:" + port + "/Searchable");