### Question:
DBDeviceStateManager extends DBManager<DeviceState> implements DeviceStateManager { @Override public final DeviceState saveState(final DeviceState state) throws DeviceStateManagerException { LOGGER.debug("saving state: {}", state); try { return save(state); } catch (DBManagerException e) { throw new DeviceStateManagerException(e); } } DBDeviceStateManager(final SessionFactory sessionFactory); @Override final DeviceState saveState(final DeviceState state); @Override final DeviceState getState(final Device device, final Class<? extends DeviceState> clazz); @Override final List<DeviceState> getStates(final Device device); @Override final void updateState(final DeviceState state); @Override final boolean deleteState(final Device device, final Class<? extends DeviceState> clazz);  }### Answer:
@Test public void testSaveDefaultState() { Assert.assertEquals(DBUtility.getCount(sessionFactory, IMADeviceState.class), 0); final IMADeviceState imaState = new IMADeviceState(device); final IMADeviceState imaSavedState = (IMADeviceState) stateManager.saveState(imaState); Assert.assertEquals(DBUtility.getCount(sessionFactory, IMADeviceState.class), 1); Assert.assertEquals(imaSavedState, imaState); Assert.assertEquals(DBUtility.getCount(sessionFactory, TPMDeviceState.class), 0); final TPMDeviceState tpmState = new TPMDeviceState(device); final TPMDeviceState tpmSavedState = (TPMDeviceState) stateManager.saveState(tpmState); Assert.assertEquals(DBUtility.getCount(sessionFactory, TPMDeviceState.class), 1); Assert.assertEquals(tpmSavedState, tpmState); Assert.assertEquals(DBUtility.getCount(sessionFactory, DeviceState.class), 2); }
@Test(expectedExceptions = DeviceStateManagerException.class) public void testSaveTPMStateTwice() { Assert.assertEquals(DBUtility.getCount(sessionFactory, DeviceState.class), 0); final TPMDeviceState state = new TPMDeviceState(device); updateTPMState(state); stateManager.saveState(state); stateManager.saveState(state); }
@Test(expectedExceptions = DeviceStateManagerException.class) public void testSaveIMAStateTwice() { Assert.assertEquals(DBUtility.getCount(sessionFactory, DeviceState.class), 0); final IMADeviceState state = new IMADeviceState(device); updateIMAState(state); stateManager.saveState(state); stateManager.saveState(state); }
@Test(expectedExceptions = DeviceStateManagerException.class) public void testSaveTPMStateWithSameDevice() { final TPMDeviceState state = new TPMDeviceState(device); final TPMDeviceState state2 = new TPMDeviceState(device); stateManager.saveState(state); stateManager.saveState(state2); }
@Test(expectedExceptions = DeviceStateManagerException.class) public void testSaveIMAStateWithSameDevice() { final IMADeviceState state = new IMADeviceState(device); final IMADeviceState state2 = new IMADeviceState(device); stateManager.saveState(state); stateManager.saveState(state2); }
@Test(expectedExceptions = NullPointerException.class) public void testSaveNullState() { stateManager.saveState(null); }