SOLR-2299: enforce resource handling, prevent test interference by running in a jvm-unique CWD (under build/, same as tempDir)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1054164 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2010-12-31 20:17:20 +00:00
parent ffef43df4f
commit cf43718bca
44 changed files with 86 additions and 47 deletions

View File

@ -361,6 +361,10 @@
classpathref="test.compile.classpath"> classpathref="test.compile.classpath">
<src path="${src}/test" /> <src path="${src}/test" />
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="${dest}/tests">
<fileset dir="${src}/test/test-files" excludes="**/*.java"/>
</copy>
</target> </target>
<!-- Run core unit tests. --> <!-- Run core unit tests. -->
@ -421,7 +425,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/test/test-files/" dir="@{tempDir}/@{threadNum}"
tempdir="@{tempDir}/@{threadNum}" tempdir="@{tempDir}/@{threadNum}"
forkmode="perBatch" forkmode="perBatch"
> >

View File

@ -118,6 +118,10 @@
classpathref="test.classpath"> classpathref="test.classpath">
<src path="src/test"/> <src path="src/test"/>
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="${dest}/test-classes">
<fileset dir="src/test/test-files" excludes="**/*.java"/>
</copy>
</target> </target>
<target name="example" depends="build,dist"> <target name="example" depends="build,dist">
@ -136,7 +140,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/test/test-files/" dir="${junit.output.dir}"
tempdir="${junit.output.dir}" tempdir="${junit.output.dir}"
forkmode="perBatch" forkmode="perBatch"
> >

View File

@ -90,6 +90,10 @@
classpathref="test.classpath"> classpathref="test.classpath">
<src path="src/test/java"/> <src path="src/test/java"/>
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="${dest}/test-classes">
<fileset dir="src/test/resources" excludes="**/*.java"/>
</copy>
</target> </target>
<target name="example" depends="build,dist"> <target name="example" depends="build,dist">
@ -108,7 +112,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/test/resources/" dir="${junit.output.dir}"
tempdir="${junit.output.dir}" tempdir="${junit.output.dir}"
forkmode="perBatch" forkmode="perBatch"
> >

View File

