From f0be657699bff6d0df221e56f98d50d6e39ae3a7 Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Tue, 6 Sep 2016 15:46:18 +0900 Subject: [PATCH] Clean up Analyze API test case Using expectThrows instead of using try-catch --- .../indices/TransportAnalyzeActionTests.java | 113 ++++++++---------- .../indices/analyze/AnalyzeActionIT.java | 33 ++--- 2 files changed, 62 insertions(+), 84 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java index 6919db1b733..9adf77cf20b 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java @@ -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]"); } } } diff --git a/core/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionIT.java b/core/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionIT.java index 8e63653dfad..cd2c34e5102 100644 --- a/core/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionIT.java +++ b/core/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionIT.java @@ -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 {