From c2338fca20994d340e60a25dd5145aee16a88dc7 Mon Sep 17 00:00:00 2001 From: "Chris M. Hostetter" Date: Wed, 19 Apr 2006 06:00:20 +0000 Subject: [PATCH] adding neccessary SolrConfig hooks so TestHarness and unit tests can specify arbitrary solrconfig.xml files, modified unit tests to demonstrate this -- per SOLR-3 and SOLR-8 git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@395148 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 2 +- src/java/org/apache/solr/core/SolrConfig.java | 55 ++- .../solr/util/AbstractSolrTestCase.java | 14 +- .../org/apache/solr/util/TestHarness.java | 25 +- .../apache/solr/BasicFunctionalityTest.java | 3 +- .../org/apache/solr/ConvertedLegacyTest.java | 3 +- src/test/org/apache/solr/SampleTest.java | 30 +- src/test/test-files/README | 4 + src/test/test-files/solr/conf/protwords.txt | 5 + src/test/test-files/solr/conf/schema.xml | 346 ++++++++++++++++++ src/test/test-files/solr/conf/solrconfig.xml | 191 ++++++++++ src/test/test-files/solr/conf/stopwords.txt | 2 + src/test/test-files/solr/conf/synonyms.txt | 6 + .../test-files/solr/crazy-path-to-config.xml | 53 +++ .../test-files/solr/crazy-path-to-schema.xml | 32 ++ 15 files changed, 749 insertions(+), 22 deletions(-) create mode 100644 src/test/test-files/README create mode 100644 src/test/test-files/solr/conf/protwords.txt create mode 100644 src/test/test-files/solr/conf/schema.xml create mode 100644 src/test/test-files/solr/conf/solrconfig.xml create mode 100644 src/test/test-files/solr/conf/stopwords.txt create mode 100644 src/test/test-files/solr/conf/synonyms.txt create mode 100644 src/test/test-files/solr/crazy-path-to-config.xml create mode 100644 src/test/test-files/solr/crazy-path-to-schema.xml diff --git a/build.xml b/build.xml index 00f495debbb..98e95b10940 100644 --- a/build.xml +++ b/build.xml @@ -164,7 +164,7 @@ haltonfailure="no" errorProperty="tests.failed" failureProperty="tests.failed" - dir="src/apps/SolrTest" + dir="src/test/test-files/" > diff --git a/src/java/org/apache/solr/core/SolrConfig.java b/src/java/org/apache/solr/core/SolrConfig.java index 42f4ffa121c..955ebb4df13 100644 --- a/src/java/org/apache/solr/core/SolrConfig.java +++ b/src/java/org/apache/solr/core/SolrConfig.java @@ -16,27 +16,64 @@ package org.apache.solr.core; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; + +import java.io.IOException; import java.io.InputStream; /** + * Provides a static refrence to a Config object modeling the main + * configuration data for a a Solr instance -- typically found in + * "solrconfig.xml". + * * @author yonik * @version $Id$ */ public class SolrConfig { + + public static final String DEFAULT_CONF_FILE = "solrconfig.xml"; + + /** + * Singleton containing all configuration. + */ public static Config config; - static { - RuntimeException e=null; - String file="solrconfig.xml"; + + /** + * (Re)loads the static configation information from the specified file. + * + *

+ * This method is called implicitly on ClassLoad, but it may be + * called explicitly to change the Configuration used for the purpose + * of testing - in which case it should be called prior to initializing + * a SolrCore. + *

+ * + *

+ * This method should only be called for testing purposes. + * Because it modifies a singleton, it is not suitable for running + * multi-threaded tests. + *

+ * + * @param file file name to load + * @see Config#openResource + */ + public static synchronized void initConfig(String file) + throws ParserConfigurationException, IOException, SAXException { + InputStream is = Config.openResource(file); - + config=new Config(file, is, "/config/"); + is.close(); + Config.log.info("Loaded SolrConfig: " + file); + } + + static { try { - config=new Config(file, is, "/config/"); - is.close(); + initConfig(DEFAULT_CONF_FILE); } catch (Exception ee) { - throw new RuntimeException("Error in solrconfig.xml", ee); + throw new RuntimeException("Error in " + DEFAULT_CONF_FILE, ee); } - - Config.log.info("Loaded Config solrconfig.xml"); } } diff --git a/src/java/org/apache/solr/util/AbstractSolrTestCase.java b/src/java/org/apache/solr/util/AbstractSolrTestCase.java index 34fc5c818e0..ff6462b102e 100644 --- a/src/java/org/apache/solr/util/AbstractSolrTestCase.java +++ b/src/java/org/apache/solr/util/AbstractSolrTestCase.java @@ -45,10 +45,16 @@ public abstract class AbstractSolrTestCase extends TestCase { protected TestHarness.LocalRequestFactory lrf; /** - * Subclasses must define this method to return the path of the + * Subclasses must define this method to return the name of the * schema.xml they wish to use. */ - public abstract String getSchemaPath(); + public abstract String getSchemaFile(); + + /** + * Subclasses must define this method to return the name of the + * solrconfig.xml they wish to use. + */ + public abstract String getSolrConfigFile(); /** * The directory used to story the index managed by the TestHarness h @@ -72,7 +78,9 @@ public abstract class AbstractSolrTestCase extends TestCase { + getClass().getName() + "-" + getName() + "-" + System.currentTimeMillis()); dataDir.mkdirs(); - h = new TestHarness(dataDir.getAbsolutePath(), getSchemaPath()); + h = new TestHarness(dataDir.getAbsolutePath(), + getSolrConfigFile(), + getSchemaFile()); lrf = h.getRequestFactory ("standard",0,20,"version","2.0"); diff --git a/src/java/org/apache/solr/util/TestHarness.java b/src/java/org/apache/solr/util/TestHarness.java index a1cf45e4072..6a03719c880 100644 --- a/src/java/org/apache/solr/util/TestHarness.java +++ b/src/java/org/apache/solr/util/TestHarness.java @@ -17,6 +17,7 @@ package org.apache.solr.util; import org.apache.solr.schema.IndexSchema; +import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrCore; import org.apache.solr.request.*; @@ -55,12 +56,34 @@ public class TestHarness { private DocumentBuilder builder; /** + * Assumes "solrconfig.xml" is the config file to use, and + * "schema.xml" is the schema path to use. + * + * @param dataDirectory path for index data, will not be cleaned up + */ + public TestHarness(String dataDirectory) { + this(dataDirectory, "schema.xml"); + } + /** + * Assumes "solrconfig.xml" is the config file to use. + * * @param dataDirectory path for index data, will not be cleaned up * @param schemaFile path of schema file */ public TestHarness(String dataDirectory, String schemaFile) { - core = new SolrCore(dataDirectory, new IndexSchema(schemaFile)); + this(dataDirectory, "solrconfig.xml", schemaFile); + } + /** + * @param dataDirectory path for index data, will not be cleaned up + * @param confFile solrconfig filename + * @param schemaFile schema filename + */ + public TestHarness(String dataDirectory, + String confFile, + String schemaFile) { try { + SolrConfig.initConfig(confFile); + core = new SolrCore(dataDirectory, new IndexSchema(schemaFile)); builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/test/org/apache/solr/BasicFunctionalityTest.java b/src/test/org/apache/solr/BasicFunctionalityTest.java index 0ebb3ea1c5a..c6b5a3b9e95 100644 --- a/src/test/org/apache/solr/BasicFunctionalityTest.java +++ b/src/test/org/apache/solr/BasicFunctionalityTest.java @@ -25,7 +25,8 @@ import org.apache.solr.util.*; */ public class BasicFunctionalityTest extends AbstractSolrTestCase { - public String getSchemaPath() { return "solr/conf/schema.xml"; } + public String getSchemaFile() { return "schema.xml"; } + public String getSolrConfigFile() { return "solrconfig.xml"; } public void setUp() throws Exception { // if you override setUp or tearDown, you better call diff --git a/src/test/org/apache/solr/ConvertedLegacyTest.java b/src/test/org/apache/solr/ConvertedLegacyTest.java index 3c68eafce10..805814f3be6 100644 --- a/src/test/org/apache/solr/ConvertedLegacyTest.java +++ b/src/test/org/apache/solr/ConvertedLegacyTest.java @@ -30,7 +30,8 @@ import java.io.IOException; */ public class ConvertedLegacyTest extends AbstractSolrTestCase { - public String getSchemaPath() { return "solr/conf/schema.xml"; } + public String getSchemaFile() { return "schema.xml"; } + public String getSolrConfigFile() { return "solrconfig.xml"; } public void testABunchOfConvertedStuff() { // these may be reused by things that need a special query diff --git a/src/test/org/apache/solr/SampleTest.java b/src/test/org/apache/solr/SampleTest.java index 5b481e10cf0..f7ee6695d34 100644 --- a/src/test/org/apache/solr/SampleTest.java +++ b/src/test/org/apache/solr/SampleTest.java @@ -29,10 +29,25 @@ import java.io.IOException; public class SampleTest extends AbstractSolrTestCase { /** - * All subclasses of AbstractSolrTestCase must define this method + * All subclasses of AbstractSolrTestCase must define this method. + * + *

+ * Note that different tests can use different schemas by refering + * to any crazy path they want (as long as it works). + *

*/ - public String getSchemaPath() { return "solr/conf/schema.xml"; } - + public String getSchemaFile() { return "solr/crazy-path-to-schema.xml"; } + + /** + * All subclasses of AbstractSolrTestCase must define this method + * + *

+ * Note that different tests can use different configs by refering + * to any crazy path they want (as long as it works). + *

+ */ + public String getSolrConfigFile() { return "solr/crazy-path-to-config.xml"; } + /** * Demonstration of some of the simple ways to use the base class */ @@ -71,7 +86,7 @@ public class SampleTest extends AbstractSolrTestCase { doc("id", "4059", "subject", "Who Me Again?") + ""); - // or really make the xml yourself + /* or really make the xml yourself */ assertU("4055" +"Hoss the Hoss man Hostetter" +""); @@ -87,9 +102,12 @@ public class SampleTest extends AbstractSolrTestCase { ,"//int[@name='id'][.='4055']" ); - /* make your own LocalRequestFactory to build a request */ + /* make your own LocalRequestFactory to build a request + * + * Note: the qt proves we are using our custom config... + */ TestHarness.LocalRequestFactory l = h.getRequestFactory - ("standard",100,200,"version","2.1"); + ("crazy_custom_qt",100,200,"version","2.1"); assertQ("how did i find Mack Daddy? ", l.makeRequest( "Mack Daddy" ) ,"//result[@numFound=0]" diff --git a/src/test/test-files/README b/src/test/test-files/README new file mode 100644 index 00000000000..b84b825edb9 --- /dev/null +++ b/src/test/test-files/README @@ -0,0 +1,4 @@ +This directory is where any non-transient, non-java files needed +for the execution of tests should live. + +It is used as the CWD when running JUnit tests. diff --git a/src/test/test-files/solr/conf/protwords.txt b/src/test/test-files/solr/conf/protwords.txt new file mode 100644 index 00000000000..f668c1c0e35 --- /dev/null +++ b/src/test/test-files/solr/conf/protwords.txt @@ -0,0 +1,5 @@ +#use a protected word file to avoid stemming two +#unrelated words to the same base word. +#to test, we will use words that would normally obviously be stemmed. +cats +ridding \ No newline at end of file diff --git a/src/test/test-files/solr/conf/schema.xml b/src/test/test-files/solr/conf/schema.xml new file mode 100644 index 00000000000..20560984d64 --- /dev/null +++ b/src/test/test-files/solr/conf/schema.xml @@ -0,0 +1,346 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text + id + + + + + + + + + + + + + diff --git a/src/test/test-files/solr/conf/solrconfig.xml b/src/test/test-files/solr/conf/solrconfig.xml new file mode 100644 index 00000000000..071223535d0 --- /dev/null +++ b/src/test/test-files/solr/conf/solrconfig.xml @@ -0,0 +1,191 @@ + + + + + + + + + + + + false + 10 + 1000 + 2147483647 + 10000 + + + 1000 + 10000 + + + + + + false + 10 + 1000 + 2147483647 + 10000 + + true + + + + + + + 10000 + 3600 + + + + 0 + + + + + + + + + + + 1024 + + + + + + + + + + + + + true + + 10 + + + + + + + + + + + + + + + + + + + + + + + + + 1000 + 1.4142135 + 12 + foo + + + sqrt 2 + log 10 + + + + + + + solr + solrconfig.xml scheam.xml + + + + + diff --git a/src/test/test-files/solr/conf/stopwords.txt b/src/test/test-files/solr/conf/stopwords.txt new file mode 100644 index 00000000000..5401d99f9fb --- /dev/null +++ b/src/test/test-files/solr/conf/stopwords.txt @@ -0,0 +1,2 @@ +stopworda +stopwordb diff --git a/src/test/test-files/solr/conf/synonyms.txt b/src/test/test-files/solr/conf/synonyms.txt new file mode 100644 index 00000000000..d56bc23f14e --- /dev/null +++ b/src/test/test-files/solr/conf/synonyms.txt @@ -0,0 +1,6 @@ +a => aa +b => b1 b2 +c => c1,c2 +a\=>a => b\=>b +a\,a => b\,b +foo,bar,baz \ No newline at end of file diff --git a/src/test/test-files/solr/crazy-path-to-config.xml b/src/test/test-files/solr/crazy-path-to-config.xml new file mode 100644 index 00000000000..7c2eb04575d --- /dev/null +++ b/src/test/test-files/solr/crazy-path-to-config.xml @@ -0,0 +1,53 @@ + + + + + + + false + 10 + 1000 + 2147483647 + 10000 + 1000 + 10000 + + + + false + 10 + 1000 + 2147483647 + 10000 + true + + + + 0 + + + + + 1024 + true + 10 + + + + + + + + solr + solrconfig.xml scheam.xml + + + + + diff --git a/src/test/test-files/solr/crazy-path-to-schema.xml b/src/test/test-files/solr/crazy-path-to-schema.xml new file mode 100644 index 00000000000..59b7dae1777 --- /dev/null +++ b/src/test/test-files/solr/crazy-path-to-schema.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + subject + id +