Skip to content

Commit

Permalink
Merge pull request #7 from zhangfj88/add-exception-on-actual
Browse files Browse the repository at this point in the history
Throw specific exceptions based on the method and configuration
  • Loading branch information
zhangfj88 authored Apr 3, 2024
2 parents 038ba02 + 3385ccf commit 3dc4ae1
Show file tree
Hide file tree
Showing 79 changed files with 1,067 additions and 239 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'com.weirddev.testme'
version = 6.2.0
version = 6.3.0

#jvmTargetVersion = 1.8
jvmTargetVersion = 17
Expand Down
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,15 @@ 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 = List.of("result", "techFighter");
List<String> selectedMethodIdList = List.of("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,17 @@ 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 = List.of("result", "techFighter");
List<String> selectedMethodIdList = List.of("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,15 @@ 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 = List.of("result", "techFighter");
List<String> selectedMethodIdList = List.of("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,15 @@ 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 = List.of("result", "techFighter");
List<String> selectedMethodIdList = List.of("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,15 @@ 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 = List.of("result", "techFighter");
List<String> selectedMethodIdList = List.of("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
Loading

0 comments on commit 3dc4ae1

Please sign in to comment.