2020
2121import org .elasticsearch .test .ESTestCase ;
2222
23- import java .util .EnumSet ;
24-
23+ import static org .hamcrest .CoreMatchers .containsString ;
2524import static org .hamcrest .CoreMatchers .is ;
2625import static org .hamcrest .CoreMatchers .not ;
2726import static org .hamcrest .CoreMatchers .sameInstance ;
@@ -33,69 +32,59 @@ public void testParse() {
3332 String [] deprecated = new String []{"barFoo" , "bar_foo" };
3433 ParseField withDeprecations = field .withDeprecation ("Foobar" , randomFrom (deprecated ));
3534 assertThat (field , not (sameInstance (withDeprecations )));
36- assertThat (field .match (randomFrom (values ), ParseField . EMPTY_FLAGS ), is (true ));
37- assertThat (field .match ("foo bar" , ParseField . EMPTY_FLAGS ), is (false ));
38- assertThat (field .match (randomFrom (deprecated ), ParseField . EMPTY_FLAGS ), is (false ));
39- assertThat (field .match ("barFoo" , ParseField . EMPTY_FLAGS ), is (false ));
35+ assertThat (field .match (randomFrom (values ), false ), is (true ));
36+ assertThat (field .match ("foo bar" , false ), is (false ));
37+ assertThat (field .match (randomFrom (deprecated ), false ), is (false ));
38+ assertThat (field .match ("barFoo" , false ), is (false ));
4039
41- assertThat (withDeprecations .match (randomFrom (values ), ParseField . EMPTY_FLAGS ), is (true ));
42- assertThat (withDeprecations .match ("foo bar" , ParseField . EMPTY_FLAGS ), is (false ));
43- assertThat (withDeprecations .match (randomFrom (deprecated ), ParseField . EMPTY_FLAGS ), is (true ));
44- assertThat (withDeprecations .match ("barFoo" , ParseField . EMPTY_FLAGS ), is (true ));
40+ assertThat (withDeprecations .match (randomFrom (values ), false ), is (true ));
41+ assertThat (withDeprecations .match ("foo bar" , false ), is (false ));
42+ assertThat (withDeprecations .match (randomFrom (deprecated ), false ), is (true ));
43+ assertThat (withDeprecations .match ("barFoo" , false ), is (true ));
4544
4645 // now with strict mode
47- EnumSet <ParseField .Flag > flags = EnumSet .of (ParseField .Flag .STRICT );
48- assertThat (field .match (randomFrom (values ), flags ), is (true ));
49- assertThat (field .match ("foo bar" , flags ), is (false ));
50- assertThat (field .match (randomFrom (deprecated ), flags ), is (false ));
51- assertThat (field .match ("barFoo" , flags ), is (false ));
52-
53- assertThat (withDeprecations .match (randomFrom (values ), flags ), is (true ));
54- assertThat (withDeprecations .match ("foo bar" , flags ), is (false ));
55- try {
56- withDeprecations .match (randomFrom (deprecated ), flags );
57- fail ();
58- } catch (IllegalArgumentException ex ) {
59-
60- }
46+ assertThat (field .match (randomFrom (values ), true ), is (true ));
47+ assertThat (field .match ("foo bar" , true ), is (false ));
48+ assertThat (field .match (randomFrom (deprecated ), true ), is (false ));
49+ assertThat (field .match ("barFoo" , true ), is (false ));
6150
62- try {
63- withDeprecations .match ("barFoo" , flags );
64- fail ();
65- } catch (IllegalArgumentException ex ) {
66-
67- }
51+ assertThat (withDeprecations .match (randomFrom (values ), true ), is (true ));
52+ assertThat (withDeprecations .match ("foo bar" , true ), is (false ));
53+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
54+ () -> withDeprecations .match (randomFrom (deprecated ), true ));
55+ assertThat (e .getMessage (), containsString ("used, expected [foo_bar] instead" ));
56+ e = expectThrows (IllegalArgumentException .class , () -> withDeprecations .match ("barFoo" , true ));
57+ assertThat (e .getMessage (), containsString ("Deprecated field [barFoo] used, expected [foo_bar] instead" ));
6858 }
6959
7060 public void testAllDeprecated () {
7161 String [] values = new String []{"like_text" , "likeText" };
7262
7363 boolean withDeprecatedNames = randomBoolean ();
7464 String [] deprecated = new String []{"text" , "same_as_text" };
75- String [] allValues = values ;
65+ String [] allValues ;
7666 if (withDeprecatedNames ) {
77- String [] newArray = new String [allValues .length + deprecated .length ];
78- System .arraycopy (allValues , 0 , newArray , 0 , allValues .length );
79- System .arraycopy (deprecated , 0 , newArray , allValues .length , deprecated .length );
67+ String [] newArray = new String [values .length + deprecated .length ];
68+ System .arraycopy (values , 0 , newArray , 0 , values .length );
69+ System .arraycopy (deprecated , 0 , newArray , values .length , deprecated .length );
8070 allValues = newArray ;
71+ } else {
72+ allValues = values ;
8173 }
8274
83- ParseField field = new ParseField ( randomFrom ( values )) ;
75+ ParseField field ;
8476 if (withDeprecatedNames ) {
85- field = field .withDeprecation (deprecated );
77+ field = new ParseField (randomFrom (values )).withDeprecation (deprecated ).withAllDeprecated ("like" );
78+ } else {
79+ field = new ParseField (randomFrom (values )).withAllDeprecated ("like" );
8680 }
87- field = field .withAllDeprecated ("like" );
8881
8982 // strict mode off
90- assertThat (field .match (randomFrom (allValues ), ParseField . EMPTY_FLAGS ), is (true ));
91- assertThat (field .match ("not a field name" , ParseField . EMPTY_FLAGS ), is (false ));
83+ assertThat (field .match (randomFrom (allValues ), false ), is (true ));
84+ assertThat (field .match ("not a field name" , false ), is (false ));
9285
9386 // now with strict mode
94- EnumSet <ParseField .Flag > flags = EnumSet .of (ParseField .Flag .STRICT );
95- try {
96- field .match (randomFrom (allValues ), flags );
97- fail ();
98- } catch (IllegalArgumentException ex ) {
99- }
87+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> field .match (randomFrom (allValues ), true ));
88+ assertThat (e .getMessage (), containsString (" used, replaced by [like]" ));
10089 }
10190}
0 commit comments