@@ -102,4 +102,70 @@ public void testHashCode() {
102
102
Object obj = new Object ();
103
103
assertEquals (obj .hashCode (), Objects .hashCode (obj ));
104
104
}
105
+
106
+ public void testRequireNonNull () {
107
+ assertEquals ("foo" , Objects .requireNonNull (hideFromCompiler ("foo" )));
108
+ assertThrows (NullPointerException .class ,
109
+ () -> Objects .requireNonNull (hideFromCompiler (null )));
110
+ }
111
+
112
+ public void testRequireNonNullElse () {
113
+ assertEquals ("foo" , Objects .requireNonNullElse (hideFromCompiler ("foo" ), "bar" ));
114
+ assertThrows (NullPointerException .class ,
115
+ () -> Objects .requireNonNullElse (hideFromCompiler (null ), null ));
116
+ }
117
+
118
+ public void testRequireNonNullElseGet () {
119
+ assertEquals ("foo" ,
120
+ Objects .requireNonNullElseGet (hideFromCompiler ("foo" ), () -> "bar" ));
121
+ assertThrows (NullPointerException .class ,
122
+ () -> Objects .requireNonNullElseGet (hideFromCompiler (null ), null ));
123
+ assertThrows (NullPointerException .class ,
124
+ () -> Objects .requireNonNullElseGet (hideFromCompiler (null ), () -> null ));
125
+ }
126
+
127
+ private String hideFromCompiler (String value ) {
128
+ if (Math .random () > 2 ) {
129
+ return "unreachable" ;
130
+ }
131
+ return value ;
132
+ }
133
+
134
+ public void testCheckIndex () {
135
+ assertEquals (5 , Objects .checkIndex (5 , 10 ));
136
+ assertThrows (IndexOutOfBoundsException .class ,
137
+ () -> Objects .checkIndex (-5 , 5 ));
138
+ assertThrows (IndexOutOfBoundsException .class ,
139
+ () -> Objects .checkIndex (10 , 5 ));
140
+ }
141
+
142
+ public void testCheckFromToIndex () {
143
+ assertEquals (5 , Objects .checkFromToIndex (5 , 7 , 10 ));
144
+ assertThrows (IndexOutOfBoundsException .class ,
145
+ () -> Objects .checkFromToIndex (-5 , 1 , 5 ));
146
+ assertThrows (IndexOutOfBoundsException .class ,
147
+ () -> Objects .checkFromToIndex (10 , 1 , 5 ));
148
+ assertThrows (IndexOutOfBoundsException .class ,
149
+ () -> Objects .checkFromToIndex (1 , 10 , 5 ));
150
+ }
151
+
152
+ public void testCheckFromIndexSize () {
153
+ assertEquals (5 , Objects .checkFromIndexSize (5 , 2 , 10 ));
154
+ assertThrows (IndexOutOfBoundsException .class ,
155
+ () -> Objects .checkFromIndexSize (-5 , 1 , 5 ));
156
+ assertThrows (IndexOutOfBoundsException .class ,
157
+ () -> Objects .checkFromIndexSize (10 , 1 , 5 ));
158
+ assertThrows (IndexOutOfBoundsException .class ,
159
+ () -> Objects .checkFromIndexSize (1 , 10 , 5 ));
160
+ }
161
+
162
+ private void assertThrows (Class <? extends Exception > thrownCheck , Runnable toTest ) {
163
+ try {
164
+ toTest .run ();
165
+ fail ("Should have failed" );
166
+ } catch (Exception ex ) {
167
+ assertEquals (ex .getClass (), thrownCheck );
168
+ }
169
+
170
+ }
105
171
}
0 commit comments