Skip to content

Commit

Permalink
Implement choice of joining strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
aykborstelmann committed Apr 19, 2024
1 parent c17fa0a commit 1b6702d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 42 deletions.
21 changes: 21 additions & 0 deletions src/main/java/de/cronn/validation_files_diff/JoiningStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.cronn.validation_files_diff;

public enum JoiningStrategy {
UNDERSCORE("_"),
DIRECTORY("/");

private final String delimiter;

JoiningStrategy(String delimiter) {
this.delimiter = delimiter;
}

public String join(String firstString, String secondString) {
if (secondString.startsWith(this.delimiter)) {
return firstString + secondString;
}

return firstString + this.delimiter + secondString;
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package de.cronn.validation_files_diff;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.MouseInputAdapter;

import org.jetbrains.annotations.NotNull;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.components.JBTextField;

import de.cronn.validation_files_diff.helper.DiffSide;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

public class ValidationDiffSettingsConfigurableGUI {
private final ValidationDiffApplicationOptionsProvider applicationSettings;
Expand Down Expand Up @@ -79,10 +76,10 @@ private static Color getDefaultValueColor() {
@NotNull
private static Color findColorByKey(String... colorKeys) {
return Arrays.stream(colorKeys)
.map(UIManager::getColor)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new RuntimeException("Can't find color for keys " + Arrays.toString(colorKeys)));
.map(UIManager::getColor)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new RuntimeException("Can't find color for keys " + Arrays.toString(colorKeys)));
}

private static <T> T getSelectedItemFromJComboBox(JComboBox<T> jComboBox) {
Expand Down Expand Up @@ -182,12 +179,12 @@ boolean isModified() {
return false;
}
return !(projectSettings.getRelativeOutputDirPath().equals(outputDirPath.getText())
&& projectSettings.getRelativeValidationDirPath().equals(validationDirPath.getText())
&& applicationSettings.getOutputSide().equals(getSelectedItemFromJComboBox(outputSide))
&& applicationSettings.getShowNewOnSource() == showNewSrc.isSelected()
&& applicationSettings.getShowDifferent() == showDifference.isSelected()
&& applicationSettings.getShowEqual() == showEqual.isSelected()
&& applicationSettings.getShowNewOnTarget() == showNewTarget.isSelected());
&& projectSettings.getRelativeValidationDirPath().equals(validationDirPath.getText())
&& applicationSettings.getOutputSide().equals(getSelectedItemFromJComboBox(outputSide))
&& applicationSettings.getShowNewOnSource() == showNewSrc.isSelected()
&& applicationSettings.getShowDifferent() == showDifference.isSelected()
&& applicationSettings.getShowEqual() == showEqual.isSelected()
&& applicationSettings.getShowNewOnTarget() == showNewTarget.isSelected());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import de.cronn.validation_files_diff.JoiningStrategy;
import de.cronn.validation_files_diff.ValidationDiffProjectOptionsProvider;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -47,34 +51,64 @@ public List<VirtualFile> findCorrespondingValidationFiles() {
return Collections.emptyList();
}

String validationFilePrefix = parseValidationFilePrefix();
List<String> validationFilePathPrefixes = Arrays.stream(JoiningStrategy.values()).map(this::parseValidationFilePrefix).toList();

return getValidationFileRelatedDirectories()
.map(moduleRoot::findFileByRelativePath)
.filter(Objects::nonNull)
.map(VirtualFile::getChildren)
.flatMap(Arrays::stream)
.filter(validationFile -> fileNameStartsWith(validationFile, validationFilePrefix))
.flatMap(rootDir -> streamMatchingValidationFiles(rootDir, validationFilePathPrefixes))
.toList();
}

private static Stream<VirtualFile> streamMatchingValidationFiles(VirtualFile rootDir, List<String> expectedValidationFilePathPrefixes) {
Stream.Builder<VirtualFile> streamBuilder = Stream.builder();

ContentIterator virtualFileVisitor = file -> {
if (file.isDirectory()) {
return true;
}

Path relativizedValidationFilePath = rootDir.toNioPath().relativize(file.toNioPath());
for (String expectedValidationFilePathPrefix : expectedValidationFilePathPrefixes) {
if (isValidationFileMatchingPathPrefix(relativizedValidationFilePath, expectedValidationFilePathPrefix)) {
streamBuilder.add(file);
}
}

return true;
};

VfsUtilCore.iterateChildrenRecursively(rootDir, null, virtualFileVisitor);

return streamBuilder.build();
}

private static boolean isValidationFileMatchingPathPrefix(Path actualValidationFilePath, String expectedValidationFilePathPrefixString) {
Path actualValidationFileParent = actualValidationFilePath.getParent();
String actualValidationFilename = actualValidationFilePath.getFileName().toString();

Path expectedValidationFilePathPrefix = Paths.get(expectedValidationFilePathPrefixString);
Path expectedValidationFileParent = expectedValidationFilePathPrefix.getParent();
String expectedValidationFilenamePrefix = expectedValidationFilePathPrefix.getFileName().toString();

boolean parentsAreEqual = Objects.equals(actualValidationFileParent, expectedValidationFileParent);
boolean filenameStartsWithPrefix = actualValidationFilename.startsWith(expectedValidationFilenamePrefix);
return parentsAreEqual && filenameStartsWithPrefix;
}

private Stream<String> getValidationFileRelatedDirectories() {
ValidationDiffProjectOptionsProvider options = ValidationDiffProjectOptionsProvider.getInstance(project);
return Stream.of(options.getRelativeOutputDirPath(), options.getRelativeValidationDirPath());
}

private boolean fileNameStartsWith(VirtualFile file, String prefix) {
return file.getName().startsWith(prefix);
}

private String parseValidationFilePrefix() {
private String parseValidationFilePrefix(JoiningStrategy joiningStrategy) {
if (element instanceof PsiMethod psiMethod) {
PsiClass psiClass = psiMethod.getContainingClass();
return PsiTestNameUtils.getTestName(psiClass, psiMethod);
return PsiTestNameUtils.getTestName(psiClass, psiMethod, joiningStrategy);
}

if (element instanceof PsiClass psiClass) {
return PsiTestNameUtils.getTestClassName(psiClass);
return PsiTestNameUtils.getTestClassName(psiClass, joiningStrategy);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import de.cronn.validation_files_diff.JoiningStrategy;

public class PsiTestNameUtils {

private PsiTestNameUtils() {
}

public static String getTestName(PsiClass psiClass, PsiMethod psiMethod) {
return join(getTestClassName(psiClass), psiMethod.getName());
public static String getTestName(PsiClass psiClass, PsiMethod psiMethod, JoiningStrategy joiningStrategy) {
return joiningStrategy.join(getTestClassName(psiClass, joiningStrategy), psiMethod.getName());
}

public static String getTestClassName(PsiClass psiClass) {
public static String getTestClassName(PsiClass psiClass, JoiningStrategy joiningStrategy) {
String nestingHierarchy = psiClass.getName();
PsiClass enclosingClass = psiClass.getContainingClass();
while (enclosingClass != null) {
nestingHierarchy = join(enclosingClass.getName(), nestingHierarchy);
nestingHierarchy = joiningStrategy.join(enclosingClass.getName(), nestingHierarchy);
enclosingClass = enclosingClass.getContainingClass();
}
return nestingHierarchy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ public void testRenameMethod_renamesAllValidationFiles() throws IOException {
assertValidationFileNotExistsDirectories(projectDir, validationFileName);
}

public void testRenameMethod_withDirectoryJoiningStrategy_renamesOnlyApplicableValidationFiles() throws IOException {
Path projectDir = createDefaultJavaModuleStructure();

Path openedTestFile = projectDir.resolve("src/test/Test.java");
Files.createFile(openedTestFile);
Files.writeString(openedTestFile, "public class Test {\n" +
"\tpublic void testSomething() {" +
"}\n" +
"}");

Path validationFileDirectory = getValidationFileDirectory(projectDir);
Path outputFileDirectory = getOutputFileDirectory(projectDir);
Path tempFileDirectory = getTempFileDirectory(projectDir);

Files.createDirectories(validationFileDirectory);
Files.createDirectories(outputFileDirectory);
Files.createDirectories(tempFileDirectory);

String validationFileName = "Test/testSomething.json";
String newValidationFileName = "Test/testSomething2.json";

createValidationFileInDirectories(projectDir, validationFileName);

refreshFileSystem(projectDir);

PsiMethod testMethod = JavaPsiFacadeEx.getInstanceEx(getProject()).findClass("Test").findMethodsByName("testSomething", false)[0];

executeRename(testMethod, "testSomething2");

assertValidationFileNotExistsDirectories(projectDir, validationFileName);
assertValidationFileExistsInDirectories(projectDir, newValidationFileName);
}

@NotNull
private static Path getTempFileDirectory(Path projectDir) {
return projectDir.resolve(TEST_TEMPORARY_DATA_DIR);
Expand Down Expand Up @@ -111,9 +144,14 @@ private static void assertValidationFileExistsInDirectories(Path projectDir, Str
}

private static void createValidationFileInDirectories(Path projectDir, String validationFileName) throws IOException {
Files.createFile(getValidationFileDirectory(projectDir).resolve(validationFileName));
Files.createFile(getOutputFileDirectory(projectDir).resolve(validationFileName));
Files.createFile(getTempFileDirectory(projectDir).resolve(validationFileName));
createDirectoryAndFileIfNecessary(getValidationFileDirectory(projectDir).resolve(validationFileName));
createDirectoryAndFileIfNecessary(getOutputFileDirectory(projectDir).resolve(validationFileName));
createDirectoryAndFileIfNecessary(getTempFileDirectory(projectDir).resolve(validationFileName));
}

private static void createDirectoryAndFileIfNecessary(Path filename) throws IOException {
Files.createDirectories(filename.getParent());
Files.createFile(filename);
}

private void refreshFileSystem(Path projectDir) {
Expand Down

0 comments on commit 1b6702d

Please sign in to comment.