Skip to content

Commit

Permalink
Merge pull request #39 from jaksonlin/new-java-version
Browse files Browse the repository at this point in the history
move to java code base
  • Loading branch information
jaksonlin authored Dec 29, 2024
2 parents 5b55df7 + 83e462a commit 86ef470
Show file tree
Hide file tree
Showing 145 changed files with 5,540 additions and 4,444 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pluginRepositoryUrl = https://github.com/jaksonlin/pitest-gradle
pluginVersion = 1.0.3

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 222
pluginSinceBuild = 201
pluginUntilBuild = 243.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/jaksonlin/pitestintellij/MyBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.jaksonlin.pitestintellij;

import com.intellij.DynamicBundle;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.PropertyKey;

import java.util.function.Supplier;

public final class MyBundle extends DynamicBundle {
@NonNls
private static final String BUNDLE = "messages.MyBundle";
private static final MyBundle INSTANCE = new MyBundle();

private MyBundle() {
super(BUNDLE);
}

public static @NotNull String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object... params) {
return INSTANCE.getMessage(key, params);
}

public static @NotNull Supplier<String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object... params) {
return INSTANCE.getLazyMessage(key, params);
}

public static MyBundle getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.jaksonlin.pitestintellij.actions;

import com.github.jaksonlin.pitestintellij.commands.unittestannotations.GenerateAnnotationCommand;
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext;
import com.github.jaksonlin.pitestintellij.util.Pair;
import com.github.jaksonlin.pitestintellij.util.PsiUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

