### Question:
JpsModelUtil { @Nullable public static JpsModule findModuleForFile(final JpsProject project, final File sourceFile) { final FilePathPrefixTree<JpsModule> ancestorModules = new FilePathPrefixTree<JpsModule>(); for (final JpsModule jpsModule : project.getModules()) { final JpsModuleSourceRoot sourceRoot = findSourceRootForFile(jpsModule, sourceFile); if (sourceRoot != null) { ancestorModules.add(sourceRoot.getFile(), jpsModule); } } final PrefixTree.Node<String, JpsModule> closestModule = ancestorModules.findNearestWithValue(sourceFile); return closestModule != null ? closestModule.getValue() : null; }  static boolean isBuildWithCloverEnabled(@NotNull final JpsProject jpsProject); static boolean isCloverEnabled(@NotNull final JpsProject jpsProject); static boolean isDumpInstrumentedSources(@NotNull final JpsProject jpsProject); @Nullable static CloverPluginConfig getCloverPluginConfig(@NotNull final JpsProject jpsProject); @Nullable static JpsModuleSourceRoot findSourceRootForFile(final @NotNull JpsModule module,
                                                            final @NotNull File sourceFile); @Nullable static JpsModule findModuleForFile(final JpsProject project, final File sourceFile); static Set<File> getExcludedRoots(final JpsModule jpsModule);  }### Answer:
@Test public void testFindModuleForFile_NoModuleFound() { final JpsProject project = new JpsModelImpl(null).getProject(); final JpsModule moduleA = factory.createModule("ModuleA", JpsJavaModuleType.INSTANCE, factory.createDummyElement()); moduleA.addSourceRoot("file: final JpsModule moduleFound = JpsModelUtil.findModuleForFile( project, new File("ModuleXYZ", "Foo.java")); assertNull(moduleFound); }
@Test public void testFindModuleForFile_SiblingModules() { final JpsProject project = new JpsModelImpl(null).getProject(); final JpsModule moduleA = factory.createModule("ModuleA", JpsJavaModuleType.INSTANCE, factory.createDummyElement()); moduleA.addSourceRoot("file: project.addModule(moduleA); final JpsModule moduleB = factory.createModule("ModuleB", JpsJavaModuleType.INSTANCE, factory.createDummyElement()); moduleB.addSourceRoot("file: project.addModule(moduleB); JpsModule actualModule = JpsModelUtil.findModuleForFile(project, new File(new File("ModuleA", "src"), "Foo.java")); assertNotNull(actualModule); assertEquals(moduleA, actualModule); actualModule = JpsModelUtil.findModuleForFile(project, new File(new File("ModuleB", "src"), "Goo.java")); assertNotNull(actualModule); assertEquals(moduleB, actualModule); actualModule = JpsModelUtil.findModuleForFile(project, new File("ModuleXYZ", "Hoo.java")); assertNull(actualModule); }
@Test public void testFindModuleForFile_NestedModules() { final JpsProject project = new JpsModelImpl(null).getProject(); final JpsModule moduleA = factory.createModule("ModuleA", JpsJavaModuleType.INSTANCE, factory.createDummyElement()); moduleA.addSourceRoot("file: project.addModule(moduleA); final JpsModule moduleB = factory.createModule("ModuleB", JpsJavaModuleType.INSTANCE, factory.createDummyElement()); moduleB.addSourceRoot("file: project.addModule(moduleB); JpsModule actualModule = JpsModelUtil.findModuleForFile(project, new File(new File("ModuleA", "src"), "Foo.java")); assertNotNull(actualModule); assertEquals(moduleA, actualModule); actualModule = JpsModelUtil.findModuleForFile(project, new File(new File(new File(new File("ModuleA", "src"), "ModuleB"), "src"), "Goo.java")); assertNotNull(actualModule); assertEquals(moduleB, actualModule); actualModule = JpsModelUtil.findModuleForFile(project, new File("ModuleXYZ", "Hoo.java")); assertNull(actualModule); }