AddMavenModuleHandler implements CreateModuleHandler { @Override public void onCreateModule(FolderEntry parentFolder, String modulePath, ProjectConfig moduleConfig, Map<String, String> options) throws ForbiddenException, ConflictException, ServerException { if (!moduleConfig.getTypeId().equals(MavenAttributes.MAVEN_ID)) { LOG.warn("Module must be Maven project to able be added to Maven project"); throw new IllegalArgumentException("Module must be Maven project to able be added to Maven project"); } 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 ("pom".equals(model.getPackaging())) { final String relativePath = modulePath.substring(parentFolder.getPath().length() + 1); if (!model.getModules().contains(relativePath)) { model.addModule(relativePath); model.writeTo(pom.getVirtualFile()); } } else { throw new IllegalArgumentException("Project must have packaging 'pom' in order to adding modules."); } } catch (IOException e) { throw new ServerException(e); } }  @Override void onCreateModule(FolderEntry parentFolder, String modulePath, ProjectConfig moduleConfig, Map<String, String> options); @Override String getProjectType();  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAddModuleIfNotPomPackage() 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"); addMavenModuleHandler .onCreateModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, "maven"), Collections.<String, String>emptyMap()); }
@Test(expected = IllegalArgumentException.class) public void pomNotFound() 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"); addMavenModuleHandler .onCreateModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, "maven"), Collections.<String, String>emptyMap()); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAddModuleIfModuleNotMaven() throws Exception { ProjectType notMaven = Mockito.mock(ProjectType.class); Mockito.when(notMaven.getId()).thenReturn("notMaven"); Mockito.when(notMaven.getDisplayName()).thenReturn("notMaven"); Mockito.when(notMaven.canBePrimary()).thenReturn(true); projectTypeRegistry.registerProjectType(notMaven); 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"); addMavenModuleHandler.onCreateModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, notMaven.getId()), Collections.<String, String>emptyMap()); }
@Test public void addModuleOk() 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"); addMavenModuleHandler.onCreateModule(project.getBaseFolder(), project.getPath() + "/" + module, new ProjectConfig(null, MavenAttributes.MAVEN_ID), Collections.<String, String>emptyMap()); 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 mavenModule = String.format("<module>%s</module>", module); Assert.assertTrue(pomContent.contains(mavenModule)); }