summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/indices/analyze/HunspellServiceIT.java
blob: c539e2a68a07b7bdcaded2a1f8e985b65d067470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.elasticsearch.indices.analyze;

import org.apache.lucene.analysis.hunspell.Dictionary;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.indices.analysis.HunspellService;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.test.ESIntegTestCase.Scope;
import org.hamcrest.Matchers;

import static org.elasticsearch.indices.analysis.HunspellService.HUNSPELL_IGNORE_CASE;
import static org.elasticsearch.indices.analysis.HunspellService.HUNSPELL_LAZY_LOAD;
import static org.hamcrest.Matchers.notNullValue;

/**
 *
 */
@ClusterScope(scope= Scope.TEST, numDataNodes=0)
public class HunspellServiceIT extends ESIntegTestCase {
    public void testLocaleDirectoryWithNodeLevelConfig() throws Exception {
        Settings settings = Settings.settingsBuilder()
                .put(Environment.PATH_CONF_SETTING.getKey(), getDataPath("/indices/analyze/conf_dir"))
                .put(HUNSPELL_LAZY_LOAD.getKey(), randomBoolean())
                .put(HUNSPELL_IGNORE_CASE.getKey(), true)
                .build();

        internalCluster().startNode(settings);
        Dictionary dictionary = internalCluster().getInstance(HunspellService.class).getDictionary("en_US");
        assertThat(dictionary, notNullValue());
        assertIgnoreCase(true, dictionary);
    }

    public void testLocaleDirectoryWithLocaleSpecificConfig() throws Exception {
        Settings settings = Settings.settingsBuilder()
                .put(Environment.PATH_CONF_SETTING.getKey(), getDataPath("/indices/analyze/conf_dir"))
                .put(HUNSPELL_LAZY_LOAD.getKey(), randomBoolean())
                .put(HUNSPELL_IGNORE_CASE.getKey(), true)
                .put("indices.analysis.hunspell.dictionary.en_US.strict_affix_parsing", false)
                .put("indices.analysis.hunspell.dictionary.en_US.ignore_case", false)
                .build();

        internalCluster().startNode(settings);
        Dictionary dictionary = internalCluster().getInstance(HunspellService.class).getDictionary("en_US");
        assertThat(dictionary, notNullValue());
        assertIgnoreCase(false, dictionary);



        // testing that dictionary specific settings override node level settings
        dictionary = internalCluster().getInstance(HunspellService.class).getDictionary("en_US_custom");
        assertThat(dictionary, notNullValue());
        assertIgnoreCase(true, dictionary);
    }

    public void testDicWithNoAff() throws Exception {
        Settings settings = Settings.settingsBuilder()
                .put(Environment.PATH_CONF_SETTING.getKey(), getDataPath("/indices/analyze/no_aff_conf_dir"))
                .put(HUNSPELL_LAZY_LOAD.getKey(), randomBoolean())
                .build();

        Dictionary dictionary = null;
        try {
            internalCluster().startNode(settings);
            dictionary = internalCluster().getInstance(HunspellService.class).getDictionary("en_US");
            fail("Missing affix file didn't throw an error");
        }
        catch (Throwable t) {
            assertNull(dictionary);
            assertThat(ExceptionsHelper.unwrap(t, ElasticsearchException.class).toString(), Matchers.containsString("Missing affix file"));
        }
    }

    public void testDicWithTwoAffs() throws Exception {
        Settings settings = Settings.settingsBuilder()
                .put(Environment.PATH_CONF_SETTING.getKey(), getDataPath("/indices/analyze/two_aff_conf_dir"))
                .put(HUNSPELL_LAZY_LOAD.getKey(), randomBoolean())
                .build();

        Dictionary dictionary = null;
        try {
            internalCluster().startNode(settings);
            dictionary = internalCluster().getInstance(HunspellService.class).getDictionary("en_US");
            fail("Multiple affix files didn't throw an error");
        } catch (Throwable t) {
            assertNull(dictionary);
            assertThat(ExceptionsHelper.unwrap(t, ElasticsearchException.class).toString(), Matchers.containsString("Too many affix files"));
        }
    }

    // TODO: on next upgrade of lucene, just use new getter
    private void assertIgnoreCase(boolean expected, Dictionary dictionary) throws Exception {
        // assertEquals(expected, dictionary.getIgnoreCase());
    }
}