OnlyLocationOrFirstZone implements ImplicitLocationSupplier { @Override public Location get() { Set<? extends Location> locations = locationsSupplier.get(); if (locations.size() == 1) return getOnlyElement(locations); try { return find(locations, isZone()); } catch (NoSuchElementException e) { throw new NoSuchElementException("none to of the locations are scope ZONE: " + transform(locations, ToIdAndScope.INSTANCE)); } } @Inject  OnlyLocationOrFirstZone(@Memoized Supplier<Set<? extends Location>> locationsSupplier); @Override Location get();  }
@Test public void testDidntFindZoneThrowsNSEEWithReasonableMessage() { Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(provider, region)); OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier); try { fn.get(); fail("Expected NoSuchElementException"); } catch (NoSuchElementException e) { assertEquals(e.getMessage(), "none to of the locations are scope ZONE: [servo:PROVIDER, servo-r:REGION]"); } }
@Test public void testNoZoneUsesProvider() { Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(provider)); OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier); assertEquals(fn.get(), provider); }
@Test public void testFirstZone() { Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(provider, region, zone)); OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier); assertEquals(fn.get(), zone); }