SOLR-13539: Introduce EmbeddedSolrServerTestBase

This groundwork commit allows tests to randomize request content-type
more flexibly.  This will be taken advantage of by subsequent commits.

Co-Authored-By: Thomas Woeckinger
Closes: #755
This commit is contained in:
Jason Gerlowski 2019-08-29 09:11:51 -04:00
parent f27665198a
commit 319cb005d3
12 changed files with 245 additions and 98 deletions

View File

@ -17,7 +17,7 @@
package org.apache.solr.update;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
@ -32,7 +32,7 @@ import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.is;
public class RootFieldTest extends SolrJettyTestBase {
public class RootFieldTest extends EmbeddedSolrServerTestBase {
private static boolean useRootSchema;
private static final String MESSAGE = "Update handler should create and process _root_ field " +
"unless there is no such a field in schema";

View File

@ -18,7 +18,7 @@ package org.apache.solr.client.solrj;
import java.util.Arrays;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
@ -27,7 +27,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class GetByIdTest extends SolrJettyTestBase {
public class GetByIdTest extends EmbeddedSolrServerTestBase {
@BeforeClass
public static void beforeClass() throws Exception {

View File

@ -16,7 +16,12 @@
*/
package org.apache.solr.client.solrj;
import org.apache.solr.SolrJettyTestBase;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
@ -25,16 +30,11 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
/**
*
* @since solr 1.3
*/
public abstract class LargeVolumeTestBase extends SolrJettyTestBase
public abstract class LargeVolumeTestBase extends EmbeddedSolrServerTestBase
{
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

View File

@ -27,6 +27,6 @@ import org.junit.BeforeClass;
public class LargeVolumeBinaryJettyTest extends LargeVolumeTestBase {
@BeforeClass
public static void beforeTest() throws Exception {
createAndStartJetty(legacyExampleCollection1SolrHome());
initCore();
}
}

View File

@ -24,6 +24,6 @@ import org.junit.BeforeClass;
public class LargeVolumeJettyTest extends LargeVolumeTestBase {
@BeforeClass
public static void beforeTest() throws Exception {
createAndStartJetty(legacyExampleCollection1SolrHome());
initCore();
}
}

View File

@ -29,6 +29,6 @@ public class SolrExampleEmbeddedTest extends SolrExampleTests {
@BeforeClass
public static void beforeTest() throws Exception {
initCore();
createAndStartJetty(legacyExampleCollection1SolrHome());
}
}

View File

@ -16,10 +16,10 @@
*/
package org.apache.solr.client.solrj.request;
import junit.framework.Assert;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@ -27,12 +27,12 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import junit.framework.Assert;
/**
* Test SolrPing in Solrj
*/
public class SolrPingTest extends SolrJettyTestBase {
public class SolrPingTest extends EmbeddedSolrServerTestBase {
@BeforeClass
public static void beforeClass() throws Exception {

View File

@ -15,31 +15,33 @@
* limitations under the License.
*/
package org.apache.solr.client.solrj.response;
import java.util.List;
import junit.framework.Assert;
import org.apache.solr.SolrJettyTestBase;
import java.util.List;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.TermsResponse.Term;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import junit.framework.Assert;
/**
* Test for TermComponent's response in Solrj
*/
public class TermsResponseTest extends SolrJettyTestBase {
public class TermsResponseTest extends EmbeddedSolrServerTestBase {
@BeforeClass
public static void beforeTest() throws Exception {
public static void beforeClass() throws Exception {
initCore();
}
@Before
@Override
public void setUp() throws Exception{
public void setUp() throws Exception {
super.setUp();
clearIndex();
assertU(commit());

View File

@ -15,8 +15,10 @@
* limitations under the License.
*/
package org.apache.solr.client.solrj.response;
import junit.framework.Assert;
import org.apache.solr.SolrJettyTestBase;
import java.util.List;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
@ -27,7 +29,7 @@ import org.apache.solr.common.params.SpellingParams;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.List;
import junit.framework.Assert;
/**
* Test for SpellCheckComponent's response in Solrj
@ -35,9 +37,10 @@ import java.util.List;
*
* @since solr 1.3
*/
public class TestSpellCheckResponse extends SolrJettyTestBase {
public class TestSpellCheckResponse extends EmbeddedSolrServerTestBase {
@BeforeClass
public static void beforeTest() throws Exception {
public static void beforeClass() throws Exception {
initCore();
}

View File

@ -15,11 +15,12 @@
* limitations under the License.
*/
package org.apache.solr.client.solrj.response;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.EmbeddedSolrServerTestBase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.QueryRequest;
@ -32,9 +33,10 @@ import org.junit.Test;
* Test for SuggesterComponent's response in Solrj
*
*/
public class TestSuggesterResponse extends SolrJettyTestBase {
public class TestSuggesterResponse extends EmbeddedSolrServerTestBase {
@BeforeClass
public static void beforeTest() throws Exception {
public static void beforeClass() throws Exception {
initCore();
}

View File

@ -0,0 +1,160 @@
/*
* 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.
*/
package org.apache.solr;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.ContentStreamBase.ByteArrayStream;
import org.apache.solr.util.ExternalPaths;
import org.junit.After;
import org.junit.AfterClass;
import com.google.common.io.ByteStreams;
abstract public class EmbeddedSolrServerTestBase extends SolrTestCaseJ4 {
protected static final String DEFAULT_CORE_NAME = "collection1";
public static EmbeddedSolrServer client = null;
@After
public synchronized void afterClass() throws Exception {
if (client != null) client.close();
client = null;
}
@AfterClass
public static void afterEmbeddedSolrServerTestBase() throws Exception {
}
public synchronized EmbeddedSolrServer getSolrClient() {
if (client == null) {
client = createNewSolrClient();
}
return client;
}
/**
* Create a new solr client. Subclasses should override for other options.
*/
public EmbeddedSolrServer createNewSolrClient() {
return new EmbeddedSolrServer(h.getCoreContainer(), DEFAULT_CORE_NAME) {
@Override
public void close() {
// do not close core container
}
};
}
public void upload(final String collection, final ContentStream... contents) {
final Path base = Paths.get(getSolrClient().getCoreContainer().getSolrHome(), collection);
writeTo(base, contents);
}
private void writeTo(final Path base, final ContentStream... contents) {
try {
if (!Files.exists(base)) {
Files.createDirectories(base);
}
for (final ContentStream content : contents) {
final File file = new File(base.toFile(), content.getName());
file.getParentFile().mkdirs();
try (OutputStream os = new FileOutputStream(file)) {
ByteStreams.copy(content.getStream(), os);
}
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public Collection<ContentStream> download(final String collection, final String... names) {
final Path base = Paths.get(getSolrClient().getCoreContainer().getSolrHome(), collection);
final List<ContentStream> result = new ArrayList<>();
if (Files.exists(base)) {
for (final String name : names) {
final File file = new File(base.toFile(), name);
if (file.exists() && file.canRead()) {
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteStreams.copy(new FileInputStream(file), os);
final ByteArrayStream stream = new ContentStreamBase.ByteArrayStream(os.toByteArray(), name);
result.add(stream);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
}
return result;
}
public static void initCore() throws Exception {
final String home = legacyExampleCollection1SolrHome();
final String config = home + "/" + DEFAULT_CORE_NAME + "/conf/solrconfig.xml";
final String schema = home + "/" + DEFAULT_CORE_NAME + "/conf/schema.xml";
initCore(config, schema, home);
}
public static String legacyExampleCollection1SolrHome() throws IOException {
final String sourceHome = ExternalPaths.SOURCE_HOME;
if (sourceHome == null)
throw new IllegalStateException("No source home! Cannot create the legacy example solr home directory.");
final File tempSolrHome = LuceneTestCase.createTempDir().toFile();
FileUtils.copyFileToDirectory(new File(sourceHome, "server/solr/solr.xml"), tempSolrHome);
final File collectionDir = new File(tempSolrHome, DEFAULT_CORE_NAME);
FileUtils.forceMkdir(collectionDir);
final File configSetDir = new File(sourceHome, "server/solr/configsets/sample_techproducts_configs/conf");
FileUtils.copyDirectoryToDirectory(configSetDir, collectionDir);
final Properties props = new Properties();
props.setProperty("name", DEFAULT_CORE_NAME);
try (Writer writer = new OutputStreamWriter(FileUtils.openOutputStream(new File(collectionDir, "core.properties")),
"UTF-8");) {
props.store(writer, null);
}
return tempSolrHome.getAbsolutePath();
}
}

View File

@ -16,10 +16,16 @@
*/
package org.apache.solr;
import java.io.File;
import java.io.OutputStreamWriter;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.Properties;
import java.util.SortedMap;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.embedded.JettyConfig;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
@ -31,14 +37,7 @@ import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.OutputStreamWriter;
import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Properties;
import java.util.SortedMap;
abstract public class SolrJettyTestBase extends SolrTestCaseJ4
{
@ -132,7 +131,6 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
}
}
public synchronized SolrClient getSolrClient() {
if (client == null) {
client = createNewSolrClient();
@ -147,23 +145,13 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
* Subclasses should override for other options.
*/
public SolrClient createNewSolrClient() {
if (jetty != null) {
try {
// setup the client...
String url = jetty.getBaseUrl().toString() + "/" + "collection1";
HttpSolrClient client = getHttpSolrClient(url, DEFAULT_CONNECTION_TIMEOUT);
final String url = jetty.getBaseUrl().toString() + "/" + "collection1";
final HttpSolrClient client = getHttpSolrClient(url, DEFAULT_CONNECTION_TIMEOUT);
return client;
}
catch( Exception ex ) {
throw new RuntimeException( ex );
}
} else {
return new EmbeddedSolrServer( h.getCoreContainer(), "collection1" ) {
@Override
public void close() {
// do not close core container
}
};
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
}
@ -179,13 +167,6 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
}
}
public static void initCore() throws Exception {
String exampleHome = legacyExampleCollection1SolrHome();
String exampleConfig = exampleHome+"/collection1/conf/solrconfig.xml";
String exampleSchema = exampleHome+"/collection1/conf/schema.xml";
initCore(exampleConfig, exampleSchema, exampleHome);
}
public static String legacyExampleCollection1SolrHome() {
String sourceHome = ExternalPaths.SOURCE_HOME;
if (sourceHome == null)
@ -226,5 +207,4 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
return legacyExampleSolrHome;
}
}