add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / remote / src / test / org / apache / lucene / search / RemoteTestCase.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
30
31 import org.apache.lucene.util.LuceneTestCase;
32 import org.junit.AfterClass;
33
34 /**
35  * Base class for remote tests.
36  * <p>
37  * Call {@link #startServer(Searchable)} in a {@link #BeforeClass} annotated method
38  * to start the server.
39  * Call {@link #lookupRemote} to get a RemoteSearchable.
40  */
41 public abstract class RemoteTestCase extends LuceneTestCase {
42   private static int port;
43
44   public static void startServer(Searchable searchable) throws Exception {
45     // publish it
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);
50       }};
51
52     class TestRMIServerSocketFactory implements RMIServerSocketFactory {
53       ServerSocket socket;
54       public ServerSocket createServerSocket(int port) throws IOException {
55         return (socket = new ServerSocket(port));
56       }
57     };
58     TestRMIServerSocketFactory serverFactory = new TestRMIServerSocketFactory();
59     
60     LocateRegistry.createRegistry(0, clientFactory, serverFactory);
61     RemoteSearchable impl = new RemoteSearchable(searchable);
62     port = serverFactory.socket.getLocalPort();
63     Naming.rebind("//localhost:" + port + "/Searchable", impl);
64   }
65   
66   @AfterClass
67   public static void stopServer() {
68     try {
69       Naming.unbind("//localhost:" + port + "/Searchable");
70     } catch (RemoteException e) {
71     } catch (MalformedURLException e) {
72     } catch (NotBoundException e) {
73     }
74   }
75   
76   public static Searchable lookupRemote() throws Exception {
77     return (Searchable)Naming.lookup("//localhost:" + port + "/Searchable");
78   }
79 }