ArtifactsBasedDataStoreService implements DataStoreService<HighlightData> { @Override public void saveData(@NotNull SBuild sBuild, HighlightData data) { List<HighlightData> currentData = loadData(sBuild); boolean found = false; for (int i=0; i<currentData.size(); i++) { HighlightData cur = currentData.get(i); if (cur.getKey().equals(data.getKey())) { currentData.set(i, data); found = true; break; } } if (!found) { currentData.add(data); } final StringWriter writer = new StringWriter(); getXStream().marshal(currentData, new CompactWriter(writer)); String serialized = writer.toString(); final File filePath = getDataFilePath(sBuild); final File parentFile = filePath.getParentFile(); if (!parentFile.isDirectory()) { if (!parentFile.mkdirs()) { LOG.warning("Could not create directory for highlighter plugin data: " + parentFile.getAbsolutePath()); } } try { final File newFile = new File(filePath.getParent(), filePath.getName() + ".new"); FileUtil.writeFile(newFile, serialized, "UTF-8"); FileUtil.rename(newFile, filePath); } catch (Exception e) { LOG.warning("Could not save highlighter plugin data under the build artifacts directory: " + filePath.getAbsolutePath() + ", error: " + e.toString()); } }  @Override void saveData(@NotNull SBuild sBuild, HighlightData data); @Override List<HighlightData> loadData(@NotNull SBuild sBuild); final static String DATA_FILE_PATH; }
@Test public void testSaveData() throws Exception { when(highlightData.getKey()).thenReturn("Some title"); when(highlightData.getSingleValue()).thenReturn("Some text"); when(highlightData.getLevel()).thenReturn(Level.error); when(highlightData.getBlock()).thenReturn(Block.collapsed); dataStoreService.saveData(sBuild, highlightData); File highlighterXml = new File(buildArtifactsDir, ArtifactsBasedDataStoreService.DATA_FILE_PATH); assertTrue(highlighterXml.isFile()); final Collection<HighlightData> data = dataStoreService.loadData(sBuild); assertEquals(1, data.size()); final HighlightData loadedData = data.iterator().next(); assertEquals("Some title", loadedData.getKey()); assertEquals("Some text", loadedData.getSingleValue()); }