ContextFileWorker { protected static Set<RecentPresentation> readRecentPresentationFromStream(final InputStream input) { final Set<RecentPresentation> presentations = new TreeSet<>(); try { final Document document = createDocumentFromInput(input); final Node recentPresentationsNode = getRecentPresentationsNode(document); if (recentPresentationsNode != null && recentPresentationsNode.hasChildNodes()) { final List<Node> recentPresentationNodes = getRecentPresentationNodes(recentPresentationsNode); presentations.addAll( recentPresentationNodes.stream() .map(ContextFileWorker::buildRecentPresentationFromNode) .filter(Objects::nonNull) .collect(Collectors.toList())); } } catch (ContextFileException e) { LOGGER.log(Level.SEVERE, "The context file can not be read", e); } return presentations; } 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 recentPresentationsAreSortedByNormalizedPath() { 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(recentPresentation3, recentPresentation1, recentPresentation2); final ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes()); final Set<RecentPresentation> presentations = ContextFileWorker.readRecentPresentationFromStream(input); final Iterator<RecentPresentation> iterator = presentations.iterator(); RecentPresentation recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation1.getNormalizedPath(), recentPresentation.getNormalizedPath()); assertEquals(recentPresentation1.getOpenedDateTime(), recentPresentation.getOpenedDateTime()); assertEquals(recentPresentation1.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation2.getNormalizedPath(), recentPresentation.getNormalizedPath()); assertEquals(recentPresentation2.getOpenedDateTime(), recentPresentation.getOpenedDateTime()); assertEquals(recentPresentation2.getId(), recentPresentation.getId()); recentPresentation = iterator.next(); assertNotNull(recentPresentation); assertEquals(recentPresentation3.getNormalizedPath(), recentPresentation.getNormalizedPath()); assertEquals(recentPresentation3.getOpenedDateTime(), recentPresentation.getOpenedDateTime()); assertEquals(recentPresentation3.getId(), recentPresentation.getId()); }
@Test public void readWithoutOpenedPresentationDate() { final ByteArrayInputStream input = new ByteArrayInputStream("<slideshowfx><recentPresentations><recentPresentation><id>presentation01</id><file>/presentation01.sfx</file></recentPresentation></recentPresentations></slideshowfx>".getBytes()); final Set<RecentPresentation> recentPresentations = readRecentPresentationFromStream(input); assertNotNull(recentPresentations); assertEquals(0, recentPresentations.size()); }
@Test public void readWithoutPath() { final ByteArrayInputStream input = new ByteArrayInputStream(("<slideshowfx><recentPresentations><recentPresentation><id>presentation01</id><openedDateTime>" + LocalDateTime.now() + "</openedDateTime></recentPresentation></recentPresentations></slideshowfx>").getBytes()); final Set<RecentPresentation> recentPresentations = readRecentPresentationFromStream(input); assertNotNull(recentPresentations); assertEquals(0, recentPresentations.size()); }