CDSShardAccessImpl implements CDSShardAccess, LeaderLocationListener, AutoCloseable { @Override public <L extends LeaderLocationListener> LeaderLocationListenerRegistration<L> registerLeaderLocationListener(final L listener) { checkNotClosed(); requireNonNull(listener); checkArgument(!listeners.contains(listener), "Listener %s is already registered with ShardAccess %s", listener, this); LOG.debug("Registering LeaderLocationListener {}", listener); listeners.add(listener); return new LeaderLocationListenerRegistration<L>() { @Override public L getInstance() { return listener; } @Override public void close() { listeners.remove(listener); } }; } CDSShardAccessImpl(final DOMDataTreeIdentifier prefix, final ActorUtils actorUtils); @Override DOMDataTreeIdentifier getShardIdentifier(); @Override LeaderLocation getLeaderLocation(); @Override CompletionStage<Void> makeLeaderLocal(); @Override LeaderLocationListenerRegistration<L> registerLeaderLocationListener(final L listener); @Override @SuppressWarnings("checkstyle:IllegalCatch") void onLeaderLocationChanged(final LeaderLocation location); @Override void close();  }
@Test @SuppressWarnings("checkstyle:IllegalCatch") public void testRegisterLeaderLocationListener() { final LeaderLocationListener listener1 = mock(LeaderLocationListener.class); shardAccess.registerLeaderLocationListener(listener1); try { shardAccess.registerLeaderLocationListener(listener1); fail("Should throw exception"); } catch (final Exception e) { assertTrue(e instanceof IllegalArgumentException); } try { shardAccess.registerLeaderLocationListener(null); fail("Should throw exception"); } catch (final Exception e) { assertTrue(e instanceof NullPointerException); } final LeaderLocationListener listener2 = mock(LeaderLocationListener.class); shardAccess.close(); try { shardAccess.registerLeaderLocationListener(listener2); fail("Should throw exception"); } catch (final Exception ex) { assertTrue(ex instanceof IllegalStateException); } }