Skip to content

Commit

Permalink
initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaWDY authored and lacinoire committed Aug 23, 2023
1 parent 39d1dc7 commit 8776a9a
Show file tree
Hide file tree
Showing 34 changed files with 2,619 additions and 190 deletions.
Binary file added dspot-3.1.1-SNAPSHOT-jar-with-dependencies.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import com.intellij.codeInsight.TestFrameworks;
import com.intellij.execution.lineMarker.RunLineMarkerContributor;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.impl.source.PsiClassImpl;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testIntegration.TestFramework;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.testshift.testcube.icons.TestCubeIcons;
import com.intellij.testIntegration.TestFinderHelper;
import com.intellij.testIntegration.LanguageTestCreators;

import java.util.Objects;
import java.util.*;

public class AmplifyTestMarkerContributor extends RunLineMarkerContributor {

Expand All @@ -41,14 +45,41 @@ public class AmplifyTestMarkerContributor extends RunLineMarkerContributor {
*/
PsiClass containingClass = PsiTreeUtil.getParentOfType(parent, PsiClass.class);
if (!isTestMethod(containingClass, (PsiMethod) parent)) {
// return null;
String targetMethodName = ((PsiMethod)parent).getName();
String targetClassName = Objects.requireNonNull(((PsiMethod) parent).getContainingClass())
.getQualifiedName();
PsiClass targetClass = Objects.requireNonNull(((PsiMethod) parent).getContainingClass());

return new Info(TestCubeIcons.AMPLIFY_TEST, tooltipProvider,
new ShowCFGAction("generate test " +
"cases for '" + element.getText() +"()'", targetMethodName, targetClassName));
// //the selected class
// PsiElement sourceElement = TestFinderHelper.findSourceElement(element);
// //the test class
// Collection<PsiElement> testClasses = ReadAction.compute(() -> TestFinderHelper.findTestsForClass(sourceElement));
// final List<PsiElement> candidates = Collections.synchronizedList(new ArrayList<>());
// candidates.addAll(testClasses);
// PsiClass testClass = null;
// PsiMethod testMethod = null;
// // test class exist
// if(candidates.size()>0) {
// testClass = (PsiClass) candidates.get(0);
// PsiMethod[] testMethods = testClass.getAllMethods();
// // consider use which method as Start or all
// if(testMethods.length>0) {
// testMethod = testMethods[0];
// }
// }

VirtualFile file = parent.getContainingFile().getVirtualFile();
if (file != null) {
VirtualFile moduleRoot = ProjectFileIndex.SERVICE.getInstance(parent.getProject())
.getContentRootForFile(file);
String moduleRootPath;
if (moduleRoot == null) {
moduleRootPath = parent.getProject().getBasePath();
} else {
moduleRootPath = moduleRoot.getPath();
}
return new Info(TestCubeIcons.AMPLIFY_TEST, tooltipProvider,
new ShowCFGAction("generate test " +
"cases for '" + element.getText() +"()'", targetClass,
(PsiMethod)parent, moduleRootPath));
}
}
// test method
String testMethodName = ((PsiMethod) parent).getName();
Expand Down
367 changes: 300 additions & 67 deletions src/main/java/org/testshift/testcube/amplify/ShowCFGAction.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.testshift.testcube.amplify;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import eu.stamp_project.dspot.common.report.output.selector.branchcoverage.json.TestClassBranchCoverageJSON;
import org.jetbrains.annotations.NotNull;
import org.testshift.testcube.branches.AllCoveredDialog;
import org.testshift.testcube.branches.CFGWindow;
import org.testshift.testcube.branches.CodeToUml;
import org.testshift.testcube.branches.NoBranchDialog;
import org.testshift.testcube.icons.TestCubeIcons;
import org.testshift.testcube.misc.Util;

import java.util.Map;
import java.util.Set;

public class ShowCFGCoverageAction extends NotificationAction {
private final Project project;
private final PsiClass targetClass;
private final PsiMethod targetMethod;
private PsiClass testClass;
private String testMethods;
private String moduleRootPath;

public ShowCFGCoverageAction(Project project, PsiClass targetClass, PsiMethod targetMethod, PsiClass testClass,
String testMethods, String moduleRootPath) {
super("Inspect Control Flow Graph with Coverage");
this.project = project;
this.targetClass = targetClass;
this.targetMethod = targetMethod;
this.testClass = testClass;
this.testMethods = testMethods;
this.moduleRootPath = moduleRootPath;
}

@Override
public void update(AnActionEvent e) {
// Set the availability based on whether a project is open
Project project = e.getProject();
e.getPresentation().setEnabledAndVisible(project != null);
}

@Override
public void actionPerformed(@NotNull AnActionEvent event, @NotNull Notification notification) {
TestClassBranchCoverageJSON coverageResult = (TestClassBranchCoverageJSON) Util.getBranchCoverageJSON(project,
testClass.getQualifiedName());
Set<String> initialCoveredLines = Util.getInitialCoveredLine(coverageResult);
Set<Util.Branch> initialCoveredBranches = Util.getInitialCoveredBranch(coverageResult);

String targetMethodText = targetMethod.getBody().getText();
PsiFile containingFile = targetMethod.getContainingFile();
Project project = containingFile.getProject();
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
Document document = psiDocumentManager.getDocument(containingFile);
int textOffset = targetMethod.getBody().getTextOffset();
int lineNumber = document.getLineNumber(textOffset); // this lineNumber +1 = starlinenumber


Map<String, Integer> result = CodeToUml.codeToUml(targetMethodText, lineNumber + 1);
String source = result.keySet().toArray()[0].toString();
int branchNum = result.get(source);

if (branchNum == 0) {
if(initialCoveredLines.isEmpty()) {
NoBranchDialog dialog = new NoBranchDialog();
dialog.pack();
dialog.setVisible(true);
}
else{
AllCoveredDialog dialog = new AllCoveredDialog();
dialog.pack();;
dialog.setVisible(true);
}
}

String targetClassName = targetClass.getQualifiedName();
String targetMethodName = targetMethod.getName();
String testClassName = testClass.getQualifiedName();

CFGWindow cfgWindow = new CFGWindow(project, targetClassName, targetMethodName, source, initialCoveredLines,
initialCoveredBranches, moduleRootPath, testClassName, testMethods,
branchNum);

ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Test Cube");

if (toolWindow != null) {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(cfgWindow.getContent(), cfgWindow.getDisplayName(), false);
content.setCloseable(true);
content.setIcon(TestCubeIcons.AMPLIFY_TEST);

toolWindow.getContentManager().addContent(content);
toolWindow.getContentManager().setSelectedContent(content);

toolWindow.show();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class StartTestCubeAction extends AnAction {
public StartTestCubeAction() {
}

public StartTestCubeAction(@Nullable @Nls(capitalization = Nls.Capitalization.Title) String text,
public StartTestCubeAction(@Nullable /*@Nls(capitalization = Nls.Capitalization.Title)*/ String text,
@NotNull String testClass, @Nullable("null means whole class") String testMethod,
String moduleRootPath) {
super(text, "Improves the selected test case by applying amplification operators", TestCubeIcons.AMPLIFY_TEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.testshift.testcube.branches.AllCoveredDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="189" width="443" height="162"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="10"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="b6aed" class="javax.swing.JLabel" binding="Content">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="The selected method has no branches. All the lines have been covered."/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.testshift.testcube.branches;

import javax.swing.*;
import java.awt.event.*;

public class AllCoveredDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JLabel Content;

public AllCoveredDialog() {
setContentPane(contentPane);
setLocationRelativeTo(null);
setModal(false);

buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});

buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});

// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});

// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}

private void onOK() {
dispose();
}

private void onCancel() {
// add your code here if necessary
dispose();
}

public static void main(String[] args) {
NoBranchDialog dialog = new NoBranchDialog();
dialog.pack();
dialog.setVisible(true);
dialog.setSize(800,400);
}
}
Loading

0 comments on commit 8776a9a

Please sign in to comment.