From 8b617fb48f1e573298a9b6a2befb14cec89ddf08 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 29 Aug 2013 12:53:53 +0200 Subject: [PATCH] Convert relative paths to URI first in tests to prevent URL encoded paths If a path to the test classes contains spaces Hunspell tests failed due to URL encoded paths. This happens on CI builds if you give the build a name containing a space. This is fixed by first converting to a URI and created a File object from the URI directly. --- .../test/integration/ElasticsearchTestCase.java | 14 ++++++++++++++ .../indices/analyze/HunspellServiceTests.java | 9 ++++----- .../analysis/HunspellTokenFilterFactoryTests.java | 15 +++++++-------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/elasticsearch/test/integration/ElasticsearchTestCase.java b/src/test/java/org/elasticsearch/test/integration/ElasticsearchTestCase.java index d898ba62eb8..103b50041d1 100644 --- a/src/test/java/org/elasticsearch/test/integration/ElasticsearchTestCase.java +++ b/src/test/java/org/elasticsearch/test/integration/ElasticsearchTestCase.java @@ -28,6 +28,8 @@ import org.apache.lucene.util.TimeUnits; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; +import java.io.File; +import java.net.URI; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -68,5 +70,17 @@ public abstract class ElasticsearchTestCase extends AbstractRandomizedTest { public static String randomNumericType(Random random) { return numericTypes[random.nextInt(numericTypes.length)]; } + + /** + * Returns a {@link File} pointing to the class path relative resource given + * as the first argument. In contrast to + * getClass().getResource(...).getFile() this method will not + * return URL encoded paths if the parent path contains spaces or other + * non-standard characters. + */ + public File getResource(String relativePath) { + URI uri = URI.create(getClass().getResource(relativePath).toString()); + return new File(uri); + } } diff --git a/src/test/java/org/elasticsearch/test/integration/indices/analyze/HunspellServiceTests.java b/src/test/java/org/elasticsearch/test/integration/indices/analyze/HunspellServiceTests.java index 3b297acaf09..c268bf74911 100644 --- a/src/test/java/org/elasticsearch/test/integration/indices/analyze/HunspellServiceTests.java +++ b/src/test/java/org/elasticsearch/test/integration/indices/analyze/HunspellServiceTests.java @@ -31,7 +31,6 @@ import org.elasticsearch.test.integration.AbstractNodesTests; import org.junit.After; import org.junit.Test; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -45,11 +44,11 @@ public class HunspellServiceTests extends AbstractNodesTests { public void closeNodes() { closeAllNodes(); } - + @Test public void testLocaleDirectoryWithNodeLevelConfig() throws Exception { Settings settings = ImmutableSettings.settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("indices.analysis.hunspell.dictionary.lazy", true) .put("indices.analysis.hunspell.dictionary.ignore_case", true) .build(); @@ -66,7 +65,7 @@ public class HunspellServiceTests extends AbstractNodesTests { @Test public void testLocaleDirectoryWithLocaleSpecificConfig() throws Exception { Settings settings = ImmutableSettings.settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("indices.analysis.hunspell.dictionary.lazy", true) .put("indices.analysis.hunspell.dictionary.ignore_case", true) .put("indices.analysis.hunspell.dictionary.en_US.strict_affix_parsing", false) @@ -92,7 +91,7 @@ public class HunspellServiceTests extends AbstractNodesTests { @Test public void testCustomizeLocaleDirectory() throws Exception { Settings settings = ImmutableSettings.settingsBuilder() - .put("indices.analysis.hunspell.dictionary.location", getClass().getResource("/indices/analyze/conf_dir/hunspell").getFile()) + .put("indices.analysis.hunspell.dictionary.location", getResource("/indices/analyze/conf_dir/hunspell")) .build(); Node node = startNode("node1", settings); diff --git a/src/test/java/org/elasticsearch/test/unit/index/analysis/HunspellTokenFilterFactoryTests.java b/src/test/java/org/elasticsearch/test/unit/index/analysis/HunspellTokenFilterFactoryTests.java index c3b744a1251..0c94f56a532 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/analysis/HunspellTokenFilterFactoryTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/analysis/HunspellTokenFilterFactoryTests.java @@ -24,22 +24,21 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.analysis.HunspellTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; +import org.elasticsearch.test.integration.ElasticsearchTestCase; import org.junit.Test; import java.io.IOException; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; -public class HunspellTokenFilterFactoryTests { - +public class HunspellTokenFilterFactoryTests extends ElasticsearchTestCase { @Test public void testDedup() throws IOException { Settings settings = settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("index.analysis.filter.en_US.type", "hunspell") .put("index.analysis.filter.en_US.locale", "en_US") .build(); @@ -51,7 +50,7 @@ public class HunspellTokenFilterFactoryTests { assertThat(hunspellTokenFilter.dedup(), is(true)); settings = settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("index.analysis.filter.en_US.type", "hunspell") .put("index.analysis.filter.en_US.dedup", false) .put("index.analysis.filter.en_US.locale", "en_US") @@ -67,7 +66,7 @@ public class HunspellTokenFilterFactoryTests { @Test public void testDefaultRecursionLevel() throws IOException { Settings settings = settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("index.analysis.filter.en_US.type", "hunspell") .put("index.analysis.filter.en_US.locale", "en_US") .build(); @@ -82,7 +81,7 @@ public class HunspellTokenFilterFactoryTests { @Test public void testCustomRecursionLevel() throws IOException { Settings settings = settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("index.analysis.filter.en_US.type", "hunspell") .put("index.analysis.filter.en_US.recursion_level", 0) .put("index.analysis.filter.en_US.locale", "en_US") @@ -98,7 +97,7 @@ public class HunspellTokenFilterFactoryTests { @Test(expected = ProvisionException.class) public void negativeRecursionLevelShouldFail() throws IOException { Settings settings = settingsBuilder() - .put("path.conf", getClass().getResource("/indices/analyze/conf_dir").getFile()) + .put("path.conf", getResource("/indices/analyze/conf_dir")) .put("index.analysis.filter.en_US.type", "hunspell") .put("index.analysis.filter.en_US.recursion_level", -1) .put("index.analysis.filter.en_US.locale", "en_US")