@@ -102,4 +102,78 @@ 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
+ assertEquals ("bar" , Objects .requireNonNullElse (hideFromCompiler (null ), "bar" ));
115
+ assertThrows (NullPointerException .class ,
116
+ () -> Objects .requireNonNullElse (hideFromCompiler (null ), null ));
117
+ }
118
+
119
+ public void testRequireNonNullElseGet () {
120
+ assertEquals ("foo" ,
121
+ Objects .requireNonNullElseGet (hideFromCompiler ("foo" ), () -> "bar" ));
122
+ assertEquals ("bar" ,
123
+ Objects .requireNonNullElseGet (hideFromCompiler (null ), () -> "bar" ));
124
+ assertThrows (NullPointerException .class ,
125
+ () -> Objects .requireNonNullElseGet (hideFromCompiler (null ), null ));
126
+ assertThrows (NullPointerException .class ,
127
+ () -> Objects .requireNonNullElseGet (hideFromCompiler (null ), () -> null ));
128
+ }
129
+
130
+ private String hideFromCompiler (String value ) {
131
+ if (Math .random () > 2 ) {
132
+ return "unreachable" ;
133
+ }
134
+ return value ;
135
+ }
136
+
137
+ public void testCheckIndex () {
138
+ assertEquals (5 , Objects .checkIndex (5 , 10 ));
139
+ assertThrows (IndexOutOfBoundsException .class ,
140
+ () -> Objects .checkIndex (-5 , 5 ));
141
+ assertThrows (IndexOutOfBoundsException .class ,
142
+ () -> Objects .checkIndex (10 , 5 ));
143
+ assertThrows (IndexOutOfBoundsException .class ,
144
+ () -> Objects .checkIndex (5 , 5 ));
145
+ }
146
+
147
+ public void testCheckFromToIndex () {
148
+ assertEquals (5 , Objects .checkFromToIndex (5 , 7 , 10 ));
149
+ assertEquals (0 , Objects .checkFromToIndex (0 , 10 , 10 ));
150
+ assertThrows (IndexOutOfBoundsException .class ,
151
+ () -> Objects .checkFromToIndex (-5 , 1 , 5 ));
152
+ assertThrows (IndexOutOfBoundsException .class ,
153
+ () -> Objects .checkFromToIndex (10 , 1 , 5 ));
154
+ assertThrows (IndexOutOfBoundsException .class ,
155
+ () -> Objects .checkFromToIndex (1 , 10 , 5 ));
156
+ }
157
+
158
+ public void testCheckFromIndexSize () {
159
+ assertEquals (5 , Objects .checkFromIndexSize (5 , 2 , 10 ));
160
+ assertEquals (0 , Objects .checkFromIndexSize (0 , 10 , 10 ));
161
+ assertThrows (IndexOutOfBoundsException .class ,
162
+ () -> Objects .checkFromIndexSize (-5 , 1 , 5 ));
163
+ assertThrows (IndexOutOfBoundsException .class ,
164
+ () -> Objects .checkFromIndexSize (10 , 1 , 5 ));
165
+ assertThrows (IndexOutOfBoundsException .class ,
166
+ () -> Objects .checkFromIndexSize (1 , 10 , 5 ));
167
+ assertThrows (IndexOutOfBoundsException .class ,
168
+ () -> Objects .checkFromIndexSize (1 , -5 , 5 ));
169
+ }
170
+
171
+ private void assertThrows (Class <? extends Exception > thrownCheck , Runnable toTest ) {
172
+ try {
173
+ toTest .run ();
174
+ fail ("Should have failed" );
175
+ } catch (Exception ex ) {
176
+ assertEquals (thrownCheck , ex .getClass ());
177
+ }
178
+ }
105
179
}
0 commit comments