CheckHelper { public static <T extends Ordered> void orderDuplicationCheck(Collection<T> collection) { Set<T> duplicated = collection.stream() .filter(n -> collection.stream() .filter(x -> x.getOrder() == n.getOrder()).count() > 1) .collect(toSet()); if (!duplicated.isEmpty()) { String duplicatedOrders = duplicated.stream() .map(i -> String.valueOf(i.getOrder())) .collect(joining(", ")); throw new InvalidConfigurationException( "There is registered more than one entity with the given order: " + duplicatedOrders); } }  static void orderDuplicationCheck(Collection<T> collection);  }
@Test public void with_correct_orders() { List<Interceptor> interceptors = Arrays.asList(new FirstInterceptor(), new SecondInterceptor(), new ThirdInterceptor()); try { orderDuplicationCheck(interceptors); } catch (InvalidConfigurationException e) { fail("There is some problem with order duplications"); } }
@Test(expected = InvalidConfigurationException.class) public void with_duplication_exception() { List<Interceptor> interceptors = Arrays .asList(new FirstInterceptor(), new SecondInterceptor(), new DuplicatedInterceptor()); orderDuplicationCheck(interceptors); }