ContextFileWorker { static Set<RecentPresentation> purgeRecentPresentations(final File contextFile, final long numberOfRecentPresentationsToKeep) throws ContextFileException { checkContextFileValidity(contextFile); if (numberOfRecentPresentationsToKeep < 0) throw new IllegalArgumentException("The number of recent presentations to keep can not be negative"); try (final InputStream input = getInputStreamForFile(contextFile); final OutputStream output = new FileOutputStream(contextFile)) { return purgeRecentPresentations(input, output, numberOfRecentPresentationsToKeep); } catch (IOException e) { throw new ContextFileException(e); } } private  ContextFileWorker(); static Set<RecentPresentation> readRecentPresentationFromFile(final File contextFile); static void saveRecentPresentationToFile(final File contextFile, final RecentPresentation recentPresentation); static void updateRecentPresentationInFile(final File contextFile, final RecentPresentation recentPresentation); static boolean recentPresentationAlreadyPresent(final File contextFile, final RecentPresentation recentPresentation); static Set<RecentPresentation> purgeRecentPresentations(final InputStream input, final OutputStream output, final long numberOfRecentPresentationsToKeep);  }
@Test public void purgeRecentPresentationsWhenEqualAsSpecified() throws ContextFileException { final int numberOfRecentPresentationsToKeep = 3; final RecentPresentation recentPresentation1 = new RecentPresentation("presentation1.sfx", LocalDateTime.now()); final RecentPresentation recentPresentation2 = new RecentPresentation("presentation2.sfx", recentPresentation1.getOpenedDateTime().plusDays(2)); final RecentPresentation recentPresentation3 = new RecentPresentation("presentation3.sfx", recentPresentation2.getOpenedDateTime().plusDays(2)); final String xml = this.createXmlStringFromRecentPresentations(recentPresentation1, recentPresentation2, recentPresentation3); final ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes(UTF_8)); final ByteArrayOutputStream output = new ByteArrayOutputStream(); final Set<RecentPresentation> presentations = ContextFileWorker.purgeRecentPresentations(input, output, numberOfRecentPresentationsToKeep); assertEquals(numberOfRecentPresentationsToKeep, presentations.size()); final Iterator<RecentPresentation> iterator = presentations.iterator(); RecentPresentation recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation1.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation2.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation3.getId(), recentPresentation.getId()); }
@Test public void purgeRecentPresentationsWhenLessThanSpecified() throws ContextFileException { final int numberOfRecentPresentationsToKeep = 4; final RecentPresentation recentPresentation1 = new RecentPresentation("presentation1.sfx", LocalDateTime.now()); final RecentPresentation recentPresentation2 = new RecentPresentation("presentation2.sfx", recentPresentation1.getOpenedDateTime().plusDays(2)); final RecentPresentation recentPresentation3 = new RecentPresentation("presentation3.sfx", recentPresentation2.getOpenedDateTime().plusDays(2)); final String xml = this.createXmlStringFromRecentPresentations(recentPresentation1, recentPresentation2, recentPresentation3); final ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes(UTF_8)); final ByteArrayOutputStream output = new ByteArrayOutputStream(); final Set<RecentPresentation> presentations = ContextFileWorker.purgeRecentPresentations(input, output, numberOfRecentPresentationsToKeep); assertEquals(3, presentations.size()); final Iterator<RecentPresentation> iterator = presentations.iterator(); RecentPresentation recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation1.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation2.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation3.getId(), recentPresentation.getId()); }
@Test public void purgeRecentPresentations() throws ContextFileException { final int numberOfRecentPresentationsToKeep = 2; final RecentPresentation recentPresentation1 = new RecentPresentation("presentation1.sfx", LocalDateTime.now()); final RecentPresentation recentPresentation2 = new RecentPresentation("presentation2.sfx", recentPresentation1.getOpenedDateTime().plusDays(2)); final RecentPresentation recentPresentation3 = new RecentPresentation("presentation3.sfx", recentPresentation2.getOpenedDateTime().plusDays(2)); final RecentPresentation recentPresentation4 = new RecentPresentation("presentation4.sfx", null); final String xml = this.createXmlStringFromRecentPresentations(recentPresentation1, recentPresentation2, recentPresentation3, recentPresentation4); final ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes(UTF_8)); final ByteArrayOutputStream output = new ByteArrayOutputStream(); final Set<RecentPresentation> presentations = ContextFileWorker.purgeRecentPresentations(input, output, numberOfRecentPresentationsToKeep); assertEquals(numberOfRecentPresentationsToKeep, presentations.size()); final Iterator<RecentPresentation> iterator = presentations.iterator(); RecentPresentation recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation2.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation3.getId(), recentPresentation.getId()); }