SOLR-2299: fix more tests to work with resources, add hack for tests that use the example config

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1053509 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2010-12-29 02:55:21 +00:00
parent 3ae880e7df
commit 7fd744a28f
14 changed files with 93 additions and 43 deletions

View File

@ -20,6 +20,7 @@ package org.apache.solr.handler.admin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
@ -122,7 +123,15 @@ public class ShowFileRequestHandler extends RequestHandlerBase
File adminFile = null;
final SolrResourceLoader loader = req.getCore().getResourceLoader();
File configdir = new File( loader.getConfigDir() );
File configdir = new File( loader.getConfigDir() );
if (!configdir.exists()) {
// TODO: maybe we should just open it this way to start with?
try {
configdir = new File( loader.getClassLoader().getResource(loader.getConfigDir()).toURI() );
} catch (URISyntaxException e) {
throw new SolrException( ErrorCode.FORBIDDEN, "Can not access configuration directory!");
}
}
String fname = req.getParams().get("file", null);
if( fname == null ) {
adminFile = configdir;

View File

@ -87,8 +87,6 @@ public class SolrInfoMBeanTest extends SolrTestCaseJ4
assertTrue( "there are at least 10 SolrInfoMBean that should be found in the classpath, found " + checked, checked > 10 );
}
static final String FOLDER = File.separator + "build" + File.separator + "solr" + File.separator + "org" + File.separator + "apache" + File.separator + "solr" + File.separator;
private static List<Class> getClassesForPackage(String pckgname) throws Exception {
ArrayList<File> directories = new ArrayList<File>();
ClassLoader cld = h.getCore().getResourceLoader().getClassLoader();
@ -96,9 +94,6 @@ public class SolrInfoMBeanTest extends SolrTestCaseJ4
Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) {
final File f = new File(resources.nextElement().toURI());
// only iterate classes from the core, not the tests (must be in dir "/build/solr/org"
if (!f.toString().contains(FOLDER))
continue;
directories.add(f);
}
@ -108,7 +103,12 @@ public class SolrInfoMBeanTest extends SolrTestCaseJ4
String[] files = directory.list();
for (String file : files) {
if (file.endsWith(".class")) {
classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
String clazzName = file.substring(0, file.length() - 6);
// exclude Test classes that happen to be in these packages.
// class.ForName'ing some of them can cause trouble.
if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) {
classes.add(Class.forName(pckgname + '.' + clazzName));
}
}
}
}

View File

@ -1024,6 +1024,19 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
return out.toString();
}
/** Gets a resource from the context classloader as {@link File}. This method should only be used,
* if a real file is needed. To get a stream, code should prefer
* {@link Class#getResourceAsStream} using {@code this.getClass()}.
*/
public static File getFile(String name) throws IOException {
try {
File file = new File(name);
if (!file.exists()) {
file = new File(Thread.currentThread().getContextClassLoader().getResource(name).toURI());
}
return file;
} catch (Exception e) {
throw new IOException("Cannot find resource: " + name);
}
}
}

View File

