Retrieve CoreContainer from EmbeddedSolrServer

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1240980 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2012-02-06 11:23:33 +00:00
parent c880ef0647
commit dfce8dd760
2 changed files with 117 additions and 0 deletions

View File

@ -242,4 +242,19 @@ public class EmbeddedSolrServer extends SolrServer
{
return BinaryResponseWriter.getParsedResponse(req, rsp);
}
/**
* Shutdown all cores within the EmbeddedSolrServer instance
*/
public void shutdown() {
coreContainer.shutdown();
}
/**
* Getter method for the CoreContainer
* @return the core container
*/
public CoreContainer getCoreContainer() {
return coreContainer;
}
}

View File

@ -0,0 +1,102 @@
package org.apache.solr.client.solrj.embedded;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.util.FileUtils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.util.AbstractSolrTestCase;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestEmbeddedSolrServer extends LuceneTestCase {
protected static Logger log = LoggerFactory.getLogger(TestEmbeddedSolrServer.class);
protected CoreContainer cores = null;
private File home;
public String getSolrHome() {
return "solrj/solr/shared";
}
public String getOrigSolrXml() {
return "solr.xml";
}
public String getSolrXml() {
return "test-solr.xml";
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
System.setProperty("solr.solr.home", getSolrHome());
home = SolrTestCaseJ4.getFile(getSolrHome());
System.setProperty("solr.solr.home", home.getAbsolutePath());
log.info("pwd: " + (new File(".")).getAbsolutePath());
File origSolrXml = new File(home, getOrigSolrXml());
File solrXml = new File(home, getSolrXml());
FileUtils.copyFile(origSolrXml, solrXml);
cores = new CoreContainer(home.getAbsolutePath(), solrXml);
}
@Override
@After
public void tearDown() throws Exception {
if (cores != null) {
cores.shutdown();
}
File dataDir = new File(home,"data");
if (!AbstractSolrTestCase.recurseDelete(dataDir)) {
log.warn("!!!! WARNING: best effort to remove " + dataDir.getAbsolutePath() + " FAILED !!!!!");
}
super.tearDown();
}
protected EmbeddedSolrServer getSolrCore0() {
return new EmbeddedSolrServer(cores, "core0");
}
protected EmbeddedSolrServer getSolrCore1() {
return new EmbeddedSolrServer(cores, "core1");
}
public void testGetCoreContainer() {
Assert.assertEquals(cores, getSolrCore0().getCoreContainer());
Assert.assertEquals(cores, getSolrCore1().getCoreContainer());
}
public void testShutdown() {
EmbeddedSolrServer solrServer = getSolrCore0();
Assert.assertEquals(2, cores.getCores().size());
List<SolrCore> solrCores = new ArrayList<SolrCore>();
for (SolrCore solrCore : cores.getCores()) {
Assert.assertEquals(false, solrCore.isClosed());
solrCores.add(solrCore);
}
solrServer.shutdown();
Assert.assertEquals(0, cores.getCores().size());
for (SolrCore solrCore : solrCores) {
Assert.assertEquals(true, solrCore.isClosed());
}
}
}