@ -28,7 +28,7 @@ public abstract class AbstractClusteringTestCase extends SolrTestCaseJ4 {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
initCore("solrconfig.xml", "schema.xml"); initCore("solrconfig.xml", "schema.xml", "solr-clustering");
numberOfDocs = 0; numberOfDocs = 0;
for (String[] doc : DOCUMENTS) { for (String[] doc : DOCUMENTS) {
assertNull(h.validateUpdate(adoc("id", Integer.toString(numberOfDocs), "url", doc[0], "title", doc[1], "snippet", doc[2]))); assertNull(h.validateUpdate(adoc("id", Integer.toString(numberOfDocs), "url", doc[0], "title", doc[1], "snippet", doc[2])));

View File

@ -123,6 +123,10 @@
classpathref="test.classpath"> classpathref="test.classpath">
<src path="src/test/java" /> <src path="src/test/java" />
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="target/test-classes">
<fileset dir="src/test/resources" excludes="**/*.java"/>
</copy>
</target> </target>
<target name="compileExtrasTests" depends="compileExtras"> <target name="compileExtrasTests" depends="compileExtras">
@ -130,6 +134,10 @@
classpathref="test.classpath"> classpathref="test.classpath">
<src path="src/extras/test/java" /> <src path="src/extras/test/java" />
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="target/extras/test-classes">
<fileset dir="src/extras/test/resources" excludes="**/*.java"/>
</copy>
</target> </target>
<property name="tempDir" value="${junit.output.dir}/temp" /> <property name="tempDir" value="${junit.output.dir}/temp" />
@ -156,7 +164,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/test/resources/" dir="${tempDir}"
tempdir="${tempDir}" tempdir="${tempDir}"
forkmode="perBatch" forkmode="perBatch"
> >
@ -217,7 +225,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/extras/test/resources/" dir="${tempDir}"
tempdir="${tempDir}" tempdir="${tempDir}"
forkmode="perBatch" forkmode="perBatch"
> >

View File

@ -25,7 +25,7 @@ import org.junit.BeforeClass;
public class TestTikaEntityProcessor extends AbstractDataImportHandlerTestCase { public class TestTikaEntityProcessor extends AbstractDataImportHandlerTestCase {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
initCore("dataimport-solrconfig.xml", "dataimport-schema-no-unique-key.xml"); initCore("dataimport-solrconfig.xml", "dataimport-schema-no-unique-key.xml", "solr-dihextras");
} }
public void testIndexingWithTikaEntityProcessor() throws Exception { public void testIndexingWithTikaEntityProcessor() throws Exception {
@ -33,7 +33,7 @@ public class TestTikaEntityProcessor extends AbstractDataImportHandlerTestCase {
"<dataConfig>" + "<dataConfig>" +
" <dataSource type=\"BinFileDataSource\"/>" + " <dataSource type=\"BinFileDataSource\"/>" +
" <document>" + " <document>" +
" <entity processor=\"TikaEntityProcessor\" url=\"../../../../../extraction/src/test/resources/solr-word.pdf\" >" + " <entity processor=\"TikaEntityProcessor\" url=\"" + getFile("solr-word.pdf").getAbsolutePath() + "\" >" +
" <field column=\"Author\" meta=\"true\" name=\"author\"/>" + " <field column=\"Author\" meta=\"true\" name=\"author\"/>" +
" <field column=\"title\" meta=\"true\" name=\"docTitle\"/>" + " <field column=\"title\" meta=\"true\" name=\"docTitle\"/>" +
" <field column=\"text\"/>" + " <field column=\"text\"/>" +

View File

@ -52,6 +52,11 @@ import java.util.Map;
public abstract class AbstractDataImportHandlerTestCase extends public abstract class AbstractDataImportHandlerTestCase extends
SolrTestCaseJ4 { SolrTestCaseJ4 {
// note, a little twisted that we shadow this static method
public static void initCore(String config, String schema) throws Exception {
initCore(config, schema, "solr-dih");
}
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {

View File

@ -39,7 +39,7 @@ import java.util.List;
* @since solr 1.4 * @since solr 1.4
*/ */
public class TestContentStreamDataSource extends AbstractDataImportHandlerTestCase { public class TestContentStreamDataSource extends AbstractDataImportHandlerTestCase {
private static final String CONF_DIR = "." + File.separator + "solr" + File.separator + "conf" + File.separator; private static final String CONF_DIR = "." + File.separator + "solr-dih" + File.separator + "conf" + File.separator;
SolrInstance instance = null; SolrInstance instance = null;
JettySolrRunner jetty; JettySolrRunner jetty;
@ -129,12 +129,12 @@ public class TestContentStreamDataSource extends AbstractDataImportHandlerTestCa
confDir.mkdirs(); confDir.mkdirs();
File f = new File(confDir, "solrconfig.xml"); File f = new File(confDir, "solrconfig.xml");
FileUtils.copyFile(new File(getSolrConfigFile()), f); FileUtils.copyFile(getFile(getSolrConfigFile()), f);
f = new File(confDir, "schema.xml"); f = new File(confDir, "schema.xml");
FileUtils.copyFile(new File(getSchemaFile()), f); FileUtils.copyFile(getFile(getSchemaFile()), f);
f = new File(confDir, "data-config.xml"); f = new File(confDir, "data-config.xml");
FileUtils.copyFile(new File(CONF_DIR + "dataconfig-contentstream.xml"), f); FileUtils.copyFile(getFile(CONF_DIR + "dataconfig-contentstream.xml"), f);
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {

View File

@ -79,6 +79,10 @@
classpathref="test.classpath"> classpathref="test.classpath">
<src path="src/test/java" /> <src path="src/test/java" />
</solr-javac> </solr-javac>
<!-- Copy any data files present to the classpath -->
<copy todir="${dest}/test-classes">
<fileset dir="src/test/resources" excludes="**/*.java"/>
</copy>
</target> </target>
<property name="tempDir" value="${junit.output.dir}/temp" /> <property name="tempDir" value="${junit.output.dir}/temp" />
@ -105,7 +109,7 @@
maxmemory="512M" maxmemory="512M"
errorProperty="tests.failed" errorProperty="tests.failed"
failureProperty="tests.failed" failureProperty="tests.failed"
dir="src/test/resources/" dir="${tempDir}"
tempdir="${tempDir}" tempdir="${tempDir}"
forkmode="perBatch" forkmode="perBatch"
> >

View File

@ -43,7 +43,7 @@ import java.io.File;
public class ExtractingRequestHandlerTest extends SolrTestCaseJ4 { public class ExtractingRequestHandlerTest extends SolrTestCaseJ4 {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
initCore("solrconfig.xml", "schema.xml"); initCore("solrconfig.xml", "schema.xml", "solr-extraction");
} }
@Before @Before
@ -367,7 +367,7 @@ public class ExtractingRequestHandlerTest extends SolrTestCaseJ4 {
// TODO: stop using locally defined streams once stream.file and // TODO: stop using locally defined streams once stream.file and
// stream.body work everywhere // stream.body work everywhere
List<ContentStream> cs = new ArrayList<ContentStream>(); List<ContentStream> cs = new ArrayList<ContentStream>();
cs.add(new ContentStreamBase.FileStream(new File(filename))); cs.add(new ContentStreamBase.FileStream(getFile(filename)));
req.setContentStreams(cs); req.setContentStreams(cs);
return h.queryAndResponse("/update/extract", req); return h.queryAndResponse("/update/extract", req);
} finally { } finally {

View File

@ -84,7 +84,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
/** Call initCore in @BeforeClass to instantiate a solr core in your test class. /** Call initCore in @BeforeClass to instantiate a solr core in your test class.
* deleteCore will be called for you via SolrTestCaseJ4 @AfterClass */ * deleteCore will be called for you via SolrTestCaseJ4 @AfterClass */
public static void initCore(String config, String schema) throws Exception { public static void initCore(String config, String schema) throws Exception {
initCore(config, schema, null); initCore(config, schema, TEST_HOME);
} }
/** Call initCore in @BeforeClass to instantiate a solr core in your test class. /** Call initCore in @BeforeClass to instantiate a solr core in your test class.
@ -1028,7 +1028,7 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
* if a real file is needed. To get a stream, code should prefer * if a real file is needed. To get a stream, code should prefer
* {@link Class#getResourceAsStream} using {@code this.getClass()}. * {@link Class#getResourceAsStream} using {@code this.getClass()}.
*/ */
public static File getFile(String name) throws IOException { public static File getFile(String name) {
try { try {
File file = new File(name); File file = new File(name);
if (!file.exists()) { if (!file.exists()) {
@ -1036,7 +1036,26 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
} }
return file; return file;
} catch (Exception e) { } catch (Exception e) {
throw new IOException("Cannot find resource: " + name); /* more friendly than NPE */
throw new RuntimeException("Cannot find resource: " + name);
} }
} }
private static final String SOURCE_HOME = determineSourceHome();
public static String TEST_HOME = getFile("solr/conf").getParent();
public static String WEBAPP_HOME = new File(SOURCE_HOME, "src/webapp/web").getAbsolutePath();
public static String EXAMPLE_HOME = new File(SOURCE_HOME, "example/solr").getAbsolutePath();
public static String EXAMPLE_MULTICORE_HOME = new File(SOURCE_HOME, "example/multicore").getAbsolutePath();
public static String EXAMPLE_SCHEMA=EXAMPLE_HOME+"/conf/schema.xml";
public static String EXAMPLE_CONFIG=EXAMPLE_HOME+"/conf/solrconfig.xml";
static String determineSourceHome() {
// ugly, ugly hack to determine the example home without depending on the CWD
// this is needed for example/multicore tests which reside outside the classpath
File base = getFile("solr/conf/");
while (!new File(base, "solr/CHANGES.txt").exists()) {
base = base.getParentFile();
}
return new File(base, "solr/").getAbsolutePath();
}
} }

View File

@ -15,13 +15,6 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
// using configs in the test directory allows more flexibility to change "example" // using configs in the test directory allows more flexibility to change "example"
// without breaking configs. // without breaking configs.
private static final String SOURCE_HOME = determineSourceHome();
public static String WEBAPP_HOME = new File(SOURCE_HOME, "src/webapp/web").getAbsolutePath();
public static String EXAMPLE_HOME = new File(SOURCE_HOME, "example/solr").getAbsolutePath();
public static String EXAMPLE_MULTICORE_HOME = new File(SOURCE_HOME, "example/multicore").getAbsolutePath();
public static String EXAMPLE_SCHEMA=EXAMPLE_HOME+"/conf/schema.xml";
public static String EXAMPLE_CONFIG=EXAMPLE_HOME+"/conf/solrconfig.xml";
public String getSolrHome() { return EXAMPLE_HOME; } public String getSolrHome() { return EXAMPLE_HOME; }
public static JettySolrRunner jetty; public static JettySolrRunner jetty;
@ -29,23 +22,6 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
public static SolrServer server; public static SolrServer server;
public static String context; public static String context;
static String determineSourceHome() {
// ugly, ugly hack to determine the example home without depending on the CWD
try {
File file = new File("../../../example/solr");
if (file.exists())
return new File("../../../").getAbsolutePath();
// let the hacks begin
File base = getFile("solr/conf/");
while (!new File(base, "solr/CHANGES.txt").exists()) {
base = base.getParentFile();
}
return new File(base, "solr/").getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Cannot determine example home!");
}
}
public static JettySolrRunner createJetty(String solrHome, String configFile, String context) throws Exception { public static JettySolrRunner createJetty(String solrHome, String configFile, String context) throws Exception {
// creates the data dir // creates the data dir
initCore(null, null); initCore(null, null);

View File

@ -19,6 +19,7 @@ package org.apache.solr.cloud;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
@ -59,7 +60,7 @@ public class BasicDistributedZkTest extends AbstractDistributedZkTestCase {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
System.setProperty("solr.solr.home", SolrTestCaseJ4.TEST_HOME);
} }
@Override @Override

View File

@ -1,5 +1,8 @@
package org.apache.solr.core; package org.apache.solr.core;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.solr.util.AbstractSolrTestCase; import org.apache.solr.util.AbstractSolrTestCase;
import org.apache.solr.request.SolrRequestHandler; import org.apache.solr.request.SolrRequestHandler;
@ -24,7 +27,9 @@ public class TestXIncludeConfig extends AbstractSolrTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
File dest = new File("solrconfig-reqHandler.incl");
dest.deleteOnExit();
FileUtils.copyFile(getFile("solr/conf/solrconfig-reqHandler.incl"), dest);
supports = true; supports = true;
javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try { try {

View File

@ -31,7 +31,7 @@ import org.junit.Test;
public class NoCacheHeaderTest extends CacheHeaderTestBase { public class NoCacheHeaderTest extends CacheHeaderTestBase {
@BeforeClass @BeforeClass
public static void beforeTest() throws Exception { public static void beforeTest() throws Exception {
createJetty("solr/", "solr/conf/solrconfig-nocache.xml", null); createJetty(TEST_HOME, "solr/conf/solrconfig-nocache.xml", null);
} }
// The tests // The tests

View File

@ -84,6 +84,14 @@ public abstract class AbstractSolrTestCase extends LuceneTestCase {
*/ */
public abstract String getSolrConfigFile(); public abstract String getSolrConfigFile();
/**
* Subclasses can override this to change a test's solr home
* (default is in test-files)
*/
public String getSolrHome() {
return SolrTestCaseJ4.TEST_HOME;
}
/** /**
* The directory used to story the index managed by the TestHarness h * The directory used to story the index managed by the TestHarness h
*/ */
@ -114,8 +122,8 @@ public abstract class AbstractSolrTestCase extends LuceneTestCase {
dataDir = new File(TEMP_DIR, dataDir = new File(TEMP_DIR,
getClass().getName() + "-" + System.currentTimeMillis()); getClass().getName() + "-" + System.currentTimeMillis());
dataDir.mkdirs(); dataDir.mkdirs();
String configFile = getSolrConfigFile(); String configFile = getSolrConfigFile();
System.setProperty("solr.solr.home", getSolrHome());
if (configFile != null) { if (configFile != null) {
solrConfig = h.createConfig(getSolrConfigFile()); solrConfig = h.createConfig(getSolrConfigFile());

View File

@ -19,5 +19,6 @@
<config> <config>
<luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion> <luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>
<xi:include href="solr/conf/solrconfig-reqHandler.incl" xmlns:xi="http://www.w3.org/2001/XInclude"/> <!-- FIXME: is there a way to make this test *not* depend on the CWD? -->
<xi:include href="solrconfig-reqHandler.incl" xmlns:xi="http://www.w3.org/2001/XInclude"/>
</config> </config>