@ -116,10 +116,10 @@ public class TestSolrCoreProperties extends LuceneTestCase {
confDir.mkdirs();
File f = new File(confDir, "solrconfig.xml");
copyFile(new File(getSolrConfigFile()), f);
copyFile(SolrTestCaseJ4.getFile(getSolrConfigFile()), f);
f = new File(confDir, "schema.xml");
copyFile(new File(getSchemaFile()), f);
copyFile(SolrTestCaseJ4.getFile(getSchemaFile()), f);
Properties p = new Properties();
p.setProperty("foo.foo1", "f1");
p.setProperty("foo.foo2", "f2");

View File

@ -37,17 +37,17 @@ public abstract class MergeIndexesExampleTestBase extends SolrExampleTestBase {
@Override
public String getSolrHome() {
return "../../../example/multicore/";
return SolrJettyTestBase.EXAMPLE_MULTICORE_HOME;
}
@Override
public String getSchemaFile() {
return getSolrHome() + "core0/conf/schema.xml";
return getSolrHome() + "/core0/conf/schema.xml";
}
@Override
public String getSolrConfigFile() {
return getSolrHome() + "core0/conf/solrconfig.xml";
return getSolrHome() + "/core0/conf/solrconfig.xml";
}
@Override

View File

@ -36,10 +36,10 @@ public abstract class MultiCoreExampleTestBase extends SolrExampleTestBase
// protected static final CoreContainer cores = new CoreContainer();
protected static CoreContainer cores;
@Override public String getSolrHome() { return "../../../example/multicore/"; }
@Override public String getSolrHome() { return SolrJettyTestBase.EXAMPLE_MULTICORE_HOME; }
@Override public String getSchemaFile() { return getSolrHome()+"core0/conf/schema.xml"; }
@Override public String getSolrConfigFile() { return getSolrHome()+"core0/conf/solrconfig.xml"; }
@Override public String getSchemaFile() { return getSolrHome()+"/core0/conf/schema.xml"; }
@Override public String getSolrConfigFile() { return getSolrHome()+"/core0/conf/solrconfig.xml"; }
@Override public void setUp() throws Exception {
super.setUp();

View File

@ -266,7 +266,7 @@ abstract public class SolrExampleTests extends SolrJettyTestBase
Assert.assertEquals( 0, rsp.getResults().getNumFound() );
ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/csv");
up.addFile(new File("books.csv"));
up.addFile(getFile("books.csv"));
up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
NamedList<Object> result = server.request(up);
assertNotNull("Couldn't upload books.csv", result);

View File

@ -1,5 +1,8 @@
package org.apache.solr.client.solrj;
import java.io.File;
import java.io.IOException;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
@ -12,9 +15,11 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
// using configs in the test directory allows more flexibility to change "example"
// without breaking configs.
public static String EXAMPLE_HOME="../../../example/solr/";
public static String EXAMPLE_SCHEMA=EXAMPLE_HOME+"conf/schema.xml";
public static String EXAMPLE_CONFIG=EXAMPLE_HOME+"conf/solrconfig.xml";
private static final String SOURCE_HOME = determineSourceHome();
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; }
@ -23,6 +28,23 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4
public static SolrServer server;
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 {
// creates the data dir
initCore(null, null);

View File

@ -245,9 +245,9 @@ public class TestLBHttpSolrServer extends LuceneTestCase {
confDir.mkdirs();
File f = new File(confDir, "solrconfig.xml");
FileUtils.copyFile(new File(getSolrConfigFile()), f);
FileUtils.copyFile(SolrTestCaseJ4.getFile(getSolrConfigFile()), f);
f = new File(confDir, "schema.xml");
FileUtils.copyFile(new File(getSchemaFile()), f);
FileUtils.copyFile(SolrTestCaseJ4.getFile(getSchemaFile()), f);
}

View File

@ -69,12 +69,12 @@ public class TestSolrProperties extends LuceneTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
System.setProperty("solr.solr.home", getSolrHome());
File home = SolrTestCaseJ4.getFile(getSolrHome());
System.setProperty("solr.solr.home", home.getAbsolutePath());
log.info("pwd: " + (new File(".")).getAbsolutePath());
File home = new File(getSolrHome());
solrXml = new File(home, "solr.xml");
cores = new CoreContainer(getSolrHome(), solrXml);
cores = new CoreContainer(home.getAbsolutePath(), solrXml);
}
@After

View File

@ -94,7 +94,7 @@ public abstract class AbstractZkTestCase extends SolrTestCaseJ4 {
private static void putConfig(SolrZkClient zkConnection, String name)
throws Exception {
zkConnection.setData("/configs/conf1/" + name, new File("solr"
zkConnection.setData("/configs/conf1/" + name, getFile("solr"
+ File.separator + "conf" + File.separator + name));
}

View File

@ -188,7 +188,7 @@ public class ZkControllerTest extends SolrTestCaseJ4 {
zkController = new ZkController(server.getZkAddress(),
TIMEOUT, 1000, "localhost", "8983", "/solr");
zkController.uploadToZK(new File("solr/conf"),
zkController.uploadToZK(getFile("solr/conf"),
ZkController.CONFIGS_ZKNODE + "/config1");
if (DEBUG) {

View File

@ -250,14 +250,14 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
masterClient.commit();
//change the schema on master
copyFile(new File(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema.xml"));
copyFile(getFile(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema.xml"));
masterJetty.stop();
masterJetty = createJetty(master);
masterClient = createNewSolrServer(masterJetty.getLocalPort());
copyFile(new File(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
slaveJetty.stop();
slaveJetty = createJetty(slave);
@ -349,7 +349,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
public void testSnapPullWithMasterUrl() throws Exception {
//change solrconfig on slave
//this has no entry for pollinginterval
copyFile(new File(CONF_DIR + "solrconfig-slave1.xml"), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(CONF_DIR + "solrconfig-slave1.xml"), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
slaveJetty.stop();
slaveJetty = createJetty(slave);
slaveClient = createNewSolrServer(slaveJetty.getLocalPort());
@ -386,7 +386,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
// NOTE: at this point, the slave is not polling any more
// restore it.
copyFile(new File(CONF_DIR + "solrconfig-slave.xml"), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(CONF_DIR + "solrconfig-slave.xml"), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
slaveJetty.stop();
slaveJetty = createJetty(slave);
slaveClient = createNewSolrServer(slaveJetty.getLocalPort());
@ -410,7 +410,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
assertEquals(nDocs, masterQueryResult.getNumFound());
//change solrconfig having 'replicateAfter startup' option on master
copyFile(new File(CONF_DIR + "solrconfig-master2.xml"),
copyFile(getFile(CONF_DIR + "solrconfig-master2.xml"),
new File(master.getConfDir(), "solrconfig.xml"));
masterJetty.stop();
@ -418,7 +418,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
masterJetty = createJetty(master);
masterClient = createNewSolrServer(masterJetty.getLocalPort());
copyFile(new File(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
//start slave
slaveJetty = createJetty(slave);
@ -435,11 +435,11 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
// NOTE: the master only replicates after startup now!
// revert that change.
copyFile(new File(CONF_DIR + "solrconfig-master.xml"), new File(master.getConfDir(), "solrconfig.xml"));
copyFile(getFile(CONF_DIR + "solrconfig-master.xml"), new File(master.getConfDir(), "solrconfig.xml"));
masterJetty.stop();
masterJetty = createJetty(master);
masterClient = createNewSolrServer(masterJetty.getLocalPort());
copyFile(new File(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
//start slave
slaveJetty.stop();
slaveJetty = createJetty(slave);
@ -477,20 +477,20 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
masterClient.commit();
//change solrconfig on master
copyFile(new File(CONF_DIR + "solrconfig-master1.xml"), new File(master.getConfDir(), "solrconfig.xml"));
copyFile(getFile(CONF_DIR + "solrconfig-master1.xml"), new File(master.getConfDir(), "solrconfig.xml"));
//change schema on master
copyFile(new File(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema.xml"));
copyFile(getFile(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema.xml"));
//keep a copy of the new schema
copyFile(new File(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema-replication2.xml"));
copyFile(getFile(CONF_DIR + "schema-replication2.xml"), new File(master.getConfDir(), "schema-replication2.xml"));
masterJetty.stop();
masterJetty = createJetty(master);
masterClient = createNewSolrServer(masterJetty.getLocalPort());
copyFile(new File(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
copyFile(getFile(SLAVE_CONFIG), new File(slave.getConfDir(), "solrconfig.xml"), masterJetty.getLocalPort());
slaveJetty.stop();
slaveJetty = createJetty(slave);
@ -522,7 +522,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
public void testBackup() throws Exception {
masterJetty.stop();
copyFile(new File(CONF_DIR + "solrconfig-master1.xml"), new File(master.getConfDir(), "solrconfig.xml"));
copyFile(getFile(CONF_DIR + "solrconfig-master1.xml"), new File(master.getConfDir(), "solrconfig.xml"));
masterJetty = createJetty(master);
masterClient = createNewSolrServer(masterJetty.getLocalPort());
@ -708,9 +708,9 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
confDir.mkdirs();
File f = new File(confDir, "solrconfig.xml");
copyFile(new File(getSolrConfigFile()), f, masterPort);
copyFile(getFile(getSolrConfigFile()), f, masterPort);
f = new File(confDir, "schema.xml");
copyFile(new File(getSchemaFile()), f);
copyFile(getFile(getSchemaFile()), f);
}
public void tearDown() throws Exception {

View File

@ -20,6 +20,7 @@ package org.apache.solr.util;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.core.SolrConfig;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@ -408,4 +409,9 @@ public abstract class AbstractSolrTestCase extends LuceneTestCase {
}
return f.delete();
}
/** @see SolrTestCaseJ4#getFile */
public static File getFile(String name) throws IOException {
return SolrTestCaseJ4.getFile(name);
}
}