ModelManager implements ModelProvider { public <T> void register(T object) { checkArgument( !(object instanceof Model<?>), "Can not register a model: %s. " + "Models can be added via Simulator.builder().", object); final boolean success = doRegister(object); checkArgument( success, "The object %s with type %s can not be registered to any model.", 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 public void registerFail() { boolean fail = false; try { emptyManager.register(new Object()); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
@Test public void addOtherFooModel() { final OtherFooModel model = new OtherFooModel(); final ModelManager mm = new ModelManager(ImmutableSet.of(model)); mm.register(new Foo()); boolean fail = false; try { mm.register(new Bar()); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); assertEquals(1, model.calledRegister); assertEquals(1, model.calledTypes); }
@Test(expected = IllegalArgumentException.class) public void canNotRegisterModel() { emptyManager.register(RoadModelBuilders.staticGraph( new MultimapGraph<LengthData>()).build(mock(DependencyProvider.class))); }
@Test(expected = RuntimeException.class) public void registerWithBrokenModel() { final ModelManager mm = new ModelManager(ImmutableSet.of( RoadModelBuilders.staticGraph( new MultimapGraph<LengthData>()) .build(mock(DependencyProvider.class)), new BrokenRoadModel(new MultimapGraph<LengthData>()))); mm.register(new RoadUser() { @Override public void initRoadUser(RoadModel model) {} }); }
@Test public void anonymousModelTest() { final ModelManager mm = new ModelManager(ImmutableSet.of( new Model<InnerObject>() { @Override public boolean register(InnerObject element) { return false; } @Override public boolean unregister(InnerObject element) { return false; } @Override public Class<InnerObject> getSupportedType() { return InnerObject.class; } @Override public <T> T get(Class<T> clazz) { throw new UnsupportedOperationException(); } })); boolean fail = false; try { mm.register(new InnerObject()); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }