Skip to content

Commit

Permalink
Rework tests to use factory methods to avoid IDEA tripping over itsel…
Browse files Browse the repository at this point in the history
…f in its hatred of constructor injections (#653)
  • Loading branch information
jshiell committed Oct 19, 2024
1 parent 0b2bfbf commit fd65a84
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# CheckStyle-IDEA Changelog

* **5.96.0** Fixed: Refactored code to avoid IDEA triggering the wrong constructor (#653).
* **5.96.0** New: Added Checkstyle 10.18.2.
* **5.95.0** Fixed: Improved property substitution for code-style importer (#651).
* **5.95.0** New: Added CheckStyle 10.18.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ public ProjectFilePaths(@NotNull final Project project) {
this(project, File.separatorChar, File::getAbsolutePath, project.getService(ProjectPaths.class));
}

public ProjectFilePaths(@NotNull final Project project,
@NotNull final ProjectPaths projectPaths) {
this(project, File.separatorChar, File::getAbsolutePath, projectPaths);
}

public ProjectFilePaths(@NotNull final Project project,
final char separatorChar,
@NotNull final Function<File, String> absolutePathOf,
@NotNull final ProjectPaths projectPaths) {
private ProjectFilePaths(@NotNull final Project project,
final char separatorChar,
@NotNull final Function<File, String> absolutePathOf,
@NotNull final ProjectPaths projectPaths) {
this.project = project;
this.separatorChar = separatorChar;
this.absolutePathOf = absolutePathOf;

this.projectPaths = projectPaths;
}

public static ProjectFilePaths testInstanceWith(@NotNull final Project project,
@NotNull final ProjectPaths projectPaths) {
return new ProjectFilePaths(project, File.separatorChar, File::getAbsolutePath, projectPaths);
}

public static ProjectFilePaths testInstanceWith(@NotNull final Project project,
final char separatorChar,
@NotNull final Function<File, String> absolutePathOf,
@NotNull final ProjectPaths projectPaths) {
return new ProjectFilePaths(project, separatorChar, absolutePathOf, projectPaths);
}

@Nullable
public String makeProjectRelative(@Nullable final String path) {
if (path == null || project.isDefault()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<change-notes>
<![CDATA[
<ul>
<li>5.96.0: Fixed: Refactored code to avoid IDEA triggering the wrong constructor (#653).</li>
<li>5.96.0: New: Added Checkstyle 10.18.2.</li>
<li>5.95.0: Fixed: Improved property substitution for code-style importer (#651).</li>
<li>5.95.0: New: Added Checkstyle 10.18.1.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DescriptorTest {
@Before
public void setUp() {
when(project.getService(ConfigurationLocationFactory.class)).thenReturn(new ConfigurationLocationFactory());
when(project.getService(ProjectFilePaths.class)).thenReturn(new ProjectFilePaths(project, new ProjectPaths()));
when(project.getService(ProjectFilePaths.class)).thenReturn(ProjectFilePaths.testInstanceWith(project, new ProjectPaths()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LegacyProjectConfigurationStateDeserialiserTest {
private final Project project = TestHelper.mockProject();
private final ProjectPaths projectPaths = mock(ProjectPaths.class);
private final ConfigurationLocationFactory configurationLocationFactory = new ConfigurationLocationFactory();
private final ProjectFilePaths projectFilePaths = new ProjectFilePaths(project, File.separatorChar, File::getAbsolutePath, projectPaths);
private final ProjectFilePaths projectFilePaths = ProjectFilePaths.testInstanceWith(project, File.separatorChar, File::getAbsolutePath, projectPaths);

@Before
public void configureMocks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ModuleConfigurationStateTest {
private final PluginConfigurationManager pluginConfigurationManager = new PluginConfigurationManager(project);
private final ProjectPaths projectPaths = mock(ProjectPaths.class);
private final ConfigurationLocationFactory configurationLocationFactory = new ConfigurationLocationFactory();
private final ProjectFilePaths projectFilePaths = new ProjectFilePaths(project, File.separatorChar, File::getAbsolutePath, projectPaths);
private final ProjectFilePaths projectFilePaths = ProjectFilePaths.testInstanceWith(project, File.separatorChar, File::getAbsolutePath, projectPaths);
private final NamedScope allScope = NamedScopeHelper.getScopeByIdWithDefaultFallback(project, "All");

private ConfigurationLocation expectedLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ConfigurationLocationFactoryTest {
@Before
public void setUp() {
when(project.getService(ConfigurationLocationFactory.class)).thenReturn(underTest);
when(project.getService(ProjectFilePaths.class)).thenReturn(new ProjectFilePaths(project, new ProjectPaths()));
when(project.getService(ProjectFilePaths.class)).thenReturn(ProjectFilePaths.testInstanceWith(project, new ProjectPaths()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Object clone() {
@Test
public void testSorting() {
final Project project = TestHelper.mockProject();
when(project.getService(ProjectFilePaths.class)).thenReturn(new ProjectFilePaths(project, new ProjectPaths()));
when(project.getService(ProjectFilePaths.class)).thenReturn(ProjectFilePaths.testInstanceWith(project, new ProjectPaths()));

List<ConfigurationLocation> list = new ArrayList<>();
FileConfigurationLocation fcl = new FileConfigurationLocation(project, "id1", ConfigurationType.LOCAL_FILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private ProjectFilePaths testProjectFilePaths(final char separatorChar, final Pr
return FilenameUtils.separatorsToUnix(file.getPath());
};

return new ProjectFilePaths(project, separatorChar, absolutePathOf, projectPaths);
return ProjectFilePaths.testInstanceWith(project, separatorChar, absolutePathOf, projectPaths);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setUp() {
};

final Project project = TestHelper.mockProject();
ProjectFilePaths testProjectFilePaths = new ProjectFilePaths(project, '/', absolutePathOf, projectPaths);
ProjectFilePaths testProjectFilePaths = ProjectFilePaths.testInstanceWith(project, '/', absolutePathOf, projectPaths);
when(project.getService(ProjectFilePaths.class)).thenReturn(testProjectFilePaths);

when(projectBase.getPath()).thenReturn(PROJECT_BASE_PATH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private ProjectFilePaths projectFilePathsForUnix() {
when(projectBaseFile.getPath()).thenReturn(UNIX_PROJECT_BASE_PATH);
when(projectPaths.projectPath(project)).thenReturn(projectBaseFile);

return new ProjectFilePaths(project, '/', File::getAbsolutePath, projectPaths);
return ProjectFilePaths.testInstanceWith(project, '/', File::getAbsolutePath, projectPaths);
}

private ProjectFilePaths projectFilePathsForWindows() {
Expand All @@ -156,7 +156,7 @@ private ProjectFilePaths projectFilePathsForWindows() {
when(projectBaseFile.getPath()).thenReturn(WINDOWS_PROJECT_BASE_PATH);
when(projectPaths.projectPath(project)).thenReturn(projectBaseFile);

return new ProjectFilePaths(project, '\\', file -> {
return ProjectFilePaths.testInstanceWith(project, '\\', file -> {
// a nasty hack to pretend we're on a Windows box when required...
if (file.getPath().startsWith("c:")) {
return file.getPath().replace('/', '\\').replaceAll("\\\\\\\\", "\\\\");
Expand Down

0 comments on commit fd65a84

Please sign in to comment.