ContextFileWorker { protected static void saveRecentPresentation(final InputStream input, final OutputStream output, final RecentPresentation recentPresentation) throws ContextFileException { final Document document = createDocumentFromInput(input); populateDocumentIfNecessary(document); final Node recentPresentationsNode = getRecentPresentationsNode(document); final Node recentPresentationNode = createNodeFromRecentPresentation(document, recentPresentation); if (recentPresentationNode != null) { recentPresentationsNode.appendChild(recentPresentationNode); writeDocument(document, output); } } 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 saveWithEmptyDocument() throws Exception { final String path = "presentation.sfx"; final LocalDateTime openedDateTime = LocalDateTime.now(); final RecentPresentation recentPresentation = new RecentPresentation(path, openedDateTime); final ByteArrayInputStream input = new ByteArrayInputStream(new byte[0]); final ByteArrayOutputStream output = new ByteArrayOutputStream(); ContextFileWorker.saveRecentPresentation(input, output, recentPresentation); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new ByteArrayInputStream(output.toByteArray())); final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); final String baseExpr = new StringBuilder("/").append(ROOT_TAG).append("/").append(RECENT_PRESENTATIONS_TAG).append("/").append(RECENT_PRESENTATION_TAG).append("[1]").toString(); final String actualFile = (String) xPath.evaluate(baseExpr + "/" + FILE_TAG, document.getDocumentElement(), STRING); final String actualId = (String) xPath.evaluate(baseExpr + "/" + ID_TAG, document.getDocumentElement(), STRING); assertNotNull(actualId); assertEquals(recentPresentation.getId(), actualId); assertNotNull(actualFile); assertEquals(recentPresentation.getNormalizedPath(), actualFile); final LocalDateTime actualOpenedDateTime = LocalDateTime.parse((String) xPath.evaluate(baseExpr + "/" + OPENED_DATE_TIME_TAG, document.getDocumentElement(), STRING)); assertEquals(openedDateTime, actualOpenedDateTime); }
@Test public void saveWithExistingDocument() throws Exception { final RecentPresentation recentPresentation1 = new RecentPresentation("presentation1.sfx", LocalDateTime.now()); final RecentPresentation recentPresentation2 = new RecentPresentation("presentation2.sfx", LocalDateTime.now().plusDays(2)); final String xml = this.createXmlStringFromRecentPresentations(recentPresentation1); final ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes(UTF_8)); final ByteArrayOutputStream output = new ByteArrayOutputStream(); ContextFileWorker.saveRecentPresentation(input, output, recentPresentation2); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new ByteArrayInputStream(output.toByteArray())); final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xPath = xPathFactory.newXPath(); final String baseExpr = new StringBuilder("/").append(ROOT_TAG).append("/").append(RECENT_PRESENTATIONS_TAG).append("/").append(RECENT_PRESENTATION_TAG).append("[2]").toString(); final String actualFile = (String) xPath.evaluate(baseExpr + "/" + FILE_TAG, document.getDocumentElement(), STRING); final String actualId = (String) xPath.evaluate(baseExpr + "/" + ID_TAG, document.getDocumentElement(), STRING); assertNotNull(actualId); assertEquals(recentPresentation2.getId(), actualId); assertNotNull(actualFile); assertEquals(recentPresentation2.getNormalizedPath(), actualFile); final LocalDateTime actualOpenedDateTime = LocalDateTime.parse((String) xPath.evaluate(baseExpr + "/" + OPENED_DATE_TIME_TAG, document.getDocumentElement(), STRING)); assertEquals(recentPresentation2.getOpenedDateTime(), actualOpenedDateTime); }
@Test public void saveWithoutOpenedPresentationDate() throws Exception { final String path = "presentation.sfx"; final RecentPresentation recentPresentation = new RecentPresentation(path, null); final ByteArrayInputStream input = new ByteArrayInputStream(new byte[0]); final ByteArrayOutputStream output = new ByteArrayOutputStream(); ContextFileWorker.saveRecentPresentation(input, output, recentPresentation); assertEquals(0, output.size()); }