3030import java .util .stream .Collectors ;
3131
3232import static org .hamcrest .Matchers .contains ;
33+ import static org .hamcrest .Matchers .containsString ;
34+ import static org .hamcrest .core .IsEqual .equalTo ;
3335
3436public class PluginInfoTests extends ESTestCase {
3537
@@ -52,43 +54,27 @@ public void testReadFromProperties() throws Exception {
5254 public void testReadFromPropertiesNameMissing () throws Exception {
5355 Path pluginDir = createTempDir ().resolve ("fake-plugin" );
5456 PluginTestUtil .writeProperties (pluginDir );
55- try {
56- PluginInfo .readFromProperties (pluginDir );
57- fail ("expected missing name exception" );
58- } catch (IllegalArgumentException e ) {
59- assertTrue (e .getMessage ().contains ("property [name] is missing in" ));
60- }
57+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
58+ assertThat (e .getMessage (), containsString ("property [name] is missing in" ));
6159
6260 PluginTestUtil .writeProperties (pluginDir , "name" , "" );
63- try {
64- PluginInfo .readFromProperties (pluginDir );
65- fail ("expected missing name exception" );
66- } catch (IllegalArgumentException e ) {
67- assertTrue (e .getMessage ().contains ("property [name] is missing in" ));
68- }
61+ e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
62+ assertThat (e .getMessage (), containsString ("property [name] is missing in" ));
6963 }
7064
7165 public void testReadFromPropertiesDescriptionMissing () throws Exception {
7266 Path pluginDir = createTempDir ().resolve ("fake-plugin" );
7367 PluginTestUtil .writeProperties (pluginDir , "name" , "fake-plugin" );
74- try {
75- PluginInfo .readFromProperties (pluginDir );
76- fail ("expected missing description exception" );
77- } catch (IllegalArgumentException e ) {
78- assertTrue (e .getMessage ().contains ("[description] is missing" ));
79- }
68+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
69+ assertThat (e .getMessage (), containsString ("[description] is missing" ));
8070 }
8171
8272 public void testReadFromPropertiesVersionMissing () throws Exception {
8373 Path pluginDir = createTempDir ().resolve ("fake-plugin" );
8474 PluginTestUtil .writeProperties (
8575 pluginDir , "description" , "fake desc" , "name" , "fake-plugin" );
86- try {
87- PluginInfo .readFromProperties (pluginDir );
88- fail ("expected missing version exception" );
89- } catch (IllegalArgumentException e ) {
90- assertTrue (e .getMessage ().contains ("[version] is missing" ));
91- }
76+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
77+ assertThat (e .getMessage (), containsString ("[version] is missing" ));
9278 }
9379
9480 public void testReadFromPropertiesElasticsearchVersionMissing () throws Exception {
@@ -97,12 +83,8 @@ public void testReadFromPropertiesElasticsearchVersionMissing() throws Exception
9783 "description" , "fake desc" ,
9884 "name" , "my_plugin" ,
9985 "version" , "1.0" );
100- try {
101- PluginInfo .readFromProperties (pluginDir );
102- fail ("expected missing elasticsearch version exception" );
103- } catch (IllegalArgumentException e ) {
104- assertTrue (e .getMessage ().contains ("[elasticsearch.version] is missing" ));
105- }
86+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
87+ assertThat (e .getMessage (), containsString ("[elasticsearch.version] is missing" ));
10688 }
10789
10890 public void testReadFromPropertiesJavaVersionMissing () throws Exception {
@@ -112,12 +94,8 @@ public void testReadFromPropertiesJavaVersionMissing() throws Exception {
11294 "name" , "my_plugin" ,
11395 "elasticsearch.version" , Version .CURRENT .toString (),
11496 "version" , "1.0" );
115- try {
116- PluginInfo .readFromProperties (pluginDir );
117- fail ("expected missing java version exception" );
118- } catch (IllegalArgumentException e ) {
119- assertTrue (e .getMessage ().contains ("[java.version] is missing" ));
120- }
97+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
98+ assertThat (e .getMessage (), containsString ("[java.version] is missing" ));
12199 }
122100
123101 public void testReadFromPropertiesJavaVersionIncompatible () throws Exception {
@@ -130,12 +108,8 @@ public void testReadFromPropertiesJavaVersionIncompatible() throws Exception {
130108 "java.version" , "1000000.0" ,
131109 "classname" , "FakePlugin" ,
132110 "version" , "1.0" );
133- try {
134- PluginInfo .readFromProperties (pluginDir );
135- fail ("expected incompatible java version exception" );
136- } catch (IllegalStateException e ) {
137- assertTrue (e .getMessage (), e .getMessage ().contains (pluginName + " requires Java" ));
138- }
111+ IllegalStateException e = expectThrows (IllegalStateException .class , () -> PluginInfo .readFromProperties (pluginDir ));
112+ assertThat (e .getMessage (), containsString (pluginName + " requires Java" ));
139113 }
140114
141115 public void testReadFromPropertiesBadJavaVersionFormat () throws Exception {
@@ -148,16 +122,9 @@ public void testReadFromPropertiesBadJavaVersionFormat() throws Exception {
148122 "java.version" , "1.7.0_80" ,
149123 "classname" , "FakePlugin" ,
150124 "version" , "1.0" );
151- try {
152- PluginInfo .readFromProperties (pluginDir );
153- fail ("expected bad java version format exception" );
154- } catch (IllegalStateException e ) {
155- assertTrue (
156- e .getMessage (),
157- e .getMessage ().equals ("version string must be a sequence of nonnegative "
158- + "decimal integers separated by \" .\" 's and may have leading zeros "
159- + "but was 1.7.0_80" ));
160- }
125+ IllegalStateException e = expectThrows (IllegalStateException .class , () -> PluginInfo .readFromProperties (pluginDir ));
126+ assertThat (e .getMessage (), equalTo ("version string must be a sequence of nonnegative decimal integers separated" +
127+ " by \" .\" 's and may have leading zeros but was 1.7.0_80" ));
161128 }
162129
163130 public void testReadFromPropertiesBogusElasticsearchVersion () throws Exception {
@@ -167,13 +134,8 @@ public void testReadFromPropertiesBogusElasticsearchVersion() throws Exception {
167134 "version" , "1.0" ,
168135 "name" , "my_plugin" ,
169136 "elasticsearch.version" , "bogus" );
170- try {
171- PluginInfo .readFromProperties (pluginDir );
172- fail ("expected bogus elasticsearch version exception" );
173- } catch (IllegalArgumentException e ) {
174- assertTrue (e .getMessage ().contains (
175- "version needs to contain major, minor, and revision" ));
176- }
137+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
138+ assertThat (e .getMessage (), containsString ("version needs to contain major, minor, and revision" ));
177139 }
178140
179141 public void testReadFromPropertiesOldElasticsearchVersion () throws Exception {
@@ -183,12 +145,8 @@ public void testReadFromPropertiesOldElasticsearchVersion() throws Exception {
183145 "name" , "my_plugin" ,
184146 "version" , "1.0" ,
185147 "elasticsearch.version" , Version .V_5_0_0 .toString ());
186- try {
187- PluginInfo .readFromProperties (pluginDir );
188- fail ("expected old elasticsearch version exception" );
189- } catch (IllegalArgumentException e ) {
190- assertTrue (e .getMessage ().contains ("was designed for version [5.0.0]" ));
191- }
148+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
149+ assertThat (e .getMessage (), containsString ("was designed for version [5.0.0]" ));
192150 }
193151
194152 public void testReadFromPropertiesJvmMissingClassname () throws Exception {
@@ -199,12 +157,8 @@ public void testReadFromPropertiesJvmMissingClassname() throws Exception {
199157 "version" , "1.0" ,
200158 "elasticsearch.version" , Version .CURRENT .toString (),
201159 "java.version" , System .getProperty ("java.specification.version" ));
202- try {
203- PluginInfo .readFromProperties (pluginDir );
204- fail ("expected old elasticsearch version exception" );
205- } catch (IllegalArgumentException e ) {
206- assertTrue (e .getMessage ().contains ("property [classname] is missing" ));
207- }
160+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> PluginInfo .readFromProperties (pluginDir ));
161+ assertThat (e .getMessage (), containsString ("property [classname] is missing" ));
208162 }
209163
210164 public void testPluginListSorted () {
0 commit comments