JavaModuleDependency implements Serializable { @Nullable public static JavaModuleDependency copy(IdeaProject project, IdeaModuleDependency original) { IdeaModule targetModule = null; for (IdeaModule module : project.getModules()) { if (module.getName().equals(original.getTargetModuleName())) { targetModule= module; } } if (targetModule != null && isNotEmpty(targetModule.getName())) { String scope = null; IdeaDependencyScope originalScope = original.getScope(); if (originalScope != null) { scope = originalScope.getScope(); } GradleProject gradleProject = targetModule.getGradleProject(); File projectFolder; try { projectFolder = gradleProject.getProjectIdentifier().getBuildIdentifier().getRootDir(); } catch (UnsupportedMethodException ex) { GradleProject rootGradleProject = gradleProject; while (rootGradleProject.getParent() != null) { rootGradleProject = rootGradleProject.getParent(); } projectFolder = rootGradleProject.getProjectDirectory(); } String moduleId = createUniqueModuleId(projectFolder, gradleProject.getPath()); return new JavaModuleDependency(targetModule.getName(), moduleId, scope, original.getExported()); } return null; } @PropertyMapping({ "myModuleName", "myModuleId", "myScope", "myExported" })  JavaModuleDependency(@NotNull String moduleName, @NotNull String moduleId, @Nullable String scope, boolean exported); @Nullable static JavaModuleDependency copy(IdeaProject project, IdeaModuleDependency original); @NotNull String getModuleName(); @NotNull String getModuleId(); @Nullable String getScope(); boolean isExported(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testCopyWithNullIdeaModule() { expect(myIdeaProject.getModules()).andReturn(ImmutableDomainObjectSet.of(ImmutableList.of())); replay(myOriginalDependency, myIdeaModule, myIdeaProject); assertNull(JavaModuleDependency.copy(myIdeaProject, myOriginalDependency)); verify(myOriginalDependency, myIdeaModule); }
@Test public void testCopyWithNullModuleName() { expect(myIdeaProject.getModules()).andReturn(ImmutableDomainObjectSet.of(ImmutableList.of())); expect(myIdeaModule.getName()).andStubReturn(null); replay(myOriginalDependency, myIdeaModule, myIdeaProject); assertNull(JavaModuleDependency.copy(myIdeaProject, myOriginalDependency)); verify(myOriginalDependency, myIdeaModule); }
@Test public void testCopyWithEmptyModuleName() { expect(myIdeaProject.getModules()).andReturn(ImmutableDomainObjectSet.of(ImmutableList.of())); expect(myIdeaModule.getName()).andStubReturn(""); replay(myOriginalDependency, myIdeaModule, myIdeaProject); assertNull(JavaModuleDependency.copy(myIdeaProject, myOriginalDependency)); verify(myOriginalDependency, myIdeaModule); }
@Test public void testCopy() { String moduleName = "lib"; IdeaDependencyScope scope = createMock(IdeaDependencyScope.class); GradleProject gradleProject = mock(GradleProject.class); ProjectIdentifier projectIdentifier = mock(ProjectIdentifier.class); BuildIdentifier buildIdentifier = mock(BuildIdentifier.class); when(buildIdentifier.getRootDir()).thenReturn(new File(Jimfs.newFileSystem().getPath("mock", "project").toString())); when(projectIdentifier.getBuildIdentifier()).thenReturn(buildIdentifier); when(gradleProject.getProjectIdentifier()).thenReturn(projectIdentifier); when(gradleProject.getPath()).thenReturn(":lib"); expect(myIdeaModule.getName()).andStubReturn(moduleName); expect(myIdeaModule.getGradleProject()).andStubReturn(gradleProject); expect(myOriginalDependency.getScope()).andStubReturn(scope); expect(myOriginalDependency.getTargetModuleName()).andReturn(moduleName); expect(myOriginalDependency.getExported()).andStubReturn(true); expect(scope.getScope()).andStubReturn("compile"); DomainObjectSet listOfModules = ImmutableDomainObjectSet.of(ImmutableList.of(myIdeaModule)); expect(myIdeaProject.getModules()).andReturn(listOfModules); replay(myOriginalDependency, myIdeaModule, myIdeaProject, scope); JavaModuleDependency copy = JavaModuleDependency.copy(myIdeaProject, myOriginalDependency); assertNotNull(copy); assertEquals(moduleName, copy.getModuleName()); assertSame("compile", copy.getScope()); assertEquals("mock" + File.separator + "project::lib", copy.getModuleId()); assertTrue(copy.isExported()); verify(myOriginalDependency, myIdeaModule, scope); }