diff --git a/lucene/contrib/remote/src/test/org/apache/lucene/search/RemoteTestCaseJ4.java b/lucene/contrib/remote/src/test/org/apache/lucene/search/RemoteTestCaseJ4.java new file mode 100644 index 00000000000..1209866e845 --- /dev/null +++ b/lucene/contrib/remote/src/test/org/apache/lucene/search/RemoteTestCaseJ4.java @@ -0,0 +1,79 @@ +package org.apache.lucene.search; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.ServerSocket; +import java.net.Socket; +import java.rmi.Naming; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.server.RMIClientSocketFactory; +import java.rmi.server.RMIServerSocketFactory; + +import org.apache.lucene.util.LuceneTestCaseJ4; +import org.junit.AfterClass; + +/** + * Base class for remote tests. + *

+ * Call {@link #startServer(Searchable)} in a {@link #BeforeClass} annotated method + * to start the server. + * Call {@link #lookupRemote} to get a RemoteSearchable. + */ +public abstract class RemoteTestCaseJ4 extends LuceneTestCaseJ4 { + private static int port; + + public static void startServer(Searchable searchable) throws Exception { + // publish it + // use our own factories for testing, so we can bind to an ephemeral port. + RMIClientSocketFactory clientFactory = new RMIClientSocketFactory() { + public Socket createSocket(String host, int port) throws IOException { + return new Socket(host, port); + }}; + + class TestRMIServerSocketFactory implements RMIServerSocketFactory { + ServerSocket socket; + public ServerSocket createServerSocket(int port) throws IOException { + return (socket = new ServerSocket(port)); + } + }; + TestRMIServerSocketFactory serverFactory = new TestRMIServerSocketFactory(); + + LocateRegistry.createRegistry(0, clientFactory, serverFactory); + RemoteSearchable impl = new RemoteSearchable(searchable); + port = serverFactory.socket.getLocalPort(); + Naming.rebind("//localhost:" + port + "/Searchable", impl); + } + + @AfterClass + public static void stopServer() { + try { + Naming.unbind("//localhost:" + port + "/Searchable"); + } catch (RemoteException e) { + } catch (MalformedURLException e) { + } catch (NotBoundException e) { + } + } + + public static Searchable lookupRemote() throws Exception { + return (Searchable)Naming.lookup("//localhost:" + port + "/Searchable"); + } +} diff --git a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteCachingWrapperFilter.java b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteCachingWrapperFilter.java index e55b56e7ae6..7b22e6a3dd0 100644 --- a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteCachingWrapperFilter.java +++ b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteCachingWrapperFilter.java @@ -17,12 +17,6 @@ package org.apache.lucene.search; * limitations under the License. */ -import java.rmi.Naming; -import java.rmi.registry.LocateRegistry; - -import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util._TestUtil; - import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; @@ -30,32 +24,17 @@ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.store.RAMDirectory; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; /** * Tests that the index is cached on the searcher side of things. - * NOTE: This is copied from TestRemoteSearchable since it already had a remote index set up. */ -public class TestRemoteCachingWrapperFilter extends LuceneTestCase { - public TestRemoteCachingWrapperFilter(String name) { - super(name); - } - - private static Searchable getRemote() throws Exception { - try { - return lookupRemote(); - } catch (Throwable e) { - startServer(); - return lookupRemote(); - } - } - - private static Searchable lookupRemote() throws Exception { - return (Searchable)Naming.lookup("//localhost:" + port + "/Searchable"); - } - - private static int port; - - private static void startServer() throws Exception { +public class TestRemoteCachingWrapperFilter extends RemoteTestCaseJ4 { + @BeforeClass + public static void beforeClass() throws Exception { // construct an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new IndexWriterConfig( @@ -74,17 +53,12 @@ public class TestRemoteCachingWrapperFilter extends LuceneTestCase { writer.addDocument(doc); writer.optimize(); writer.close(); - - // publish it - port = _TestUtil.getRandomSocketPort(); - LocateRegistry.createRegistry(port); Searchable local = new IndexSearcher(indexStore, true); - RemoteSearchable impl = new RemoteSearchable(local); - Naming.rebind("//localhost:" + port + "/Searchable", impl); + startServer(local); } private static void search(Query query, Filter filter, int hitNumber, String typeValue) throws Exception { - Searchable[] searchables = { getRemote() }; + Searchable[] searchables = { lookupRemote() }; Searcher searcher = new MultiSearcher(searchables); ScoreDoc[] result = searcher.search(query,filter, 1000).scoreDocs; assertEquals(1, result.length); @@ -94,7 +68,7 @@ public class TestRemoteCachingWrapperFilter extends LuceneTestCase { assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 3, document.getFields().size() == 3); } - + @Test public void testTermRemoteFilter() throws Exception { CachingWrapperFilterHelper cwfh = new CachingWrapperFilterHelper(new QueryWrapperFilter(new TermQuery(new Term("type", "a")))); diff --git a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSearchable.java b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSearchable.java index f160563e982..8b134a8f92b 100644 --- a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSearchable.java +++ b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSearchable.java @@ -17,46 +17,25 @@ package org.apache.lucene.search; * limitations under the License. */ -import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util._TestUtil; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.store.RAMDirectory; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; -import java.rmi.Naming; -import java.rmi.registry.LocateRegistry; import java.util.Collections; import java.util.Set; import java.util.HashSet; -public class TestRemoteSearchable extends LuceneTestCase { - public TestRemoteSearchable(String name) { - super(name); - } +public class TestRemoteSearchable extends RemoteTestCaseJ4 { - private static int port = -1; - - private static Searchable getRemote() throws Exception { - if (port == -1) { - startServer(); - } - - try { - return lookupRemote(); - } catch (Throwable e) { - startServer(); - return lookupRemote(); - } - } - - private static Searchable lookupRemote() throws Exception { - return (Searchable)Naming.lookup("//localhost:" + port + "/Searchable"); - } - - private static void startServer() throws Exception { + @BeforeClass + public static void beforeClass() throws Exception { // construct an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new IndexWriterConfig( @@ -67,18 +46,13 @@ public class TestRemoteSearchable extends LuceneTestCase { writer.addDocument(doc); writer.optimize(); writer.close(); - - // publish it - port = _TestUtil.getRandomSocketPort(); - LocateRegistry.createRegistry(port); Searchable local = new IndexSearcher(indexStore, true); - RemoteSearchable impl = new RemoteSearchable(local); - Naming.rebind("//localhost:" + port + "/Searchable", impl); + startServer(local); } private static void search(Query query) throws Exception { // try to search the published index - Searchable[] searchables = { getRemote() }; + Searchable[] searchables = { lookupRemote() }; Searcher searcher = new MultiSearcher(searchables); ScoreDoc[] result = searcher.search(query, null, 1000).scoreDocs; @@ -99,16 +73,19 @@ public class TestRemoteSearchable extends LuceneTestCase { assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1); } + @Test public void testTermQuery() throws Exception { search(new TermQuery(new Term("test", "test"))); } + @Test public void testBooleanQuery() throws Exception { BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("test", "test")), BooleanClause.Occur.MUST); search(query); } + @Test public void testPhraseQuery() throws Exception { PhraseQuery query = new PhraseQuery(); query.add(new Term("test", "test")); @@ -117,9 +94,10 @@ public class TestRemoteSearchable extends LuceneTestCase { } // Tests bug fix at http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20290 + @Test public void testQueryFilter() throws Exception { // try to search the published index - Searchable[] searchables = { getRemote() }; + Searchable[] searchables = { lookupRemote() }; Searcher searcher = new MultiSearcher(searchables); ScoreDoc[] hits = searcher.search( new TermQuery(new Term("test", "text")), @@ -131,9 +109,10 @@ public class TestRemoteSearchable extends LuceneTestCase { assertEquals(0, nohits.length); } + @Test public void testConstantScoreQuery() throws Exception { // try to search the published index - Searchable[] searchables = { getRemote() }; + Searchable[] searchables = { lookupRemote() }; Searcher searcher = new MultiSearcher(searchables); ScoreDoc[] hits = searcher.search( new ConstantScoreQuery(new QueryWrapperFilter( diff --git a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java index d61e1380d4f..687cb6b68f7 100644 --- a/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java +++ b/lucene/contrib/remote/src/test/org/apache/lucene/search/TestRemoteSort.java @@ -17,19 +17,14 @@ package org.apache.lucene.search; * limitations under the License. */ +import static org.junit.Assert.assertEquals; + import java.io.IOException; -import java.io.Serializable; -import java.rmi.Naming; -import java.rmi.registry.LocateRegistry; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; import java.util.Random; -import junit.framework.Test; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; @@ -38,10 +33,11 @@ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LogMergePolicy; import org.apache.lucene.index.Term; -import org.apache.lucene.util.BytesRef; import org.apache.lucene.store.RAMDirectory; -import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util._TestUtil; +import org.apache.lucene.util.BytesRef; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /** * Unit tests for remote sorting code. @@ -49,40 +45,15 @@ import org.apache.lucene.util._TestUtil; * methods and therefore unused members and methodes. */ -public class TestRemoteSort extends LuceneTestCase implements Serializable { +public class TestRemoteSort extends RemoteTestCaseJ4 { - private Searcher full; + private static IndexSearcher full; private Query queryX; private Query queryY; private Query queryA; private Query queryF; private Sort sort; - - public TestRemoteSort (String name) { - super (name); - } - - public static void main (String[] argv) { - if (argv == null || argv.length < 1) - TestRunner.run (suite()); - else if ("server".equals (argv[0])) { - TestRemoteSort test = new TestRemoteSort (null); - try { - test.startServer(); - Thread.sleep (500000); - } catch (Exception e) { - System.out.println (e); - e.printStackTrace(); - } - } - } - - public static Test suite() { - return new TestSuite (TestRemoteSort.class); - } - - // document data: // the tracer field is used to determine which document was hit // the contents field is used to search and sort by relevance @@ -90,7 +61,7 @@ public class TestRemoteSort extends LuceneTestCase implements Serializable { // the float field to sort by float // the string field to sort by string // the i18n field includes accented characters for testing locale-specific sorting - private String[][] data = new String[][] { + private static final String[][] data = new String[][] { // tracer contents int float string custom i18n long double, 'short', byte, 'custom parser encoding' { "A", "x a", "5", "4f", "c", "A-3", "p\u00EAche", "10", "-4.0", "3", "126", "J"},//A, x { "B", "y a", "5", "3.4028235E38", "i", "B-10", "HAT", "1000000000", "40.0", "24", "1", "I"},//B, y @@ -109,15 +80,14 @@ public class TestRemoteSort extends LuceneTestCase implements Serializable { }; // create an index of all the documents, or just the x, or just the y documents - private Searcher getIndex (boolean even, boolean odd) - throws IOException { + @BeforeClass + public static void beforeClass() throws Exception { RAMDirectory indexStore = new RAMDirectory (); IndexWriter writer = new IndexWriter(indexStore, new IndexWriterConfig( TEST_VERSION_CURRENT, new SimpleAnalyzer(TEST_VERSION_CURRENT)) .setMaxBufferedDocs(2)); ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(1000); for (int i=0; i scoresA = getScores (full.search (queryA, null, 1000).scoreDocs, full); // we'll test searching locally, remote and multi - MultiSearcher remote = new MultiSearcher (new Searchable[] { getRemote() }); + MultiSearcher remote = new MultiSearcher (new Searchable[] { lookupRemote() }); // change sorting and make sure relevancy stays the same @@ -447,32 +413,4 @@ public class TestRemoteSort extends LuceneTestCase implements Serializable { } } } - - private Searchable getRemote () throws Exception { - try { - return lookupRemote (); - } catch (Throwable e) { - startServer (); - return lookupRemote (); - } - } - - private Searchable lookupRemote () throws Exception { - return (Searchable) Naming.lookup ("//localhost:" + port + "/SortedSearchable"); - } - - private int port = -1; - - private void startServer () throws Exception { - // construct an index - port = _TestUtil.getRandomSocketPort(); - Searcher local = getFullIndex(); - // local.search (queryA, new Sort()); - - // publish it - LocateRegistry.createRegistry (port); - RemoteSearchable impl = new RemoteSearchable (local); - Naming.rebind ("//localhost:" + port + "/SortedSearchable", impl); - } - } diff --git a/lucene/src/test/org/apache/lucene/util/_TestUtil.java b/lucene/src/test/org/apache/lucene/util/_TestUtil.java index 0a61002a9e0..075335a57c6 100644 --- a/lucene/src/test/org/apache/lucene/util/_TestUtil.java +++ b/lucene/src/test/org/apache/lucene/util/_TestUtil.java @@ -111,9 +111,4 @@ public class _TestUtil { buf.append("]"); return buf.toString(); } - - public static int getRandomSocketPort() { - return 1024 + new Random().nextInt(64512); - } - }