PointToPointValidator { void validate(Object subscriber, List<Class<?>> subscribedNotifications) { Class<?> subscriberClass = subscriber.getClass(); subscribedNotifications.stream() .filter(PointToPointNotification.class::isAssignableFrom) .flatMap(this::streamP2PSupertypes) .forEach(classImplementingP2P -> { Class<?> previousSubscriberClass = subscriptions.putIfAbsent(classImplementingP2P, subscriberClass); if (previousSubscriberClass != null) { throw new IllegalStateException(getErrorMessage(subscriberClass, classImplementingP2P, previousSubscriberClass)); } }); } PointToPointValidator();   }
@Test void whenNoP2P_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummyNotification.class)); validator.validate(new DummySubscriberB(), ImmutableList.of(DummyNotification.class)); }
@Test void whenSameClassSubscribesTwice_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummyNotification.class)); validator.validate(new DummySubscriberA(), ImmutableList.of(DummyNotification.class)); }
@Test void whenSameClassSubscribesTwiceToP2P_thenException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummyP2PNotification.class)); IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> validator.validate(new DummySubscriberA(), ImmutableList.of(DummyP2PNotification.class))); Assertions.assertTrue(e.getMessage().contains(DummyP2PNotification.class.getSimpleName())); }
@Test void whenTwoP2PSubscribers_thenException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummyP2PNotification.class)); IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> validator.validate(new DummySubscriberB(), ImmutableList.of(DummyP2PNotification.class))); Assertions.assertTrue(e.getMessage().contains(DummyP2PNotification.class.getSimpleName())); }
@Test void whenSubtypeIsP2P_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummySupertypeNotification.class)); validator.validate(new DummySubscriberB(), ImmutableList.of(DummyP2PSubtypeNotification.class)); }
@Test void whenBothSubscribersForSuperAndSubtypeIsP2P_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummySupertypeNotification.class)); validator.validate(new DummySubscriberB(), ImmutableList.of(DummySupertypeNotification.class)); }
@Test void whenSupertypeIsP2P_thenException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummyP2PSupertypeNotification.class)); IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> validator.validate(new DummySubscriberB(), ImmutableList.of(DummySubtypeNotification.class))); Assertions.assertTrue(e.getMessage().contains(DummyP2PSupertypeNotification.class.getSimpleName())); }
@Test void whenCommonSubtypeIsP2P_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummySuperTypeInterfaceA.class)); validator.validate(new DummySubscriberB(), ImmutableList.of(DummySuperTypeInterfaceB.class)); }
@Test void whenMiddleTypeIsP2P_thenNoException() { validator.validate(new DummySubscriberA(), ImmutableList.of(DummySupertypeNotificationA.class)); validator.validate(new DummySubscriberB(), ImmutableList.of(DummySubtypeNotificationC.class)); }