ModelManager implements ModelProvider { @SuppressWarnings("unchecked") public <T> void unregister(T object) { checkArgument(!(object instanceof Model), "can not unregister a model"); boolean result = false; final Set<Class<?>> modelSupportedTypes = registry.keySet(); for (final Class<?> modelSupportedType : modelSupportedTypes) { if (modelSupportedType.isAssignableFrom(object.getClass())) { final Collection<Model<?>> assignableModels = registry .get(modelSupportedType); for (final Model<?> m : assignableModels) { result |= ((Model<T>) m).unregister(object); } } } checkArgument(result, "Object %s with type %s can not be unregistered.", object, object.getClass()); } @SuppressWarnings("unchecked")  ModelManager(ImmutableSet<? extends Model<?>> ms); void register(T object); @SuppressWarnings("unchecked") void unregister(T object); @SuppressWarnings("unchecked") @Nullable @Override T tryGetModel(Class<T> clazz); @Override @Nonnull T getModel(Class<T> clazz); ImmutableSet<Model<?>> getModels(); Optional<UserInterface> getUserInterface(); static Builder builder();  }
@Test(expected = IllegalArgumentException.class) public void unregisterFail() { emptyManager.unregister(new Object()); }
@Test(expected = IllegalArgumentException.class) public void unregisterFailModel() { emptyManager.unregister(RoadModelBuilders.staticGraph( new MultimapGraph<LengthData>()).build(mock(DependencyProvider.class))); }
@Test public void unregisterFailNotRegisteredObject() { final ModelManager mm = new ModelManager( ImmutableSet.of( RoadModelBuilders.staticGraph(new MultimapGraph<LengthData>()) .build(mock(DependencyProvider.class)), RoadModelBuilders.staticGraph(new MultimapGraph<LengthData>()) .build(mock(DependencyProvider.class)))); boolean fail = false; try { mm.unregister(new RoadUser() { @Override public void initRoadUser(RoadModel model) {} }); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
@Test public void unregisterWithBrokenModel() { final ModelManager mm = new ModelManager( ImmutableSet.of(RoadModelBuilders.staticGraph( new MultimapGraph<LengthData>()) .build(mock(DependencyProvider.class)), new BrokenRoadModel(new MultimapGraph<LengthData>()))); boolean fail = false; try { mm.unregister(new RoadUser() { @Override public void initRoadUser(RoadModel model) {} }); } catch (final RuntimeException e) { fail = true; } assertThat(fail).isTrue(); }