SOLR-3112: Fix tests not to write to src/test-files

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1304441 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-03-23 15:32:44 +00:00
parent 37acabf4a8
commit 2590469186
6 changed files with 142 additions and 159 deletions

View File

@ -768,6 +768,8 @@ Build
----------------------
* SOLR-2487: Add build target to package war without slf4j jars (janhoy)
* SOLR-3112: Fix tests not to write to src/test-files (Luca Cavanna via Robert Muir)
================== 3.5.0 ==================
New Features

View File

@ -43,15 +43,10 @@ public class DistributedQueryElevationComponentTest extends BaseDistributedSearc
schemaString = "schema11.xml";
}
private static String elevateFilename = "elevate-data-distrib.xml";
@BeforeClass
public static void beforeClass() throws IOException {
System.setProperty("elevate.data.file", elevateFilename);
System.setProperty("elevate.data.file", "elevate.xml");
File parent = new File(TEST_HOME(), "conf");
File elevateFile = new File(parent, "elevate.xml");
File elevateDataFile = new File(parent, elevateFilename);
FileUtils.copyFile(elevateFile, elevateDataFile);
}
@AfterClass

View File

@ -22,7 +22,7 @@
-->
<config>
<luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>
<dataDir>${solr.solr.home}/data/${l10n:}-${version:}</dataDir>
<dataDir>${tempDir}/data/${l10n:}-${version:}</dataDir>
<directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.RAMDirectoryFactory}"/>
<updateHandler class="solr.DirectUpdateHandler2" />

View File

@ -0,0 +1,83 @@
package org.apache.solr.client.solrj.embedded;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.util.AbstractSolrTestCase;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public abstract class AbstractEmbeddedSolrServerTest extends LuceneTestCase {
protected static Logger log = LoggerFactory.getLogger(AbstractEmbeddedSolrServerTest.class);
protected static final File SOLR_HOME = SolrTestCaseJ4.getFile("solrj/solr/shared");
protected CoreContainer cores = null;
protected File tempDir;
private void createTempDir() {
tempDir = new File(TEMP_DIR, "solrtest-" + getTestClass().getSimpleName() + "-" + System.currentTimeMillis());
tempDir.mkdirs();
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
System.setProperty("solr.solr.home", SOLR_HOME.getAbsolutePath());
//The index is always stored within a temporary directory
createTempDir();
System.setProperty("tempDir", tempDir.getAbsolutePath());
cores = new CoreContainer(SOLR_HOME.getAbsolutePath(), getSolrXml());
}
protected abstract File getSolrXml() throws Exception;
@Override
@After
public void tearDown() throws Exception {
if (cores != null)
cores.shutdown();
deleteAdditionalFiles();
File dataDir = new File(tempDir,"data");
String skip = System.getProperty("solr.test.leavedatadir");
if (null != skip && 0 != skip.trim().length()) {
log.info("NOTE: per solr.test.leavedatadir, dataDir will not be removed: " + dataDir.getAbsolutePath());
} else {
//Removing the temporary directory which contains the index (all other files should have been removed before)
if (!AbstractSolrTestCase.recurseDelete(tempDir)) {
log.warn("!!!! WARNING: best effort to remove " + dataDir.getAbsolutePath() + " FAILED !!!!!");
}
}
super.tearDown();
}
protected void deleteAdditionalFiles() {
}
protected SolrServer getSolrCore0() {
return getSolrCore("core0");
}
protected SolrServer getSolrCore1() {
return getSolrCore("core1");
}
protected SolrServer getSolrCore(String name) {
return new EmbeddedSolrServer(cores, name);
}
}

View File

