Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
 * use java.nio APIs for file manipulations in UpToDateCheckingTest
 * simplify index file creation in NoopCheckerTest
 * annotate getter's return type with `@Nullable`
  • Loading branch information
lutovich committed Jan 6, 2022
1 parent 16c67bc commit 0a94634
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
class FileIndexConfig {
private final MavenProject project;
private final PluginFingerprint pluginFingerprint;

private final Path indexFile;

FileIndexConfig(MavenProject project, Path indexFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.File;
import java.nio.file.Path;

import javax.annotation.Nullable;

import org.apache.maven.plugins.annotations.Parameter;

public class UpToDateChecking {
Expand All @@ -32,6 +34,7 @@ public boolean isEnabled() {
return enabled;
}

@Nullable
public Path getIndexFile() {
return indexFile == null ? null : new File(indexFile).toPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ class NoopCheckerTest extends ResourceHarness {
private Path existingSourceFile;
private Path nonExistingSourceFile;

private Path getIndexFile() {
return project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index");
}

@BeforeEach
void beforeEach() throws Exception {
project = buildMavenProject();
indexFile = new FileIndexConfig(project, getIndexFile()).getIndexFile();
indexFile = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index");
existingSourceFile = project.getBasedir().toPath().resolve("existing.txt");
Files.write(existingSourceFile, "foo".getBytes(UTF_8), CREATE_NEW);
nonExistingSourceFile = project.getBasedir().toPath().resolve("non-existing.txt");
Expand All @@ -65,12 +61,12 @@ void beforeEach() throws Exception {
@Test
void deletesExistingIndexFileWhenCreated() {
Log log = mock(Log.class);
try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, getIndexFile(), singletonList(dummyFormatter()), log)) {
try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, indexFile, singletonList(dummyFormatter()), log)) {
realChecker.setUpToDate(existingSourceFile);
}
assertThat(indexFile).exists();

try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) {
try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) {
assertThat(noopChecker).isNotNull();
}
assertThat(indexFile).doesNotExist();
Expand All @@ -82,7 +78,7 @@ void doesNothingWhenIndexFileDoesNotExist() {
assertThat(indexFile).doesNotExist();

Log log = mock(Log.class);
try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), log)) {
try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) {
assertThat(noopChecker).isNotNull();
}
assertThat(indexFile).doesNotExist();
Expand All @@ -91,7 +87,7 @@ void doesNothingWhenIndexFileDoesNotExist() {

@Test
void neverUpToDate() {
try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, getIndexFile(), mock(Log.class))) {
try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, mock(Log.class))) {
assertThat(noopChecker.isUpToDate(existingSourceFile)).isFalse();
assertThat(noopChecker.isUpToDate(nonExistingSourceFile)).isFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void disableUpToDateChecking() throws Exception {

@Test
void enableUpToDateCheckingCustomIndexFile() throws Exception {
Path tempDirectory = Files.createTempDirectory("index-files");
Path tempDirectory = newFolder("index-files").toPath();
Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index");
assertThat(indexFile.getParent()).doesNotExist();
assertThat(indexFile).doesNotExist();
Expand All @@ -86,10 +86,10 @@ void enableUpToDateCheckingCustomIndexFile() throws Exception {

@Test
void disableUpToDateCheckingCustomIndexFile() throws Exception {
Path tempDirectory = Files.createTempDirectory("index-files");
Path tempDirectory = newFolder("index-files").toPath();
Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index");
indexFile.toFile().getParentFile().mkdirs();
indexFile.toFile().createNewFile();
Files.createDirectories(indexFile.getParent());
Files.createFile(indexFile);
assertThat(indexFile.getParent()).exists();
assertThat(indexFile).exists();
writePomWithUpToDateCheckingEnabledIndexFile(false, tempDirectory + "/${project.groupId}/${project.artifactId}.index");
Expand Down

0 comments on commit 0a94634

Please sign in to comment.