@@ -247,52 +247,69 @@ void withDefault() {
247247 @ Test
248248 @ DisplayName ("With default and other implementation, should return other" )
249249 void withDefaultAndOther () {
250- factories .add (new OtherHttpClientFactory ( ));
250+ factories .add (new ScoredHttpClientFactory ( "other" , 0 ));
251251 factories .add (new DefaultHttpClientFactory ());
252- assertEquals (OtherHttpClientFactory .class , HttpClientUtils .getHttpClientFactory ().getClass ());
252+ assertEquals (ScoredHttpClientFactory .class , HttpClientUtils .getHttpClientFactory ().getClass ());
253253 }
254254
255255 @ Test
256- @ DisplayName ("With default and other implementation, should return Priority" )
256+ @ DisplayName ("With default, other, and priority implementations; should return Priority" )
257257 void withDefaultAndPriorityAndOther () {
258- factories .add (new OtherHttpClientFactory ( ));
259- factories .add (new PriorityHttpClientFactory ( ));
258+ factories .add (new ScoredHttpClientFactory ( "other" , 0 ));
259+ factories .add (new ScoredHttpClientFactory ( "priority" , Integer . MAX_VALUE ));
260260 factories .add (new DefaultHttpClientFactory ());
261- assertEquals (PriorityHttpClientFactory .class , HttpClientUtils .getHttpClientFactory ().getClass ());
261+ final HttpClient .Factory result = HttpClientUtils .getHttpClientFactory ();
262+ assertEquals (ScoredHttpClientFactory .class , result .getClass ());
263+ assertEquals (Integer .MAX_VALUE , result .priority ());
264+ assertEquals ("priority" , ((ScoredHttpClientFactory ) result ).id );
262265 }
263266
264- private final class DefaultHttpClientFactory implements HttpClient .Factory {
267+ @ Test
268+ @ DisplayName ("With multiple implementations and several with max priority, should return first of max priority" )
269+ void withMultipleAndCollision () {
270+ factories .add (new DefaultHttpClientFactory ());
271+ factories .add (new ScoredHttpClientFactory ("other" , 0 ));
272+ factories .add (new ScoredHttpClientFactory ("priority-1" , Integer .MAX_VALUE ));
273+ factories .add (new ScoredHttpClientFactory ("priority-2" , Integer .MAX_VALUE ));
274+ factories .add (new DefaultHttpClientFactory ());
275+ final HttpClient .Factory result = HttpClientUtils .getHttpClientFactory ();
276+ assertEquals (ScoredHttpClientFactory .class , result .getClass ());
277+ assertEquals (Integer .MAX_VALUE , result .priority ());
278+ assertEquals ("priority-1" , ((ScoredHttpClientFactory ) result ).id );
279+ }
265280
266- @ Override
267- public HttpClient .Builder newBuilder () {
268- return null ;
269- }
281+ private final class ScoredHttpClientFactory implements HttpClient .Factory {
282+ private final String id ;
283+ private final int priority ;
270284
271- @ Override
272- public boolean isDefault () {
273- return true ;
285+ public ScoredHttpClientFactory ( String id , int priority ) {
286+ this . id = id ;
287+ this . priority = priority ;
274288 }
275- }
276-
277- private final class OtherHttpClientFactory implements HttpClient .Factory {
278289
279290 @ Override
280291 public HttpClient .Builder newBuilder () {
281292 return null ;
282293 }
294+
295+ @ Override
296+ public int priority () {
297+ return priority ;
298+ }
283299 }
284300
285- private final class PriorityHttpClientFactory implements HttpClient .Factory {
301+ private final class DefaultHttpClientFactory implements HttpClient .Factory {
286302
287303 @ Override
288304 public HttpClient .Builder newBuilder () {
289305 return null ;
290306 }
291307
292308 @ Override
293- public int priority () {
294- return Integer . MAX_VALUE ;
309+ public boolean isDefault () {
310+ return true ;
295311 }
296312 }
313+
297314 }
298315}
0 commit comments