@Override public Path save(final Path pomPath, final POM pom, final Metadata metadata, final String comment) { return save(pomPath, pom, metadata, comment, false); }
@Override public Path save(final Path path, final POM content, final Metadata metadata, final String comment, final boolean updateModules) { try { ioService.startBatch(new FileSystem[]{Paths.convert(path).getFileSystem()}, optionsFactory.makeCommentedOption(comment != null ? comment : "")); save(path, content, metadata); saveSubModules(path, content, updateModules); return path; } catch (Exception e) { throw ExceptionUtilities.handleException(e); } finally { ioService.endBatch(); } }
@Override public Path save(final Path pomPath, final POM pom, final Metadata metadata, final String comment) { return save(pomPath, pom, metadata, comment, false); }
@Override public Path save(final Path path, final POM content, final Metadata metadata, final String comment) { try { return save(path, content, metadata); } catch (Exception e) { throw ExceptionUtilities.handleException(e); } }
private Path save(final Path path, final POM content, final Metadata metadata) throws IOException, XmlPullParserException { if (metadata == null) { save(path, content); } else { ioService.write(Paths.convert(path), pomContentHandler.toString(content, loadPomXMLString(path)), metadataService.setUpAttributes(path, metadata)); } return path; }
@Test public void whenProjectRenamed_ModuleUpdatedEventIsFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); service.save(path, new POM("newName", "desctiption", "url", new GAV()), mock(Metadata.class), ""); verify(moduleUpdatedEvent).fire(any()); } }
@Test public void whenProjectRenamed_ModuleUpdatedEventIsFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); service.save(path, new POM("newName", "desctiption", "url", new GAV()), mock(Metadata.class), ""); verify(moduleUpdatedEvent).fire(any()); } }
@Test public void whenProjectSavedWithoutChanges_ModuleUpdatedEventNotFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); POM originalPOM = new POM("mymodule", "desctiption", "url", new GAV()); service.save(path, originalPOM, mock(Metadata.class), ""); verify(ioService).startBatch(any(), any()); verify(ioService).write(pathArgumentCaptor.capture(), anyString(), anyMap()); verify(ioService).endBatch(); verify(moduleUpdatedEvent, never()).fire(any()); assertTrue(path.toURI().endsWith(pathArgumentCaptor.getValue().toString())); }
@Test public void whenProjectSubModulesEdited_UpdateModuleEventIsFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); doReturn(true).when(ioService).exists(any()); final POM pom = new POM("newName", "desctiption", "url", new GAV()); pom.setPackaging("pom"); final ArrayList<String> modules = new ArrayList<>(); modules.add("module1"); modules.add("module2"); pom.setModules(modules); service.save(path, pom, mock(Metadata.class), "", true); verify(moduleUpdatedEvent, times(3)).fire(any()); } }
@Test public void whenProjectSavedWithoutChanges_ModuleUpdatedEventNotFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); POM originalPOM = new POM("mymodule", "desctiption", "url", new GAV()); service.save(path, originalPOM, mock(Metadata.class), ""); verify(ioService).startBatch(any(), any()); verify(ioService).write(pathArgumentCaptor.capture(), anyString(), anyMap()); verify(ioService).endBatch(); verify(moduleUpdatedEvent, never()).fire(any()); assertTrue(path.toURI().endsWith(pathArgumentCaptor.getValue().toString())); }
@Test public void whenProjectSubModulesEdited_UpdateModuleEventIsFired() throws Exception { final Path path = testFileSystem.createTempFile("testproject/pom.xml"); doReturn(true).when(ioService).exists(any()); final POM pom = new POM("newName", "desctiption", "url", new GAV()); pom.setPackaging("pom"); final ArrayList<String> modules = new ArrayList<>(); modules.add("module1"); modules.add("module2"); pom.setModules(modules); service.save(path, pom, mock(Metadata.class), "", true); verify(moduleUpdatedEvent, times(3)).fire(any()); } }
private void saveSubModules(final Path path, final POM content, final boolean updateModules) throws IOException, XmlPullParserException { if (updateModules && content.isMultiModule() && content.getModules() != null) { for (String module : content.getModules()) { org.uberfire.java.nio.file.Path childPath = Paths.convert(path).getParent().resolve(module).resolve("pom.xml"); if (ioService.exists(childPath)) { POM child = load(Paths.convert(childPath)); if (child != null) { child.setParent(content.getGav()); child.getGav().setGroupId(content.getGav().getGroupId()); child.getGav().setVersion(content.getGav().getVersion()); save(Paths.convert(childPath), child); } } } } }