@ -17,28 +17,20 @@ package org.apache.solr.client.solrj.embedded;
* limitations under the License.
*/
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.lucene.util.SystemPropertiesRestoreRule;
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.junit.Rule;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestEmbeddedSolrServer extends LuceneTestCase {
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class TestEmbeddedSolrServer extends AbstractEmbeddedSolrServerTest {
@Rule
public TestRule solrTestRules =
@ -46,66 +38,23 @@ 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");
}
@Override
protected File getSolrXml() throws Exception {
return new File(SOLR_HOME, "solr.xml");
}
public void testGetCoreContainer() {
Assert.assertEquals(cores, getSolrCore0().getCoreContainer());
Assert.assertEquals(cores, getSolrCore1().getCoreContainer());
Assert.assertEquals(cores, ((EmbeddedSolrServer)getSolrCore0()).getCoreContainer());
Assert.assertEquals(cores, ((EmbeddedSolrServer)getSolrCore1()).getCoreContainer());
}
public void testShutdown() {
EmbeddedSolrServer solrServer = getSolrCore0();
EmbeddedSolrServer solrServer = (EmbeddedSolrServer)getSolrCore0();
Assert.assertEquals(3, cores.getCores().size());
List<SolrCore> solrCores = new ArrayList<SolrCore>();
@ -121,7 +70,6 @@ public class TestEmbeddedSolrServer extends LuceneTestCase {
for (SolrCore solrCore : solrCores) {
Assert.assertEquals(true, solrCore.isClosed());
}
}
}

View File

@ -17,18 +17,7 @@
package org.apache.solr.client.solrj.embedded;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SystemPropertiesRestoreRule;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrQuery;
@ -41,9 +30,6 @@ import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.FileUtils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.util.AbstractSolrTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
@ -53,15 +39,24 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileInputStream;
/**
*
* @since solr 1.3
*/
public class TestSolrProperties extends LuceneTestCase {
public class TestSolrProperties extends AbstractEmbeddedSolrServerTest {
protected static Logger log = LoggerFactory.getLogger(TestSolrProperties.class);
protected CoreContainer cores = null;
private File home;
private File solrXml;
private static final String SOLR_XML = "solr.xml";
private static final String SOLR_PERSIST_XML = "solr-persist.xml";
@Rule
public TestRule solrTestRules =
@ -69,61 +64,25 @@ public class TestSolrProperties extends LuceneTestCase {
private static final XPathFactory xpathFactory = XPathFactory.newInstance();
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());
solrXml = new File(home, getSolrXml());
protected File getSolrXml() throws Exception {
//This test writes on the directory where the solr.xml is located. Better to copy the solr.xml to
//the temporary directory where we store the index
File origSolrXml = new File(SOLR_HOME, SOLR_XML);
File solrXml = new File(tempDir, SOLR_XML);
FileUtils.copyFile(origSolrXml, solrXml);
cores = new CoreContainer(home.getAbsolutePath(), solrXml);
return solrXml;
}
@Override
@After
public void tearDown() throws Exception {
if (cores != null)
cores.shutdown();
File dataDir = new File(home,"data");
String skip = System.getProperty("solr.test.leavedatadir");
if (null != skip && 0 != skip.trim().length()) {
log.info("NOTE: per solr.test.leavedatadir, dataDir will not be removed: " + dataDir.getAbsolutePath());
} else {
if (!AbstractSolrTestCase.recurseDelete(dataDir)) {
log.warn("!!!! WARNING: best effort to remove " + dataDir.getAbsolutePath() + " FAILED !!!!!");
}
}
File persistedFile = new File(home,"solr-persist.xml");
protected void deleteAdditionalFiles() {
super.deleteAdditionalFiles();
//Cleans the solr.xml persisted while testing and the solr.xml copied to the temporary directory
File persistedFile = new File(tempDir, SOLR_PERSIST_XML);
assertTrue("Failed to delete "+persistedFile, persistedFile.delete());
assertTrue("Failed to delete "+solrXml, solrXml.delete());
super.tearDown();
}
protected SolrServer getSolrCore0() {
return new EmbeddedSolrServer(cores, "core0");
}
protected SolrServer getSolrCore1() {
return new EmbeddedSolrServer(cores, "core1");
File solrXml = new File(tempDir, SOLR_XML);
assertTrue("Failed to delete "+ solrXml, solrXml.delete());
}
protected SolrServer getSolrAdmin() {
@ -134,10 +93,6 @@ public class TestSolrProperties extends LuceneTestCase {
return new EmbeddedSolrServer(cores, "renamed_core");
}
protected SolrServer getSolrCore(String name) {
return new EmbeddedSolrServer(cores, name);
}
@Test
public void testProperties() throws Exception {
UpdateRequest up = new UpdateRequest();
@ -209,14 +164,14 @@ public class TestSolrProperties extends LuceneTestCase {
long after = mcr.getStartTime(name).getTime();
assertTrue("should have more recent time: " + after + "," + before, after > before);
mcr = CoreAdminRequest.persist("solr-persist.xml", coreadmin);
mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, coreadmin);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
FileInputStream fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
FileInputStream fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML));
try {
Document document = builder.parse(fis);
fis.close();
fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML));
String solrPersistXml = IOUtils.toString(fis);
//System.out.println("xml:" + solrPersistXml);
assertTrue("\"/solr/cores[@defaultCoreName='core0']\" doesn't match in:\n" + solrPersistXml,
@ -235,14 +190,14 @@ public class TestSolrProperties extends LuceneTestCase {
}
CoreAdminRequest.renameCore(name, "renamed_core", coreadmin);
mcr = CoreAdminRequest.persist("solr-persist.xml", getRenamedSolrAdmin());
mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin());
// fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
// fis = new FileInputStream(new File(solrXml.getParent(), SOLR_PERSIST_XML));
// String solrPersistXml = IOUtils.toString(fis);
// System.out.println("xml:" + solrPersistXml);
// fis.close();
fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML));
try {
Document document = builder.parse(fis);
assertTrue(exists("/solr/cores/core[@name='renamed_core']", document));
@ -254,21 +209,21 @@ public class TestSolrProperties extends LuceneTestCase {
}
coreadmin = getRenamedSolrAdmin();
CoreAdminRequest.createCore("newCore", home.getAbsolutePath(), coreadmin);
CoreAdminRequest.createCore("newCore", SOLR_HOME.getAbsolutePath(), coreadmin);
// fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
// fis = new FileInputStream(new File(solrXml.getParent(), SOLR_PERSIST_XML));
// solrPersistXml = IOUtils.toString(fis);
// System.out.println("xml:" + solrPersistXml);
// fis.close();
mcr = CoreAdminRequest.persist("solr-persist.xml", getRenamedSolrAdmin());
mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin());
// fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
// fis = new FileInputStream(new File(solrXml.getParent(), SOLR_PERSIST_XML));
// solrPersistXml = IOUtils.toString(fis);
// System.out.println("xml:" + solrPersistXml);
// fis.close();
fis = new FileInputStream(new File(solrXml.getParent(), "solr-persist.xml"));
fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML));
try {
Document document = builder.parse(fis);
assertTrue(exists("/solr/cores/core[@name='collection1' and (@instanceDir='./' or @instanceDir='.\\')]", document));
@ -279,13 +234,13 @@ public class TestSolrProperties extends LuceneTestCase {
// test reload and parse
cores.shutdown();
cores = new CoreContainer(home.getAbsolutePath(), new File(solrXml.getParent(), "solr-persist.xml"));
cores = new CoreContainer(SOLR_HOME.getAbsolutePath(), new File(tempDir, SOLR_PERSIST_XML));
mcr = CoreAdminRequest.persist("solr-persist.xml", getRenamedSolrAdmin());
mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin());
// fis = new FileInputStream(new File(solrXml.getParent(),
// "solr-persist.xml"));
// SOLR_PERSIST_XML));
// solrPersistXml = IOUtils.toString(fis);
// System.out.println("xml:" + solrPersistXml);
// fis.close();