@@ -102,4 +102,62 @@ 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 ("foo" ));
108
+ assertThrows (NullPointerException .class ,
109
+ () -> Objects .requireNonNull (null ));
110
+ }
111
+
112
+ public void testRequireNonNullElse () {
113
+ assertEquals ("foo" , Objects .requireNonNullElse ("foo" , "bar" ));
114
+ assertThrows (NullPointerException .class ,
115
+ () -> Objects .requireNonNullElse (null , null ));
116
+ }
117
+
118
+ public void testRequireNonNullElseGet () {
119
+ assertEquals ("foo" , Objects .requireNonNullElseGet ("foo" , () -> "bar" ));
120
+ assertThrows (NullPointerException .class ,
121
+ () -> Objects .requireNonNullElseGet (null , null ));
122
+ assertThrows (NullPointerException .class ,
123
+ () -> Objects .requireNonNullElseGet (null , () -> null ));
124
+ }
125
+
126
+ public void testCheckIndex () {
127
+ assertEquals (5 , Objects .checkIndex (5 , 10 ));
128
+ assertThrows (IndexOutOfBoundsException .class ,
129
+ () -> Objects .checkIndex (-5 , 5 ));
130
+ assertThrows (IndexOutOfBoundsException .class ,
131
+ () -> Objects .checkIndex (10 , 5 ));
132
+ }
133
+
134
+ public void testCheckFromToIndex () {
135
+ assertEquals (5 , Objects .checkFromToIndex (5 , 7 , 10 ));
136
+ assertThrows (IndexOutOfBoundsException .class ,
137
+ () -> Objects .checkFromToIndex (-5 , 1 , 5 ));
138
+ assertThrows (IndexOutOfBoundsException .class ,
139
+ () -> Objects .checkFromToIndex (10 , 1 , 5 ));
140
+ assertThrows (IndexOutOfBoundsException .class ,
141
+ () -> Objects .checkFromToIndex (1 , 10 , 5 ));
142
+ }
143
+
144
+ public void testCheckFromIndexSize () {
145
+ assertEquals (5 , Objects .checkFromIndexSize (5 , 2 , 10 ));
146
+ assertThrows (IndexOutOfBoundsException .class ,
147
+ () -> Objects .checkFromIndexSize (-5 , 1 , 5 ));
148
+ assertThrows (IndexOutOfBoundsException .class ,
149
+ () -> Objects .checkFromIndexSize (10 , 1 , 5 ));
150
+ assertThrows (IndexOutOfBoundsException .class ,
151
+ () -> Objects .checkFromIndexSize (1 , 10 , 5 ));
152
+ }
153
+
154
+ private void assertThrows (Class <? extends Exception > thrownCheck , Runnable toTest ) {
155
+ try {
156
+ toTest .run ();
157
+ fail ("Should have failed" );
158
+ } catch (Exception ex ) {
159
+ assertEquals (ex .getClass (), thrownCheck );
160
+ }
161
+
162
+ }
105
163
}
0 commit comments