22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44import static org .junit .jupiter .api .Assertions .assertFalse ;
5+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
56import static org .junit .jupiter .api .Assertions .assertNull ;
67import static org .junit .jupiter .api .Assertions .assertThrows ;
78import static org .junit .jupiter .api .Assertions .assertTrue ;
1213class ContextTest {
1314 static final ContextKey <String > STRING_KEY = ContextKey .named ("string-key" );
1415 static final ContextKey <Boolean > BOOLEAN_KEY = ContextKey .named ("boolean-key" );
16+ static final ContextKey <Float > FLOAT_KEY = ContextKey .named ("float-key" );
17+ static final ContextKey <Long > LONG_KEY = ContextKey .named ("long-key" );
1518
1619 // demonstrate how values can hide their context keys
1720 static class ValueWithKey implements ImplicitContextKeyed {
@@ -86,19 +89,33 @@ void testEqualsAndHashCode() {
8689 Context context1 = empty .with (STRING_KEY , "value" );
8790 Context context2 = empty .with (STRING_KEY , "value " );
8891 Context context3 = empty .with (STRING_KEY , "value " .trim ());
92+ Context context4 = empty .with (STRING_KEY , "value" ).with (BOOLEAN_KEY , true );
8993 // Test equals on self
9094 assertTrue (empty .equals (empty ));
9195 assertTrue (context1 .equals (context1 ));
96+ assertTrue (context4 .equals (context4 ));
9297 // Test equals on null
9398 assertFalse (context1 .equals (null ));
99+ assertFalse (context4 .equals (null ));
94100 // Test equals on different object type
95101 assertFalse (context1 .equals ("value" ));
102+ assertFalse (context4 .equals ("value" ));
96103 // Test equals on different contexts with the same values
97104 assertTrue (context1 .equals (context3 ));
98105 assertEquals (context1 .hashCode (), context3 .hashCode ());
99106 // Test equals on different contexts
100107 assertFalse (context1 .equals (empty ));
108+ assertNotEquals (context1 .hashCode (), empty .hashCode ());
101109 assertFalse (context1 .equals (context2 ));
110+ assertNotEquals (context1 .hashCode (), context2 .hashCode ());
111+ assertFalse (context1 .equals (context4 ));
112+ assertNotEquals (context1 .hashCode (), context4 .hashCode ());
113+ assertFalse (empty .equals (context1 ));
114+ assertNotEquals (empty .hashCode (), context1 .hashCode ());
115+ assertFalse (context2 .equals (context1 ));
116+ assertNotEquals (context2 .hashCode (), context1 .hashCode ());
117+ assertFalse (context4 .equals (context1 ));
118+ assertNotEquals (context4 .hashCode (), context1 .hashCode ());
102119 }
103120
104121 @ Test
@@ -110,4 +127,41 @@ void testImplicitKey() {
110127 assertNull (ValueWithKey .from (empty ));
111128 assertEquals (valueWithKey , ValueWithKey .from (context ));
112129 }
130+
131+ @ SuppressWarnings ({"SimplifiableAssertion" })
132+ @ Test
133+ void testInflation () {
134+ Context empty = Context .root ();
135+
136+ Context one = empty .with (STRING_KEY , "unset" ).with (STRING_KEY , "one" );
137+ Context two = one .with (BOOLEAN_KEY , false ).with (BOOLEAN_KEY , true );
138+ Context three = two .with (FLOAT_KEY , 0.0f ).with (FLOAT_KEY , 3.3f );
139+
140+ assertNull (empty .get (STRING_KEY ));
141+ assertNull (empty .get (BOOLEAN_KEY ));
142+ assertNull (empty .get (FLOAT_KEY ));
143+ assertNull (empty .get (LONG_KEY ));
144+
145+ assertEquals ("one" , one .get (STRING_KEY ));
146+ assertNull (one .get (BOOLEAN_KEY ));
147+ assertNull (one .get (FLOAT_KEY ));
148+ assertNull (one .get (LONG_KEY ));
149+
150+ assertEquals ("one" , two .get (STRING_KEY ));
151+ assertEquals (true , two .get (BOOLEAN_KEY ));
152+ assertNull (two .get (FLOAT_KEY ));
153+ assertNull (two .get (LONG_KEY ));
154+
155+ assertEquals ("one" , three .get (STRING_KEY ));
156+ assertEquals (true , three .get (BOOLEAN_KEY ));
157+ assertEquals (3.3f , three .get (FLOAT_KEY ));
158+ assertNull (three .get (LONG_KEY ));
159+
160+ assertFalse (empty .equals (one ));
161+ assertFalse (one .equals (two ));
162+ assertFalse (two .equals (three ));
163+ assertNotEquals (one .hashCode (), empty .hashCode ());
164+ assertNotEquals (two .hashCode (), one .hashCode ());
165+ assertNotEquals (three .hashCode (), two .hashCode ());
166+ }
113167}
0 commit comments