Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user check dialog #28

Merged
merged 16 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;

import java.util.ArrayList;
import java.util.List;

public class TestMeGeneratorJunit4PowerMockTest extends TestMeGeneratorTestBase {

Expand Down Expand Up @@ -39,4 +43,18 @@ public void testRenderInternalMethodCallStubsIgnored() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

public void testFileTemplateCustomization() {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setOpenCustomizeTestDialog(true);
final FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(testMeConfig);
List<String> selectedFieldNameList = new ArrayList<>();
selectedFieldNameList.add("result");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, project jdk version has bean upgraded in recent years. can use List.of()

selectedFieldNameList.add("techFighter");
List<String> selectedMethodIdList = new ArrayList<>();
selectedMethodIdList.add("com.example.services.impl.Foo#fight(com.example.foes.Fire,java.lang.String)");
final FileTemplateCustomization customization =
new FileTemplateCustomization(selectedFieldNameList, selectedMethodIdList, true);
doTest(fileTemplateConfig, customization);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;

import java.util.ArrayList;
import java.util.List;

/**
* Date: 10/20/2016
Expand Down Expand Up @@ -192,6 +196,20 @@ public void testMockFieldsInDiWithCtor() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

public void testFileTemplateCustomization() {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setOpenCustomizeTestDialog(true);
final FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(testMeConfig);
List<String> selectedFieldNameList = new ArrayList<>();
selectedFieldNameList.add("result");
selectedFieldNameList.add("techFighter");
List<String> selectedMethodIdList = new ArrayList<>();
selectedMethodIdList.add("com.example.services.impl.Foo#fight(com.example.foes.Fire,java.lang.String)");
final FileTemplateCustomization customization =
new FileTemplateCustomization(selectedFieldNameList, selectedMethodIdList, true);
doTest(fileTemplateConfig, customization);
}

//todo TC - use static init method when constructor not available

// TODO TC different test target dir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.weirddev.testme.intellij.generator;

import com.weirddev.testme.intellij.configuration.TestMeConfig;
import com.weirddev.testme.intellij.configuration.TestMeConfigPersistent;
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;

import java.util.ArrayList;
import java.util.List;

/**
* Date: 13/12/2016
Expand Down Expand Up @@ -48,4 +53,18 @@ public void testMockFieldsInDiWithSetter() {
public void testMockFieldsInDiWithCtor() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

public void testFileTemplateCustomization() {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setOpenCustomizeTestDialog(true);
final FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(testMeConfig);
List<String> selectedFieldNameList = new ArrayList<>();
selectedFieldNameList.add("result");
selectedFieldNameList.add("techFighter");
List<String> selectedMethodIdList = new ArrayList<>();
selectedMethodIdList.add("com.example.services.impl.Foo#fight(com.example.foes.Fire,java.lang.String)");
final FileTemplateCustomization customization =
new FileTemplateCustomization(selectedFieldNameList, selectedMethodIdList, true);
doTest(fileTemplateConfig, customization);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.LightProjectDescriptor;
import com.weirddev.testme.intellij.configuration.TestMeConfig;
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.weirddev.testme.intellij.generator;

import com.weirddev.testme.intellij.configuration.TestMeConfig;
import com.weirddev.testme.intellij.configuration.TestMeConfigPersistent;
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;

import java.util.ArrayList;
import java.util.List;

/**
* Date: 24/02/2017
Expand Down Expand Up @@ -41,4 +46,18 @@ public void testMockFieldsInDiWithSetter() {
public void testMockFieldsInDiWithCtor() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

public void testFileTemplateCustomization() {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setOpenCustomizeTestDialog(true);
final FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(testMeConfig);
List<String> selectedFieldNameList = new ArrayList<>();
selectedFieldNameList.add("result");
selectedFieldNameList.add("techFighter");
List<String> selectedMethodIdList = new ArrayList<>();
selectedMethodIdList.add("com.example.services.impl.Foo#fight(com.example.foes.Fire,java.lang.String)");
final FileTemplateCustomization customization =
new FileTemplateCustomization(selectedFieldNameList, selectedMethodIdList, true);
doTest(fileTemplateConfig, customization);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.FileTemplateContext;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Properties;
import java.util.*;

/**
* Date: 13/12/2016
Expand Down Expand Up @@ -78,6 +77,10 @@ protected void doTest(FileTemplateConfig fileTemplateConfig) {
doTest("com.example.services.impl", "Foo", "FooTest", fileTemplateConfig);
}

protected void doTest(FileTemplateConfig fileTemplateConfig, FileTemplateCustomization fileTemplateCustomization) {
doTest("com.example.services.impl", "Foo", "FooTest", fileTemplateConfig, fileTemplateCustomization);
}

protected void doTest(final String packageName, String testSubjectClassName, final String expectedTestClassName, final boolean reformatCode, final boolean optimizeImports, final boolean replaceFqn, final boolean ignoreUnusedProperties, final int minPercentOfExcessiveSettersToPreferDefaultCtor, boolean stubMockMethodCallsReturnValues) {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setGenerateTestsForInheritedMethods(true);
Expand All @@ -97,7 +100,13 @@ protected void doTest(final String packageName, String testSubjectClassName, fin
doTest(packageName, testSubjectClassName, expectedTestClassName, fileTemplateConfig);
}

protected void doTest(final String packageName, String testSubjectClassName, final String expectedTestClassName, final FileTemplateConfig fileTemplateConfig) {
protected void doTest(final String packageName, String testSubjectClassName, final String expectedTestClassName,
final FileTemplateConfig fileTemplateConfig) {
doTest(packageName, testSubjectClassName, expectedTestClassName, fileTemplateConfig, null);
}

protected void doTest(final String packageName, String testSubjectClassName, final String expectedTestClassName,
final FileTemplateConfig fileTemplateConfig, final FileTemplateCustomization fileTemplateCustomization) {
if (!testEnabled) {
System.out.println(expectedTestClassExtension+ " idea plugin disabled. Skipping test");
return;
Expand All @@ -106,17 +115,21 @@ protected void doTest(final String packageName, String testSubjectClassName, fin
final PsiDirectory srcDir = fooClass.getContainingFile().getContainingDirectory();
final PsiPackage targetPackage = JavaDirectoryService.getInstance().getPackage(srcDir);

FileTemplateCustomization customization = null != fileTemplateCustomization ? fileTemplateCustomization
: new FileTemplateCustomization(new ArrayList<>(), new ArrayList<>(), false);

CommandProcessor.getInstance().executeCommand(getProject(), () -> {
myFixture.openFileInEditor(fooClass.getContainingFile().getVirtualFile());

PsiElement result = new TestMeGenerator(new TestClassElementsLocator(), testTemplateContextBuilder,new CodeRefactorUtil()).generateTest(new FileTemplateContext(new FileTemplateDescriptor(templateFilename), language, getProject(),
expectedTestClassName,
targetPackage,
getModule(),
getModule(),
srcDir,
fooClass,
fileTemplateConfig));
expectedTestClassName,
targetPackage,
getModule(),
getModule(),
srcDir,
fooClass,
fileTemplateConfig,
customization));
System.out.println("result:"+result);
verifyGeneratedTest(packageName, expectedTestClassName);
}, CodeInsightBundle.message("intention.create.test"), this);
Expand Down Expand Up @@ -157,7 +170,7 @@ public void setUp() throws Exception {
ignoreTrailingWhitespaces = false;
}
// @Override //relevant when JavaCodeInsightFixtureTestCase is used
// protected void tuneFixture(JavaModuleFixtureBuilder moduleBuilder) throws Exception {
// moduleBuilder.addJdk(new File(System.getProperty("java.home")).getParentContainerClass());
// }
// protected void tuneFixture(JavaModuleFixtureBuilder moduleBuilder) throws Exception {
// moduleBuilder.addJdk(new File(System.getProperty("java.home")).getParentContainerClass());
// }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.weirddev.testme.intellij.generator;

import com.weirddev.testme.intellij.configuration.TestMeConfig;
import com.weirddev.testme.intellij.configuration.TestMeConfigPersistent;
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.TemplateRegistry;
import com.weirddev.testme.intellij.template.context.Language;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;

import java.util.ArrayList;
import java.util.List;

/**
* Date: 23/05/2017
Expand Down Expand Up @@ -49,4 +54,18 @@ public void testMockFieldsInDiWithCtor() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

public void testFileTemplateCustomization() {
final TestMeConfig testMeConfig = new TestMeConfig();
testMeConfig.setOpenCustomizeTestDialog(true);
final FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(testMeConfig);
List<String> selectedFieldNameList = new ArrayList<>();
selectedFieldNameList.add("result");
selectedFieldNameList.add("techFighter");
List<String> selectedMethodIdList = new ArrayList<>();
selectedMethodIdList.add("com.example.services.impl.Foo#fight(com.example.foes.Fire,java.lang.String)");
final FileTemplateCustomization customization =
new FileTemplateCustomization(selectedFieldNameList, selectedMethodIdList, true);
doTest(fileTemplateConfig, customization);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.weirddev.testme.intellij.template.FileTemplateConfig;
import com.weirddev.testme.intellij.template.FileTemplateContext;
import com.weirddev.testme.intellij.template.TemplateDescriptor;
import com.weirddev.testme.intellij.ui.customizedialog.CustomizeTestDialog;
import com.weirddev.testme.intellij.ui.customizedialog.FileTemplateCustomization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
Expand Down Expand Up @@ -107,23 +109,42 @@ public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiEl
}
LOG.debug("targetDirectory:"+targetDirectory.getVirtualFile().getUrl());
final ClassNameSelection classNameSelection = generatedClassNameResolver.resolveClassName(project, targetDirectory, srcClass, templateDescriptor);
if (classNameSelection.getUserDecision() != ClassNameSelection.UserDecision.Abort) {
final Module finalTestModule = testModule;
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
testMeGenerator.generateTest(
new FileTemplateContext(
new FileTemplateDescriptor(templateDescriptor.getFilename()),templateDescriptor.getLanguage(),project, classNameSelection.getClassName(), srcPackage, srcModule, finalTestModule,targetDirectory, srcClass,
new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState())
)
);
}
}, "TestMe Generate Test", this);
if (classNameSelection.getUserDecision().equals(ClassNameSelection.UserDecision.Abort)) {
return;
}

FileTemplateConfig fileTemplateConfig = new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState());
final Module finalTestModule = testModule;
boolean openUserCheckDialog = TestMeConfigPersistent.getInstance().getState().isOpenCustomizeTestDialog();
FileTemplateContext fileTemplateContext = new FileTemplateContext(
new FileTemplateDescriptor(templateDescriptor.getFilename()), templateDescriptor.getLanguage(), project,
classNameSelection.getClassName(), srcPackage, srcModule, finalTestModule, targetDirectory, srcClass,
fileTemplateConfig, new FileTemplateCustomization(new ArrayList<>(), new ArrayList<>(), openUserCheckDialog));
if (openUserCheckDialog) {
// create filed and method check dialog
final CustomizeTestDialog dialog = createTestMeDialog(project, srcClass, fileTemplateContext);
// if not ok button selected the return
if (dialog.isModal() && !dialog.showAndGet()) {
return;
}
}

CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
testMeGenerator.generateTest(fileTemplateContext);
}
}, "TestMe Generate Test", this);
LOG.debug("End CreateTestMeAction.invoke");
}

/**
* create user check dialog to decide fields to mock and methods to test
*/
private CustomizeTestDialog createTestMeDialog(Project project, PsiClass srcClass, FileTemplateContext fileTemplateContext) {
return new CustomizeTestDialog(project, "Select fields/methods to be mocked/tested", srcClass, fileTemplateContext);
}

/**
* @see CreateTestAction#computeSuitableTestRootUrls
*/
Expand All @@ -150,7 +171,7 @@ private static List<SourceFolder> suitableTestSourceFolders(@NotNull Module modu
return sourceFolders;
}
/**
* @see JavaProjectRootsUtil#isForGeneratedSources(com.intellij.openapi.roots.SourceFolder)
* @see JavaProjectRootsUtil#isForGeneratedSources(SourceFolder)
*/
private static boolean isForGeneratedSources(SourceFolder sourceFolder) {
JavaSourceRootProperties properties = sourceFolder.getJpsElement().getProperties(JavaModuleSourceRootTypes.SOURCES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private static String resolveOwnerClassName(PsiMethod psiMethod) {
return psiMethod.getContainingClass() == null ? null : psiMethod.getContainingClass().getQualifiedName();
}

private static boolean isTestable(PsiMethod psiMethod, @Nullable PsiClass srcClass){
public static boolean isTestable(PsiMethod psiMethod, @Nullable PsiClass srcClass){
return !TypeUtils.isLanguageBaseClass(resolveOwnerClassName(psiMethod)) && !PropertyUtils.isPropertySetter(psiMethod) && (!PropertyUtils.isPropertyGetter(psiMethod) || srcClass!=null && srcClass.isEnum()) &&
!psiMethod.isConstructor() && isVisibleForTest(psiMethod, srcClass) && !isOverriddenInChild(psiMethod, srcClass)
&& !isInterface(psiMethod) && !psiMethod.hasModifierProperty(PsiModifier.ABSTRACT) && !isSyntheticMethod(psiMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public class TestMeConfig {
*/
private boolean renderInternalMethodCallStubs = false;

/**
* Test generator behavior option. open user check dialog
* Valid values:true,false
* Default:false
*/
private boolean openCustomizeTestDialog = false;

public boolean getGenerateTestsForInheritedMethods() {
return generateTestsForInheritedMethods;
}
Expand Down Expand Up @@ -77,4 +84,12 @@ public boolean isRenderInternalMethodCallStubs() {
public void setRenderInternalMethodCallStubs(boolean renderInternalMethodCallStubs) {
this.renderInternalMethodCallStubs = renderInternalMethodCallStubs;
}

public boolean isOpenCustomizeTestDialog() {
return openCustomizeTestDialog;
}

public void setOpenCustomizeTestDialog(boolean openCustomizeTestDialog) {
this.openCustomizeTestDialog = openCustomizeTestDialog;
}
}
Loading
Loading