RemoveMavenModuleHandler implements RemoveModuleHandler { @Override public void onRemoveModule(FolderEntry parentFolder, String modulePath, ProjectConfig moduleConfig) throws ForbiddenException, ConflictException, ServerException { if (!moduleConfig.getTypeId().equals(MavenAttributes.MAVEN_ID)) { logger.warn("Module isn't Maven module"); throw new IllegalArgumentException("Module isn't Maven module"); } VirtualFileEntry pom = parentFolder.getChild("pom.xml"); if (pom == null) { throw new IllegalArgumentException("Can't find pom.xml file in path: " + parentFolder.getPath()); } try { Model model = Model.readFrom(pom.getVirtualFile()); if (model.getModules().contains(modulePath)) { model.removeModule(modulePath); model.writeTo(pom.getVirtualFile()); } } catch (IOException e) { throw new ServerException(e); } }  @Override String getProjectType(); @Override void onRemoveModule(FolderEntry parentFolder, String modulePath, ProjectConfig moduleConfig);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenPomNotFound() throws Exception { String parent = NameGenerator.generate("parent", 5); String module = NameGenerator.generate("module", 5); Project project = projectManager.createProject(workspace, parent, new ProjectConfig(null, MavenAttributes.MAVEN_ID), null, "public"); removeMavenModuleHandler .onRemoveModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, "maven")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenModuleIsNotMavenModule() throws Exception { String parent = NameGenerator.generate("parent", 5); String module = NameGenerator.generate("module", 5); Project project = projectManager.createProject(workspace, parent, new ProjectConfig(null, MavenAttributes.MAVEN_ID), null, "public"); project.getBaseFolder().createFile("pom.xml", String.format(POM_XML_TEMPL, "jar").getBytes(), "text/xml"); removeMavenModuleHandler .onRemoveModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, "notmaven")); }
@Test public void shouldRemoveModule() throws Exception { String parent = NameGenerator.generate("parent", 5); Project project = projectManager.createProject(workspace, parent, new ProjectConfig(null, MavenAttributes.MAVEN_ID), null, "public"); project.getBaseFolder().createFile("pom.xml", String.format(POM_XML_TEMPL, "pom").getBytes(), "text/xml"); removeMavenModuleHandler.onRemoveModule(project.getBaseFolder(), FIRST_MODULE, new ProjectConfig(null, MavenAttributes.MAVEN_ID)); VirtualFileEntry pom = project.getBaseFolder().getChild("pom.xml"); Assert.assertNotNull(pom); ContentStream content = pom.getVirtualFile().getContent(); Assert.assertNotNull(content); InputStream stream = content.getStream(); String pomContent = IoUtil.readStream(stream); Assert.assertNotNull(pomContent); Assert.assertFalse(pomContent.isEmpty()); String firstMavenModule = String.format("<module>%s</module>", FIRST_MODULE); String secondMavenModule = String.format("<module>%s</module>", SECOND_MODULE); Assert.assertFalse(pomContent.contains(firstMavenModule)); Assert.assertTrue(pomContent.contains(secondMavenModule)); }
@Test public void shouldNotRemoveModuleWhenPomNotContainsModule() throws Exception { String parent = NameGenerator.generate("parent", 5); String module = NameGenerator.generate("module", 5); Project project = projectManager.createProject(workspace, parent, new ProjectConfig(null, MavenAttributes.MAVEN_ID), null, "public"); project.getBaseFolder().createFile("pom.xml", String.format(POM_XML_TEMPL, "pom").getBytes(), "text/xml"); removeMavenModuleHandler.onRemoveModule(project.getBaseFolder(), module, new ProjectConfig(null, MavenAttributes.MAVEN_ID)); VirtualFileEntry pom = project.getBaseFolder().getChild("pom.xml"); Assert.assertNotNull(pom); ContentStream content = pom.getVirtualFile().getContent(); Assert.assertNotNull(content); InputStream stream = content.getStream(); String pomContent = IoUtil.readStream(stream); Assert.assertNotNull(pomContent); Assert.assertFalse(pomContent.isEmpty()); String firstMavenModule = String.format("<module>%s</module>", FIRST_MODULE); String secondMavenModule = String.format("<module>%s</module>", SECOND_MODULE); Assert.assertTrue(pomContent.contains(firstMavenModule)); Assert.assertTrue(pomContent.contains(secondMavenModule)); }