public class GenerateAnnotationCommandAction extends AnAction {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Pair<PsiMethod, PsiClass> psiMethodInfo = PsiUtil.findMethodAtCaret(e); // Call the static method
if (psiMethodInfo == null) {
return;
}
CaseCheckContext context = CaseCheckContext.create(psiMethodInfo.getKey(), psiMethodInfo.getValue());
Project project = e.getProject();
if (project != null) {
GenerateAnnotationCommand command = new GenerateAnnotationCommand(project, context);
command.execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.jaksonlin.pitestintellij.actions;

import com.github.jaksonlin.pitestintellij.commands.unittestannotations.CheckAnnotationCommand;
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext;
import com.github.jaksonlin.pitestintellij.util.Pair;
import com.github.jaksonlin.pitestintellij.util.PsiUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;


public class RunCaseAnnoationCheckAction extends AnAction {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Pair<PsiMethod, PsiClass> psiMethodInfo = PsiUtil.findMethodAtCaret(e);
if (psiMethodInfo == null) {
return;
}
CaseCheckContext context = CaseCheckContext.create(psiMethodInfo.getKey(), psiMethodInfo.getValue());
Project project = e.getProject();
if (project != null) {
CheckAnnotationCommand command = new CheckAnnotationCommand(project, context);
command.execute();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.jaksonlin.pitestintellij.actions;

import com.github.jaksonlin.pitestintellij.services.PitestService;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

public class RunPitestAction extends AnAction {

private final PitestService pitestService;

public RunPitestAction() {
pitestService = ApplicationManager.getApplication().getService(PitestService.class);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project targetProject = e.getProject();
if (targetProject == null) {
return;
}

VirtualFile testVirtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
if (testVirtualFile == null) {
return;
}

pitestService.runPitest(targetProject, testVirtualFile.getPath());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.jaksonlin.pitestintellij.actions;

import com.github.jaksonlin.pitestintellij.commands.unittestannotations.CheckAnnotationCommand;
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

public class RunTestFileAnnoationCheckAction extends AnAction {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
batchCheckAnnotation(e);
}

private void batchCheckAnnotation(@NotNull AnActionEvent e) {
PsiJavaFile psiJavaFile = (PsiJavaFile) e.getData(CommonDataKeys.PSI_FILE);
if (psiJavaFile == null) {
return;
}

psiJavaFile.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitMethod(PsiMethod method) {
super.visitMethod(method);
// inspect the method annotations
PsiAnnotation[] annotations = method.getAnnotations();
for (PsiAnnotation annotation : annotations) {
// inspect the annotation
String annotationName = annotation.getQualifiedName();
if (annotationName != null &&
(annotationName.equals("org.junit.Test") || annotationName.equals("org.junit.jupiter.api.Test") || annotationName.equals("Test"))) {
PsiClass psiClass = PsiTreeUtil.getParentOfType(method, PsiClass.class);
if (psiClass != null) {
CaseCheckContext context = CaseCheckContext.create(method, psiClass);
Project project = e.getProject();
if (project != null) {
new CheckAnnotationCommand(project, context).execute();
break;
}
}
}
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.github.jaksonlin.pitestintellij.annotations;

import org.jetbrains.annotations.Nullable;

public class AnnotationFieldConfig {
private String name;
private AnnotationFieldType type;
private boolean required;
private DefaultValue defaultValue;
@Nullable
private FieldValidation validation;
@Nullable
private ValueProvider valueProvider;

public AnnotationFieldConfig() {
this.required = false;
this.defaultValue = new DefaultValue.NullValue();
}

public AnnotationFieldConfig(String name, AnnotationFieldType type) {
this.name = name;
this.type = type;
this.required = false;
this.defaultValue = new DefaultValue.NullValue();
}

public AnnotationFieldConfig(String name, AnnotationFieldType type, FieldValidation validation) {
this.name = name;
this.type = type;
this.required = false;
this.defaultValue = new DefaultValue.NullValue();
this.validation = validation;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public AnnotationFieldType getType() {
return type;
}

public void setType(AnnotationFieldType type) {
this.type = type;
}

public boolean isRequired() {
return required;
}

public void setRequired(boolean required) {
this.required = required;
}

public DefaultValue getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(DefaultValue defaultValue) {
this.defaultValue = defaultValue;
}

@Nullable
public FieldValidation getValidation() {
return validation;
}

public void setValidation(@Nullable FieldValidation validation) {
this.validation = validation;
}

@Nullable
public ValueProvider getValueProvider() {
return valueProvider;
}

public void setValueProvider(@Nullable ValueProvider valueProvider) {
this.valueProvider = valueProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.jaksonlin.pitestintellij.annotations;

public enum AnnotationFieldType {
STRING,
STRING_LIST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.jaksonlin.pitestintellij.annotations;

import com.github.jaksonlin.pitestintellij.context.UnittestCase;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AnnotationParser {
private final AnnotationSchema schema;
private final AnnotationValidator validator;

public AnnotationParser(AnnotationSchema schema) {
this.schema = schema;
this.validator = new AnnotationValidator(schema);
}

public UnittestCase parseAnnotation(Map<String, Object> annotationValues) {
AnnotationValidator.ValidationResult result = validator.validate(annotationValues);

if (result instanceof AnnotationValidator.ValidationResult.Valid) {
Map<String, Object> parsedValues = new HashMap<>();
for (AnnotationFieldConfig field : schema.getFields()) {
if (field.isRequired()) {
if (!annotationValues.containsKey(field.getName())) {
throw new IllegalArgumentException("Missing required field: " + field.getName());
}
if (annotationValues.get(field.getName()) == null) {
throw new IllegalArgumentException("Required field cannot be null: " + field.getName());
}
} else {
if (!annotationValues.containsKey(field.getName()) || annotationValues.get(field.getName()) == null) {
parsedValues.put(field.getName(), field.getDefaultValue());
continue;
}
}
Object rawValue = annotationValues.get(field.getName());
parsedValues.put(field.getName(), convertValue(rawValue, field));
}
return new UnittestCase(parsedValues);
} else if (result instanceof AnnotationValidator.ValidationResult.Invalid) {
AnnotationValidator.ValidationResult.Invalid invalidResult = (AnnotationValidator.ValidationResult.Invalid) result;
throw new IllegalArgumentException(
"Invalid annotation values:\n" + String.join("\n", invalidResult.getErrors())
);
}
// Should not happen if ValidationResult is properly implemented
throw new IllegalStateException("Unexpected ValidationResult type");
}

@Nullable
private Object convertValue(@Nullable Object value, AnnotationFieldConfig field) {
if (value == null) {
DefaultValue defaultValue = field.getDefaultValue();
if (defaultValue instanceof DefaultValue.StringValue) {
return ((DefaultValue.StringValue) defaultValue).getValue();
} else if (defaultValue instanceof DefaultValue.StringListValue) {
return ((DefaultValue.StringListValue) defaultValue).getValue();
} else if (defaultValue instanceof DefaultValue.NullValue) {
return null;
}
return null; // Should ideally not reach here if DefaultValue is exhaustive
}

switch (field.getType()) {
case STRING:
return value instanceof String ? value : (field.getDefaultValue() instanceof DefaultValue.StringValue ? ((DefaultValue.StringValue) field.getDefaultValue()).getValue() : null);
case STRING_LIST:
if (value instanceof List<?>) {
return ((List<?>) value).stream()
.filter(String.class::isInstance)
.map(String.class::cast)
.collect(Collectors.toList());
} else {
return java.util.Collections.emptyList();
}
default:
return null; // Should not happen if AnnotationFieldType is exhaustive
}
}
}
Loading

0 comments on commit 86ef470

Please sign in to comment.