Skip to content

Commit

Permalink
Merge pull request #31 from huangliang992/feature/add-user-select-dialog
Browse files Browse the repository at this point in the history
file template customization bug fix. NPE when encountering primitive field
  • Loading branch information
yaronyam authored Apr 1, 2024
2 parents 59c7491 + 439bd06 commit ac5d5e8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ 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)");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,8 @@ 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)");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ 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)");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ 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)");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ 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)");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,29 @@ protected void doOKAction() {
}

private boolean isMockable(PsiField psiField, PsiClass testedClass, String templateFileName) {
boolean isPrimitive = psiField.getType() instanceof PsiPrimitiveType;
// make a direct return if isPrimitive, because PsiUtil.resolveClassInType(psiField.getType()) returns null
if (isPrimitive) {
return false;
}
boolean overridden = Field.isOverriddenInChild(psiField, testedClass);
if (overridden) {
return false;
}
boolean isFinal =
psiField.getModifierList() != null && psiField.getModifierList().hasExplicitModifier(PsiModifier.FINAL);
boolean isPrimitive = psiField.getType() instanceof PsiPrimitiveType;
boolean isMockitoMockMakerInlineOn = MockBuilderFactory.isMockInline(fileTemplateContext);
if (!(canMockFinal(templateFileName) || !isFinal || isMockitoMockMakerInlineOn)) {
return false;
}
PsiClass psiClass = PsiUtil.resolveClassInType(psiField.getType());
String canonicalText = JavaTypeUtils.resolveCanonicalName(psiClass, null);
boolean isArray = ClassNameUtils.isArray(canonicalText);
boolean isEnum = JavaPsiTreeUtils.resolveIfEnum(psiClass);
boolean isMockitoMockMakerInlineOn = MockBuilderFactory.isMockInline(fileTemplateContext);
boolean isWrapperType = MockitoMockBuilder.WRAPPER_TYPES.contains(canonicalText);
return !isPrimitive && !isWrapperType
&& (canMockFinal(templateFileName) || !isFinal || isMockitoMockMakerInlineOn) && !overridden && !isArray
&& !isEnum;
if (isEnum) {
return false;
}
String canonicalText = JavaTypeUtils.resolveCanonicalName(psiClass, null);
return (null != canonicalText) && !MockitoMockBuilder.WRAPPER_TYPES.contains(canonicalText)
&& !ClassNameUtils.isArray(canonicalText);
}

/**
Expand Down

0 comments on commit ac5d5e8

Please sign in to comment.