@Test public void loadDoesNotThrowIfReaderCloseThrows() throws IOException { when(io.fileExists()).thenReturn(true); Reader reader = mock(Reader.class); when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1); doThrow(new IOException("Foo")).when(reader).close(); when(io.getReader()).thenReturn(reader); storage.load(); verify(reader, times(1)).close(); }
@Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); }
ProjectStorage implements DataSource<Project> { @Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); } }
ProjectStorage implements DataSource<Project> { @Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); } ProjectStorage(FileIO io); }
ProjectStorage implements DataSource<Project> { @Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); } ProjectStorage(FileIO io); @Override List<Project> load(); @Override void save(List<Project> projects); }
ProjectStorage implements DataSource<Project> { @Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); } ProjectStorage(FileIO io); @Override List<Project> load(); @Override void save(List<Project> projects);  }