Clean up Analyze API test case

Using expectThrows instead of using try-catch
This commit is contained in:
Jun Ohtani 2016-09-06 15:46:18 +09:00
parent 433cae47ed
commit f0be657699
2 changed files with 62 additions and 84 deletions

View File

@ -191,75 +191,66 @@ public class TransportAnalyzeActionTests extends ESTestCase {
}
public void testGetIndexAnalyserWithoutAnalysisService() throws IOException {
AnalyzeRequest request = new AnalyzeRequest();
request.analyzer("custom_analyzer");
request.text("the qu1ck brown fox-dog");
try {
TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, null, registry, environment);
fail("no analysis service provided");
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "failed to find global analyzer [custom_analyzer]");
}
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> TransportAnalyzeAction.analyze(
new AnalyzeRequest()
.analyzer("custom_analyzer")
.text("the qu1ck brown fox-dog"),
AllFieldMapper.NAME, null, null, registry, environment));
assertEquals(e.getMessage(), "failed to find global analyzer [custom_analyzer]");
}
public void testUnknown() throws IOException {
boolean notGlobal = randomBoolean();
try {
AnalyzeRequest request = new AnalyzeRequest();
request.analyzer("foobar");
request.text("the qu1ck brown fox");
TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment);
fail("no such analyzer");
} catch (IllegalArgumentException e) {
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find analyzer [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global analyzer [foobar]");
}
}
try {
AnalyzeRequest request = new AnalyzeRequest();
request.tokenizer("foobar");
request.text("the qu1ck brown fox");
TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment);
fail("no such analyzer");
} catch (IllegalArgumentException e) {
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find tokenizer under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global tokenizer under [foobar]");
}
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> TransportAnalyzeAction.analyze(
new AnalyzeRequest()
.analyzer("foobar")
.text("the qu1ck brown fox"),
AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment));
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find analyzer [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global analyzer [foobar]");
}
try {
AnalyzeRequest request = new AnalyzeRequest();
request.tokenizer("whitespace");
request.addTokenFilter("foobar");
request.text("the qu1ck brown fox");
TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment);
fail("no such analyzer");
} catch (IllegalArgumentException e) {
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find token filter under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global token filter under [foobar]");
}
e = expectThrows(IllegalArgumentException.class,
() -> TransportAnalyzeAction.analyze(
new AnalyzeRequest()
.tokenizer("foobar")
.text("the qu1ck brown fox"),
AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment));
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find tokenizer under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global tokenizer under [foobar]");
}
try {
AnalyzeRequest request = new AnalyzeRequest();
request.tokenizer("whitespace");
request.addTokenFilter("lowercase");
request.addCharFilter("foobar");
request.text("the qu1ck brown fox");
TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment);
fail("no such analyzer");
} catch (IllegalArgumentException e) {
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find char filter under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global char filter under [foobar]");
}
e = expectThrows(IllegalArgumentException.class,
() -> TransportAnalyzeAction.analyze(
new AnalyzeRequest()
.tokenizer("whitespace")
.addTokenFilter("foobar")
.text("the qu1ck brown fox"),
AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment));
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find token filter under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global token filter under [foobar]");
}
e = expectThrows(IllegalArgumentException.class,
() -> TransportAnalyzeAction.analyze(
new AnalyzeRequest()
.tokenizer("whitespace")
.addTokenFilter("lowercase")
.addCharFilter("foobar")
.text("the qu1ck brown fox"),
AllFieldMapper.NAME, null, notGlobal ? analysisService : null, registry, environment));
if (notGlobal) {
assertEquals(e.getMessage(), "failed to find char filter under [foobar]");
} else {
assertEquals(e.getMessage(), "failed to find global char filter under [foobar]");
}
}
}

View File

@ -32,7 +32,6 @@ import java.util.Map;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
@ -76,18 +75,11 @@ public class AnalyzeActionIT extends ESIntegTestCase {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("test", "long", "type=long", "double", "type=double"));
ensureGreen("test");
try {
client().admin().indices().prepareAnalyze(indexOrAlias(), "123").setField("long").get();
fail("shouldn't get here");
} catch (IllegalArgumentException ex) {
//all good
}
try {
client().admin().indices().prepareAnalyze(indexOrAlias(), "123.0").setField("double").get();
fail("shouldn't get here");
} catch (IllegalArgumentException ex) {
//all good
}
expectThrows(IllegalArgumentException.class,
() -> client().admin().indices().prepareAnalyze(indexOrAlias(), "123").setField("long").get());
expectThrows(IllegalArgumentException.class,
() -> client().admin().indices().prepareAnalyze(indexOrAlias(), "123.0").setField("double").get());
}
public void testAnalyzeWithNoIndex() throws Exception {
@ -450,18 +442,13 @@ public class AnalyzeActionIT extends ESIntegTestCase {
}
public void testNonExistTokenizer() {
try {
AnalyzeResponse analyzeResponse = client().admin().indices()
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> client().admin().indices()
.prepareAnalyze("this is a test")
.setAnalyzer("not_exist_analyzer")
.get();
fail("shouldn't get here");
} catch (Exception e) {
assertThat(e, instanceOf(IllegalArgumentException.class));
assertThat(e.getMessage(), startsWith("failed to find global analyzer"));
}
.get()
);
assertThat(e.getMessage(), startsWith("failed to find global analyzer"));
}
public void testCustomTokenFilterInRequest() throws Exception {