Skip to content
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ intellij {

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.10.0'
// https://mvnrepository.com/artifact/org.assertj/assertj-core
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.12.2'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bruce.intellijplugin.generatesetter;

public enum TestEngine {
ASSERT("Plain java assert", "java.util", "Objects"),
JUNIT4("JUnit 4", "org.junit", "Assert"),
JUNIT5("JUnit 5", "org.junit.jupiter.api", "Assertions"),
TESTNG("TestNG", "org.testng.asserts", "Assertion"),
ASSERTJ("AssertJ", "org.assertj.core.api", "Assertions"),
HAMCREST("Hamcrest", "org.hamcrest.core", "MatcherAssert");

private final String formattedName;
private final String assertionsPackage;
private final String assertionsClassName;

TestEngine(String formattedName, String assertionsPackage, String assertionsClassName) {
this.formattedName = formattedName;
this.assertionsPackage = assertionsPackage;
this.assertionsClassName = assertionsClassName;
}

public String getFormattedName() {
return formattedName;
}

public String getAssertionsPackage() {
return assertionsPackage;
}

public String getAssertionsClassName() {
return assertionsClassName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,43 @@

import com.bruce.intellijplugin.generatesetter.CommonConstants;
import com.bruce.intellijplugin.generatesetter.GenerateAllHandlerAdapter;
import com.bruce.intellijplugin.generatesetter.TestEngine;
import com.bruce.intellijplugin.generatesetter.template.GenerateSetterService;
import com.bruce.intellijplugin.generatesetter.template.GenerateSetterState;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiImportList;
import com.intellij.psi.PsiImportStatement;
import com.intellij.psi.PsiImportStaticStatement;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.compiled.ClsClassImpl;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.ASSERT;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.ASSERTJ;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.JUNIT4;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.JUNIT5;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.TESTNG;
import static com.bruce.intellijplugin.generatesetter.TestEngine.*;

/**
* @author bruce ge
*/
public class AssertAllGetterAction extends GenerateAllSetterBase {
enum TestEngine {ASSERT, JUNIT4, JUNIT5, TESTNG, ASSERTJ}

// imports to add when generating asserts.
private static final Map<TestEngine, String> engineImports = ImmutableMap.<TestEngine, String>builder()
.put(JUNIT4, "static org.junit.Assert.assertEquals")
.put(JUNIT5, "static org.junit.jupiter.api.Assertions.assertEquals")
.put(TESTNG, "static org.testng.Assert.assertEquals")
.put(ASSERTJ, "static org.assertj.core.api.Assertions.assertThat")
.put(ASSERT, "java.util.Objects")
private static final Map<TestEngine, List<String>> engineImports = ImmutableMap.<TestEngine, List<String>>builder()
.put(JUNIT4, ImmutableList.of("static org.junit.Assert.assertEquals"))
.put(JUNIT5, ImmutableList.of("static org.junit.jupiter.api.Assertions.assertEquals"))
.put(TESTNG, ImmutableList.of("static org.testng.Assert.assertEquals"))
.put(ASSERTJ, ImmutableList.of("static org.assertj.core.api.Assertions.assertThat"))
.put(ASSERT, ImmutableList.of("java.util.Objects"))
.put(HAMCREST, ImmutableList.of("static org.hamcrest.MatcherAssert.assertThat", "org.hamcrest.Matchers.*"))
.build();

// className like 'java.util.Objects' -> engine (only java.util.Objects)
Expand All @@ -70,6 +66,7 @@ enum TestEngine {ASSERT, JUNIT4, JUNIT5, TESTNG, ASSERTJ}
.put("org.junit.jupiter.api.Assertions", JUNIT5)
.put("org.testng.Assert", TESTNG)
.put("org.assertj.core.api.Assertions", ASSERTJ)
.put("org.hamcrest.MatcherAssert", HAMCREST)
.build();

// engine -> assert static method
Expand All @@ -78,6 +75,7 @@ enum TestEngine {ASSERT, JUNIT4, JUNIT5, TESTNG, ASSERTJ}
.put(JUNIT5, "assertEquals")
.put(TESTNG, "assertEquals")
.put(ASSERTJ, "assertThat")
.put(HAMCREST, "assertThat")
.build();

protected Project project;
Expand Down Expand Up @@ -136,18 +134,25 @@ private TestEngine detectCurrentTestEngineInternal(Project project, PsiFile cont
PsiJavaFile javaFile = (PsiJavaFile) containingFile;
PsiImportList importList = javaFile.getImportList();

// prefer AssertJ if it is in classpath

// prefer TestEngine from settings if it is in classpath
ProjectFileIndex index = ProjectFileIndex.getInstance(project);
Module module = index.getModuleForFile(containingFile.getVirtualFile());
if (module != null) {
GenerateSetterState state = GenerateSetterService.getInstance().getState();
TestEngine preferredTestEngine = state.getPreferredTestEngine();

String assertionsClassName = preferredTestEngine.getAssertionsClassName();

GlobalSearchScope searchScope = GlobalSearchScope.moduleRuntimeScope(module, true);
PsiClass[] lists = PsiShortNamesCache.getInstance(project)
.getClassesByName("Assertions", searchScope);
PsiClass[] projectAssertionClasses = PsiShortNamesCache.getInstance(project)
.getClassesByName(assertionsClassName, searchScope);

for (PsiClass psiClass : lists) {
if ("org.assertj.core.api.Assertions".equals(psiClass.getName())) {
for (PsiClass psiClass : projectAssertionClasses) {
String psiClassQualifiedName = psiClass.getQualifiedName();
if (String.format("%s.%s", preferredTestEngine.getAssertionsPackage(), preferredTestEngine.getAssertionsClassName()).equals(psiClassQualifiedName)) {
detectImportedEngines(importList);
return ASSERTJ;
return preferredTestEngine;
}
}
}
Expand All @@ -165,22 +170,25 @@ private TestEngine detectCurrentTestEngineInternal(Project project, PsiFile cont
}

if (qualifiedName.startsWith("org.junit.jupiter.api.")) {
return TestEngine.JUNIT5;
return JUNIT5;
}
if (qualifiedName.startsWith("org.junit.")) {
return TestEngine.JUNIT4;
return JUNIT4;
}
if (qualifiedName.startsWith("org.assertj.")) {
return TestEngine.ASSERTJ;
return ASSERTJ;
}
if (qualifiedName.startsWith("org.testng.")) {
return TestEngine.TESTNG;
return TESTNG;
}
if (qualifiedName.startsWith("org.hamcrest.")) {
return HAMCREST;
}
}
}
}

return TestEngine.ASSERT;
return ASSERT;
}

private void detectImportedEngines(PsiImportList importList) {
Expand All @@ -195,15 +203,27 @@ private void detectImportedEngines(PsiImportList importList) {
for (PsiImportStaticStatement importStaticStatement : importStaticStatements) {
PsiClass psiClass = importStaticStatement.resolveTargetClass();
if (psiClass == null) {
continue;
}

String qualifiedName = psiClass.getQualifiedName();
TestEngine testEngine = engineStaticImportsReversed.get(qualifiedName);
if (testEngine != null) {
String referenceName = importStaticStatement.getReferenceName(); // like assertEquals
if (referenceName == null || referenceName.equals(engineStaticImportsMethod.get(testEngine))) {
currentFileImportedEngines.add(testEngine);
//if it is a static import of a method from any testengine
PsiJavaCodeReferenceElement importReference = importStaticStatement.getImportReference();
if (importReference == null || importReference.getQualifiedName() == null) {
continue;
}
String qualifiedName = importReference.getQualifiedName();
engineStaticImportsReversed.entrySet()
.stream()
.filter(entry -> qualifiedName.startsWith(entry.getKey()))
.findFirst()
.ifPresent(entry -> currentFileImportedEngines.add(entry.getValue()));

} else {

String qualifiedName = psiClass.getQualifiedName();
TestEngine testEngine = engineStaticImportsReversed.get(qualifiedName);
if (testEngine != null) {
String referenceName = importStaticStatement.getReferenceName(); // like assertEquals
if (referenceName == null || referenceName.equals(engineStaticImportsMethod.get(testEngine))) {
currentFileImportedEngines.add(testEngine);
}
}
}
}
Expand Down Expand Up @@ -263,6 +283,8 @@ public String formatLine(String line) {
return "assertThat(" + getter + ").isEqualTo(" + value + ");";
case ASSERT:
return "assert Objects.equals(" + value + ", " + getter + ");";
case HAMCREST:
return "assertThat(" + value + ", equalTo(" + getter + ");";
default:
throw new Error("Unknown case: " + currentFileTestEngine);
}
Expand All @@ -271,7 +293,7 @@ public String formatLine(String line) {
@Override
public void appendImportList(Set<String> newImportList) {
if (!currentFileImportedEngines.contains(currentFileTestEngine)) {
newImportList.add(engineImports.get(currentFileTestEngine));
newImportList.addAll(engineImports.get(currentFileTestEngine));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bruce.intellijplugin.generatesetter.actions;

import com.bruce.intellijplugin.generatesetter.CommonConstants;
import com.bruce.intellijplugin.generatesetter.TestEngine;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
Expand All @@ -9,20 +11,15 @@
import java.util.Map;
import java.util.Set;

import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.ASSERT;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.ASSERTJ;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.JUNIT4;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.JUNIT5;
import static com.bruce.intellijplugin.generatesetter.actions.AssertAllGetterAction.TestEngine.TESTNG;

public class AssertNotNullAction extends AssertAllGetterAction {
// imports to add when generating asserts.
private static final Map<TestEngine, String> engineImports = ImmutableMap.<TestEngine, String>builder()
.put(JUNIT4, "static org.junit.Assert.assertNotNull")
.put(JUNIT5, "static org.junit.jupiter.api.Assertions.assertNotNull")
.put(TESTNG, "static org.testng.Assert.assertNotNull")
.put(ASSERTJ, "static org.assertj.core.api.Assertions.assertThat")
.put(ASSERT, "java.util.Objects")
private static final Map<TestEngine, List<String>> engineImports = ImmutableMap.<TestEngine, List<String>>builder()
.put(TestEngine.JUNIT4, ImmutableList.of("static org.junit.Assert.assertNotNull"))
.put(TestEngine.JUNIT5, ImmutableList.of("static org.junit.jupiter.api.Assertions.assertNotNull"))
.put(TestEngine.TESTNG, ImmutableList.of("static org.testng.Assert.assertNotNull"))
.put(TestEngine.ASSERTJ, ImmutableList.of("static org.assertj.core.api.Assertions.assertThat"))
.put(TestEngine.ASSERT, ImmutableList.of("java.util.Objects"))
.put(TestEngine.HAMCREST, ImmutableList.of("static org.hamcrest.MatcherAssert.assertThat", "org.hamcrest.Matchers.*"))
.build();

private final GenerateAllAssertsHandlerAdapter generateAllHandler;
Expand All @@ -48,7 +45,7 @@ protected String generateStringForNoParam(String generateName,
Set<TestEngine> currentFileImportedEngines = generateAllHandler.currentFileImportedEngines;

if (!currentFileImportedEngines.contains(currentFileTestEngine)) {
newImportList.add(engineImports.get(currentFileTestEngine));
newImportList.addAll(engineImports.get(currentFileTestEngine));
}

switch (currentFileTestEngine) {
Expand All @@ -60,6 +57,8 @@ protected String generateStringForNoParam(String generateName,
return splitText + "assertThat(" + generateName + ").isNotNull();";
case ASSERT:
return splitText + "assert " + generateName + " != null";
case HAMCREST:
return splitText + "assertThat( " + generateName + ", notNullValue());";
default:
throw new Error("Unknown case: " + currentFileTestEngine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,13 +581,12 @@ else if (psiClassOfParameter!=null && psiClassOfParameter.isEnum()) {
}
builder.append(");");

generateAllHandler.appendImportList(newImportList);

if (generateAllHandler.forAssertWithDefaultValues()) {
mainBuilder.append(generateAllHandler.formatLine(builder.toString()));
} else {
mainBuilder.append(builder);
}
generateAllHandler.appendImportList(newImportList);
}

private static void appendCollectNotEmpty(StringBuilder builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<grid id="27dc6" binding="panel1" default-binding="true" 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>
<xy x="20" y="20" width="500" height="400"/>
<xy x="20" y="20" width="521" height="400"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -15,7 +15,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="a574c" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="a574c" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/>
<properties/>
Expand Down Expand Up @@ -47,7 +47,7 @@
<grid id="99e56" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -124,6 +124,38 @@
</grid>
</children>
</grid>
<grid id="e4650" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="687d3" class="javax.swing.JLabel" binding="preferAssertionsFromLabel" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="ea787"/>
<text value="Prefer assertions from:"/>
</properties>
</component>
<component id="ea787" class="javax.swing.JComboBox" binding="preferredTestEngineComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="false"/>
</properties>
</component>
<hspacer id="e5454">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</children>
</grid>
<grid id="ca9b3" binding="splitterPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
Expand Down
Loading