### Question:
JpaStorage implements Storage { @Override public DataFile getDatafileWithTransaction(String path) { try { persistenceManager.transaction(); final DataFile dataFile = getDatafile(path); persistenceManager.commit(); return dataFile; } catch (Exception e) { throw new ToolException("Database error", e, ToolException.TOOL_DB_ERROR); } } JpaStorage(PersistenceManager persistenceManager); @Override DataFile getDatafile(int id); @Override final DataFile getDatafile(final String path); @Override DataFile getDatafileWithTransaction(String path); void store(DataFile dataFile); @Override int storeWithTransaction(DataFile dataFile); @Override Observation getObservation(int id); @Override RelatedObservation getRelatedObservation(int id); @Override ReferenceObservation getReferenceObservation(int id); @Override final Sensor getSensor(final String sensorName); @Override Sensor getSensorWithTransaction(String sensorName); @Override void storeWithTransaction(Sensor sensor); @Override List<RelatedObservation> getRelatedObservations(String sensorName, Date startDate, Date stopDate); @Override void store(InsituObservation insituObservation);  }### Answer:
@Test public void testGetDataFileWithTransaction() { final String sql = "select f from DataFile f where f.path = ?1"; final String path = "/over/the/rainbow"; final DataFile dataFile = createDataFile(path); when(persistenceManager.pick(sql, path)).thenReturn(dataFile); final DataFile toolStorageDatafile = jpaStorage.getDatafileWithTransaction(path); assertNotNull(toolStorageDatafile); assertEquals(path, toolStorageDatafile.getPath()); verify(persistenceManager, times(1)).transaction(); verify(persistenceManager, times(1)).pick(sql, path); verify(persistenceManager, times(1)).commit(); verifyNoMoreInteractions(persistenceManager); }
@Test public void testGetDataFileWithTransaction_fails() { final String sql = "select f from DataFile f where f.path = ?1"; final String path = "/over/the/rainbow"; doThrow(new PersistenceException(null, null, null, true)).when(persistenceManager).pick(sql, path); try { jpaStorage.getDatafileWithTransaction(path); fail("ToolException expected"); } catch (ToolException expected) { } verify(persistenceManager, times(1)).transaction(); verify(persistenceManager, times(1)).pick(sql, path); verifyNoMoreInteractions(persistenceManager); }