Compare commits

...
70 Commits
Author SHA1 Message Date
chrisly42 48349a6528 Prepared for release. 2020-02-25 14:39:30 +01:00
chrisly42 9b25e50183 Improvements for TwistedAssertionInspection to no longer report .class types as constant to bother, nor matches() and doesNotMatch() for regexps. If both sides are constants, they will only show as weak problems. 2020-02-17 20:17:33 +01:00
chrisly42 e8ce8ce2c6 BogusAssertionInspection will no longer warn if the expression contains method calls and now tries to avoid valid hashCode() and equals() tests. 2020-02-17 18:33:11 +01:00
chrisly42 62f59b0fe2 Added new TwistedAssertion inspection that will warn about assertions with the actual expression being a constant indicating swapped use of actual and expected expressions.
Added new BogusAssertion inspection that showing typical copy and paste errors where actual and expected expressions are the same.
2020-02-14 13:36:04 +01:00
chrisly42 58298fabc6 Extended Testing-Framework to work around IntelliJ IDEA introducing an unwanted assertj-core dependency conflicting with our newer one. 2020-02-08 16:33:38 +01:00
chrisly42 77d3608fd3 Minor NPE Bugfix. Fixed use of "experimental API". Upgraded dependencies. 2020-02-04 20:01:05 +01:00
chrisly42 a0909d8c39 Prepared release. 2019-11-19 19:47:40 +01:00
chrisly42 5113cc15ab Added hasSize(), isEmpty() and isNotEmpty() for AssertThatFileExpression when using AssertJ >= 3.14.0. 2019-11-18 21:13:30 +01:00
chrisly42 ae2076a425 Bumped to AssertJ 3.14.0, AssertJ-Guava 3.3.0, Kotlin 1.3.60. IntelliJ-Plugin 0.4.13, Jacoco 0.8.5 2019-11-18 21:10:45 +01:00
chrisly42 42429c0f72 Added AssertThatComparableExpression for funny compareTo() uses. 2019-11-17 21:21:45 +01:00
chrisly42 8133f3850f Added first version of AssertThatPathExpression for a limited number transformations (more stuff is possible, but requires detection and transformation of static Files-methods). 2019-11-17 19:10:41 +01:00
chrisly42 8d03b3734c Fixed a lapsuus in AssertThatFileExpression also transforming listFiles() with a filter argument. 2019-11-17 17:14:28 +01:00
chrisly42 2f0d855d1e Minor documentation fixes. Prepared release. 2019-09-30 22:17:23 +02:00
chrisly42 6dab8ad552 Added several transformations to AssertThatStringExpression inspection. Specifically, uses of matches(), compareToIgnoreCase(), indexOf(), and trim(). 2019-09-30 20:27:43 +02:00
chrisly42 1983750077 Internal refactoring: Reduced code duplication by moving stuff into a common base class. 2019-09-30 17:25:16 +02:00
chrisly42 a0ed4eab76 Added new AssertThatFileExpression to move out many common methods from inside the assertThat() expression (exists(), getName(), getParent(), and many more). 2019-09-29 21:56:36 +02:00
chrisly42 acc81863f5 Fixed a bug in AssertThatBinaryExpression inspection for assertThat(null != expression) and related that would not correctly invert the condition on transformation. 2019-09-29 12:32:32 +02:00
chrisly42 2b97494c17 Really fixed AssertThatGuavaOptional inspections to avoid conversions from .get() to .contains() for array types. 2019-09-25 18:38:35 +02:00
chrisly42 e3444db213 Fixes to AssertThatSize inspection after extending it for Maps in previous release as not all combinations for .hasSameSizeAs() are supported.
Bumped to IntelliJ IDEA 2019.2.3.
2019-09-24 22:55:02 +02:00
chrisly42 855fb03f7c One more option for AssertThatCollectionOrMap inspection for warnings without quickfix.
AssertThatGuavaOptional inspections will now avoid conversions from .get() to .contains() for array types (currently not correctly supported by AssertJ-Guava).
2019-09-24 22:16:11 +02:00
chrisly42 8d678411b5 Fix for AssertThatCollectionOrMap inspection sometimes causing an index out of bounds exception.
Added an settings option for AssertThatCollectionOrMap inspection respecting the degenerated case of maps with null values.
Upgrade to JUnit Jupiter 5.5.2.
2019-09-09 23:13:45 +02:00
chrisly42 341d1877df Prepared for 1.4 release. 2019-08-25 18:40:58 +02:00
chrisly42 05dc4905ca Extended AssertThatCollectionOrMap inspection for several assertThat(map.get()) cases as suggested by Stefan H. 2019-08-23 19:58:11 +02:00
chrisly42 315b93d90d Extended AssertThatSize inspection to Maps, too. 2019-08-23 18:21:08 +02:00
chrisly42 36f63d26d2 Minor fix for highlighting of JoinVarArgsContains inspection.
Upgraded to kotlin 1.3.50, intellij plugin 0.4.10, assertj 3.13.2, idea 2019.2.1
2019-08-23 18:04:07 +02:00
chrisly42 a4535afbbb Fixed references to wrong AssertJ version 13.2.0 instead of 3.12.0, courtesy of Bernhard R. 2019-08-04 18:29:59 +02:00
chrisly42 df11939589 New JoinVarArgsContains inspection that will detect multiple .contains(), .doesNotContain(), and .containsOnlyOnce() calls within the same statement that could be joined together using variadic arguments. 2019-08-03 20:57:34 +02:00
chrisly42 4420a0a392 AssertThatJava8Optional and AssertThatGuavaOptional inspections do not longer try to fix assertThat(optional).isEqualTo(Optional.fromNullable(expression)) to contains() when expression is not a non-null constant expression. 2019-07-30 20:02:40 +02:00
chrisly42 8fb3ecce95 Bumped kotlin to 1.3.41, jupiter 5.5.1, added compatibility with IDEA 2019.2 and fixed regressions with new AssertJ 3.13.0. 2019-07-30 19:43:40 +02:00
chrisly42 5048c898ec Fixed broken code in test classes (missing imports, undefined variables). 2019-07-30 19:39:16 +02:00
chrisly42 b84a781134 Reverted removed method (identical to default implementation) that causes compatibility problem with older IDEA versions (again, sigh)... 2019-06-23 19:24:36 +02:00
chrisly42 095345a456 The JoinAssertThatStatements inspection will now add line breaks on joining statements.
Unrelated: Upgraded Gradle to 5.4.1. Upgraded kotlin to 1.3.40. Upgraded jacoco to 0.8.4. Minor style change for immediate returns. Increased test/branch coverage.
2019-06-23 18:51:12 +02:00
chrisly42 5c455c3ca9 Fix for multiple JUnit Conversions in batch mode with and without delta creating an exception.
Added new AssertThatObjectExpression inspection for toString() and hashCode() and moved equals() from AssertThatBinaryExpression there.
2019-06-09 21:35:23 +02:00
chrisly42 6795622202 Finally was able to remove JUnit workaround for test invokation with availability of JUnit 5.5.0-RC1. 2019-06-09 14:17:44 +02:00
chrisly42 828e61a73b New ImplicitAssertion inspection for implicit isNotNull(), isNotEmpty() and isPresent() assertions that will be covered by chained assertions. 2019-06-09 14:04:01 +02:00
chrisly42 178b7d368a Extended JUnitAssertToAssertJ inspection to convert JUnit assume-Statements, too.
Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
2019-05-17 12:17:01 +02:00
chrisly42 e797dc2515 Extended documentation about assumeThat() preconditions. 2019-05-16 20:06:14 +02:00
chrisly42 7133c55710 Added Guava Optional opt.orNull() == null case. Added Java 8 Optional opt.orElse(null) == null case, too. 2019-05-15 20:50:30 +02:00
chrisly42 94c695617a Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection. 2019-05-11 13:40:37 +02:00
chrisly42 ecb5029154 Fixed a NPE in AssumeThatInsteadOfReturn inspection quickfix for empty else branches.
Fixed missing description for AssumeThatInsteadOfReturn inspection.
Added new AssertThatCollectionOrMapExpression inspection that tries to pull out methods such as isEmpty() or contains() out of an actual assertThat() expression. Added more method calls that are considered extension points.
2019-05-06 20:17:11 +02:00
chrisly42 0e2007641c Compatibility fixes for IDEA <= 2019.1 :-/ 2019-05-05 20:06:12 +02:00
chrisly42 eab50f590b Added a first version of a new inspection that tries to detect bogus uses of return statements in test methods and replaces them by assumeThat() calls. Removed oraclejdk8 from travis-ci. Added custom Jupiter DisplayNameGenerator for user friendly test names in report. Extended documentation. Removed Playground. 2019-05-05 19:02:35 +02:00
chrisly42 66f1467b23 Added coveralls. 2019-05-04 16:40:05 +02:00
chrisly42 6a36294a2b Added travis Build Status to readme. 2019-05-04 16:15:36 +02:00
chrisly42 0b79a6d7dc Added early bail-outs via simple text matchings to optimize speed of false positive detection. 2019-05-04 15:54:27 +02:00
chrisly42 6fb23ea89c Heavily reworked inspections for edge cases, such as multiple isEqualTo() calls inside a single statement. Corrected highlighting for most inspections. Lots of refactorings, tweakings. Added travis-ci file (untested). Added jacoco. 2019-05-04 15:36:04 +02:00
chrisly42 362c4210a5 Fixed isEmpty() for enumerables and strings and isNull() for object conversions to be applied only if it is the terminal method call as isEmpty() and isNull() return void. 2019-05-01 13:15:31 +02:00
chrisly42 e55acf9c74 Extended test cases for AssertThatBooleanCondition and AssertThatObjectIsNullOrNotNull. 2019-04-29 20:10:55 +02:00
chrisly42 3ece81b024 Minor fixes to descriptions. 2019-04-29 19:58:05 +02:00
chrisly42 0b2ce470db Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions. 2019-04-29 19:58:00 +02:00
chrisly42 941ddfdb5e Implemented first version of JoinAssertThatStatements inspection that will try to merge assertThat() statements with the same actual object together, preserving comments. 2019-04-28 20:43:42 +02:00
chrisly42 666e373405 Extended AssertThatSize inspection to transform hasSize() into hasSameSizeAs(), if possible. 2019-04-23 20:16:43 +02:00
chrisly42 8b0da63f86 Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions (really, Jetbrains, this sucks for no reason). Consolidated location of methods. 2019-04-23 18:19:47 +02:00
chrisly42 66508ceb2c New AssertThatInstanceOf inspection that moves instanceof expressions out of assertThat(). Fixes to documentation. 2019-04-22 19:03:59 +02:00
chrisly42 db02f7fb93 New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside assertThat().
Renamed a few inspections to better/shorter names (and fixed file and directory names accordingly).
2019-04-20 18:40:38 +02:00
chrisly42 a707eee9ad Extended AssertThatSize intention to take strings and CharSequences into account, too. 2019-04-20 09:04:38 +02:00
chrisly42 533c20906a Added a note on referencing. Moved AssertThatStringExpression to a more appropriate place in documentation. 2019-04-19 19:44:40 +02:00
chrisly42 da83f7f101 New AssertThatStringExpression that will move isEmpty(), equals(), equalsIgnoreCase(), contains(), startsWith(), and endsWith() out of actual expression. Bumped intellij-plugin to latest version. 2019-04-19 19:33:14 +02:00
chrisly42 faeb509797 Fixed default method not available in IDEA <2018.3. Sigh :-( 2019-04-18 22:29:30 +02:00
chrisly42 9f91fb3ccf Added support for referencing and refactoring inside ``.extracting()`` methods with fields, properties and methods (though getter renaming does not work that perfect, but I'm giving up for now as the IntelliJ SDK docs are seriously lacking).
Fixed an exception in batch mode if the description string was the same but for different fixes. Now descriptions are different for quick fixes triggered by AssertThatJava8OptionalInspection and AssertThatGuavaOptionalInspection. Minor refactorings. Extended documentation.
2019-04-18 22:12:48 +02:00
chrisly42 b58d8cfd2f Fix for incompatible method call regarding IDEA <2018.2.
Heavy refactoring regarding quick fixes (using more extensions).
Even more refactoring.
Fixed/extended AssertThatGuavaOptionalInspection regarding static.
imports and wrong reference to core assertThat() where Guava assertThat() actually was necessary.
2019-04-13 18:35:49 +02:00
chrisly42 4e6d53b3dc Reduced minimal supported IDEA version from 2018.2 to 2017.2.
Added AssertThatGuavaOptional inspection. Prepared next release.
Bumped to Kotlin 1.3.30.
2019-04-11 23:55:35 +02:00
chrisly42 2cdc242125 Refactored Code to avoid string duplications... added Boolean.map() extension. 2019-04-11 21:09:10 +02:00
chrisly42 d49b7bf17b Optimized even more code. Fixed BinaryExpression for Boolean.TRUE/FALSE. Added new AssertThatJava8Optional inspection. Added changelog to README. 2019-04-11 16:26:11 +02:00
chrisly42 c1d8ade7b1 Optimized code. More kotlin-style stuff. Made detecting of Boolean.TRUE/FALSE available to all constant evaluations. 2019-04-08 11:56:13 +02:00
chrisly42 bdabcc7255 Removed TODO about AssertThatBinaryExpressionIsTrueOrFalse. 2019-04-07 11:37:10 +02:00
chrisly42 5812e72227 New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and equals() statements (more than 150 combinations) inside assertThat(). 2019-04-07 11:29:07 +02:00
chrisly42 6a44237ca0 Added tacky icon. 2019-04-06 21:29:18 +02:00
chrisly42 51703e8499 Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
Extended tests to be more explicit regarding the expected quick fix messages.
2019-04-06 21:29:18 +02:00
chrisly42 ba56325299 Internally: Upgraded to AssertJ 13.2.2.
Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
Really fixed highlighting for JUnit conversion. Sorry.
Fixed testing code to work against IDEA 2019.1.
2019-04-06 21:29:13 +02:00
185 changed files with 9759 additions and 1064 deletions
+10
View File
@@ -0,0 +1,10 @@
language: java
jdk:
- openjdk8
before_script:
- chmod +x gradlew
- chmod +x gradle/wrapper/gradle-wrapper.jar
script:
- ./gradlew clean test jacocoTestReport coveralls
+828 -18
View File
@@ -1,15 +1,17 @@
# Cajon - Concise AssertJ Optimizing Nitpicker
# Cajon - Concise AssertJ Optimizing Nitpicker [![Build Status](https://travis-ci.org/chrisly42/cajon-plugin.svg?branch=master)](https://travis-ci.org/chrisly42/cajon-plugin) [![Coverage Status](https://coveralls.io/repos/github/chrisly42/cajon-plugin/badge.svg?branch=master)](https://coveralls.io/github/chrisly42/cajon-plugin?branch=master)
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions.
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing [AssertJ](https://assertj.github.io/doc/) assertions.
## Why?
## Purpose
First, code is easier to read, when it is concise and reflects the intention clearly.
AssertJ has plenty of different convenience methods that describing various intentions precisely.
Why write longer, more complex code that can be expressed in brevity?
Second, AssertJ is able to output more meaningful descriptions when an assertion fails.
Second, when using the available special assertion methods of AssertJ, a failure of a condition
can be expressed in better detail and with more meaningful descriptions.
This makes finding bugs and fixing failed tests more efficient.
Nobody likes to read failures of the kind "failed because true is not false".
For example:
@@ -17,7 +19,7 @@ For example:
assertThat(collection.size()).isEqualTo(5);
```
If the collection has more or less than 5 elements, the assertion will fail, but will not
If the collection has more or less than five elements, the assertion will fail, but will not
tell you about the contents, making it hard to guess what went wrong.
Instead, if you wrote the same assertion the following way:
@@ -26,41 +28,399 @@ Instead, if you wrote the same assertion the following way:
assertThat(collection).hasSize(5);
```
Then AssertJ would tell you the contents of the collection on failure.
Then AssertJ would tell you the _actual contents_ of the collection on failure.
## Conversion of JUnit assertions to AssertJ
The plugin also supports the conversion of the most common JUnit 4 assertions to AssertJ.
## Implemented inspections
## Wrong use of AssertJ
Cajon also warns about bogus or incorrect uses of AssertJ.
## Lookup and refactoring of string-based extracting()
AssertJ allows [extracting POJO fields/properties on iterables/arrays](http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-properties-assertion).
Using strings is not safe for refactoring (and before Java 8 Lambdas were available,
creating extractor functions just for testing purpose was a bit too tedious).
This plugin adds support for referencing these fields (so you can ctrl(/cmd)-click on the
string to go to the definition) and also allows safe refactoring on the
fields (refactoring a getter method without a corresponding field will not work
correctly right now).
## Usage
The plugin will report inspections in your opened editor file as warnings.
You can then quick-fix these with your quick-fix hotkey (usually Alt-Return or Opt-Return).
Or, you can use the "Run Inspection by Name..." action to run one inspection on a bigger scope (e.g. the whole project).
Applying a quick fix might result in further optimization possibilities, so
you might need to perform a couple of fixes before you get to the final result.
Check out this example where every line represents the result after a Cajon quickfix:
```
assertFalse(!(array.length == collection.size()));
assertThat(!(array.length == collection.size())).isFalse();
assertThat(array.length == collection.size()).isTrue();
assertThat(array.length).isEqualTo(collection.size());
assertThat(array).hasSameSizeAs(collection);
```
You can toggle the various inspections in the Settings/Editor/Inspections in the AssertJ group.
## Implemented inspections and quickfixes
- JoinAssertThatStatements
Joins multiple ```assertThat()``` statements with same actual expression together.
```
from: assertThat(expected).someCondition();
assertThat(expected).anotherCondition();
to: assertThat(expected).someCondition()
.anotherCondition();
```
Joining will work on actual expressions inside ```assertThat()``` that are equivalent expressions,
except for method calls with known side-effect methods such as ```Iterator.next()``` and
pre/post-increment/decrement operations -- please notify me about others.
The comments of the statements will be preserved. When using ```extracting()``` or similar,
the statements will not be merged.
The behavior regarding the insertion of line breaks between the expressions can be configured in the
inspection settings.
- JoinVarArgsContains
Looks for ```.contains()```, ```.doesNotContain()```, and .```containsOnlyOnce()``` calls for iterables
within the same statement. The available quickfix can join the arguments to variadic version of the call
and remove the surplus one.
```
from: assertThat(expected).contains("foo").doesNotContain("bar").contains("etc").doesNotContain("huh");
to: assertThat(expected).contains("foo", "etc").doesNotContain("bar", "huh");
```
Will not be performed on more complex statements with ```.extracting()``` or ```.as()``` to avoid
changing semantics or losing descriptions.
Note that the quickfix does not handle comments very well and might remove them during the operation.
You may need to perform some manual reformatting, if the line gets too long after applying the fix.
- AssertThatObjectIsNullOrNotNull
Uses ```isNull()``` and ```isNotNull()``` instead.
- AssertThatObjectIsNull
```
from: assertThat(object).isEqualTo(null);
to: assertThat(object).isNull();
```
- AssertThatObjectIsNotNull
```
from: assertThat(object).isNotEqualTo(null);
to: assertThat(object).isNotNull();
```
- AssertThatBooleanIsTrueOrFalse
- AssertThatBooleanCondition
Uses ```isTrue()``` and ```isFalse()``` instead.
```
from: assertThat(booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE);
to: assertThat(booleanValue).isTrue()/isFalse();
```
- AssertThatInvertedBooleanCondition
Inverts the boolean condition to make it more readable.
```
from: assertThat(!booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE);
from: assertThat(!booleanValue).isTrue()/isFalse();
to: assertThat(booleanValue).isFalse()/isTrue();
```
- AssertThatInstanceOf
Moves ```instanceof``` expressions out of ```assertThat()```.
```
from: assertThat(object instanceof classname).isEqualTo(true);
from: assertThat(object instanceof classname).isTrue();
to: assertThat(object).isInstanceOf(classname.class);
from: assertThat(object instanceof classname).isEqualTo(false);
from: assertThat(object instanceof classname).isFalse();
to: assertThat(object).isNotInstanceOf(classname.class);
```
- AssertThatStringIsEmpty
Uses ```isEmpty()``` for empty string assertions.
```
from: assertThat(charSequence/string).isEqualTo("");
from: assertThat(charSequence/string).hasSize(0);
to: assertThat(charSequence/string).isEmpty();
```
The ```assertThat(string.length()).isEqualTo(0);``` case is handled in the AssertThatSize inspection.
- AssertThatStringExpression
Moves string operations inside ```assertThat()``` out.
```
from: assertThat(stringActual.isEmpty()).isTrue();
to: assertThat(stringActual).isEmpty();
from: assertThat(stringActual.equals(stringExpected)).isTrue();
from: assertThat(stringActual.contentEquals(charSeqExpected)).isTrue();
to: assertThat(stringActual).isEqualTo(stringExpected);
from: assertThat(stringActual.equalsIgnoreCase(stringExpected)).isTrue();
to: assertThat(stringActual).isEqualToIgnoringCase(stringExpected);
from: assertThat(stringActual.contains(stringExpected)).isTrue();
to: assertThat(stringActual).contains(stringExpected);
from: assertThat(stringActual.startsWith(stringExpected)).isTrue();
to: assertThat(stringActual).startsWith(stringExpected);
from: assertThat(stringActual.endsWith(stringExpected)).isTrue();
to: assertThat(stringActual).endsWith(stringExpected);
from: assertThat(stringActual.matches(stringExpected)).isTrue();
to: assertThat(stringActual).matches(stringExpected);
```
Analogously with ```isFalse()```.
More funny stuff (excerpt):
```
from: assertThat(stringActual.compareToIgnoreCase(stringExpected)).isEqualTo(0);
to: assertThat(stringActual).isEqualToIgnoringCase(stringExpected);
from: assertThat(stringActual.indexOf(stringExpected)).isEqualTo(0);
from: assertThat(stringActual.indexOf(stringExpected)).isZero();
to: assertThat(stringActual).startsWith(stringExpected);
from: assertThat(stringActual.indexOf(stringExpected)).isNotZero();
to: assertThat(stringActual).doesNotStartWith(stringExpected);
from: assertThat(stringActual.indexOf(stringExpected)).isEqualTo(-1);
from: assertThat(stringActual.indexOf(stringExpected)).isNegative();
to: assertThat(stringActual).doesNotContain(stringExpected);
from: assertThat(stringActual.indexOf(stringExpected)).isGreaterThanOrEqualTo(0);
to: assertThat(stringActual).contains(stringExpected);
from: assertThat(stringActual.trim()).isNotEmpty();
to: assertThat(stringActual).isNotBlank();
```
- AssertThatObjectExpression
Handles ```equals()```, ```toString()``` and ```hashCode()``` inside an expected expression.
```
from: assertThat(objActual.equals(objExpected)).isTrue();
to: assertThat(objActual).isEqualTo(objExpected);
from: assertThat(objActual.toString()).isEqualTo(stringExpected);
to: assertThat(objActual).hasToString(stringExpected);
from: assertThat(objActual.hashCode()).isEqualTo(objExpected.hashCode());
to: assertThat(objActual).hasSameHashCodeAs(objExpected);
```
- AssertThatComparableExpression
Handles ```compareTo()``` inside an expected expression.
```
from: assertThat(obj1.compareTo(obj2)).isEqualTo(0);
to: assertThat(obj1).isEqualByComparingTo(obj2);
from: assertThat(obj1.compareTo(obj2)).isNotZero();
to: assertThat(obj1).isNotEqualByComparingTo(obj2);
from: assertThat(obj1.compareTo(obj2)).isNotEqualTo(-1);
from: assertThat(obj1.compareTo(obj2)).isGreaterThanOrEqualTo(0);
from: assertThat(obj1.compareTo(obj2)).isGreaterThan(-1);
from: assertThat(obj1.compareTo(obj2)).isNotNegative();
to: assertThat(obj1).isGreaterThanOrEqualTo(obj2);
from: assertThat(obj1.compareTo(obj2)).isOne();
to: assertThat(obj1).isGreaterThan(obj2);
from: assertThat(obj1.compareTo(obj2)).isNotPositive();
to: assertThat(obj1).isLessThanOrEqualTo(obj2);
from: assertThat(obj1.compareTo(obj2)).isLessThan(0);
to: assertThat(obj1).isLessThan(obj2);
```
Several more combinations omitted...
- AssertThatCollectionOrMapExpression
Moves ```Collection``` and ```Map``` operations inside ```assertThat()``` out.
```
from: assertThat(collection.isEmpty()).isTrue();
to: assertThat(collection).isEmpty();
from: assertThat(collection.contains("foobar")).isTrue();
to: assertThat(collection).contains("foobar");
from: assertThat(collection.containsAll(otherCollection)).isTrue();
to: assertThat(collection).containsAll(otherCollection);
from: assertThat(map.isEmpty()).isTrue();
to: assertThat(map).isEmpty();
from: assertThat(map.containsKey(key)).isTrue();
to: assertThat(map).containsKey(key);
from: assertThat(map.containsValue(value)).isTrue();
to: assertThat(map).containsValue(value);
```
Analogously with ```isFalse()``` (except for ```containsAll()```).
Additional transformations for ```Map``` instances:
```
from: assertThat(map.get(key)).isEqualTo(value);
to: assertThat(map).containsEntry(key, value);
from: assertThat(map.get(key)).isNotEqualTo(value);
to: assertThat(map).doesNotContainEntry(key, value);
from: assertThat(map.get(key)).isNotNull();
to: assertThat(map).containsKey(key);
from: assertThat(map.get(key)).isNull();
to: assertThat(map).doesNotContainKey(key);
```
The last transformation is the default, but may not be 100% equivalent depending whether the map
is a degenerated case with ```null``` values, where ```map.get(key)``` returns ```null```,
but ```containsKey(key)``` is ```true```.
For that special case (which usually is the result of a bad design decision!)
the quickfix should rather generate ```assertThat(map).containsEntry(key, null)```.
Therefore, the behavior can be configured in the settings for this inspection to either
create the default case (```doesNotContainKey```), the degenerated case (```containsEntry```),
choosing between both fixes (does not work well for batch processing), or ignore this edge case
altogether (just to be sure to not break any code).
- AssertThatFileExpression
Moves ```File``` method calls inside ```assertThat()``` out.
```
from: assertThat(file.canRead()).isTrue();
to: assertThat(file).canRead();
from: assertThat(file.canWrite()).isTrue();
to: assertThat(file).canWrite();
from: assertThat(file.exists()).isTrue();
to: assertThat(file).exists();
from: assertThat(file.exists()).isFalse();
to: assertThat(file).doesNotExist();
from: assertThat(file.isAbsolute()).isTrue();
to: assertThat(file).isAbsolute();
from: assertThat(file.isAbsolute()).isFalse();
to: assertThat(file).isRelative();
from: assertThat(file.isDirectory()).isTrue();
to: assertThat(file).isDirectory();
from: assertThat(file.isFile()).isTrue();
to: assertThat(file).isFile();
from: assertThat(file.getName()).isEqualTo(filename);
to: assertThat(file).hasName(filename);
from: assertThat(file.getParent()).isEqualTo(pathname);
to: assertThat(file).hasParent(pathname);
from: assertThat(file.getParent()).isNull();
from: assertThat(file.getParentFile()).isNull();
to: assertThat(file).hasNoParent();
from: assertThat(file.list()).isEmpty();
from: assertThat(file.listFiles()).isEmpty();
to: assertThat(file).isEmptyDirectory();
from: assertThat(file.list()).isNotEmpty();
from: assertThat(file.listFiles()).isNotEmpty();
to: assertThat(file).isNotEmptyDirectory();
```
and additionally with AssertJ 3.14.0 or later
```
from: assertThat(file.length()).isEqualTo(0);
from: assertThat(file.length()).isZero();
to: assertThat(file).isEmpty();
from: assertThat(file.length()).isNotEqualTo(0);
from: assertThat(file.length()).isNotZero();
to: assertThat(file).isNotEmpty();
from: assertThat(file.length()).isEqualTo(len);
to: assertThat(file).hasSize(len);
```
- AssertThatPathExpression
Moves ```Path``` method calls inside ```assertThat()``` out.
Note: Uses hasParentRaw() instead of hasParent() for quickfixes, because it is semantically
equivalent. For most cases though, hasParent() will show identical behavior.
```
from: assertThat(path.isAbsolute()).isTrue();
to: assertThat(path).isAbsolute();
from: assertThat(path.isAbsolute()).isFalse();
to: assertThat(path).isRelative();
from: assertThat(path.getParent()).isEqualTo(pathname);
to: assertThat(path).hasParentRaw(pathname);
from: assertThat(path.getParent()).isNull();
to: assertThat(path).hasNoParentRaw();
from: assertThat(path.startsWith(otherPath)).isTrue();
to: assertThat(path).startsWithRaw(otherPath);
from: assertThat(path.endsWith(otherPath)).isTrue();
to: assertThat(path).endsWithRaw(otherPath);
```
- AssertThatEnumerableIsEmpty
Uses ```isEmpty()``` for ```hasSize(0)``` iterable assertions instead.
```
from: assertThat(enumerable).hasSize(0);
to: assertThat(enumerable).isEmpty();
```
- AssertThatSize
Makes assertions on sizes on ```Array```, ```Collection```,
```Map```, ```String```, or ```CharSequence``` instances more concise.
```
from: assertThat(array.length).isEqualTo(0);
from: assertThat(array.length).isLessThanOrEqualTo(0);
@@ -75,11 +435,288 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to
from: assertThat(array.length).isEqualTo(anotherArray.length);
to: assertThat(array).hasSameSizeAs(anotherArray);
from: assertThat(array).hasSize(anotherArray.length);
to: assertThat(array).hasSameSizeAs(anotherArray);
```
and analogously for collections...
and additionally with AssertJ 3.12.0 or later
```
from: assertThat(array.length).isLessThanOrEqualTo(expression);
to: assertThat(array).hasSizeLessThanOrEqualTo(expression);
from: assertThat(array.length).isLessThan(expression);
to: assertThat(array).hasSizeLessThan(expression);
from: assertThat(array.length).isGreaterThan(expression);
to: assertThat(array).hasSizeGreaterThan(expression);
from: assertThat(array.length).isGreaterThanOrEqualTo(expression);
to: assertThat(array).hasSizeGreaterThanOrEqualTo(expression);
```
and analogously for ```Collection```, ```Map```, ```String``` and
```CharSequence``` objects, e.g:
```
from: assertThat("string".length()).isLessThan(1);
to: assertThat("string").isEmpty();
from: assertThat(map.size()).isEqualTo(anotherMap.size())
to: assertThat(map).hasSameSizeAs(anotherMap);
from: assertThat("string".length()).hasSize("strong".length())
to: assertThat("string").hasSameSizeAs("strong");
```
- AssertThatBinaryExpression
Splits a boolean condition represented by binary expression out of ```assertThat()```.
```
from: assertThat(primActual == primExpected).isTrue();
to: assertThat(primActual).isEqualTo(primExpected);
from: assertThat(10 < primActual).isNotEqualTo(false);
to: assertThat(primActual).isGreaterThan(10);
from: assertThat(objActual != objExpected).isEqualTo(true);
to: assertThat(objActual).isNotSameAs(objExpected);
from: assertThat(null == objActual).isFalse();
to: assertThat(objActual).isNotNull();
```
...and many, many more combinations (more than 150).
- TwistedAssertion
Examines the actual expression for common mistakes such as mixing expected and actual expression.
For simple cases, a quick fix is offered to swap them. Otherwise, only a warning is issued.
```
from: assertThat(5).isEqualTo(variable);
to: assertThat(variable).isEqualTo(5);
from: assertThat(8.0).isGreaterThan(variable);
to: assertThat(variable).isLessOrEqualTo(8.0);
```
There are, of course, more variations of the theme.
If both sides of an assertion are constant expressions, the problem will only appear as
a weak warning without a quick fix.
Constants used on the actual side of ```.matches()``` and ```doesNotMatch()``` will not be
reported for regular expression testing.
Neither will a ```Class``` type be considered a constant in the classic sense, so
```assertThat(SomeClass.class).isAssignableFrom(SomeOtherClass.class)``` will not be reported.
- BogusAssertion
Sometimes programmers make copy and paste or logical errors writing down assertions
that will never fail due to the same actual and expected assertions.
This inspection will warn about obvious cases such as the following ones.
```
assertThat(object).isEqualTo(object);
assertThat(object).isSameAs(object);
assertThat(object).hasSameClassAs(object);
assertThat(object).hasSameHashCodeAs(object);
assertThat(array).hasSameSizeAs(array);
assertThat(array).contains(array);
assertThat(array).containsAnyOf(array);
assertThat(array).containsExactly(array);
assertThat(array).containsExactlyInAnyOrder(array);
assertThat(array).containsExactlyInAnyOrder(array);
assertThat(array).containsOnly(array);
assertThat(array).containsSequence(array);
assertThat(array).containsSubsequence(array);
assertThat(array).startsWith(array);
assertThat(array).endsWith(array);
assertThat(enumerable).hasSameSizeAs(enumerable);
assertThat(iterable).hasSameElementsAs(iterable);
assertThat(iterable).containsAll(iterable);
assertThat(iterable).containsAnyElementOf(iterable);
assertThat(iterable).containsOnlyElementsOf(iterable);
assertThat(iterable).containsExactlyElementsOf(iterable);
assertThat(iterable).containsSequence(iterable);
assertThat(iterable).containsSubsequence(iterable);
assertThat(charSeq).isEqualToIgnoringCase(charSeq);
assertThat(charSeq).startsWith(charSeq);
assertThat(charSeq).endsWith(charSeq);
assertThat(charSeq).containsSequence(charSeq);
assertThat(charSeq).containsSubsequence(charSeq);
assertThat(map).containsAllEntriesOf(map);
assertThat(map).containsExactlyEntriesOf(map);
assertThat(map).containsExactlyInAnyOrderEntriesOf(map);
assertThat(map).hasSameSizeAs(map);
```
Note that expressions with method calls will not cause a warning as the method call might have side effects
that result in the assertion not being bogus at all.
If the assertions is either ```isEqualTo()``` or ```hasSameHashCodeAs()``` it may be checking custom
```equals()``` or ```hashCode()``` behavior. If the test method name containing the statement has a
name that contains 'equal' or 'hashcode' (case insensitive), the warning will be weakened to information
level.
- ImplicitAssertion
Detects and removes implicit use of ```isNotNull()```, ```isNotEmpty()``` and
```isPresent()``` when followed by an assertion that will implicitly cover this
check.
```
from: assertThat(string).isNotNull().startsWith("foo");
to: assertThat(string).startsWith("foo");
from: assertThat(list).isNotEmpty().hasSize(10);
to: assertThat(list).hasSize(10);
from: assertThat(optional).isPresent().contains("foo");
to: assertThat(optional).contains("foo");
```
...and many more combinations (more than 100).
- AssertThatJava8Optional
Examines the statement for Java 8 ```Optional``` type and whether the statement
effectively tries to assert the presence, absence or content and then
replaces the statement by better assertions.
```
from: assertThat(opt.isPresent()).isEqualTo(true);
from: assertThat(opt.isPresent()).isNotEqualTo(false);
from: assertThat(opt.isPresent()).isTrue();
to: assertThat(opt).isPresent();
from: assertThat(opt.isPresent()).isEqualTo(false);
from: assertThat(opt.isPresent()).isNotEqualTo(true);
from: assertThat(opt.isPresent()).isFalse();
to: assertThat(opt).isNotPresent();
from: assertThat(opt.get()).isEqualTo("foo");
to: assertThat(opt).contains("foo");
from: assertThat(opt.get()).isSameAs("foo");
to: assertThat(opt).containsSame("foo");
from: assertThat(opt.orElse(null)).isEqualTo(null);
to: assertThat(opt).isNotPresent();
from: assertThat(opt.orElse(null)).isNotEqualTo(null);
to: assertThat(opt).isPresent();
from: assertThat(opt).isEqualTo(Optional.of("foo"));
from: assertThat(opt).isEqualTo(Optional.ofNullable("foo")); // only for constant "foo"
to: assertThat(opt).contains("foo");
from: assertThat(opt).isEqualTo(Optional.empty());
to: assertThat(opt).isNotPresent();
from: assertThat(opt).isNotEqualTo(Optional.empty());
to: assertThat(opt).isPresent();
```
- AssertThatGuavaOptional
Examines the statement for Google Guava ```Optional``` type and whether the statement
effectively tries to assert the presence, absence or content and then
replaces the statement by better assertions.
```
from: assertThat(opt.isPresent()).isEqualTo(true);
from: assertThat(opt.isPresent()).isNotEqualTo(false);
from: assertThat(opt.isPresent()).isTrue();
to: assertThat(opt).isPresent();
from: assertThat(opt.isPresent()).isEqualTo(false);
from: assertThat(opt.isPresent()).isNotEqualTo(true);
from: assertThat(opt.isPresent()).isFalse();
to: assertThat(opt).isAbsent();
from: assertThat(opt.get()).isEqualTo("foo");
to: assertThat(opt).contains("foo");
from: assertThat(opt.orNull()).isEqualTo(null);
to: assertThat(opt).isAbsent();
from: assertThat(opt.orNull()).isNotEqualTo(null);
to: assertThat(opt).isPresent();
from: assertThat(opt).isEqualTo(Optional.of("foo"));
from: assertThat(opt).isEqualTo(Optional.fromNullable("foo")); // only for constant "foo"
to: assertThat(opt).contains("foo");
from: assertThat(opt).isEqualTo(Optional.absent());
to: assertThat(opt).isAbsent();
from: assertThat(opt).isNotEqualTo(Optional.absent());
to: assertThat(opt).isPresent();
```
AssertJ for Guava needs to be available in the classpath for this inspection to work.
- AssumeThatInsteadOfReturn
Tries to detect bogus uses of return statements in test methods and replaces them by ```assumeThat()``` calls.
Novices will use these to skip test execution by bailing out early on some preconditions not met.
However, this suggests that the test has actually been run and passed instead of showing the test
as being skipped.
Return statements in ```if``` statements in main test methods (must be annotated with JUnit 4 or
Jupiter ```@Test``` annotations) will be verified to have at least one ```assertThat()``` statement in the code flow.
Method calls within the same class will be examined for ```assertThat()``` statements, too.
However, at most 50 statements and down to five recursions will be tolerated before giving up.
Currently, the quickfix may lose some comments during operation. The other branch of the ```if``` statement
will be inlined (blocks with declarations will remain a code block due to variable scope).
The quickfix will only work with AssertJ >= 2.9.0 (for 2.x releases) or >= 3.9.0 (for 3.x releases).
The generated ```assumeThat()``` statement could be optimized further (similar to ```assertThat()```), but
there is currently no support in Cajon for this (you could rename the method to ```assertThat()``` optimize it
and turn it back into ```assumeThat()``` in the end).
Example:
```
@Test
public void check_fuel_emission() {
if (System.getProperty("manufacturer").equals("Volkswagen")) {
return;
}
double nitroxppm = doWltpDrivingCycle();
assertThat(nitroxppm).isLessThan(500.0);
}
```
will be transformed to
```
@Test
public void check_fuel_emission() {
assumeThat(System.getProperty("manufacturer").equals("Volkswagen")).isFalse();
double nitroxppm = doWltpDrivingCycle();
assertThat(nitroxppm).isLessThan(500.0);
}
```
- JUnitAssertToAssertJ
Tries to convert most of the JUnit 4 assertions and assumptions to AssertJ format.
Sometimes the expected and actual expressions are specified in wrong order --
Cajon tries to swap these when it detects the supposed actual expression to be a
constant while the expected one is not.
Does not support Hamcrest-Matchers.
If you need that kind of conversion, you might want to check out the
[Assertions2AssertJ plugin](https://plugins.jetbrains.com/plugin/10345-assertions2assertj) by Ric Emery.
```
assertTrue(condition);
assertTrue(message, condition);
@@ -87,8 +724,8 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to
assertFalse(message, condition);
assertNull(object);
assertNull(message, object);
assertNonNull(object);
assertNonNull(message, object);
assertNotNull(object);
assertNotNull(message, object);
assertEquals(expected, actual);
assertEquals(message, expected, actual);
assertEquals(expectedDoubleOrFloat, actualDoubleOrFloat, delta);
@@ -105,8 +742,181 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to
assertArrayEquals(message, expectedArray, actualArray);
assertArrayEquals(expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assertArrayEquals(message, expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assumeTrue(condition);
assumeTrue(message, condition);
assumeFalse(condition);
assumeFalse(message, condition);
assumeNotNull(object); // single argument only!
assumeNoException(throwable);
assumeNoException(message, throwable);
```
## TODO
- AssertThatGuavaOptionalContains
- extraction with property names to lambda with Java 8
### Implemented referencing
You can ctrl-click on references inside .extracting() method parameters to go the
referencing method definition.
```
.extracting("field")
.extracting("outerField.fieldInsideObjectTypeOfOuterField.andSoOn")
.extracting("property") // where the class has a getProperty() (or isProperty() for boolean) method
.extracting("bareMethod") // supported with AssertJ 13.12.0
.extracting(Extractors.byName("fieldOrPropertyOrBareMethod")
.extracting(Extractors.byName("fieldOrPropertyOrBareMethod.orAPathLikeAbove")
.extracting(Extractors.resultOf("bareMethod")
.extractingResultOf("bareMethod")
.flatExtracting("fieldOrPropertyOrBareMethod.orAPathLikeAbove")
.flatExtracting(Extractors.byName("fieldOrPropertyOrBareMethod.orAPathLikeAbove")
.flatExtracting(Extractors.resultOf("bareMethod")
```
This works on both POJOs and ```Iterable```s/```Array```s.
Implementation is very basic though and does not work with fancy cascaded ```.extracting()``` sequences.
If there's demand, I could add it.
## Development notice
Cajon is written in Kotlin 1.3.
Cajon is probably the only plugin that uses JUnit 5 Jupiter for unit testing so far (or at least the only one that I'm aware of ;) ).
The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and it took me quite a while to make it work with JUnit 5.
Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for your projects (with attribution).
## Planned features
- More Optional fixes such as ```opt1.get() == opt2.get()``` etc.
- More moving out of methods for LocalDate/Time etc.
- Extraction with property names to lambda/method reference with Java 8
```
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
to: assertThat(object).extracting(type::getPropOne, it -> it.propNoGetter, it -> it.getPropTwo().getInnerProp())...
```
## Changelog
#### V1.9 (25-Feb-20) Mardi Gras Edition
- TwistedAssertion inspection will no longer warn for ```.matches()``` and ```doesNotMatch()``` for regular expressions.
Apparently, ```assertThat("somestring").matches(regex)``` is a valid test if the regex is what needs to be tested.
If the actual expression is of ```Class``` type, this will no longer be reported.
- If the expected expression in TwistedAssertion is also a constant, the warning will be weakened and
no quick fix will be available.
- BogusAssertion inspection will no longer warn if the expression contains method calls.
Moreover, for assertions of ```isEqualTo()``` and ```hasSameHashCodeAs()```, AND if the containing method name contains 'equal' or 'hashcode',
the warning will be reduced to information level as the assertion may be testing ```equals()``` or ```hashCode()``` for validity.
#### V1.8 (14-Feb-20) Valentine Edition
- Maintenance. Removed experimental API use. Updated dependencies. Fixed testing problems introduced with IntelliJ IDEA 2019.3
- Added new TwistedAssertion inspection that will warn about assertions with the actual expression being a constant indicating
swapped use of actual and expected expressions.
- Added new BogusAssertion inspection that showing typical copy and paste errors where actual and expected expressions are the same.
#### V1.7 (19-Nov-19)
- Fixed a lapsuus in AssertThatFileExpression also transforming ```.listFiles()``` with a filter argument.
- Added first version of AssertThatPathExpression for a limited number transformations (more stuff is possible,
but requires detection and transformation of static ```Files```-methods).
- Added AssertThatComparableExpression for funny ```compareTo()``` uses.
- Added ```hasSize(), isEmpty()``` and ```isNotEmpty()``` for AssertThatFileExpression when using AssertJ >= 3.14.0.
#### V1.6 (30-Sep-19)
- Really fixed AssertThatGuavaOptional inspections to avoid conversions from ```.get()``` to ```.contains()```
for array types. Sigh. Shouldn't be working >12h a day and then do some more stuff at home.
- Fixed a bug in AssertThatBinaryExpression inspection for ```assertThat(null != expression)``` and related
that would not correctly invert the condition on transformation.
- Added new AssertThatFileExpression to move out many common methods from inside the
```assertThat()``` expression (```exists(), getName(), getParent()```, and many more).
- Added several transformations to AssertThatStringExpression inspection.
Specifically, uses of ```matches()```, ```compareToIgnoreCase()```, ```indexOf()```, and ```trim()```.
#### V1.5 (24-Sep-19)
- Fix for AssertThatCollectionOrMap inspection sometimes causing an index out of bounds exception.
- AssertThatGuavaOptional inspections will now avoid conversions from ```.get()``` to ```.contains()```
for array types (currently not correctly supported by ```contains()``` in AssertJ-Guava).
- Added an settings option for AssertThatCollectionOrMap inspection respecting the degenerated case of maps with ```null``` values.
It is now possible to change the behavior for ```map.get(key) == null```, so it can offer either ```.doesNotContainKey()``` (default)
or ```.containsEntry(key, null)```, or even both.
- Fixes to AssertThatSize inspection after extending it for Maps in previous release as not all
combinations for ```.hasSameSizeAs()``` are supported.
#### V1.4 (25-Aug-19)
- Minor fix for highlighting of JoinVarArgsContains inspection.
- Extended AssertThatSize inspection to Maps, too.
- Extended AssertThatCollectionOrMap inspection for several ```assertThat(map.get())``` cases as suggested by Georgij G.
#### V1.3 (03-Aug-19)
- New JoinVarArgsContains inspection that will detect multiple ```.contains()```, ```.doesNotContain()```,
and ```.containsOnlyOnce()``` calls within the same statement that could be joined together using variadic arguments.
- AssertJ 3.13.0 broke some inspections due to new ```AbstractStringAssert::isEqualTo()``` method.
- AssertThatJava8Optional and AssertThatGuavaOptional inspections do not longer try to fix
```assertThat(optional).isEqualTo(Optional.fromNullable(expression))``` to ```contains()```
when ```expression``` is not a non-null constant expression.
#### V1.2 (23-Jun-19)
- Due to popular demand the JoinAssertThatStatements inspection will now add line breaks on joining statements.
The amount of statements joined without causing line breaks can be configured but defaults to 1 (always).
#### V1.1 (09-Jun-19)
- Improved JoinAssertThatStatements detection of expressions with side-effects and added pre/post-increment/decrement detection.
- Added Guava Optional ```opt.orNull() == null``` case. You know, I'm not making this stuff up, people actually write this kind of code.
- Added Java 8 Optional ```opt.orElse(null) == null``` case, too.
- Extended JUnitAssertToAssertJ inspection to convert JUnit ```assume``` statements, too.
- Improved JUnitAssertToAssertJ quick fix to swap expected and actual expressions if the actual one is a constant.
- New ImplicitAssertion inspection for implicit ```isNotNull()```, ```isNotEmpty()``` and ```isPresent()``` assertions that will be covered by chained assertions.
- Fix for multiple JUnit Conversions in batch mode with and without delta creating an exception.
- Added new AssertThatObjectExpression inspection for ```toString()``` and ```hashCode()``` and moved ```equals()``` from AssertThatBinaryExpression there.
#### V1.0 (06-May-19)
- First release to be considered stable enough for production use.
- Fixed a NPE in AssumeThatInsteadOfReturn inspection quickfix for empty else branches.
- Fixed missing description for AssumeThatInsteadOfReturn inspection.
- Added new AssertThatCollectionOrMapExpression inspection that tries to pull out methods such as ```isEmpty()``` or ```contains()``` out of an actual ```assertThat()``` expression.
#### V0.8 (05-May-19)
- Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily).
- Fixed ```isEmpty()``` for enumerables and strings and ```isNull()``` for object conversions to be applied only if it is the terminal method call as ```isEmpty()``` and ```isNull()``` return void.
- Heavily reworked inspections for edge cases, such as multiple ```isEqualTo()``` calls inside a single statement.
- Some inspections could generate bogus code for weird situations, this has been made more fool-proof.
- Corrected highlighting for many inspections.
- Fixed family names for inspections in batch mode.
- Reworded many inspection messages for better understanding.
- Added a first version of a new inspection that tries to detect bogus uses of return statements in test methods and replaces them by ```assumeThat()``` calls.
#### V0.7 (28-Apr-19)
- Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions
(really, Jetbrains, this sucks for no reason).
- Extended AssertThatSize inspection to transform ```hasSize()``` into ```hasSameSizeAs()```, if possible.
- Implemented first version of JoinAssertThatStatements inspection that will try to merge ```assertThat()``` statements with the same
actual object together, preserving comments.
#### V0.6 (22-Apr-19)
- New AssertThatStringExpression inspection that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
```startsWith()```, and ```endsWith()``` out of actual expression.
- Extended AssertThatSize inspection to take ```String```s and ```CharSequences``` into account, too.
- New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside ```assertThat()```.
- Renamed a few inspections to better/shorter names.
- New AssertThatInstanceOf inspection that moves instanceof expressions out of ```assertThat()```.
#### V0.5 (18-Apr-19)
- Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3.
- Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right.
- Added support for referencing and refactoring inside ```.extracting()``` methods with fields, properties and methods (though
getter renaming does not work that perfect, but I'm giving up for now as the IntelliJ SDK docs are seriously lacking).
- Fixed an exception in batch mode if the description string was the same but for different fixes.
Now descriptions are different for quick fixes triggered by AssertThatJava8OptionalInspection and AssertThatGuavaOptionalInspection.
#### V0.4 (11-Apr-19)
- Reduced minimal supported IDEA version from 2018.2 to 2017.2.
- New inspection AssertThatJava8Optional that operates on Java 8 ```Optional``` objects and tries to use ```contains()```, ```containsSame()```, ```isPresent()```, and ```isNotPresent()``` instead.
- New inspection AssertThatGuavaOptional that operates on Guava ```Optional``` objects and tries to use ```contains()```, ```isPresent()```, and ```isAbsent()``` instead.
- Added support in AssertThatBinaryExpressionIsTrueOrFalse for ```is(Not)EqualTo(Boolean.TRUE/FALSE)```.
#### V0.3 (07-Apr-19)
- New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and ```equals()``` statements (more than 150 combinations) inside ```assertThat()```.
- Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
- Support for ```hasSizeLessThan()```, ```hasSizeLessThanOrEqualTo()```, ```hasSizeGreaterThanOrEqualTo()```, and ```hasSizeGreaterThan()``` for AssertThatSizeInspection (with AssertJ >=3.12.0).
- Really fixed highlighting for JUnit conversion. Sorry.
#### V0.2 (01-Apr-19)
- Fixed descriptions and quick fix texts.
- Fixed highlighting of found problems and also 'Run inspection by Name' returning nothing.
#### V0.1 (31-Mar-19)
- Initial release.
+41 -17
View File
@@ -1,11 +1,13 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.3'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.intellij' version '0.4.16'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'jacoco'
id 'com.github.kt3k.coveralls' version '2.9.0'
}
group 'de.platon42'
version '0.2'
version '1.9'
repositories {
mavenCentral()
@@ -18,12 +20,12 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile "org.assertj:assertj-core:3.11.1"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.4.0'
testCompile "org.assertj:assertj-core:3.15.0"
testCompile "org.assertj:assertj-guava:3.3.0"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
// testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
}
compileKotlin {
@@ -33,30 +35,52 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
intellij {
version '2018.3.4'
version '2019.3.3'
// pluginName 'Concise AssertJ Optimizing Nitpicker (Cajon)'
updateSinceUntilBuild false
plugins = ['java']
}
patchPluginXml {
changeNotes """
<h4>V0.2 (01-Apr-19)</h4>
<h4>V1.9 (25-Feb-20) Mardi Gras Edition</h4>
<ul>
<li>Fixed descriptions and quick fix texts.
<li>Fixed highlighting of found problems and also 'Run inspection by Name' returning nothing.
<li>TwistedAssertion inspection will no longer warn for .matches() and doesNotMatch() for regular expressions.
Apparently, assertThat("somestring").matches(regex) is a valid test if the regex is what needs to be tested.
If the actual expression is of Class type, this will no longer be reported.
<li>If the expected expression in TwistedAssertion is also a constant, the warning will be weakened and
no quick fix will be available.
<li>BogusAssertion inspection will no longer warn if the expression contains method calls.
Moreover, for assertions of isEqualTo() and hasSameHashCodeAs(), AND if the containing method name contains 'equal' or 'hashcode',
the warning will be reduced to information level as the assertion may be testing equals() or hashCode() for validity.
</ul>
<h4>V0.1 (31-Mar-19)</h4>
<h4>V1.8 (14-Feb-20) Valentine Edition</h4>
<ul>
<li>Initial release.
<li>Maintenance. Removed experimental API use. Updated dependencies. Fixed testing problems introduced with IntelliJ IDEA 2019.3.x
<li>Added new TwistedAssertion inspection that will warn about assertions with the actual expression being a constant indicating
swapped use of actual and expected expressions.
<li>Added new BogusAssertion inspection that showing typical copy and paste errors where actual and expected expressions are the same.
</ul>
<p>Full changelog available at <a href="https://github.com/chrisly42/cajon-plugin#changelog">Github project site</a>.</p>
"""
}
test {
useJUnitPlatform()
// testLogging {
// events "passed", "skipped", "failed"
// }
testLogging {
events "passed", "skipped", "failed"
}
}
jacoco {
toolVersion = '0.8.5'
}
jacocoTestReport {
reports {
xml.enabled true
csv.enabled false
}
}
publishPlugin {
+1 -1
View File
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
@@ -0,0 +1,80 @@
package de.platon42.intellij.plugins.cajon
import org.jetbrains.annotations.NonNls
class AssertJClassNames {
companion object {
@NonNls
const val ASSERTIONS_CLASSNAME = "org.assertj.core.api.Assertions"
@NonNls
const val ASSUMPTIONS_CLASSNAME = "org.assertj.core.api.Assumptions"
@NonNls
const val DESCRIPTABLE_INTERFACE = "org.assertj.core.api.Descriptable"
@NonNls
const val EXTENSION_POINTS_INTERFACE = "org.assertj.core.api.ExtensionPoints"
@NonNls
const val ENUMERABLE_ASSERT_INTERFACE = "org.assertj.core.api.EnumerableAssert"
@NonNls
const val OBJECT_ENUMERABLE_ASSERT_INTERFACE = "org.assertj.core.api.ObjectEnumerableAssert"
@NonNls
const val ASSERT_INTERFACE = "org.assertj.core.api.Assert"
@NonNls
const val ABSTRACT_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractAssert"
@NonNls
const val ABSTRACT_OBJECT_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractObjectAssert"
@NonNls
const val ABSTRACT_BOOLEAN_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractBooleanAssert"
@NonNls
const val ABSTRACT_INTEGER_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIntegerAssert"
@NonNls
const val ABSTRACT_LONG_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractLongAssert"
@NonNls
const val ABSTRACT_FLOAT_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractFloatAssert"
@NonNls
const val ABSTRACT_DOUBLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractDoubleAssert"
@NonNls
const val ABSTRACT_COMPARABLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractComparableAssert"
@NonNls
const val ABSTRACT_STRING_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractStringAssert"
@NonNls
const val ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractCharSequenceAssert"
@NonNls
const val ABSTRACT_MAP_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractMapAssert"
@NonNls
const val ABSTRACT_BOOLEAN_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractBooleanArrayAssert"
@NonNls
const val ABSTRACT_BYTE_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractByteArrayAssert"
@NonNls
const val ABSTRACT_SHORT_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractShortArrayAssert"
@NonNls
const val ABSTRACT_INT_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIntArrayAssert"
@NonNls
const val ABSTRACT_LONG_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractLongArrayAssert"
@NonNls
const val ABSTRACT_FLOAT_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractFloatArrayAssert"
@NonNls
const val ABSTRACT_DOUBLE_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractDoubleArrayAssert"
@NonNls
const val ABSTRACT_CHAR_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractCharArrayAssert"
@NonNls
const val ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractObjectArrayAssert"
@NonNls
const val ABSTRACT_ITERABLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIterableAssert"
@NonNls
const val ABSTRACT_FILE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractFileAssert"
@NonNls
const val ABSTRACT_OPTIONAL_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractOptionalAssert"
@NonNls
const val EXTRACTORS_CLASSNAME = "org.assertj.core.extractor.Extractors"
@NonNls
const val GUAVA_OPTIONAL_CLASSNAME = "com.google.common.base.Optional"
@NonNls
const val GUAVA_ASSERTIONS_CLASSNAME = "org.assertj.guava.api.Assertions"
@NonNls
const val GUAVA_OPTIONAL_ASSERTIONS_CLASSNAME = "org.assertj.guava.api.OptionalAssert"
}
}
@@ -0,0 +1,53 @@
package de.platon42.intellij.plugins.cajon
import com.intellij.psi.CommonClassNames
import com.siyeh.ig.callMatcher.CallMatcher
val CORE_ASSERT_THAT_MATCHER = CallMatcher.staticCall(AssertJClassNames.ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)!!
val GUAVA_ASSERT_THAT_MATCHER = CallMatcher.staticCall(AssertJClassNames.GUAVA_ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)!!
val ALL_ASSERT_THAT_MATCHERS = CallMatcher.anyOf(CORE_ASSERT_THAT_MATCHER, GUAVA_ASSERT_THAT_MATCHER)!!
val EXTRACTING_FROM_OBJECT = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ASSERT_CLASSNAME, "extracting")!!
val EXTRACTING_FROM_ITERABLE = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_ITERABLE_ASSERT_CLASSNAME, "extracting")!!
val FLAT_EXTRACTING_FROM_ITERABLE = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_ITERABLE_ASSERT_CLASSNAME, "flatExtracting")!!
val EXTRACTING_RESULT_OF_FROM_ITERABLE = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_ITERABLE_ASSERT_CLASSNAME, "extractingResultOf")!!
val EXTRACTING_CALL_MATCHERS = CallMatcher.anyOf(
EXTRACTING_FROM_OBJECT,
EXTRACTING_FROM_ITERABLE,
FLAT_EXTRACTING_FROM_ITERABLE,
EXTRACTING_RESULT_OF_FROM_ITERABLE
)!!
val DESCRIBED_AS = CallMatcher.instanceCall(AssertJClassNames.DESCRIPTABLE_INTERFACE, MethodNames.DESCRIBED_AS, MethodNames.AS)!!
val WITH_REPRESENTATION_AND_SUCH = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, "withRepresentation", "withThreadDumpOnError")!!
val USING_COMPARATOR = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, "usingComparator", "usingDefaultComparator")!!
val IN_HEXADECIMAL_OR_BINARY = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_ASSERT_CLASSNAME, MethodNames.IN_HEXADECIMAL, MethodNames.IN_BINARY)!!
val EXTENSION_POINTS = CallMatcher.instanceCall(
AssertJClassNames.EXTENSION_POINTS_INTERFACE,
"is", "isNot", "has", "doesNotHave",
"satisfies"
)!!
val MORE_EXTENSION_POINTS = CallMatcher.instanceCall(
AssertJClassNames.OBJECT_ENUMERABLE_ASSERT_INTERFACE,
"are", "areNot", "have", "doNotHave",
"areAtLeast", "areAtLeastOne", "areAtMost", "areExactly",
"haveAtLeastOne", "haveAtLeast", "haveAtMost", "haveExactly",
"hasOnlyOneElementSatisfying", "anyMatch", "noneMatch", "anySatisfy", "noneSatisfy"
)!!
val COMPLEX_CALLS_THAT_MAKES_STUFF_TRICKY = CallMatcher.anyOf(
DESCRIBED_AS,
WITH_REPRESENTATION_AND_SUCH,
USING_COMPARATOR,
IN_HEXADECIMAL_OR_BINARY
)!!
val NOT_ACTUAL_ASSERTIONS = CallMatcher.anyOf(
ALL_ASSERT_THAT_MATCHERS,
COMPLEX_CALLS_THAT_MAKES_STUFF_TRICKY
)!!
val KNOWN_METHODS_WITH_SIDE_EFFECTS = CallMatcher.anyOf(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_ITERATOR, "next")
)!!
@@ -0,0 +1,184 @@
package de.platon42.intellij.plugins.cajon
import com.intellij.psi.*
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.inspections.AbstractAssertJInspection
val PsiMethodCallExpression.qualifierExpression: PsiExpression get() = methodExpression.qualifierExpression!!
val PsiMethodCallExpression.firstArg: PsiExpression get() = getArg(0)
fun PsiElement.hasAssertThat(): Boolean {
val elementText = text
return elementText.startsWith("${MethodNames.ASSERT_THAT}(") || elementText.contains(".${MethodNames.ASSERT_THAT}(")
}
fun PsiMethodCallExpression.replaceQualifier(qualifier: PsiElement) {
qualifierExpression.replace(qualifier)
}
fun PsiMethodCallExpression.replaceQualifierFromMethodCall(oldMethodCall: PsiMethodCallExpression) {
qualifierExpression.replace(oldMethodCall.qualifierExpression)
}
fun PsiElement.findOutmostMethodCall(): PsiMethodCallExpression? {
val statement = PsiTreeUtil.getParentOfType(this, PsiStatement::class.java, false) ?: return null
return PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java)
}
fun PsiElement.findStaticMethodCall(): PsiMethodCallExpression? {
var elem: PsiElement? = this
while (elem != null) {
if ((elem is PsiMethodCallExpression) && (elem.resolveMethod()?.hasModifierProperty(PsiModifier.STATIC) == true)) {
return elem
}
elem = elem.firstChild
}
return null
}
fun PsiElement.gatherAssertionCalls(): List<PsiMethodCallExpression> {
val assertThatMethodCall = findStaticMethodCall() ?: return emptyList()
return assertThatMethodCall.collectMethodCallsUpToStatement()
.filterNot(NOT_ACTUAL_ASSERTIONS::test)
.toList()
}
fun PsiMethodCallExpression.collectMethodCallsUpToStatement(): Sequence<PsiMethodCallExpression> {
return generateSequence(this) { PsiTreeUtil.getParentOfType(it, PsiMethodCallExpression::class.java, true, PsiStatement::class.java) }
}
fun PsiMethodCallExpression.findFluentCallTo(matcher: CallMatcher): PsiMethodCallExpression? {
return collectMethodCallsUpToStatement().find { matcher.test(it) }
}
fun PsiMethodCallExpression.getArg(n: Int): PsiExpression = PsiUtil.skipParenthesizedExprDown(argumentList.expressions[n])!!
fun PsiMethodCallExpression.getArgOrNull(n: Int): PsiExpression? = argumentList.expressions.getOrNull(n)
fun <T> Boolean.map(forTrue: T, forFalse: T) = if (this) forTrue else forFalse
fun PsiMethod.addAsStaticImport(context: PsiElement, vararg allowedClashes: String) {
val factory = JavaPsiFacade.getElementFactory(context.project)
val methodName = name
val containingClass = containingClass ?: return
val importList = (context.containingFile as PsiJavaFile).importList ?: return
val notImportedStatically = importList.importStaticStatements.none {
val targetClass = it.resolveTargetClass() ?: return@none false
((it.referenceName == methodName) && !allowedClashes.contains(targetClass.qualifiedName))
|| (it.isOnDemand && (targetClass == containingClass))
}
if (notImportedStatically) {
importList.add(factory.createImportStaticStatement(containingClass, methodName))
}
}
fun PsiElement.shortenAndReformat() {
val codeStyleManager = JavaCodeStyleManager.getInstance(project)
codeStyleManager.shortenClassReferences(this)
CodeStyleManager.getInstance(project).reformat(this)
}
fun PsiMethodCallExpression.getExpectedBooleanResult(): Boolean? {
val isTrue = AbstractAssertJInspection.IS_TRUE.test(this)
val isFalse = AbstractAssertJInspection.IS_FALSE.test(this)
if (isTrue || isFalse) {
return isTrue
} else {
val isEqualTo = AbstractAssertJInspection.IS_EQUAL_TO_BOOLEAN.test(this) || AbstractAssertJInspection.IS_EQUAL_TO_OBJECT.test(this)
val isNotEqualTo =
AbstractAssertJInspection.IS_NOT_EQUAL_TO_BOOLEAN.test(this) || AbstractAssertJInspection.IS_NOT_EQUAL_TO_OBJECT.test(this)
if (isEqualTo || isNotEqualTo) {
val constValue = calculateConstantParameterValue(0) as? Boolean ?: return null
return isNotEqualTo xor constValue
}
}
return null
}
fun PsiMethodCallExpression.getExpectedNullNonNullResult(): Boolean? {
val isNull = AbstractAssertJInspection.IS_NULL.test(this)
val isNotNull = AbstractAssertJInspection.IS_NOT_NULL.test(this)
if (isNull || isNotNull) {
return isNotNull
} else {
val isEqualTo = CallMatcher.anyOf(AbstractAssertJInspection.IS_EQUAL_TO_OBJECT, AbstractAssertJInspection.IS_EQUAL_TO_STRING).test(this)
val isNotEqualTo = AbstractAssertJInspection.IS_NOT_EQUAL_TO_OBJECT.test(this)
if ((isEqualTo || isNotEqualTo) && firstArg.type == PsiType.NULL) {
return isNotEqualTo
}
}
return null
}
fun PsiMethodCallExpression.calculateConstantParameterValue(argIndex: Int): Any? {
if (argIndex >= argumentList.expressions.size) return null
return getArg(argIndex).calculateConstantValue()
}
fun PsiExpression.calculateConstantValue(): Any? {
val constantEvaluationHelper = JavaPsiFacade.getInstance(project).constantEvaluationHelper
val value = constantEvaluationHelper.computeConstantExpression(this)
if (value == null) {
val field = (this as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
return when (field.name) {
"TRUE" -> true
"FALSE" -> false
else -> null
}
}
}
return value
}
fun PsiExpression.getAllTheSameExpectedBooleanConstants(): Boolean? {
val assertThatMethodCall = findStaticMethodCall() ?: return null
var lockedResult: Boolean? = null
val methodsToView = generateSequence(assertThatMethodCall) { PsiTreeUtil.getParentOfType(it, PsiMethodCallExpression::class.java) }
for (methodCall in methodsToView) {
val expectedResult = methodCall.getExpectedBooleanResult()
if (expectedResult != null) {
if ((lockedResult != null) && (lockedResult != expectedResult)) return null
lockedResult = expectedResult
} else {
val isNotConstant = CallMatcher.anyOf(
EXTENSION_POINTS,
MORE_EXTENSION_POINTS,
AbstractAssertJInspection.IS_EQUAL_TO_BOOLEAN,
AbstractAssertJInspection.IS_EQUAL_TO_OBJECT,
AbstractAssertJInspection.IS_NOT_EQUAL_TO_BOOLEAN,
AbstractAssertJInspection.IS_NOT_EQUAL_TO_OBJECT
).test(methodCall)
if (isNotConstant) return null
}
}
return lockedResult
}
fun PsiExpression.getAllTheSameNullNotNullConstants(): Boolean? {
val assertThatMethodCall = findStaticMethodCall() ?: return null
var lockedResult: Boolean? = null
val methodsToView = generateSequence(assertThatMethodCall) { PsiTreeUtil.getParentOfType(it, PsiMethodCallExpression::class.java) }
for (methodCall in methodsToView) {
val expectedResult = methodCall.getExpectedNullNonNullResult()
if (expectedResult != null) {
if ((lockedResult != null) && (lockedResult != expectedResult)) return null
lockedResult = expectedResult
} else {
val isNotConstant = CallMatcher.anyOf(
EXTENSION_POINTS,
MORE_EXTENSION_POINTS,
AbstractAssertJInspection.IS_EQUAL_TO_OBJECT,
AbstractAssertJInspection.IS_NOT_EQUAL_TO_OBJECT
).test(methodCall)
if (isNotConstant) return null
}
}
return lockedResult
}
@@ -0,0 +1,44 @@
package de.platon42.intellij.plugins.cajon
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.search.GlobalSearchScope
fun createAssertThat(context: PsiElement, actualExpression: PsiExpression): PsiMethodCallExpression {
return createAssertThat(context, AssertJClassNames.ASSERTIONS_CLASSNAME, actualExpression)
}
fun createGuavaAssertThat(context: PsiElement, actualExpression: PsiExpression): PsiMethodCallExpression {
return createAssertThat(context, AssertJClassNames.GUAVA_ASSERTIONS_CLASSNAME, actualExpression)
}
fun createAssertThat(context: PsiElement, baseclass: String, actualExpression: PsiExpression): PsiMethodCallExpression {
return createMethodCall(context, "$baseclass.${MethodNames.ASSERT_THAT}", actualExpression)
}
fun createAssumeThat(context: PsiElement, actualExpression: PsiExpression): PsiMethodCallExpression {
return createMethodCall(context, "${AssertJClassNames.ASSUMPTIONS_CLASSNAME}.${MethodNames.ASSUME_THAT}", actualExpression)
}
fun createExpectedMethodCall(context: PsiElement, methodName: String, vararg arguments: PsiElement): PsiMethodCallExpression {
return createMethodCall(context, "a.$methodName", *arguments)
}
fun createMethodCall(context: PsiElement, fullQualifiedMethodName: String, vararg arguments: PsiElement): PsiMethodCallExpression {
val factory = JavaPsiFacade.getElementFactory(context.project)
val argString = generateSequence('b') { it + 1 }.take(arguments.size).joinToString(", ")
val expectedExpression = factory.createExpressionFromText(
"$fullQualifiedMethodName($argString)", context
) as PsiMethodCallExpression
arguments.forEachIndexed { index, newArg -> expectedExpression.getArg(index).replace(newArg) }
return expectedExpression
}
fun hasAssertJMethod(element: PsiElement, classname: String, methodname: String): Boolean {
val findClass =
JavaPsiFacade.getInstance(element.project).findClass(classname, GlobalSearchScope.allScope(element.project))
?: return false
return findClass.allMethods.any { it.name == methodname }
}
@@ -0,0 +1,130 @@
package de.platon42.intellij.plugins.cajon
import org.jetbrains.annotations.NonNls
class MethodNames {
companion object {
@NonNls
const val EQUALS = "equals"
@NonNls
const val ASSERT_THAT = "assertThat"
@NonNls
const val ASSUME_THAT = "assumeThat"
@NonNls
const val AS = "as"
@NonNls
const val DESCRIBED_AS = "describedAs"
@NonNls
const val IN_HEXADECIMAL = "inHexadecimal"
@NonNls
const val IN_BINARY = "inBinary"
@NonNls
const val IS_EQUAL_TO = "isEqualTo"
@NonNls
const val IS_NOT_EQUAL_TO = "isNotEqualTo"
@NonNls
const val IS_SAME_AS = "isSameAs"
@NonNls
const val IS_NOT_SAME_AS = "isNotSameAs"
@NonNls
const val HAS_TO_STRING = "hasToString"
@NonNls
const val IS_GREATER_THAN = "isGreaterThan"
@NonNls
const val IS_GREATER_THAN_OR_EQUAL_TO = "isGreaterThanOrEqualTo"
@NonNls
const val IS_LESS_THAN = "isLessThan"
@NonNls
const val IS_LESS_THAN_OR_EQUAL_TO = "isLessThanOrEqualTo"
@NonNls
const val IS_ZERO = "isZero"
@NonNls
const val IS_NOT_ZERO = "isNotZero"
@NonNls
const val IS_TRUE = "isTrue"
@NonNls
const val IS_FALSE = "isFalse"
@NonNls
const val IS_NULL = "isNull" // terminal, returns void
@NonNls
const val IS_NOT_NULL = "isNotNull"
@NonNls
const val IS_CLOSE_TO = "isCloseTo"
@NonNls
const val IS_NOT_CLOSE_TO = "isNotCloseTo"
@NonNls
const val IS_INSTANCE_OF = "isInstanceOf"
@NonNls
const val IS_NOT_INSTANCE_OF = "isNotInstanceOf"
@NonNls
const val IS_NULL_OR_EMPTY = "isNullOrEmpty" // terminal, returns void
@NonNls
const val IS_EMPTY = "isEmpty" // terminal, returns void
@NonNls
const val IS_NOT_EMPTY = "isNotEmpty"
@NonNls
const val HAS_SIZE = "hasSize"
@NonNls
const val HAS_SIZE_LESS_THAN = "hasSizeLessThan"
@NonNls
const val HAS_SIZE_LESS_THAN_OR_EQUAL_TO = "hasSizeLessThanOrEqualTo"
@NonNls
const val HAS_SIZE_GREATER_THAN = "hasSizeGreaterThan"
@NonNls
const val HAS_SIZE_GREATER_THAN_OR_EQUAL_TO = "hasSizeGreaterThanOrEqualTo"
@NonNls
const val HAS_SAME_SIZE_AS = "hasSameSizeAs"
@NonNls
const val CONTAINS = "contains"
@NonNls
const val CONTAINS_ONLY_ONCE = "containsOnlyOnce"
@NonNls
const val DOES_NOT_CONTAIN = "doesNotContain"
@NonNls
const val CONTAINS_EXACTLY = "containsExactly"
@NonNls
const val CONTAINS_ALL = "containsAll"
@NonNls
const val CONTAINS_KEY = "containsKey"
@NonNls
const val DOES_NOT_CONTAIN_KEY = "doesNotContainKey"
@NonNls
const val CONTAINS_VALUE = "containsValue"
@NonNls
const val DOES_NOT_CONTAIN_VALUE = "doesNotContainValue"
@NonNls
const val CONTAINS_ENTRY = "containsEntry"
@NonNls
const val DOES_NOT_CONTAIN_ENTRY = "doesNotContainEntry"
@NonNls
const val IS_EQUAL_TO_IC = "isEqualToIgnoringCase"
@NonNls
const val IS_NOT_EQUAL_TO_IC = "isNotEqualToIgnoringCase"
@NonNls
const val STARTS_WITH = "startsWith"
@NonNls
const val ENDS_WITH = "endsWith"
@NonNls
const val DOES_NOT_START_WITH = "doesNotStartWith"
@NonNls
const val DOES_NOT_END_WITH = "doesNotEndWith"
@NonNls
const val CONTAINS_SAME = "containsSame"
@NonNls
const val IS_PRESENT = "isPresent"
@NonNls
const val IS_NOT_PRESENT = "isNotPresent"
@NonNls
const val IS_ABSENT = "isAbsent"
}
}
@@ -1,97 +1,202 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.CommonClassNames
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiCapturedWildcardType
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTypesUtil
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_BOOLEAN_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_COMPARABLE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_DOUBLE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_FLOAT_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_INTEGER_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_LONG_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_STRING_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ASSERTIONS_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ASSERT_INTERFACE
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ENUMERABLE_ASSERT_INTERFACE
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.GUAVA_ASSERTIONS_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.GUAVA_OPTIONAL_CLASSNAME
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.qualifierExpression
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSimpleMethodCallQuickFix
import org.jetbrains.annotations.NonNls
open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
abstract class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
companion object {
const val SIMPLIFY_MESSAGE_TEMPLATE = "%s can be simplified to %s"
const val SIMPLIFY_MESSAGE_TEMPLATE = "%s() can be simplified to %s()"
const val MORE_CONCISE_MESSAGE_TEMPLATE = "%s() would be more concise than %s()"
const val REPLACE_DESCRIPTION_TEMPLATE = "Replace %s with %s"
const val REPLACE_DESCRIPTION_TEMPLATE = "Replace %s() with %s()"
const val REMOVE_EXPECTED_OUTMOST_DESCRIPTION_TEMPLATE = "Unwrap expected expression and replace %s() with %s()"
const val MOVE_ACTUAL_EXPRESSION_DESCRIPTION_TEMPLATE = "Remove %s() of actual expression and use assertThat().%s() instead"
const val MOVING_OUT_MESSAGE_TEMPLATE = "Moving %s() expression out of assertThat() would be more concise"
@NonNls
const val ASSERTIONS_CLASSNAME = "org.assertj.core.api.Assertions"
val TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP = mapOf<IElementType, String>(
JavaTokenType.EQEQ to MethodNames.IS_EQUAL_TO,
JavaTokenType.NE to MethodNames.IS_NOT_EQUAL_TO,
JavaTokenType.GT to MethodNames.IS_GREATER_THAN,
JavaTokenType.GE to MethodNames.IS_GREATER_THAN_OR_EQUAL_TO,
JavaTokenType.LT to MethodNames.IS_LESS_THAN,
JavaTokenType.LE to MethodNames.IS_LESS_THAN_OR_EQUAL_TO
)
@NonNls
const val ABSTRACT_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractAssert"
@NonNls
const val ABSTRACT_BOOLEAN_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractBooleanAssert"
@NonNls
const val ABSTRACT_INTEGER_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIntegerAssert"
@NonNls
const val ABSTRACT_COMPARABLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractComparableAssert"
@NonNls
const val ABSTRACT_STRING_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractStringAssert"
@NonNls
const val ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractCharSequenceAssert"
@NonNls
const val ABSTRACT_ENUMERABLE_ASSERT_CLASSNAME = "org.assertj.core.api.EnumerableAssert"
val TOKEN_TO_ASSERTJ_FOR_OBJECT_MAPPINGS = mapOf<IElementType, String>(
JavaTokenType.EQEQ to MethodNames.IS_SAME_AS,
JavaTokenType.NE to MethodNames.IS_NOT_SAME_AS
)
@NonNls
const val ASSERT_THAT_METHOD = "assertThat"
@NonNls
const val IS_EQUAL_TO_METHOD = "isEqualTo"
@NonNls
const val IS_NOT_EQUAL_TO_METHOD = "isNotEqualTo"
@NonNls
const val IS_GREATER_THAN_METHOD = "isGreaterThan"
@NonNls
const val IS_GREATER_THAN_OR_EQUAL_TO_METHOD = "isGreaterThanOrEqualTo"
@NonNls
const val IS_LESS_THAN_METHOD = "isLessThan"
@NonNls
const val IS_LESS_THAN_OR_EQUAL_TO_METHOD = "isLessThanOrEqualTo"
@NonNls
const val IS_ZERO_METHOD = "isZero"
@NonNls
const val IS_NOT_ZERO_METHOD = "isNotZero"
@NonNls
const val HAS_SIZE_METHOD = "hasSize"
val SWAP_SIDE_OF_BINARY_OPERATOR = mapOf<IElementType, IElementType>(
JavaTokenType.GT to JavaTokenType.LT,
JavaTokenType.GE to JavaTokenType.LE,
JavaTokenType.LT to JavaTokenType.GT,
JavaTokenType.LE to JavaTokenType.GE
)
val ASSERT_THAT_INT = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, ASSERT_THAT_METHOD)
val INVERT_BINARY_OPERATOR = mapOf<IElementType, IElementType>(
JavaTokenType.EQEQ to JavaTokenType.NE,
JavaTokenType.NE to JavaTokenType.EQEQ,
JavaTokenType.GT to JavaTokenType.LE,
JavaTokenType.GE to JavaTokenType.LT,
JavaTokenType.LT to JavaTokenType.GE,
JavaTokenType.LE to JavaTokenType.GT
)
val ASSERT_THAT_INT = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)
.parameterTypes("int")!!
val IS_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_NOT_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO_METHOD)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
val ASSERT_THAT_BOOLEAN = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)
.parameterTypes("boolean")!!
val IS_NOT_EQUAL_TO_BOOLEAN =
CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO_METHOD)
val ASSERT_THAT_ANY = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)
.parameterCount(1)!!
val ASSERT_THAT_JAVA8_OPTIONAL = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)
.parameterTypes(CommonClassNames.JAVA_UTIL_OPTIONAL)!!
val GUAVA_ASSERT_THAT_ANY = CallMatcher.staticCall(GUAVA_ASSERTIONS_CLASSNAME, MethodNames.ASSERT_THAT)
.parameterCount(1)!!
val IS_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_EQUAL_TO)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_EQUAL_TO_STRING = CallMatcher.instanceCall(ABSTRACT_STRING_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes(CommonClassNames.JAVA_LANG_STRING)!!
val IS_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes("int")!!
val IS_EQUAL_TO_LONG = CallMatcher.instanceCall(ABSTRACT_LONG_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes("long")!!
val IS_EQUAL_TO_FLOAT = CallMatcher.instanceCall(ABSTRACT_FLOAT_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes("float")!!
val IS_EQUAL_TO_DOUBLE = CallMatcher.instanceCall(ABSTRACT_DOUBLE_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes("double")!!
val IS_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO)
.parameterTypes("boolean")!!
val HAS_SIZE = CallMatcher.instanceCall(ABSTRACT_ENUMERABLE_ASSERT_CLASSNAME, HAS_SIZE_METHOD)
.parameterTypes("int")!!
val IS_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
.parameterTypes("int")!!
val IS_GREATER_THAN_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, IS_GREATER_THAN_METHOD)
.parameterTypes("int")!!
val IS_GREATER_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, IS_GREATER_THAN_OR_EQUAL_TO_METHOD)
val IS_NOT_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_NOT_EQUAL_TO_BOOLEAN = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes("boolean")!!
val IS_NOT_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes("int")!!
val IS_NOT_EQUAL_TO_LONG = CallMatcher.instanceCall(ABSTRACT_LONG_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes("long")!!
val IS_NOT_EQUAL_TO_FLOAT = CallMatcher.instanceCall(ABSTRACT_FLOAT_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes("float")!!
val IS_NOT_EQUAL_TO_DOUBLE = CallMatcher.instanceCall(ABSTRACT_DOUBLE_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO)
.parameterTypes("double")!!
val IS_LESS_THAN_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, IS_LESS_THAN_METHOD)
.parameterTypes("int")!!
val IS_LESS_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, IS_LESS_THAN_OR_EQUAL_TO_METHOD)
.parameterTypes("int")!!
val IS_SAME_AS_OBJECT = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_SAME_AS)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_NOT_SAME_AS_OBJECT = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_NOT_SAME_AS)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_ZERO = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, IS_ZERO_METHOD)
val IS_NULL = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_NULL)
.parameterCount(0)!!
val IS_NOT_ZERO = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, IS_NOT_ZERO_METHOD)
val IS_NOT_NULL = CallMatcher.instanceCall(ASSERT_INTERFACE, MethodNames.IS_NOT_NULL)
.parameterCount(0)!!
val IS_EMPTY = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.IS_EMPTY)
.parameterCount(0)!!
val IS_NOT_EMPTY = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.IS_NOT_EMPTY)
.parameterCount(0)!!
val HAS_SIZE = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE)
.parameterTypes("int")!!
val IS_GREATER_THAN_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_GREATER_THAN)
.parameterTypes("int")!!
val IS_GREATER_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_GREATER_THAN_OR_EQUAL_TO)
.parameterTypes("int")!!
val IS_LESS_THAN_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_LESS_THAN)
.parameterTypes("int")!!
val IS_LESS_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_LESS_THAN_OR_EQUAL_TO)
.parameterTypes("int")!!
val IS_ZERO_INT = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, MethodNames.IS_ZERO)
.parameterCount(0)!!
val IS_NOT_ZERO_INT = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, MethodNames.IS_NOT_ZERO)
.parameterCount(0)!!
val IS_ZERO_LONG = CallMatcher.instanceCall(ABSTRACT_LONG_ASSERT_CLASSNAME, MethodNames.IS_ZERO)
.parameterCount(0)!!
val IS_NOT_ZERO_LONG = CallMatcher.instanceCall(ABSTRACT_LONG_ASSERT_CLASSNAME, MethodNames.IS_NOT_ZERO)
.parameterCount(0)!!
val IS_ONE = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, "isOne")
.parameterCount(0)!!
val IS_NEGATIVE = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, "isNegative")
.parameterCount(0)!!
val IS_NOT_NEGATIVE = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, "isNotNegative")
.parameterCount(0)!!
val IS_POSITIVE = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, "isPositive")
.parameterCount(0)!!
val IS_NOT_POSITIVE = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, "isNotPositive")
.parameterCount(0)!!
val IS_TRUE = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, MethodNames.IS_TRUE)
.parameterCount(0)!!
val IS_FALSE = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, MethodNames.IS_FALSE)
.parameterCount(0)!!
val COLLECTION_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "size")
.parameterCount(0)!!
val MAP_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "size")
.parameterCount(0)!!
val CHAR_SEQUENCE_LENGTH = CallMatcher.instanceCall("java.lang.CharSequence", "length")
.parameterCount(0)!!
val OBJECT_EQUALS = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "equals")
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val OPTIONAL_GET = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "get")
.parameterCount(0)!!
val OPTIONAL_IS_PRESENT = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "isPresent")
.parameterCount(0)!!
val OPTIONAL_OR_ELSE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "orElse")
.parameterCount(1)!!
val OPTIONAL_OF = CallMatcher.staticCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "of")
.parameterCount(1)!!
val OPTIONAL_OF_NULLABLE = CallMatcher.staticCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "ofNullable")
.parameterCount(1)!!
val OPTIONAL_EMPTY = CallMatcher.staticCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "empty")
.parameterCount(0)!!
val GUAVA_OPTIONAL_GET = CallMatcher.instanceCall(GUAVA_OPTIONAL_CLASSNAME, "get")
.parameterCount(0)!!
val GUAVA_OPTIONAL_IS_PRESENT = CallMatcher.instanceCall(GUAVA_OPTIONAL_CLASSNAME, "isPresent")
.parameterCount(0)!!
val GUAVA_OPTIONAL_OR_NULL = CallMatcher.instanceCall(GUAVA_OPTIONAL_CLASSNAME, "orNull")
.parameterCount(0)!!
val GUAVA_OPTIONAL_OF = CallMatcher.staticCall(GUAVA_OPTIONAL_CLASSNAME, "of")
.parameterCount(1)!!
val GUAVA_OPTIONAL_FROM_NULLABLE = CallMatcher.staticCall(GUAVA_OPTIONAL_CLASSNAME, "fromNullable")
.parameterCount(1)!!
val GUAVA_OPTIONAL_ABSENT = CallMatcher.staticCall(GUAVA_OPTIONAL_CLASSNAME, "absent")
.parameterCount(0)!!
}
override fun getGroupDisplayName(): String {
@@ -99,8 +204,8 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
}
protected fun checkAssertedType(expression: PsiMethodCallExpression, classname: String): Boolean {
var assertedType = expression.methodExpression.qualifierExpression?.type ?: return false
if (assertedType is PsiCapturedWildcardType) {
var assertedType = expression.qualifierExpression.type ?: return false
while (assertedType is PsiCapturedWildcardType) {
assertedType = assertedType.upperBound
}
val assertedClass = PsiTypesUtil.getPsiClass(assertedType) ?: return false
@@ -109,8 +214,7 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
return assertedClass.isEquivalentTo(expectedClass) || assertedClass.isInheritor(expectedClass, true)
}
protected fun getOriginalMethodName(expression: PsiMethodCallExpression) =
expression.resolveMethod()?.name?.plus("()")
protected fun getOriginalMethodName(expression: PsiMethodCallExpression) = expression.resolveMethod()?.name
protected fun registerSimplifyMethod(
holder: ProblemsHolder,
@@ -120,17 +224,72 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = SIMPLIFY_MESSAGE_TEMPLATE.format(originalMethod, replacementMethod)
holder.registerProblem(
expression,
message,
ReplaceSimpleMethodCallQuickFix(description, replacementMethod)
)
val quickFix = ReplaceSimpleMethodCallQuickFix(description, replacementMethod)
val textRange = TextRange(expression.qualifierExpression.textLength, expression.textLength)
holder.registerProblem(expression, textRange, message, quickFix)
}
protected fun calculateConstantParameterValue(expression: PsiMethodCallExpression, argIndex: Int): Any? {
if (argIndex >= expression.argumentList.expressionCount) return null
val valueExpression = expression.argumentList.expressions[argIndex] ?: return null
val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
return constantEvaluationHelper.computeConstantExpression(valueExpression)
protected fun registerMoveOutMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldActualExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val originalMethod = getOriginalMethodName(oldActualExpression) ?: return
val description = MOVE_ACTUAL_EXPRESSION_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = MOVING_OUT_MESSAGE_TEMPLATE.format(originalMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, message, quickfix)
}
protected fun registerMoveOutMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldActualExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String) -> List<LocalQuickFix>
) {
val originalMethod = getOriginalMethodName(oldActualExpression) ?: return
val description = MOVE_ACTUAL_EXPRESSION_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = MOVING_OUT_MESSAGE_TEMPLATE.format(originalMethod)
val quickfixes = quickFixSupplier(description)
holder.registerProblem(expression, message, *quickfixes.toTypedArray())
}
protected fun registerReplaceMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldExpectedCallExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
registerConciseMethod(REPLACE_DESCRIPTION_TEMPLATE, holder, expression, oldExpectedCallExpression, replacementMethod, quickFixSupplier)
}
protected fun registerConciseMethod(
descriptionTemplate: String,
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldExpectedCallExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val originalMethod = getOriginalMethodName(oldExpectedCallExpression) ?: return
val description = descriptionTemplate.format(originalMethod, replacementMethod)
val message = MORE_CONCISE_MESSAGE_TEMPLATE.format(replacementMethod, originalMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
val textRange = TextRange(expression.qualifierExpression.textLength, expression.textLength)
holder.registerProblem(expression, textRange, message, quickfix)
}
protected fun registerRemoveExpectedOutmostMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldExpectedCallExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
registerConciseMethod(REMOVE_EXPECTED_OUTMOST_DESCRIPTION_TEMPLATE, holder, expression, oldExpectedCallExpression, replacementMethod, quickFixSupplier)
}
}
@@ -7,12 +7,10 @@ import org.jetbrains.annotations.NonNls
open class AbstractJUnitAssertInspection : AbstractBaseJavaLocalInspectionTool() {
companion object {
const val CONVERT_MESSAGE_TEMPLATE = "%s can be converted to AssertJ style"
const val REPLACE_DESCRIPTION_TEMPLATE = "Replace %s with assertThat().%s"
@NonNls
const val JUNIT_ASSERT_CLASSNAME = "org.junit.Assert"
@NonNls
const val JUNIT_ASSUME_CLASSNAME = "org.junit.Assume"
@NonNls
const val ASSERT_TRUE_METHOD = "assertTrue"
@@ -32,12 +30,21 @@ open class AbstractJUnitAssertInspection : AbstractBaseJavaLocalInspectionTool()
const val ASSERT_NOT_SAME_METHOD = "assertNotSame"
@NonNls
const val ASSERT_ARRAY_EQUALS_METHOD = "assertArrayEquals"
@NonNls
const val ASSUME_TRUE_METHOD = "assumeTrue"
@NonNls
const val ASSUME_FALSE_METHOD = "assumeFalse"
@NonNls
const val ASSUME_NOT_NULL_METHOD = "assumeNotNull"
@NonNls
const val ASSUME_NO_EXCEPTION = "assumeNoException"
}
override fun getGroupDisplayName(): String {
return "AssertJ"
}
protected fun getOriginalMethodName(expression: PsiMethodCallExpression) =
expression.resolveMethod()?.name?.plus("()")
protected fun getOriginalMethodName(expression: PsiMethodCallExpression) = expression.resolveMethod()?.name
}
@@ -0,0 +1,64 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiExpressionStatement
import com.intellij.psi.PsiMethodCallExpression
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
abstract class AbstractMoveOutInspection : AbstractAssertJInspection() {
protected fun createInspectionsForMappings(
statement: PsiExpressionStatement,
holder: ProblemsHolder,
mappings: List<MoveOutMapping>
) {
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
val assertThatArgument = staticMethodCall.getArgOrNull(0) as? PsiMethodCallExpression ?: return
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
for (mapping in mappings.filter { it.callMatcher.test(assertThatArgument) }) {
if (mapping.expectBoolean && ASSERT_THAT_BOOLEAN.test(staticMethodCall)) {
val expectedBooleanResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: continue
if (mapping.additionalCondition?.invoke(statement, expectedCallExpression) == false) continue
val replacementMethod = if (expectedBooleanResult) mapping.replacementForTrue else mapping.replacementForFalse ?: return
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method)
}
} else if (mapping.expectNullNonNull != null) {
val expectedNullNonNullResult = expectedCallExpression.getExpectedNullNonNullResult() ?: continue
if (mapping.additionalCondition?.invoke(statement, expectedCallExpression) == false) continue
val replacementMethod = if (expectedNullNonNullResult xor mapping.expectNullNonNull) mapping.replacementForTrue else mapping.replacementForFalse ?: continue
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true)
}
} else if (mapping.expectedMatcher?.test(expectedCallExpression) == true) {
if (mapping.additionalCondition?.invoke(statement, expectedCallExpression) == false) continue
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, mapping.replacementForTrue) { desc, method ->
MoveOutMethodCallExpressionQuickFix(
desc, method,
replaceOnlyThisMethod = mapping.expectedMatcher,
replaceFromOriginalMethod = mapping.replaceFromOriginalMethod,
noExpectedExpression = mapping.noExpectedExpression
)
}
}
}
}
class MoveOutMapping(
val callMatcher: CallMatcher,
val replacementForTrue: String,
val replacementForFalse: String? = null,
val expectBoolean: Boolean = false,
val expectNullNonNull: Boolean? = null,
val expectedMatcher: CallMatcher? = null,
val replaceFromOriginalMethod: Boolean = false,
val noExpectedExpression: Boolean = false,
val additionalCondition: ((PsiExpressionStatement, PsiMethodCallExpression) -> Boolean)? = null
)
}
@@ -0,0 +1,86 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a binary expression"
private const val SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE = "Split binary expression out of assertThat()"
private const val MORE_MEANINGFUL_MESSAGE_TEMPLATE = "Moving binary expression out of assertThat() would be more meaningful"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) return
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
val expectedResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: return
val binaryExpression = staticMethodCall.firstArg as? PsiBinaryExpression ?: return
val leftType = binaryExpression.lOperand.type ?: return
val rightType = binaryExpression.rOperand?.type ?: return
val bothTypes = listOf(leftType, rightType)
val (isLeftNull, isRightNull) = bothTypes.map(TypeConversionUtil::isNullType)
if (isLeftNull && isRightNull) return
if (isLeftNull || isRightNull) {
val expectedResultOnOp =
when (binaryExpression.operationTokenType) {
JavaTokenType.EQEQ -> expectedResult
JavaTokenType.NE -> !expectedResult
else -> return
}
val replacementMethod = expectedResultOnOp.map(MethodNames.IS_NULL, MethodNames.IS_NOT_NULL)
registerSplitMethod(holder, expectedCallExpression, replacementMethod) { desc, method ->
SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = isLeftNull, noExpectedExpression = true)
}
return
}
val isPrimitive = bothTypes.all(TypeConversionUtil::isPrimitiveAndNotNull)
val isNumericType = bothTypes.all(TypeConversionUtil::isNumericType)
val constantEvaluationHelper = JavaPsiFacade.getInstance(statement.project).constantEvaluationHelper
val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null
val tokenType = binaryExpression.operationTokenType
.let {
if (swapExpectedAndActual) SWAP_SIDE_OF_BINARY_OPERATOR.getOrDefault(it, it) else it
}
.let {
if (expectedResult) it else INVERT_BINARY_OPERATOR.getOrDefault(it, it)
}
val mappingToUse =
(isPrimitive || isNumericType).map(TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP, TOKEN_TO_ASSERTJ_FOR_OBJECT_MAPPINGS)
val replacementMethod = mappingToUse[tokenType] ?: return
registerSplitMethod(holder, expectedCallExpression, replacementMethod) { desc, method ->
SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = swapExpectedAndActual)
}
}
}
}
private fun registerSplitMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val quickfix = quickFixSupplier(SPLIT_EXPRESSION_DESCRIPTION_TEMPLATE, replacementMethod)
holder.registerProblem(expression, MORE_MEANINGFUL_MESSAGE_TEMPLATE, quickfix)
}
}
@@ -0,0 +1,42 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_BOOLEAN_ASSERT_CLASSNAME
class AssertThatBooleanConditionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a boolean condition"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val matchingCalls = listOf(
IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_BOOLEAN,
IS_NOT_EQUAL_TO_OBJECT, IS_NOT_EQUAL_TO_BOOLEAN
).map { it.test(expression) }
if (matchingCalls.none { it }) return
if (!checkAssertedType(expression, ABSTRACT_BOOLEAN_ASSERT_CLASSNAME)) return
val expectedExpression = expression.firstArg
if (!TypeConversionUtil.isBooleanType(expectedExpression.type)) return
val expectedResult = expression.calculateConstantParameterValue(0) as? Boolean ?: return
val flippedBooleanTest = matchingCalls.drop(2).any { it }
val replacementMethod = (expectedResult xor flippedBooleanTest).map(MethodNames.IS_TRUE, MethodNames.IS_FALSE)
registerSimplifyMethod(holder, expression, replacementMethod)
}
}
}
}
@@ -1,53 +0,0 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.util.TypeConversionUtil
class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting true or false"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val isEqualToObject = IS_EQUAL_TO_OBJECT.test(expression)
val isEqualToBoolean = IS_EQUAL_TO_BOOLEAN.test(expression)
val isNotEqualToObject = IS_NOT_EQUAL_TO_OBJECT.test(expression)
val isNotEqualToBoolean = IS_NOT_EQUAL_TO_BOOLEAN.test(expression)
val normalBooleanTest = isEqualToObject || isEqualToBoolean
val flippedBooleanTest = isNotEqualToObject || isNotEqualToBoolean
if (!(normalBooleanTest || flippedBooleanTest)) {
return
}
if (!checkAssertedType(expression, ABSTRACT_BOOLEAN_ASSERT_CLASSNAME)) {
return
}
val equalToExpression = expression.argumentList.expressions[0] ?: return
if (!TypeConversionUtil.isBooleanType(equalToExpression.type)) {
return
}
var value = calculateConstantParameterValue(expression, 0)
if (value == null) {
val field = (equalToExpression as? PsiReferenceExpression)?.resolve() as? PsiField
if (field?.containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_BOOLEAN) {
when {
field.name == "TRUE" -> value = true
field.name == "FALSE" -> value = false
}
}
}
val expectedResult = value as? Boolean ?: return
val replacementMethod = if (expectedResult xor flippedBooleanTest) "isTrue()" else "isFalse()"
registerSimplifyMethod(holder, expression, replacementMethod)
}
}
}
}
@@ -0,0 +1,158 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.ui.ComboBox
import com.intellij.psi.*
import com.intellij.util.ui.FormBuilder
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
import java.awt.BorderLayout
import javax.swing.JComponent
import javax.swing.JPanel
class AssertThatCollectionOrMapExpressionInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a collection or map specific expression"
private const val DEFAULT_MAP_VALUES_NEVER_NULL = 2
private val MAP_GET_MATCHER = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "get").parameterCount(1)
private val ANY_IS_EQUAL_TO_MATCHER = CallMatcher.anyOf(
IS_EQUAL_TO_OBJECT,
IS_EQUAL_TO_STRING,
IS_EQUAL_TO_INT,
IS_EQUAL_TO_LONG,
IS_EQUAL_TO_FLOAT,
IS_EQUAL_TO_DOUBLE,
IS_EQUAL_TO_BOOLEAN
)
private val ANY_IS_NOT_EQUAL_TO_MATCHER = CallMatcher.anyOf(
IS_NOT_EQUAL_TO_OBJECT,
IS_NOT_EQUAL_TO_INT,
IS_NOT_EQUAL_TO_LONG,
IS_NOT_EQUAL_TO_FLOAT,
IS_NOT_EQUAL_TO_DOUBLE,
IS_NOT_EQUAL_TO_BOOLEAN
)
private val MAPPINGS = listOf(
MoveOutMapping(
CallMatcher.anyOf(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, MethodNames.IS_EMPTY).parameterCount(0),
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, MethodNames.IS_EMPTY).parameterCount(0)
),
MethodNames.IS_EMPTY, MethodNames.IS_NOT_EMPTY, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, MethodNames.CONTAINS).parameterCount(1),
MethodNames.CONTAINS, MethodNames.DOES_NOT_CONTAIN, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, MethodNames.CONTAINS_ALL).parameterCount(1),
MethodNames.CONTAINS_ALL, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, MethodNames.CONTAINS_KEY).parameterCount(1),
MethodNames.CONTAINS_KEY, MethodNames.DOES_NOT_CONTAIN_KEY, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, MethodNames.CONTAINS_VALUE).parameterCount(1),
MethodNames.CONTAINS_VALUE, MethodNames.DOES_NOT_CONTAIN_VALUE, expectBoolean = true
)
)
}
override fun getDisplayName() = DISPLAY_NAME
@JvmField
var behaviorForMapValueEqualsNull: Int = DEFAULT_MAP_VALUES_NEVER_NULL
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
val assertThatArgument = staticMethodCall.getArgOrNull(0) as? PsiMethodCallExpression ?: return
if (MAP_GET_MATCHER.test(assertThatArgument)) {
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
val nullOrNotNull = expectedCallExpression.getAllTheSameNullNotNullConstants()
if (nullOrNotNull == true) {
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.CONTAINS_KEY) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true)
}
} else if (nullOrNotNull == false) {
when (behaviorForMapValueEqualsNull) {
1 -> // warning only
registerMoveOutMethod(
holder,
expectedCallExpression,
assertThatArgument,
""
) { _ -> emptyList() }
2 -> // as doesNotContainKey(key)
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.DOES_NOT_CONTAIN_KEY) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true)
}
3 -> // as containsEntry(key, null)
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.CONTAINS_ENTRY) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, keepExpectedAsSecondArgument = true, useNullNonNull = true)
}
4 -> // both
registerMoveOutMethod(
holder,
expectedCallExpression,
assertThatArgument,
MethodNames.DOES_NOT_CONTAIN_KEY + "/" + MethodNames.CONTAINS_ENTRY
) { _ ->
listOf(
MoveOutMethodCallExpressionQuickFix(
"Remove get() of actual expression and use assertThat().doesNotContainKey() instead (regular map)",
MethodNames.DOES_NOT_CONTAIN_KEY,
useNullNonNull = true
),
MoveOutMethodCallExpressionQuickFix(
"Remove get() of actual expression and use assertThat().containsEntry(key, null) instead (degenerated map)",
MethodNames.CONTAINS_ENTRY,
keepExpectedAsSecondArgument = true,
useNullNonNull = true
)
)
}
}
} else {
if (ANY_IS_EQUAL_TO_MATCHER.test(expectedCallExpression)) {
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.CONTAINS_ENTRY) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, keepExpectedAsSecondArgument = true)
}
} else if (ANY_IS_NOT_EQUAL_TO_MATCHER.test(expectedCallExpression)) {
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, MethodNames.DOES_NOT_CONTAIN_ENTRY) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, keepExpectedAsSecondArgument = true)
}
}
}
} else {
createInspectionsForMappings(statement, holder, MAPPINGS)
}
}
}
}
override fun createOptionsPanel(): JComponent {
val comboBox = ComboBox(
arrayOf("ignore", "warning only, no fixes", "as doesNotContainKey(key)", "as containsEntry(key, null)", "both choices")
)
comboBox.selectedIndex = behaviorForMapValueEqualsNull
comboBox.addActionListener { behaviorForMapValueEqualsNull = comboBox.selectedIndex }
val panel = JPanel(BorderLayout())
panel.add(
FormBuilder.createFormBuilder().addLabeledComponent("Fix get() on maps expecting null values:", comboBox).panel,
BorderLayout.NORTH
)
return panel
}
}
@@ -0,0 +1,115 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.calculateConstantValue
import de.platon42.intellij.plugins.cajon.firstArg
class AssertThatComparableInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a compareTo() expression"
private val ARG_IS_ZERO_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == 0 }
private val ARG_IS_PLUS_ONE_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == 1 }
private val ARG_IS_MINUS_ONE_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == -1 }
private val COMPARABLE_COMPARE_TO =
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_COMPARABLE, "compareTo").parameterCount(1)
private val MAPPINGS = listOf(
MoveOutMapping(
COMPARABLE_COMPARE_TO,
"isEqualByComparingTo", expectedMatcher = IS_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
"isEqualByComparingTo", expectedMatcher = IS_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
"isNotEqualByComparingTo", expectedMatcher = IS_NOT_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
"isNotEqualByComparingTo", expectedMatcher = IS_NOT_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN_OR_EQUAL_TO, expectedMatcher = IS_GREATER_THAN_OR_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN_OR_EQUAL_TO, expectedMatcher = CallMatcher.anyOf(IS_NOT_EQUAL_TO_INT, IS_GREATER_THAN_INT), replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN_OR_EQUAL_TO, expectedMatcher = IS_NOT_NEGATIVE, replaceFromOriginalMethod = true
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN, expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_INT, IS_GREATER_THAN_OR_EQUAL_TO_INT), replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_PLUS_ONE_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN, expectedMatcher = IS_GREATER_THAN_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_GREATER_THAN, expectedMatcher = CallMatcher.anyOf(IS_POSITIVE, IS_ONE), replaceFromOriginalMethod = true
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN_OR_EQUAL_TO, expectedMatcher = IS_LESS_THAN_OR_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN_OR_EQUAL_TO, expectedMatcher = CallMatcher.anyOf(IS_NOT_EQUAL_TO_INT, IS_LESS_THAN_INT), replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_PLUS_ONE_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN_OR_EQUAL_TO, expectedMatcher = IS_NOT_POSITIVE, replaceFromOriginalMethod = true
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN, expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_INT, IS_LESS_THAN_OR_EQUAL_TO_INT), replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN, expectedMatcher = IS_LESS_THAN_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
COMPARABLE_COMPARE_TO,
MethodNames.IS_LESS_THAN, expectedMatcher = IS_NEGATIVE, replaceFromOriginalMethod = true
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
createInspectionsForMappings(statement, holder, MAPPINGS)
}
}
}
}
@@ -4,6 +4,10 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue
import de.platon42.intellij.plugins.cajon.hasAssertThat
class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
@@ -17,14 +21,13 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val hasSize = HAS_SIZE.test(expression)
if (!hasSize) {
return
}
if (!expression.hasAssertThat()) return
val isLastExpression = expression.parent is PsiStatement
if (!(HAS_SIZE.test(expression) && isLastExpression)) return
val value = calculateConstantParameterValue(expression, 0) ?: return
val value = expression.calculateConstantParameterValue(0) ?: return
if (value == 0) {
registerSimplifyMethod(holder, expression, "isEmpty()")
registerSimplifyMethod(holder, expression, MethodNames.IS_EMPTY)
}
}
}
@@ -0,0 +1,119 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
class AssertThatFileExpressionInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a file specific expression"
private val ARG_IS_ZERO_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == 0 }
private val ARG_IS_NOT_ZERO_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call ->
val constant =
call.firstArg.calculateConstantValue()
(constant != null) && (constant != 0)
}
private val MAPPINGS = listOf(
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "canRead").parameterCount(0),
"canRead", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "canWrite").parameterCount(0),
"canWrite", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "exists").parameterCount(0),
"exists", "doesNotExist", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isAbsolute").parameterCount(0),
"isAbsolute", "isRelative", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isDirectory").parameterCount(0),
"isDirectory", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isFile").parameterCount(0),
"isFile", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getName").parameterCount(0),
"hasName",
expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING)
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParent", "getParentFile").parameterCount(0),
"hasNoParent", expectNullNonNull = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParent").parameterCount(0),
"hasParent",
expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING)
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParentFile").parameterCount(0),
"hasParent",
expectedMatcher = IS_EQUAL_TO_OBJECT
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "list", "listFiles").parameterCount(0),
"isEmptyDirectory",
expectedMatcher = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME, MethodNames.IS_EMPTY)
.parameterCount(0)
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "list", "listFiles").parameterCount(0),
"isNotEmptyDirectory",
expectedMatcher = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME, MethodNames.IS_NOT_EMPTY)
.parameterCount(0)
)
)
private val MAPPINGS_SINCE_ASSERTJ_3_14_0 = listOf(
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "length").parameterCount(0),
"isEmpty", expectedMatcher = IS_ZERO_LONG, noExpectedExpression = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "length").parameterCount(0),
"isEmpty", expectedMatcher = IS_EQUAL_TO_LONG, noExpectedExpression = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "length").parameterCount(0),
"isNotEmpty", expectedMatcher = IS_NOT_ZERO_LONG, noExpectedExpression = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "length").parameterCount(0),
"isNotEmpty", expectedMatcher = IS_NOT_EQUAL_TO_LONG, noExpectedExpression = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "length").parameterCount(0),
"hasSize", expectedMatcher = IS_EQUAL_TO_LONG,
additionalCondition = ARG_IS_NOT_ZERO_CONST
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
createInspectionsForMappings(statement, holder, MAPPINGS)
if (hasAssertJMethod(statement, AssertJClassNames.ABSTRACT_FILE_ASSERT_CLASSNAME, MethodNames.HAS_SIZE)) {
createInspectionsForMappings(statement, holder, MAPPINGS_SINCE_ASSERTJ_3_14_0)
}
}
}
}
}
@@ -0,0 +1,116 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.*
class AssertThatGuavaOptionalInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting an Optional (Guava)"
private const val REPLACE_GUAVA_DESCRIPTION_TEMPLATE = "Replace %s() with Guava assertThat().%s()"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
if (!checkPreconditions(staticMethodCall)) return
val actualExpression = staticMethodCall.firstArg as? PsiMethodCallExpression ?: return
val outmostMethodCall = statement.findOutmostMethodCall() ?: return
if (GUAVA_OPTIONAL_GET.test(actualExpression)) {
if (actualExpression.type is PsiArrayType) return
val expectedCallExpression = staticMethodCall.gatherAssertionCalls().singleOrNull() ?: return
if (CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING).test(expectedCallExpression)) {
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, MethodNames.CONTAINS) { desc, method ->
QuickFixWithPostfixDelegate(
RemoveActualOutmostMethodCallQuickFix(desc, method),
ForGuavaPostFix.REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT
)
}
}
} else if (GUAVA_OPTIONAL_IS_PRESENT.test(actualExpression)) {
val expectedPresence = outmostMethodCall.getAllTheSameExpectedBooleanConstants() ?: return
val replacementMethod = expectedPresence.map(MethodNames.IS_PRESENT, MethodNames.IS_ABSENT)
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, replacementMethod) { desc, method ->
QuickFixWithPostfixDelegate(
MoveOutMethodCallExpressionQuickFix(desc, method),
ForGuavaPostFix.REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT
)
}
} else if (GUAVA_OPTIONAL_OR_NULL.test(actualExpression)) {
val expectedPresence = outmostMethodCall.getAllTheSameNullNotNullConstants() ?: return
val replacementMethod = expectedPresence.map(MethodNames.IS_PRESENT, MethodNames.IS_ABSENT)
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, replacementMethod) { desc, method ->
QuickFixWithPostfixDelegate(
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true),
ForGuavaPostFix.REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT
)
}
}
}
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val staticMethodCall = expression.findStaticMethodCall() ?: return
if (!checkPreconditions(staticMethodCall)) return
// We're not calling an assertThat() from Guava, but a core-AssertJ one!
// We need to replace that by the Guava one, if we want to apply a formally correct fix.
if (IS_EQUAL_TO_OBJECT.test(expression)) {
val innerExpectedCall = expression.firstArg as? PsiMethodCallExpression ?: return
if (CallMatcher.anyOf(GUAVA_OPTIONAL_OF, GUAVA_OPTIONAL_FROM_NULLABLE).test(innerExpectedCall)) {
if (GUAVA_OPTIONAL_FROM_NULLABLE.test(innerExpectedCall)) {
innerExpectedCall.firstArg.calculateConstantValue() ?: return
}
registerRemoveExpectedOutmostMethod(holder, expression, expression, MethodNames.CONTAINS) { desc, method ->
QuickFixWithPostfixDelegate(
UnwrapExpectedStaticMethodCallQuickFix(desc, method),
ForGuavaPostFix.REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT
)
}
} else if (GUAVA_OPTIONAL_ABSENT.test(innerExpectedCall)) {
registerSimplifyForGuavaMethod(holder, expression, MethodNames.IS_ABSENT)
}
} else if (IS_NOT_EQUAL_TO_OBJECT.test(expression)) {
val innerExpectedCall = expression.firstArg as? PsiMethodCallExpression ?: return
if (GUAVA_OPTIONAL_ABSENT.test(innerExpectedCall)) {
registerSimplifyForGuavaMethod(holder, expression, MethodNames.IS_PRESENT)
}
}
}
private fun checkPreconditions(staticMethodCall: PsiMethodCallExpression): Boolean {
if (CallMatcher.anyOf(ASSERT_THAT_ANY, GUAVA_ASSERT_THAT_ANY).test(staticMethodCall)) {
JavaPsiFacade.getInstance(staticMethodCall.project)
.findClass(AssertJClassNames.GUAVA_ASSERTIONS_CLASSNAME, GlobalSearchScope.allScope(staticMethodCall.project)) ?: return false
return true
}
return false
}
}
}
private fun registerSimplifyForGuavaMethod(holder: ProblemsHolder, expression: PsiMethodCallExpression, replacementMethod: String) {
val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_GUAVA_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = SIMPLIFY_MESSAGE_TEMPLATE.format(originalMethod, replacementMethod)
val quickFix = QuickFixWithPostfixDelegate(
ReplaceSimpleMethodCallQuickFix(description, replacementMethod),
ForGuavaPostFix.REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT
)
val textRange = TextRange(expression.qualifierExpression.textLength, expression.textLength)
holder.registerProblem(expression, textRange, message, quickFix)
}
}
@@ -0,0 +1,48 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutInstanceOfExpressionQuickFix
class AssertThatInstanceOfInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a class instance"
private const val REMOVE_INSTANCEOF_DESCRIPTION_TEMPLATE = "Remove instanceof in actual expression and use assertThat().%s() instead"
private const val MOVE_OUT_INSTANCEOF_MESSAGE = "instanceof expression could be moved out of assertThat()"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) return
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
val expectedResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: return
if (staticMethodCall.firstArg is PsiInstanceOfExpression) {
val replacementMethod = expectedResult.map(MethodNames.IS_INSTANCE_OF, MethodNames.IS_NOT_INSTANCE_OF)
registerMoveOutInstanceOfMethod(holder, expectedCallExpression, replacementMethod, ::MoveOutInstanceOfExpressionQuickFix)
}
}
}
}
private fun registerMoveOutInstanceOfMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val description = REMOVE_INSTANCEOF_DESCRIPTION_TEMPLATE.format(replacementMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, MOVE_OUT_INSTANCEOF_MESSAGE, quickfix)
}
}
@@ -0,0 +1,34 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.InvertUnaryStatementQuickFix
class AssertThatInvertedBooleanConditionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting an inverted boolean condition"
private const val INVERT_CONDITION_MESSAGE = "Condition inside assertThat() could be inverted"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val staticMethodCall = expression.findStaticMethodCall() ?: return
if (!ASSERT_THAT_BOOLEAN.test(staticMethodCall)) return
expression.getExpectedBooleanResult() ?: return
val prefixExpression = staticMethodCall.firstArg as? PsiPrefixExpression ?: return
if (prefixExpression.operationTokenType == JavaTokenType.EXCL) {
val outmostMethodCall = expression.findOutmostMethodCall() ?: return
holder.registerProblem(outmostMethodCall, INVERT_CONDITION_MESSAGE, InvertUnaryStatementQuickFix())
}
}
}
}
}
@@ -0,0 +1,79 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.RemoveActualOutmostMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.UnwrapExpectedStaticMethodCallQuickFix
class AssertThatJava8OptionalInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting an Optional (Java 8)"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
if (!ASSERT_THAT_ANY.test(staticMethodCall)) return
val actualExpression = staticMethodCall.firstArg as? PsiMethodCallExpression ?: return
val outmostMethodCall = statement.findOutmostMethodCall() ?: return
if (OPTIONAL_GET.test(actualExpression)) {
val expectedCallExpression = staticMethodCall.gatherAssertionCalls().singleOrNull() ?: return
if (CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING).test(expectedCallExpression)) {
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, MethodNames.CONTAINS) { desc, method ->
RemoveActualOutmostMethodCallQuickFix(desc, method)
}
} else if (IS_SAME_AS_OBJECT.test(expectedCallExpression)) {
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, MethodNames.CONTAINS_SAME) { desc, method ->
RemoveActualOutmostMethodCallQuickFix(desc, method)
}
}
} else if (OPTIONAL_IS_PRESENT.test(actualExpression)) {
val expectedPresence = outmostMethodCall.getAllTheSameExpectedBooleanConstants() ?: return
val replacementMethod = expectedPresence.map(MethodNames.IS_PRESENT, MethodNames.IS_NOT_PRESENT)
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method)
}
} else if (OPTIONAL_OR_ELSE.test(actualExpression) && (actualExpression.firstArg.type == PsiType.NULL)) {
val expectedPresence = outmostMethodCall.getAllTheSameNullNotNullConstants() ?: return
val replacementMethod = expectedPresence.map(MethodNames.IS_PRESENT, MethodNames.IS_NOT_PRESENT)
registerMoveOutMethod(holder, outmostMethodCall, actualExpression, replacementMethod) { desc, method ->
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true, noExpectedExpression = true)
}
}
}
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val staticMethodCall = expression.findStaticMethodCall() ?: return
if (!ASSERT_THAT_JAVA8_OPTIONAL.test(staticMethodCall)) return
if (IS_EQUAL_TO_OBJECT.test(expression)) {
val innerExpectedCall = expression.firstArg as? PsiMethodCallExpression ?: return
if (CallMatcher.anyOf(OPTIONAL_OF, OPTIONAL_OF_NULLABLE).test(innerExpectedCall)) {
if (OPTIONAL_OF_NULLABLE.test(innerExpectedCall)) {
innerExpectedCall.firstArg.calculateConstantValue() ?: return
}
registerRemoveExpectedOutmostMethod(holder, expression, expression, MethodNames.CONTAINS, ::UnwrapExpectedStaticMethodCallQuickFix)
} else if (OPTIONAL_EMPTY.test(innerExpectedCall)) {
registerSimplifyMethod(holder, expression, MethodNames.IS_NOT_PRESENT)
}
} else if (IS_NOT_EQUAL_TO_OBJECT.test(expression)) {
val innerExpectedCall = expression.firstArg as? PsiMethodCallExpression ?: return
if (OPTIONAL_EMPTY.test(innerExpectedCall)) {
registerSimplifyMethod(holder, expression, MethodNames.IS_PRESENT)
}
}
}
}
}
}
@@ -0,0 +1,53 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.HasHashCodeQuickFix
class AssertThatObjectExpressionInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting equals(), toString(), or hashCode()"
private const val HASHCODE_MESSAGE_TEMPLATE = "Replacing hashCode() expressions by hasSameHashCode() would be more concise"
private val OBJECT_TO_STRING = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "toString").parameterCount(0)
private val OBJECT_HASHCODE = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "hashCode").parameterCount(0)
private val MAPPINGS = listOf(
MoveOutMapping(
OBJECT_EQUALS,
MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO, expectBoolean = true
),
MoveOutMapping(
OBJECT_TO_STRING,
MethodNames.HAS_TO_STRING, expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING)
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
val assertThatArgument = staticMethodCall.getArgOrNull(0) as? PsiMethodCallExpression ?: return
if (OBJECT_HASHCODE.test(assertThatArgument)) {
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
val isEqualTo = staticMethodCall.findFluentCallTo(IS_EQUAL_TO_INT) ?: return
val expectedExpression = isEqualTo.firstArg as? PsiMethodCallExpression ?: return
if (OBJECT_HASHCODE.test(expectedExpression)) {
holder.registerProblem(expectedCallExpression, HASHCODE_MESSAGE_TEMPLATE, HasHashCodeQuickFix())
}
} else {
createInspectionsForMappings(statement, holder, MAPPINGS)
}
}
}
}
}
@@ -1,31 +0,0 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiType
class AssertThatObjectIsNotNullInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting non-null"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!IS_NOT_EQUAL_TO_OBJECT.test(expression)) {
return
}
if (expression.argumentList.expressions[0].type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, "isNotNull()")
}
}
}
}
}
@@ -1,31 +0,0 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiType
class AssertThatObjectIsNullInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting null"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!IS_EQUAL_TO_OBJECT.test(expression)) {
return
}
if (expression.argumentList.expressions[0].type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, "isNull()")
}
}
}
}
}
@@ -0,0 +1,35 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.hasAssertThat
import de.platon42.intellij.plugins.cajon.map
class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting null or not-null"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val isNotEqualTo = IS_NOT_EQUAL_TO_OBJECT.test(expression)
val isEqualTo = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING).test(expression)
val isLastExpression = expression.parent is PsiStatement
if (!((isEqualTo && isLastExpression) || isNotEqualTo)) return
if (expression.firstArg.type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, isEqualTo.map(MethodNames.IS_NULL, MethodNames.IS_NOT_NULL))
}
}
}
}
}
@@ -0,0 +1,51 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiExpressionStatement
import com.siyeh.ig.callMatcher.CallMatcher
class AssertThatPathExpressionInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a path specific expression"
private const val JAVA_NIO_PATH = "java.nio.file.Path"
private val MAPPINGS = listOf(
MoveOutMapping(
CallMatcher.instanceCall(JAVA_NIO_PATH, "isAbsolute").parameterCount(0),
"isAbsolute", "isRelative", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(JAVA_NIO_PATH, "startsWith").parameterTypes(JAVA_NIO_PATH),
"startsWithRaw", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(JAVA_NIO_PATH, "endsWith").parameterTypes(JAVA_NIO_PATH),
"endsWithRaw", expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(JAVA_NIO_PATH, "getParent").parameterCount(0),
"hasParentRaw",
expectedMatcher = IS_EQUAL_TO_OBJECT
),
MoveOutMapping(
CallMatcher.instanceCall(JAVA_NIO_PATH, "getParent").parameterCount(0),
"hasNoParentRaw", expectNullNonNull = true
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
createInspectionsForMappings(statement, holder, MAPPINGS)
}
}
}
}
@@ -2,86 +2,147 @@ package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_ITERABLE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_MAP_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceHasSizeMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSizeMethodCallQuickFix
class AssertThatSizeInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting the size of an collection or array"
private const val MORE_CONCISE_MESSAGE_TEMPLATE = "%s would be more concise than %s"
}
private const val DISPLAY_NAME = "Asserting the size of an collection, map, array or string"
private const val REMOVE_SIZE_DESCRIPTION_TEMPLATE = "Remove size determination of expected expression and replace %s() with %s()"
private const val REMOVE_ALL_MESSAGE = "Try to operate on the iterable itself rather than its size"
override fun getDisplayName() = DISPLAY_NAME
private val BONUS_EXPRESSIONS_CALL_MATCHER_MAP = listOf(
IS_LESS_THAN_INT to MethodNames.HAS_SIZE_LESS_THAN,
IS_LESS_THAN_OR_EQUAL_TO_INT to MethodNames.HAS_SIZE_LESS_THAN_OR_EQUAL_TO,
IS_GREATER_THAN_INT to MethodNames.HAS_SIZE_GREATER_THAN,
IS_GREATER_THAN_OR_EQUAL_TO_INT to MethodNames.HAS_SIZE_GREATER_THAN_OR_EQUAL_TO
)
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!ASSERT_THAT_INT.test(expression)) {
return
}
val actualExpression = expression.argumentList.expressions[0] ?: return
if (isArrayLength(actualExpression) || isCollectionSize(actualExpression)) {
val statement = PsiTreeUtil.getParentOfType(expression, PsiStatement::class.java) ?: return
val expectedCallExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val constValue = calculateConstantParameterValue(expectedCallExpression, 0)
if (IS_EQUAL_TO_INT.test(expectedCallExpression)) {
if (constValue == 0) {
registerSizeMethod(holder, expression, expectedCallExpression, "isEmpty()", noExpectedExpression = true)
return
}
val equalToExpression = expectedCallExpression.argumentList.expressions[0]
if (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression)) {
registerSizeMethod(holder, expression, expectedCallExpression, "hasSameSizeAs()", expectedIsCollection = true)
return
}
registerSizeMethod(holder, expression, expectedCallExpression, "hasSize()")
} else {
if ((IS_LESS_THAN_OR_EQUAL_TO_INT.test(expectedCallExpression) && (constValue == 0))
|| (IS_LESS_THAN_INT.test(expectedCallExpression) && (constValue == 1))
|| IS_ZERO.test(expectedCallExpression)
) {
registerSizeMethod(holder, expression, expectedCallExpression, "isEmpty()", noExpectedExpression = true)
return
}
if ((IS_GREATER_THAN_INT.test(expectedCallExpression) && (constValue == 0))
|| (IS_GREATER_THAN_OR_EQUAL_TO_INT.test(expectedCallExpression) && (constValue == 1))
|| IS_NOT_ZERO.test(expectedCallExpression)
) {
registerSizeMethod(holder, expression, expectedCallExpression, "isNotEmpty()", noExpectedExpression = true)
return
}
}
}
}
private fun isCharSequenceLength(expression: PsiExpression) = (expression is PsiMethodCallExpression) && CHAR_SEQUENCE_LENGTH.test(expression)
private fun isCollectionSize(expression: PsiExpression) = (expression is PsiMethodCallExpression) && COLLECTION_SIZE.test(expression)
private fun isMapSize(expression: PsiExpression) = (expression is PsiMethodCallExpression) && MAP_SIZE.test(expression)
private fun isArrayLength(expression: PsiExpression): Boolean {
val psiReferenceExpression = expression as? PsiReferenceExpression ?: return false
return ((psiReferenceExpression.qualifierExpression?.type is PsiArrayType)
&& ((psiReferenceExpression.resolve() as? PsiField)?.name == "length"))
}
private fun registerSizeMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
expectedCallExpression: PsiMethodCallExpression,
replacementMethod: String,
noExpectedExpression: Boolean = false,
expectedIsCollection: Boolean = false
fun getMatch(expression: PsiMethodCallExpression, isForArrayOrCollection: Boolean, isForMap: Boolean, isForString: Boolean): Match? {
val isLastExpression = expression.parent is PsiStatement
val constValue = expression.calculateConstantParameterValue(0)
if (IS_EQUAL_TO_INT.test(expression)) {
return if ((constValue == 0) && isLastExpression) {
Match(expression, MethodNames.IS_EMPTY, noExpectedExpression = true)
} else {
val equalToExpression = expression.firstArg
val equalsArrayOrCollectionSize = isArrayLength(equalToExpression) ||
isCollectionSize(equalToExpression)
if ((isForArrayOrCollection && equalsArrayOrCollectionSize)
|| (isForMap && (equalsArrayOrCollectionSize || isMapSize(equalToExpression)))
|| (isForString && (equalsArrayOrCollectionSize || isCharSequenceLength(equalToExpression)))
) {
val originalMethod = getOriginalMethodName(expectedCallExpression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = MORE_CONCISE_MESSAGE_TEMPLATE.format(replacementMethod, originalMethod)
holder.registerProblem(
Match(expression, MethodNames.HAS_SAME_SIZE_AS, expectedIsCollection = true)
} else {
Match(expression, MethodNames.HAS_SIZE)
}
}
} else {
val isTestForEmpty = ((IS_LESS_THAN_OR_EQUAL_TO_INT.test(expression) && (constValue == 0))
|| (IS_LESS_THAN_INT.test(expression) && (constValue == 1))
|| IS_ZERO_INT.test(expression))
val isTestForNotEmpty = ((IS_GREATER_THAN_INT.test(expression) && (constValue == 0))
|| (IS_GREATER_THAN_OR_EQUAL_TO_INT.test(expression) && (constValue == 1))
|| IS_NOT_ZERO_INT.test(expression))
if ((isTestForEmpty && isLastExpression) || isTestForNotEmpty) {
val replacementMethod = isTestForEmpty.map(MethodNames.IS_EMPTY, MethodNames.IS_NOT_EMPTY)
return Match(expression, replacementMethod, noExpectedExpression = true)
} else if (hasAssertJMethod(expression, ABSTRACT_ITERABLE_ASSERT_CLASSNAME, MethodNames.HAS_SIZE_LESS_THAN)) {
// new stuff in AssertJ 3.12.0
val replacementMethod = BONUS_EXPRESSIONS_CALL_MATCHER_MAP.find { it.first.test(expression) }?.second ?: return null
return Match(expression, replacementMethod)
}
return null
}
}
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val staticMethodCall = statement.findStaticMethodCall() ?: return
if (!ASSERT_THAT_INT.test(staticMethodCall)) return
val actualExpression = staticMethodCall.firstArg
val isForArrayOrCollection = isArrayLength(actualExpression) || isCollectionSize(actualExpression)
val isForMap = isMapSize(actualExpression)
val isForString = isCharSequenceLength(actualExpression)
if (!(isForArrayOrCollection || isForMap || isForString)) return
val matches = staticMethodCall.collectMethodCallsUpToStatement()
.mapNotNull { getMatch(it, isForArrayOrCollection, isForMap, isForString) }
.toList()
if (matches.isNotEmpty()) {
if (matches.size == 1) {
val match = matches.single()
val expression = match.methodCall
registerReplaceMethod(
holder,
expression,
message,
ReplaceSizeMethodCallQuickFix(description, replacementMethod, noExpectedExpression, expectedIsCollection)
expression,
match.replacementMethod
)
{ desc, method ->
ReplaceSizeMethodCallQuickFix(desc, method, noExpectedExpression = match.noExpectedExpression, expectedIsCollection = match.expectedIsCollection)
}
} else {
// I could try to create a quickfix for this, too, but it's probably not worth the effort
holder.registerProblem(statement, REMOVE_ALL_MESSAGE)
}
}
}
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
if (!HAS_SIZE.test(expression)) return
val actualExpression = expression.firstArg
val isForArrayOrCollection = isArrayLength(actualExpression) || isCollectionSize(actualExpression)
val isForMap = isMapSize(actualExpression)
val isForString = isCharSequenceLength(actualExpression)
if (!(isForArrayOrCollection
|| (isForMap && checkAssertedType(expression, ABSTRACT_MAP_ASSERT_CLASSNAME))
|| (isForString && checkAssertedType(expression, ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME)))
) return
registerConciseMethod(
REMOVE_SIZE_DESCRIPTION_TEMPLATE,
holder,
expression,
expression,
MethodNames.HAS_SAME_SIZE_AS,
::ReplaceHasSizeMethodCallQuickFix
)
}
}
}
class Match(
val methodCall: PsiMethodCallExpression,
val replacementMethod: String,
val noExpectedExpression: Boolean = false,
val expectedIsCollection: Boolean = false
)
}
@@ -0,0 +1,152 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.calculateConstantValue
import de.platon42.intellij.plugins.cajon.firstArg
class AssertThatStringExpressionInspection : AbstractMoveOutInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a string specific expression"
private val ARG_IS_ZERO_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == 0 }
private val ARG_IS_MINUS_ONE_CONST: (PsiExpressionStatement, PsiMethodCallExpression) -> Boolean = { _, call -> call.firstArg.calculateConstantValue() == -1 }
private val STRING_COMPARE_TO_IGNORE_CASE =
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "compareToIgnoreCase").parameterTypes(CommonClassNames.JAVA_LANG_STRING)
private val STRING_INDEX_OF = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "indexOf").parameterTypes(CommonClassNames.JAVA_LANG_STRING)
private val STRING_TRIM = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "trim").parameterCount(0)
private val MAPPINGS = listOf(
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "isEmpty").parameterCount(0),
MethodNames.IS_EMPTY, MethodNames.IS_NOT_EMPTY, expectBoolean = true
),
MoveOutMapping(
CallMatcher.anyOf(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "equals").parameterCount(1),
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "contentEquals").parameterCount(1)
),
MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "equalsIgnoreCase").parameterTypes(CommonClassNames.JAVA_LANG_STRING),
MethodNames.IS_EQUAL_TO_IC, MethodNames.IS_NOT_EQUAL_TO_IC, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "contains").parameterCount(1),
MethodNames.CONTAINS, MethodNames.DOES_NOT_CONTAIN, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "startsWith").parameterTypes(CommonClassNames.JAVA_LANG_STRING),
MethodNames.STARTS_WITH, MethodNames.DOES_NOT_START_WITH, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "endsWith").parameterTypes(CommonClassNames.JAVA_LANG_STRING),
MethodNames.ENDS_WITH, MethodNames.DOES_NOT_END_WITH, expectBoolean = true
),
MoveOutMapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "matches").parameterTypes(CommonClassNames.JAVA_LANG_STRING),
"matches", "doesNotMatch", expectBoolean = true
),
MoveOutMapping(
STRING_COMPARE_TO_IGNORE_CASE,
MethodNames.IS_EQUAL_TO_IC, expectedMatcher = IS_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_COMPARE_TO_IGNORE_CASE,
MethodNames.IS_EQUAL_TO_IC, expectedMatcher = IS_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_COMPARE_TO_IGNORE_CASE,
MethodNames.IS_NOT_EQUAL_TO_IC, expectedMatcher = IS_NOT_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_COMPARE_TO_IGNORE_CASE,
MethodNames.IS_NOT_EQUAL_TO_IC, expectedMatcher = IS_NOT_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.STARTS_WITH, expectedMatcher = IS_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.STARTS_WITH, expectedMatcher = IS_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_START_WITH, expectedMatcher = IS_NOT_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_START_WITH, expectedMatcher = IS_NOT_ZERO_INT, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.CONTAINS, expectedMatcher = IS_NOT_NEGATIVE, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.CONTAINS, expectedMatcher = IS_NOT_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.CONTAINS, expectedMatcher = IS_GREATER_THAN_OR_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.CONTAINS, expectedMatcher = IS_GREATER_THAN_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_CONTAIN, expectedMatcher = IS_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_CONTAIN, expectedMatcher = IS_NEGATIVE, replaceFromOriginalMethod = true
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_CONTAIN, expectedMatcher = IS_LESS_THAN_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_ZERO_CONST
),
MoveOutMapping(
STRING_INDEX_OF,
MethodNames.DOES_NOT_CONTAIN, expectedMatcher = IS_LESS_THAN_OR_EQUAL_TO_INT, replaceFromOriginalMethod = true,
additionalCondition = ARG_IS_MINUS_ONE_CONST
),
MoveOutMapping(
STRING_TRIM,
"isNotBlank", expectedMatcher = IS_NOT_EMPTY
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
createInspectionsForMappings(statement, holder, MAPPINGS)
}
}
}
}
@@ -4,6 +4,12 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue
import de.platon42.intellij.plugins.cajon.hasAssertThat
class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
@@ -17,19 +23,17 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val isEqual = IS_EQUAL_TO_OBJECT.test(expression)
if (!expression.hasAssertThat()) return
val isEqual = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING).test(expression)
val hasSize = HAS_SIZE.test(expression)
if (!(isEqual || hasSize)) {
return
}
val isLastExpression = expression.parent is PsiStatement
if (!((isEqual || hasSize) && isLastExpression)) return
if (!checkAssertedType(expression, ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME)) {
return
}
if (!checkAssertedType(expression, ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME)) return
val value = calculateConstantParameterValue(expression, 0) ?: return
val value = expression.calculateConstantParameterValue(0) ?: return
if ((isEqual && (value == "")) || (hasSize && (value == 0))) {
registerSimplifyMethod(holder, expression, "isEmpty()")
registerSimplifyMethod(holder, expression, MethodNames.IS_EMPTY)
}
}
}
@@ -0,0 +1,144 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import de.platon42.intellij.plugins.cajon.hasAssertThat
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceIfByAssumeThatQuickFix
class AssumeThatInsteadOfReturnInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Replace conditional test exits by assumeThat() statements with same actual expression"
private const val REPLACE_RETURN_BY_ASSUME_THAT_DESCRIPTION = "Conditional return should probably be an assumeThat() statement instead"
private const val MAX_RECURSION_DEPTH = 5
private const val MAX_STATEMENTS_COUNT = 50
private val TEST_ANNOTATIONS = setOf(
"org.junit.Test",
"org.junit.jupiter.api.Test",
"org.junit.jupiter.api.TestTemplate",
"org.junit.jupiter.api.params.ParameterizedTest"
)
private fun hasEmptyReturn(statement: PsiStatement): Boolean {
return when (statement) {
is PsiBlockStatement -> {
val psiReturnStatement = (statement.firstChild as? PsiCodeBlock)?.statements?.singleOrNull() as? PsiReturnStatement
(psiReturnStatement != null) && (psiReturnStatement.returnValue == null)
}
is PsiReturnStatement -> statement.returnValue == null
else -> false
}
}
private fun registerProblem(holder: ProblemsHolder, isOnTheFly: Boolean, statement: PsiStatement, removeElse: Boolean) {
val problemDescriptor = holder.manager.createProblemDescriptor(
statement,
statement,
REPLACE_RETURN_BY_ASSUME_THAT_DESCRIPTION,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOnTheFly,
ReplaceIfByAssumeThatQuickFix(removeElse)
)
holder.registerProblem(problemDescriptor)
}
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethod(method: PsiMethod) {
super.visitMethod(method)
// Note: replace with if(TEST_ANNOTATIONS.none(method::hasAnnotation)) for IDEA >= 2018.2
val annotations = method.annotations.mapNotNull { it.qualifiedName }
if (annotations.none(TEST_ANNOTATIONS::contains)) return
val containingClass = method.containingClass ?: return
val visitor: PsiElementVisitor = TestMethodVisitor(holder, isOnTheFly, containingClass)
method.accept(visitor)
}
}
}
class TestMethodVisitor(
private val holder: ProblemsHolder,
private val isOnTheFly: Boolean,
private val containingClass: PsiClass
) : JavaRecursiveElementWalkingVisitor() {
private var contSearch = true
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
if (contSearch) {
val methodCallExpression = statement.expression as? PsiMethodCallExpression
if (methodCallExpression != null) {
if (methodCallExpression.hasAssertThat()) {
contSearch = false
} else {
val method = methodCallExpression.resolveMethod()
if (method?.containingClass == containingClass) {
val recursionVisitor = CheckForAssertThatCallsVisitor(containingClass, 1)
method.accept(recursionVisitor)
if (recursionVisitor.aborted || recursionVisitor.foundAssertThat) {
contSearch = false
}
}
}
}
}
if (contSearch) {
super.visitExpressionStatement(statement)
}
}
override fun visitIfStatement(statement: PsiIfStatement) {
if (contSearch) {
checkBranch(statement, statement.thenBranch, false)
checkBranch(statement, statement.elseBranch, true)
}
}
private fun checkBranch(statement: PsiIfStatement, branch: PsiStatement?, removeElse: Boolean) {
if (branch != null) {
if (hasEmptyReturn(branch)) {
registerProblem(holder, isOnTheFly, statement, removeElse)
} else {
branch.accept(TestMethodVisitor(holder, isOnTheFly, containingClass))
}
}
}
}
class CheckForAssertThatCallsVisitor(private val containingClass: PsiClass, private var depth: Int) : JavaRecursiveElementWalkingVisitor() {
var foundAssertThat = false
private var statementCount = 0
var aborted = false
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
if (foundAssertThat || aborted) return
if (++statementCount > MAX_STATEMENTS_COUNT) {
aborted = true
return
}
super.visitExpressionStatement(statement)
val methodCallExpression = statement.expression as? PsiMethodCallExpression
if (methodCallExpression != null) {
foundAssertThat = methodCallExpression.hasAssertThat()
val method = methodCallExpression.resolveMethod()
if (method?.containingClass == containingClass) {
if (depth < MAX_RECURSION_DEPTH) {
val recursionVisitor = CheckForAssertThatCallsVisitor(containingClass, depth + 1)
method.accept(recursionVisitor)
foundAssertThat = recursionVisitor.foundAssertThat
statementCount += recursionVisitor.statementCount
aborted = recursionVisitor.aborted
} else {
aborted = true
}
}
}
}
}
}
@@ -0,0 +1,176 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import com.siyeh.ig.callMatcher.CallMatcher
import com.siyeh.ig.psiutils.EquivalenceChecker
import de.platon42.intellij.plugins.cajon.*
class BogusAssertionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Bogus assertion due to same actual and expected expressions"
private const val ACTUAL_IS_EQUAL_TO_EXPECTED_MESSAGE = "Actual expression in assertThat() is the same as expected"
private const val WEAK_ACTUAL_IS_EQUAL_TO_EXPECTED_MESSAGE = "Same actual and expected expression, but may be testing equals() or hashCode()"
private val SAME_OBJECT =
CallMatcher.instanceCall(
AssertJClassNames.ASSERT_INTERFACE,
MethodNames.IS_EQUAL_TO,
MethodNames.IS_SAME_AS,
"hasSameClassAs",
"hasSameHashCodeAs"
).parameterCount(1)
private val ARRAY_METHODS = arrayOf(
"hasSameSizeAs",
MethodNames.CONTAINS,
"containsAnyOf",
"containsExactly",
"containsExactlyInAnyOrder",
"containsOnly",
"containsSequence",
"containsSubsequence",
"startsWith",
"endsWith"
)
private val SAME_BOOLEAN_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_BOOLEAN_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_BYTE_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_BYTE_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_SHORT_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_SHORT_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_INT_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_INT_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_LONG_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_LONG_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_FLOAT_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_FLOAT_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_DOUBLE_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_DOUBLE_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_CHAR_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_CHAR_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val SAME_OBJECT_ARRAY_CONTENTS =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME, *ARRAY_METHODS).parameterCount(1)
private val HASHCODE_OR_IS_EQUAL_TO =
CallMatcher.instanceCall(
AssertJClassNames.ASSERT_INTERFACE,
MethodNames.IS_EQUAL_TO, "hasSameHashCodeAs"
).parameterCount(1)
private val SAME_ENUMERABLE_CONTENTS =
CallMatcher.instanceCall(
AssertJClassNames.ENUMERABLE_ASSERT_INTERFACE,
MethodNames.HAS_SAME_SIZE_AS
).parameterCount(1)
private val SAME_ITERABLE_CONTENTS =
CallMatcher.instanceCall(
AssertJClassNames.ABSTRACT_ITERABLE_ASSERT_CLASSNAME,
"hasSameElementsAs",
MethodNames.CONTAINS_ALL,
"containsAnyElementsOf",
"containsOnlyElementsOf",
"containsExactlyElementsOf",
"containsSequence",
"containsSubsequence"
).parameterCount(1)
private val SAME_MAP_CONTENTS =
CallMatcher.instanceCall(
AssertJClassNames.ABSTRACT_MAP_ASSERT_CLASSNAME,
"containsAllEntriesOf",
"containsExactlyEntriesOf",
"containsExactlyInAnyOrderEntriesOf",
MethodNames.HAS_SAME_SIZE_AS
).parameterCount(1)
private val SAME_CHAR_SEQUENCE_CONTENTS =
CallMatcher.instanceCall(
AssertJClassNames.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME,
MethodNames.IS_EQUAL_TO,
MethodNames.IS_EQUAL_TO_IC,
MethodNames.STARTS_WITH,
MethodNames.ENDS_WITH,
"containsSequence",
"containsSubsequence"
).parameterCount(1)
private val SAME_ACTUAL_AND_EXPECTED_MATCHERS = CallMatcher.anyOf(
SAME_OBJECT,
SAME_ENUMERABLE_CONTENTS,
SAME_ITERABLE_CONTENTS,
SAME_MAP_CONTENTS,
SAME_CHAR_SEQUENCE_CONTENTS,
SAME_BOOLEAN_ARRAY_CONTENTS,
SAME_BYTE_ARRAY_CONTENTS,
SAME_SHORT_ARRAY_CONTENTS,
SAME_INT_ARRAY_CONTENTS,
SAME_LONG_ARRAY_CONTENTS,
SAME_FLOAT_ARRAY_CONTENTS,
SAME_DOUBLE_ARRAY_CONTENTS,
SAME_CHAR_ARRAY_CONTENTS,
SAME_OBJECT_ARRAY_CONTENTS
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val assertThatCall = PsiTreeUtil.findChildrenOfType(statement, PsiMethodCallExpression::class.java).find { ALL_ASSERT_THAT_MATCHERS.test(it) } ?: return
val actualExpression = assertThatCall.firstArg
val allCalls = assertThatCall.collectMethodCallsUpToStatement().toList()
// Note: replace with TrackingEquivalenceChecker() for IDEA >= 2019.1
val equivalenceChecker = EquivalenceChecker.getCanonicalPsiEquivalence()!!
val isSameExpression = allCalls
.filter(SAME_ACTUAL_AND_EXPECTED_MATCHERS::test)
.any { equivalenceChecker.expressionsAreEquivalent(actualExpression, it.firstArg) }
if (isSameExpression) {
if (!hasExpressionWithSideEffects(actualExpression)) {
if (allCalls.any(HASHCODE_OR_IS_EQUAL_TO::test)) {
val method = PsiTreeUtil.getParentOfType(statement, PsiMethod::class.java, true)
val methodName = method?.name
if ((methodName != null)
&& ((methodName.contains("equal", ignoreCase = true) || methodName.contains("hashcode", ignoreCase = true)))
) {
if (isOnTheFly) {
holder.registerProblem(statement, WEAK_ACTUAL_IS_EQUAL_TO_EXPECTED_MESSAGE, ProblemHighlightType.INFORMATION)
}
return
}
}
holder.registerProblem(statement, ACTUAL_IS_EQUAL_TO_EXPECTED_MESSAGE)
}
}
}
private fun hasExpressionWithSideEffects(actualExpression: PsiExpression): Boolean {
var result = false
PsiTreeUtil.processElements(actualExpression) { element ->
val matched = when (element) {
is PsiUnaryExpression -> (element.operationTokenType == JavaTokenType.PLUSPLUS)
|| (element.operationTokenType == JavaTokenType.MINUSMINUS)
is PsiMethodCallExpression -> true
else -> false
}
if (matched) {
result = true
false
} else {
true
}
}
return result
}
}
}
}
@@ -0,0 +1,130 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.DeleteMethodCallQuickFix
class ImplicitAssertionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting implicitly covered conditions"
private const val DELETE_IMPLICIT_DESCRIPTION_TEMPLATE = "Delete implicit %s() covered by %s()"
private const val SURPLUS_ASSERTION_MESSAGE = "Implicit %s() assertion is covered by %s()"
private val IS_PRESENT = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OPTIONAL_ASSERT_CLASSNAME, MethodNames.IS_PRESENT, MethodNames.IS_NOT_EMPTY)
.parameterCount(0)!!
private val IS_NOT_PRESENT = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OPTIONAL_ASSERT_CLASSNAME, MethodNames.IS_NOT_PRESENT, MethodNames.IS_EMPTY)
.parameterCount(0)!!
private val OPTIONAL_CONTAINS =
CallMatcher.instanceCall(
AssertJClassNames.ABSTRACT_OPTIONAL_ASSERT_CLASSNAME,
MethodNames.CONTAINS, MethodNames.CONTAINS_SAME,
"hasValue", "hasValueSatisfying", "containsInstanceOf"
).parameterCount(1)!!
private val OBJECT_ENUMERABLE_ANY_CONTENT_ASSERTIONS = CallMatcher.instanceCall(
AssertJClassNames.OBJECT_ENUMERABLE_ASSERT_INTERFACE,
MethodNames.CONTAINS, "containsOnly", "containsOnlyNulls", MethodNames.CONTAINS_ONLY_ONCE,
"containsExactly", "containsExactlyInAnyOrder", "containsExactlyInAnyOrderElementsOf",
"containsAll", "containsAnyOf",
"containsAnyElementsOf", "containsExactlyElementsOf", "containsOnlyElementsOf",
"isSubsetOf", "containsSequence", "containsSubsequence",
"doesNotContainSequence", "doesNotContainSubsequence", "doesNotContain",
"doesNotContainAnyElementsOf", "doesNotHaveDuplicates",
"startsWith", "endsWith", "containsNull", "doesNotContainNull",
"are", "areNot", "have", "doNotHave", "areAtLeastOne", "areAtLeast", "areAtMost", "areExactly",
"haveAtLeastOne", "haveAtLeast", "haveAtMost", "haveExactly",
"doesNotHave", "doesNotHaveSameClassAs",
"hasAtLeastOneElementOfType", "hasOnlyElementsOfType", "hasOnlyElementsOfTypes",
"doesNotHaveAnyElementsOfTypes",
"has", "doesNotHave",
"hasOnlyOneElementSatisfying", "hasSameElementsAs",
"allMatch", "allSatisfy", "anyMatch", "anySatisfy", "noneMatch", "noneSatisfy"
)!!
private val OBJECT_ENUMERABLE_AT_LEAST_ONE_CONTENT_ASSERTIONS = CallMatcher.instanceCall(
AssertJClassNames.OBJECT_ENUMERABLE_ASSERT_INTERFACE,
"containsOnlyNulls",
"startsWith", "endsWith", "containsNull",
"areAtLeastOne",
"haveAtLeastOne",
"hasAtLeastOneElementOfType",
"anyMatch", "anySatisfy"
)!!
private val ENUMERABLE_NON_NULL_ASSERTIONS = CallMatcher.instanceCall(
AssertJClassNames.ENUMERABLE_ASSERT_INTERFACE,
MethodNames.IS_EMPTY, MethodNames.IS_NOT_EMPTY,
MethodNames.HAS_SIZE, MethodNames.HAS_SIZE_GREATER_THAN, MethodNames.HAS_SIZE_GREATER_THAN_OR_EQUAL_TO,
MethodNames.HAS_SIZE_LESS_THAN, MethodNames.HAS_SIZE_LESS_THAN_OR_EQUAL_TO,
"hasSizeBetween", MethodNames.HAS_SAME_SIZE_AS
)!!
private val ENUMERABLE_AT_LEAST_ONE_CONTENT_ASSERTIONS = CallMatcher.instanceCall(
AssertJClassNames.ENUMERABLE_ASSERT_INTERFACE,
MethodNames.HAS_SIZE, MethodNames.HAS_SIZE_GREATER_THAN,
MethodNames.HAS_SAME_SIZE_AS
)!!
private val NON_NULL_CORE_ASSERTIONS = CallMatcher.instanceCall(
AssertJClassNames.ASSERT_INTERFACE,
"isInstanceOf", "isInstanceOfSatisfying", "isInstanceOfAny", "isExactlyInstanceOf", "isOfAnyClassIn",
"isNotInstanceOf", "isNotInstanceOfAny", "isNotExactlyInstanceOf", "isNotOfAnyClassIn",
"hasSameClassAs", "doesNotHaveSameClassAs",
"hasToString", "hasSameHashCodeAs"
)!!
private val GUAVA_IS_PRESENT = CallMatcher.instanceCall(AssertJClassNames.GUAVA_OPTIONAL_ASSERTIONS_CLASSNAME, MethodNames.IS_PRESENT)
.parameterCount(0)!!
private val GUAVA_IS_ABSENT = CallMatcher.instanceCall(AssertJClassNames.GUAVA_OPTIONAL_ASSERTIONS_CLASSNAME, MethodNames.IS_ABSENT)
.parameterCount(0)!!
private val GUAVA_OPTIONAL_CONTAINS = CallMatcher.instanceCall(
AssertJClassNames.GUAVA_OPTIONAL_ASSERTIONS_CLASSNAME,
MethodNames.CONTAINS, "extractingValue", "extractingCharSequence"
)!!
private val MAPPINGS = listOf(
IS_NOT_NULL to CallMatcher.anyOf(
NON_NULL_CORE_ASSERTIONS,
ENUMERABLE_NON_NULL_ASSERTIONS,
OBJECT_ENUMERABLE_ANY_CONTENT_ASSERTIONS,
IS_PRESENT, IS_NOT_PRESENT, OPTIONAL_CONTAINS,
GUAVA_IS_PRESENT, GUAVA_IS_ABSENT, GUAVA_OPTIONAL_CONTAINS
)!!,
IS_NOT_EMPTY to CallMatcher.anyOf(
ENUMERABLE_AT_LEAST_ONE_CONTENT_ASSERTIONS,
OBJECT_ENUMERABLE_AT_LEAST_ONE_CONTENT_ASSERTIONS
)!!,
IS_PRESENT to OPTIONAL_CONTAINS,
GUAVA_IS_PRESENT to GUAVA_OPTIONAL_CONTAINS
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!expression.hasAssertThat()) return
val mapping = MAPPINGS.firstOrNull { it.first.test(expression) } ?: return
val followupExpression = expression.findFluentCallTo(mapping.second) ?: return
val redundantName = getOriginalMethodName(expression) ?: return
val followupName = getOriginalMethodName(followupExpression) ?: return
val description = DELETE_IMPLICIT_DESCRIPTION_TEMPLATE.format(redundantName, followupName)
val quickFix = DeleteMethodCallQuickFix(description)
val textRange = TextRange(expression.qualifierExpression.textLength, expression.textLength)
holder.registerProblem(expression, textRange, SURPLUS_ASSERTION_MESSAGE.format(redundantName, followupName), quickFix)
}
}
}
}
@@ -1,112 +1,143 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.CommonClassNames
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.siyeh.ig.callMatcher.CallMatcher
import com.siyeh.ig.callMatcher.CallMatcher.anyOf
import com.siyeh.ig.callMatcher.CallMatcher.staticCall
import de.platon42.intellij.plugins.cajon.AssertJClassNames
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitAssertMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitAssumeMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceJUnitDeltaAssertMethodCallQuickFix
class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
companion object {
private const val DISPLAY_NAME = "Convert JUnit assertions to AssertJ"
private const val DISPLAY_NAME = "Convert JUnit assertions/assumptions to AssertJ"
private const val CONVERT_MESSAGE_TEMPLATE = "%s can be converted to AssertJ style"
private const val CONVERT_DESCRIPTION_TEMPLATE = "Convert %s() to %s().%s()"
private val MAPPINGS = listOf(
private val ASSERT_MAPPINGS = listOf(
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes("boolean")
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_TRUE_METHOD).parameterTypes("boolean")
),
"isTrue()", false
MethodNames.IS_TRUE, hasExpected = false
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes("boolean")
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_FALSE_METHOD).parameterTypes("boolean")
),
"isFalse()", false
MethodNames.IS_FALSE, hasExpected = false
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
),
"isNull()", false
MethodNames.IS_NULL, hasExpected = false
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_OBJECT),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_NULL_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
),
"isNotNull()", false
MethodNames.IS_NOT_NULL, hasExpected = false
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double", "double", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes("double", "double", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float", "float", "float"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes("float", "float", "float")
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double", "double", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes("double", "double", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float", "float", "float"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterTypes("float", "float", "float")
),
"isCloseTo()", hasDelta = true
MethodNames.IS_CLOSE_TO, hasDelta = true
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterCount(3),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterCount(2)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterCount(3),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_EQUALS_METHOD).parameterCount(2)
),
"isEqualTo()"
MethodNames.IS_EQUAL_TO
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double", "double", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes("double", "double", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float", "float", "float"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes("float", "float", "float")
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double", "double", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes("double", "double", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float", "float", "float"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterTypes("float", "float", "float")
),
"isNotCloseTo()", hasDelta = true
MethodNames.IS_NOT_CLOSE_TO, hasDelta = true
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterCount(3),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterCount(2)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterCount(3),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_EQUALS_METHOD).parameterCount(2)
),
"isNotEqualTo()"
MethodNames.IS_NOT_EQUAL_TO
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_SAME_METHOD).parameterCount(3),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_SAME_METHOD).parameterCount(2)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_SAME_METHOD).parameterCount(3),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_SAME_METHOD).parameterCount(2)
),
"isSameAs()"
MethodNames.IS_SAME_AS
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_SAME_METHOD).parameterCount(3),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_SAME_METHOD).parameterCount(2)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_SAME_METHOD).parameterCount(3),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_NOT_SAME_METHOD).parameterCount(2)
),
"isNotSameAs()"
MethodNames.IS_NOT_SAME_AS
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double[]", "double[]", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes("double[]", "double[]", "double"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float[]", "float[]", "float"),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes("float[]", "float[]", "float")
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "double[]", "double[]", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes("double[]", "double[]", "double"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "float[]", "float[]", "float"),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterTypes("float[]", "float[]", "float")
),
"containsExactly()", hasDelta = true
MethodNames.CONTAINS_EXACTLY, hasDelta = true
),
Mapping(
anyOf(
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterCount(2),
CallMatcher.staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterCount(3)
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterCount(2),
staticCall(JUNIT_ASSERT_CLASSNAME, ASSERT_ARRAY_EQUALS_METHOD).parameterCount(3)
),
"containsExactly()"
MethodNames.CONTAINS_EXACTLY
)
)
private val ASSUME_MAPPINGS = listOf(
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_TRUE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_TRUE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_TRUE, hasExpected = false
),
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_FALSE_METHOD).parameterTypes(CommonClassNames.JAVA_LANG_STRING, "boolean"),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_FALSE_METHOD).parameterTypes("boolean")
),
MethodNames.IS_FALSE, hasExpected = false
),
Mapping(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NOT_NULL_METHOD).parameterCount(1),
MethodNames.IS_NOT_NULL, hasExpected = false, singleArgument = true
),
Mapping(
anyOf(
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NO_EXCEPTION).parameterTypes(CommonClassNames.JAVA_LANG_STRING, CommonClassNames.JAVA_LANG_THROWABLE),
staticCall(JUNIT_ASSUME_CLASSNAME, ASSUME_NO_EXCEPTION).parameterTypes(CommonClassNames.JAVA_LANG_THROWABLE)
),
"doesNotThrowAnyException", hasExpected = false
)
)
}
@@ -117,63 +148,53 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
val isJUnitAssertCall = expression.resolveMethod()?.containingClass?.qualifiedName == JUNIT_ASSERT_CLASSNAME
if (!isJUnitAssertCall) {
return // early exit
}
for (mapping in MAPPINGS) {
if (mapping.callMatcher.test(expression)) {
when (expression.resolveMethod()?.containingClass?.qualifiedName) {
JUNIT_ASSERT_CLASSNAME -> {
JavaPsiFacade.getInstance(expression.project)
.findClass(AssertJClassNames.ASSERTIONS_CLASSNAME, GlobalSearchScope.allScope(expression.project)) ?: return
val mapping = ASSERT_MAPPINGS.firstOrNull { it.callMatcher.test(expression) } ?: return
if (mapping.hasDelta) {
registerDeltaReplacementMethod(holder, expression, mapping.replacement)
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSERT_THAT, ::ReplaceJUnitDeltaAssertMethodCallQuickFix)
} else {
registerSimpleReplacementMethod(holder, expression, mapping.hasExpected, mapping.replacement)
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSERT_THAT) { desc, method ->
ReplaceJUnitAssertMethodCallQuickFix(desc, method, !mapping.hasExpected)
}
}
}
JUNIT_ASSUME_CLASSNAME -> {
JavaPsiFacade.getInstance(expression.project)
.findClass(AssertJClassNames.ASSUMPTIONS_CLASSNAME, GlobalSearchScope.allScope(expression.project)) ?: return
val mapping = ASSUME_MAPPINGS.firstOrNull { it.callMatcher.test(expression) } ?: return
if (!mapping.singleArgument || expression.argumentList.expressions.size == 1) {
registerConvertMethod(holder, expression, mapping.replacement, MethodNames.ASSUME_THAT) { desc, method ->
ReplaceJUnitAssumeMethodCallQuickFix(desc, method)
}
}
return
}
}
}
}
}
private fun registerSimpleReplacementMethod(
private fun registerConvertMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
hasExpected: Boolean,
replacementMethod: String
replacementMethod: String,
staticMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val description = CONVERT_DESCRIPTION_TEMPLATE.format(originalMethod, staticMethod, replacementMethod)
val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod)
holder.registerProblem(
expression,
message,
ProblemHighlightType.INFORMATION,
null as TextRange?,
ReplaceJUnitAssertMethodCallQuickFix(description, hasExpected, replacementMethod)
)
}
private fun registerDeltaReplacementMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String
) {
val originalMethod = getOriginalMethodName(expression) ?: return
val description = REPLACE_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = CONVERT_MESSAGE_TEMPLATE.format(originalMethod)
holder.registerProblem(
expression,
message,
ProblemHighlightType.INFORMATION,
null as TextRange?,
ReplaceJUnitDeltaAssertMethodCallQuickFix(description, replacementMethod)
)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, message, quickfix)
}
private class Mapping(
val callMatcher: CallMatcher,
val replacement: String,
val hasExpected: Boolean = true,
val hasDelta: Boolean = false
val hasDelta: Boolean = false,
val singleArgument: Boolean = false
)
}
@@ -0,0 +1,116 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.codeInspection.ui.SingleIntegerFieldOptionsPanel
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import com.siyeh.ig.psiutils.EquivalenceChecker
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.JoinStatementsQuickFix
import javax.swing.JComponent
class JoinAssertThatStatementsInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Join multiple assertThat() statements with same actual expression"
private const val CAN_BE_JOINED_DESCRIPTION = "Multiple assertThat() statements can be joined together"
private const val DEFAULT_SEPARATE_LINE_LIMIT = 1
}
@JvmField
var separateLineLimit: Int = DEFAULT_SEPARATE_LINE_LIMIT
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitCodeBlock(block: PsiCodeBlock) {
super.visitCodeBlock(block)
var lastActualExpression: PsiExpression? = null
var sameCount = 0
var firstStatement: PsiStatement? = null
var lastStatement: PsiStatement? = null
// Note: replace with TrackingEquivalenceChecker() for IDEA >= 2019.1
val equivalenceChecker = EquivalenceChecker.getCanonicalPsiEquivalence()!!
for (statement in block.statements) {
val assertThatCall = isLegitAssertThatCall(statement)
var reset = true
var actualExpression: PsiExpression? = null
if (assertThatCall != null) {
reset = (lastActualExpression == null)
actualExpression = assertThatCall.firstArg
if (!reset) {
val isSame = equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression)
&& !hasExpressionWithSideEffects(actualExpression)
if (isSame) {
sameCount++
lastStatement = statement
} else {
reset = true
}
}
}
if (reset) {
if (sameCount > 1) {
registerProblem(holder, isOnTheFly, firstStatement!!, lastStatement!!)
}
firstStatement = statement
lastStatement = null
lastActualExpression = actualExpression
sameCount = 1
}
}
if (sameCount > 1) {
registerProblem(holder, isOnTheFly, firstStatement!!, lastStatement!!)
}
}
private fun isLegitAssertThatCall(statement: PsiStatement?): PsiMethodCallExpression? {
if ((statement is PsiExpressionStatement) && (statement.expression is PsiMethodCallExpression)) {
if (!statement.hasAssertThat()) return null
val assertThatCall = PsiTreeUtil.findChildrenOfType(statement, PsiMethodCallExpression::class.java).find { ALL_ASSERT_THAT_MATCHERS.test(it) }
return assertThatCall?.takeIf { it.findFluentCallTo(EXTRACTING_CALL_MATCHERS) == null }
}
return null
}
private fun hasExpressionWithSideEffects(actualExpression: PsiExpression): Boolean {
var result = false
PsiTreeUtil.processElements(actualExpression) { element ->
val matched = when (element) {
is PsiUnaryExpression -> (element.operationTokenType == JavaTokenType.PLUSPLUS)
|| (element.operationTokenType == JavaTokenType.MINUSMINUS)
is PsiMethodCallExpression -> KNOWN_METHODS_WITH_SIDE_EFFECTS.test(element)
else -> false
}
if (matched) {
result = true
false
} else {
true
}
}
return result
}
}
}
override fun createOptionsPanel(): JComponent {
return SingleIntegerFieldOptionsPanel("Limit for joins before adding line breaks:", this, "separateLineLimit")
}
private fun registerProblem(holder: ProblemsHolder, isOnTheFly: Boolean, firstStatement: PsiStatement, lastStatement: PsiStatement) {
val problemDescriptor = holder.manager.createProblemDescriptor(
firstStatement,
lastStatement,
CAN_BE_JOINED_DESCRIPTION,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOnTheFly,
JoinStatementsQuickFix(separateLineLimit)
)
holder.registerProblem(problemDescriptor)
}
}
@@ -0,0 +1,53 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiExpressionStatement
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.util.PsiTreeUtil
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.JoinVarArgsContainsQuickFix
class JoinVarArgsContainsInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Join arguments to variadic for contains()/containsOnlyOnce()/doesNotContain()"
private const val JOIN_VARARGS_MESSAGE = "Calls to same methods may be joined to variadic version"
private val MATCHERS = listOf(MethodNames.CONTAINS, MethodNames.CONTAINS_ONLY_ONCE, MethodNames.DOES_NOT_CONTAIN)
.map { CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_ITERABLE_ASSERT_CLASSNAME, it) }
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val assertThatCall = PsiTreeUtil.findChildrenOfType(statement, PsiMethodCallExpression::class.java).find { ALL_ASSERT_THAT_MATCHERS.test(it) } ?: return
val allCalls = assertThatCall.collectMethodCallsUpToStatement().toList()
if (allCalls.find(COMPLEX_CALLS_THAT_MAKES_STUFF_TRICKY::test) != null) return
val onlyAssertionCalls = allCalls
.filterNot(NOT_ACTUAL_ASSERTIONS::test)
.toList()
for (methodMatcher in MATCHERS) {
if (onlyAssertionCalls.count(methodMatcher::test) > 1) {
val outmostMethodCall = statement.findOutmostMethodCall() ?: return
val quickFix = JoinVarArgsContainsQuickFix(MATCHERS)
val textRange = TextRange(assertThatCall.textLength, outmostMethodCall.textLength)
holder.registerProblem(outmostMethodCall, textRange, JOIN_VARARGS_MESSAGE, quickFix)
return
}
}
}
}
}
}
@@ -0,0 +1,108 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.quickfixes.SwapActualAndExpectedExpressionMethodCallQuickFix
class TwistedAssertionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Twisted or suspicious actual and expected expressions"
private const val TWISTED_ACTUAL_AND_EXPECTED_MESSAGE = "Twisted actual and expected expressions in assertion"
private const val SWAP_ACTUAL_AND_EXPECTED_DESCRIPTION = "Swap actual and expected expressions in assertion"
private const val SWAP_ACTUAL_AND_EXPECTED_AND_REPLACE_DESCRIPTION_TEMPLATE = "Replace %s() by %s() and swap actual and expected expressions"
private const val ACTUAL_IS_A_CONSTANT_MESSAGE = "Actual expression in assertThat() is a constant"
private val GENERIC_IS_EQUAL_TO = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, MethodNames.IS_EQUAL_TO).parameterCount(1)
private val GENERIC_IS_NOT_EQUAL_TO = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, MethodNames.IS_NOT_EQUAL_TO).parameterCount(1)
private val GENERIC_IS_SAME_AS = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, MethodNames.IS_SAME_AS).parameterCount(1)
private val GENERIC_IS_NOT_SAME_AS = CallMatcher.instanceCall(AssertJClassNames.ASSERT_INTERFACE, MethodNames.IS_NOT_SAME_AS).parameterCount(1)
private val GENERIC_IS_GREATER_THAN = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_GREATER_THAN).parameterCount(1)
private val GENERIC_IS_GREATER_THAN_OR_EQUAL_TO =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_GREATER_THAN_OR_EQUAL_TO).parameterCount(1)
private val GENERIC_IS_LESS_THAN = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_LESS_THAN).parameterCount(1)
private val GENERIC_IS_LESS_THAN_OR_EQUAL_TO =
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_LESS_THAN_OR_EQUAL_TO).parameterCount(1)
private val STRING_IS_EQUAL_TO_IC = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME, MethodNames.IS_EQUAL_TO_IC).parameterCount(1)
private val STRING_REGEX_MATCHING = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME, "matches", "doesNotMatch").parameterCount(1)
private val CALL_MATCHER_TO_REPLACEMENT_MAP = mapOf(
GENERIC_IS_EQUAL_TO to MethodNames.IS_EQUAL_TO,
GENERIC_IS_NOT_EQUAL_TO to MethodNames.IS_NOT_EQUAL_TO,
GENERIC_IS_SAME_AS to MethodNames.IS_SAME_AS,
GENERIC_IS_NOT_SAME_AS to MethodNames.IS_NOT_SAME_AS,
GENERIC_IS_GREATER_THAN to MethodNames.IS_LESS_THAN_OR_EQUAL_TO,
GENERIC_IS_GREATER_THAN_OR_EQUAL_TO to MethodNames.IS_LESS_THAN,
GENERIC_IS_LESS_THAN to MethodNames.IS_GREATER_THAN_OR_EQUAL_TO,
GENERIC_IS_LESS_THAN_OR_EQUAL_TO to MethodNames.IS_GREATER_THAN,
STRING_IS_EQUAL_TO_IC to MethodNames.IS_EQUAL_TO_IC,
CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME, MethodNames.IS_NOT_EQUAL_TO_IC).parameterCount(1)
to MethodNames.IS_NOT_EQUAL_TO_IC
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
super.visitExpressionStatement(statement)
if (!statement.hasAssertThat()) return
val assertThatCall = PsiTreeUtil.findChildrenOfType(statement, PsiMethodCallExpression::class.java).find { ALL_ASSERT_THAT_MATCHERS.test(it) } ?: return
val actualExpression = assertThatCall.firstArg
actualExpression.calculateConstantValue() ?: return
val allCalls = assertThatCall.collectMethodCallsUpToStatement().toList()
val tooComplex = allCalls.find(USING_COMPARATOR::test) != null
var severity = ProblemHighlightType.GENERIC_ERROR_OR_WARNING
if (actualExpression.type is PsiClassType) {
val psiManager = PsiManager.getInstance(statement.project)
val javaLangClass = PsiType.getJavaLangClass(psiManager, GlobalSearchScope.allScope(statement.project))
if (actualExpression.type!!.isAssignableFrom(javaLangClass)) {
return
}
}
if (!tooComplex) {
val onlyAssertionCalls = allCalls
.filterNot(NOT_ACTUAL_ASSERTIONS::test)
.toList()
if (onlyAssertionCalls.size == 1) {
val expectedMethodCall = onlyAssertionCalls.first()
if (STRING_REGEX_MATCHING.test(expectedMethodCall)) {
return
}
if (expectedMethodCall.getArgOrNull(0)?.calculateConstantValue() == null) {
val matchedMethod = CALL_MATCHER_TO_REPLACEMENT_MAP.asSequence().firstOrNull { it.key.test(expectedMethodCall) }
if (matchedMethod != null) {
val originalMethodName = getOriginalMethodName(expectedMethodCall)
val replacementMethod = matchedMethod.value
val description = if (originalMethodName == replacementMethod) {
SWAP_ACTUAL_AND_EXPECTED_DESCRIPTION
} else {
SWAP_ACTUAL_AND_EXPECTED_AND_REPLACE_DESCRIPTION_TEMPLATE.format(originalMethodName, replacementMethod)
}
holder.registerProblem(
statement,
TWISTED_ACTUAL_AND_EXPECTED_MESSAGE,
SwapActualAndExpectedExpressionMethodCallQuickFix(description, replacementMethod)
)
return
}
} else {
severity = ProblemHighlightType.WEAK_WARNING
}
}
}
holder.registerProblem(statement, ACTUAL_IS_A_CONSTANT_MESSAGE, severity)
}
}
}
}
@@ -1,32 +1,10 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.psi.PsiElementFactory
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiMethodCallExpression
import org.jetbrains.annotations.NonNls
abstract class AbstractCommonQuickFix(private val description: String) : LocalQuickFix {
override fun getName() = description
override fun getFamilyName() = description
companion object {
@NonNls
const val GUAVA_ASSERTIONS_CLASSNAME = "org.assertj.guava.api.Assertions"
}
protected fun addStaticImport(method: PsiMethod, element: PsiMethodCallExpression, factory: PsiElementFactory, vararg allowedClashes: String) {
val methodName = method.name
val containingClass = method.containingClass ?: return
val importList = (element.containingFile as PsiJavaFile).importList ?: return
val notImportedStatically = importList.importStaticStatements.none {
val targetClass = it.resolveTargetClass() ?: return@none false
((it.referenceName == methodName) && !allowedClashes.contains(targetClass.qualifiedName))
|| (it.isOnDemand && (targetClass == method.containingClass))
}
if (notImportedStatically) {
importList.add(factory.createImportStaticStatement(containingClass, methodName))
}
}
}
@@ -0,0 +1,23 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.qualifierExpression
class DeleteMethodCallQuickFix(description: String) : AbstractCommonQuickFix(description) {
companion object {
private const val DELETE_METHOD_DESCRIPTION = "Delete unnecessary method calls"
}
override fun getFamilyName(): String {
return DELETE_METHOD_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val methodCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
methodCallExpression.replace(methodCallExpression.qualifierExpression)
}
}
@@ -0,0 +1,26 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement
import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.*
class ForGuavaPostFix {
companion object {
val REPLACE_BY_GUAVA_ASSERT_THAT_AND_STATIC_IMPORT: (Project, ProblemDescriptor) -> Unit = exit@
{ _, descriptor ->
val element = descriptor.startElement
val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return@exit
val assertThatCall = statement.findStaticMethodCall() ?: return@exit
val newMethodCall = createGuavaAssertThat(element, assertThatCall.firstArg)
newMethodCall.resolveMethod()?.addAsStaticImport(element, AssertJClassNames.ASSERTIONS_CLASSNAME)
val parentCall = PsiTreeUtil.getParentOfType(assertThatCall, PsiMethodCallExpression::class.java) ?: return@exit
parentCall.replaceQualifier(newMethodCall)
parentCall.shortenAndReformat()
}
}
}
@@ -0,0 +1,37 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class HasHashCodeQuickFix :
AbstractCommonQuickFix(HASHCODE_DESCRIPTION) {
companion object {
private const val HASHCODE_DESCRIPTION = "Replace calls to hashCode() with hasSameHashCodeAs()"
}
override fun getFamilyName(): String {
return HASHCODE_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
val methodsToFix = assertThatMethodCall.gatherAssertionCalls()
assertExpression.replace(assertExpression.qualifierExpression)
methodsToFix
.forEach {
val innerHashCodeObject = (it.firstArg as PsiMethodCallExpression).qualifierExpression
val expectedExpression = createExpectedMethodCall(it, "hasSameHashCodeAs", innerHashCodeObject)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}
@@ -0,0 +1,37 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiUnaryExpression
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil
import de.platon42.intellij.plugins.cajon.*
class InvertUnaryStatementQuickFix : AbstractCommonQuickFix(INVERT_CONDITION_DESCRIPTION) {
companion object {
private const val INVERT_CONDITION_DESCRIPTION = "Invert condition in assertThat()"
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiUnaryExpression ?: return
val operand = PsiUtil.skipParenthesizedExprDown(assertExpression.operand) ?: return
assertExpression.replace(operand)
var methodCall: PsiMethodCallExpression? = assertThatMethodCall
while (methodCall != null) {
val expectedResult = methodCall.getExpectedBooleanResult()
val nextMethodCall = PsiTreeUtil.getParentOfType(methodCall, PsiMethodCallExpression::class.java)
if (expectedResult != null) {
val replacementMethod = expectedResult.map(MethodNames.IS_FALSE, MethodNames.IS_TRUE)
val expectedExpression = createExpectedMethodCall(methodCall, replacementMethod)
expectedExpression.replaceQualifierFromMethodCall(methodCall)
methodCall.replace(expectedExpression)
}
methodCall = nextMethodCall
}
}
}
@@ -0,0 +1,80 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import de.platon42.intellij.plugins.cajon.findStaticMethodCall
import de.platon42.intellij.plugins.cajon.shortenAndReformat
class JoinStatementsQuickFix(private val separateLineLimit: Int) : AbstractCommonQuickFix(JOIN_STATEMENTS_MESSAGE) {
companion object {
private const val JOIN_STATEMENTS_MESSAGE = "Join assertThat() statements"
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val firstStatement = descriptor.startElement as PsiExpressionStatement
val lastStatement = descriptor.endElement as PsiExpressionStatement
val expressionCount = countExpressions(firstStatement, lastStatement)
val addLineBreaks = (expressionCount > separateLineLimit)
do {
val commentsToKeep = ArrayList<PsiComment>()
val stuffToDelete = ArrayList<PsiElement>()
var previousStatement = lastStatement.prevSibling!!
while (previousStatement !is PsiExpressionStatement) {
if (previousStatement is PsiComment) {
commentsToKeep.add(previousStatement.copy() as PsiComment)
}
stuffToDelete.add(previousStatement)
previousStatement = previousStatement.prevSibling!!
}
stuffToDelete.forEach { if (it.isValid) it.delete() }
val statementComments = PsiTreeUtil.getChildrenOfAnyType(previousStatement, PsiComment::class.java)
commentsToKeep.addAll(statementComments)
val assertThatCallOfCursorStatement = lastStatement.findStaticMethodCall()!!
val lastElementBeforeConcat = assertThatCallOfCursorStatement.parent
commentsToKeep.forEach {
lastElementBeforeConcat.addAfter(it, lastElementBeforeConcat.firstChild)
addLineBreak(project, lastElementBeforeConcat)
}
if (commentsToKeep.isEmpty() && addLineBreaks) {
addLineBreak(project, lastElementBeforeConcat)
}
val newLeaf = previousStatement.firstChild
assertThatCallOfCursorStatement.replace(newLeaf)
previousStatement.delete()
} while (previousStatement !== firstStatement)
val codeBlock = PsiTreeUtil.getParentOfType(lastStatement, PsiCodeBlock::class.java) ?: return
codeBlock.shortenAndReformat()
}
private fun addLineBreak(project: Project, lastElementBeforeConcat: PsiElement) {
val newLineNode =
PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\t")
lastElementBeforeConcat.addAfter(newLineNode, lastElementBeforeConcat.firstChild)
}
private fun countExpressions(firstStatement: PsiElement, lastStatement: PsiElement): Int {
var count = 0
var currentStatement = firstStatement
do {
while (currentStatement !is PsiExpressionStatement) {
currentStatement = currentStatement.nextSibling!!
}
count++
if (currentStatement === lastStatement) {
break
}
currentStatement = currentStatement.nextSibling!!
} while (true)
return count
}
}
@@ -0,0 +1,38 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
class JoinVarArgsContainsQuickFix(private val matchers: Iterable<CallMatcher>) : AbstractCommonQuickFix(JOIN_VARARGS_DESCRIPTION) {
companion object {
private const val JOIN_VARARGS_DESCRIPTION = "Join multiple arguments to variadic argument method calls"
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
var outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
for (matcher in matchers) {
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val methodsToFix = assertThatMethodCall.gatherAssertionCalls()
val matchedCalls = methodsToFix.filter(matcher::test)
if (matchedCalls.size > 1) {
val mainCall = matchedCalls.first()
val args = mutableListOf(*mainCall.argumentList.expressions)
for (secondaryCall in matchedCalls.asSequence().drop(1)) {
args.addAll(secondaryCall.argumentList.expressions)
}
val newMainCall = createExpectedMethodCall(mainCall, mainCall.methodExpression.qualifiedName, *args.toTypedArray())
newMainCall.replaceQualifierFromMethodCall(mainCall)
mainCall.replace(newMainCall)
for (secondaryCall in matchedCalls.asSequence().drop(1)) {
val newQualifier = secondaryCall.qualifierExpression
outmostCallExpression = secondaryCall.replace(newQualifier).findOutmostMethodCall() ?: return
}
}
}
}
}
@@ -0,0 +1,44 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiInstanceOfExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.util.PsiUtil
import de.platon42.intellij.plugins.cajon.*
class MoveOutInstanceOfExpressionQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
companion object {
private const val REMOVE_INSTANCEOF_DESCRIPTION = "Move instanceof in actual expressions out of assertThat()"
}
override fun getFamilyName(): String {
return REMOVE_INSTANCEOF_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiInstanceOfExpression ?: return
val expectedClass = assertExpression.checkType ?: return
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
.filter { it.getExpectedBooleanResult() != null }
.toList()
val factory = JavaPsiFacade.getElementFactory(project)
val classObjectAccess = factory.createExpressionFromText("${expectedClass.type.canonicalText}.class", null)
val operand = PsiUtil.deparenthesizeExpression(assertExpression.operand) ?: return
assertExpression.replace(operand)
methodsToFix
.forEach {
val expectedExpression = createExpectedMethodCall(it, replacementMethod, classObjectAccess)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}
@@ -0,0 +1,82 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
class MoveOutMethodCallExpressionQuickFix(
description: String,
private val replacementMethod: String,
private val useNullNonNull: Boolean = false,
private val noExpectedExpression: Boolean = false,
private val keepExpectedAsSecondArgument: Boolean = false,
private val replaceOnlyThisMethod: CallMatcher? = null,
private val replaceFromOriginalMethod: Boolean = false
) :
AbstractCommonQuickFix(description) {
companion object {
private const val REMOVE_ACTUAL_EXPRESSION_DESCRIPTION = "Move method calls in actual expressions out of assertThat()"
}
override fun getFamilyName(): String {
return REMOVE_ACTUAL_EXPRESSION_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
val assertExpressionArg = if (noExpectedExpression) null else assertExpression.getArgOrNull(0)?.copy() as PsiExpression?
when {
replaceOnlyThisMethod != null -> {
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
.filter(replaceOnlyThisMethod::test)
.toList()
assertExpression.replace(assertExpression.qualifierExpression)
methodsToFix
.forEach {
val expectedExpression = createExpectedMethodCall(
it,
replacementMethod,
*if (replaceFromOriginalMethod || noExpectedExpression) listOfNotNull(assertExpressionArg).toTypedArray() else it.argumentList.expressions
)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
keepExpectedAsSecondArgument -> {
assertExpressionArg ?: return
val secondArg =
if (useNullNonNull) JavaPsiFacade.getElementFactory(project).createExpressionFromText("null", null) else outmostCallExpression.getArgOrNull(0)?.copy() ?: return
assertExpression.replace(assertExpression.qualifierExpression)
val expectedExpression = createExpectedMethodCall(outmostCallExpression, replacementMethod, assertExpressionArg, secondArg)
expectedExpression.replaceQualifierFromMethodCall(outmostCallExpression)
outmostCallExpression.replace(expectedExpression)
}
else -> {
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
.filter { (if (useNullNonNull) it.getExpectedNullNonNullResult() else it.getExpectedBooleanResult()) != null }
.toList()
assertExpression.replace(assertExpression.qualifierExpression)
methodsToFix
.forEach {
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *listOfNotNull(assertExpressionArg).toTypedArray())
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}
}
}
@@ -0,0 +1,20 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
class QuickFixWithPostfixDelegate(
private val mainFix: LocalQuickFix,
private val postfix: (Project, ProblemDescriptor) -> Unit
) : LocalQuickFix by mainFix {
override fun getName(): String {
return mainFix.name
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
mainFix.applyFix(project, descriptor)
postfix(project, descriptor)
}
}
@@ -0,0 +1,39 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class RemoveActualOutmostMethodCallQuickFix(
description: String,
private val replacementMethod: String,
private val noExpectedExpression: Boolean = false
) : AbstractCommonQuickFix(description) {
companion object {
private const val REMOVE_ACTUAL_EXPRESSION_DESCRIPTION = "Remove method calls in actual expressions and use better assertion"
}
override fun getFamilyName(): String {
return REMOVE_ACTUAL_EXPRESSION_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
val methodsToFix = assertThatMethodCall.gatherAssertionCalls()
assertExpression.replace(assertExpression.qualifierExpression)
methodsToFix
.forEach {
val args = if (noExpectedExpression) emptyArray() else it.argumentList.expressions
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *args)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}
@@ -0,0 +1,35 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiReferenceExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.qualifierExpression
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class ReplaceHasSizeMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val methodCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
replaceCollectionAndMapSizeOrArrayLength(methodCallExpression.firstArg)
val expectedExpression = createExpectedMethodCall(methodCallExpression, replacementMethod, methodCallExpression.firstArg)
expectedExpression.replaceQualifierFromMethodCall(methodCallExpression)
methodCallExpression.replace(expectedExpression)
}
private fun replaceCollectionAndMapSizeOrArrayLength(assertExpression: PsiExpression) {
assertExpression.replace(
when (assertExpression) {
is PsiReferenceExpression -> assertExpression.qualifierExpression!!
is PsiMethodCallExpression -> assertExpression.qualifierExpression
else -> return
}
)
}
}
@@ -0,0 +1,51 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiBlockStatement
import com.intellij.psi.PsiDeclarationStatement
import com.intellij.psi.PsiIfStatement
import de.platon42.intellij.plugins.cajon.*
class ReplaceIfByAssumeThatQuickFix(private val removeElse: Boolean) : AbstractCommonQuickFix(REPLACE_IF_MESSAGE) {
companion object {
private const val REPLACE_IF_MESSAGE = "Replace if statement by assumeTrue()"
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val ifStatement = descriptor.startElement as PsiIfStatement
val condition = ifStatement.condition ?: return
val factory = JavaPsiFacade.getElementFactory(ifStatement.project)
val assumptionExpression = if (removeElse) MethodNames.IS_TRUE else MethodNames.IS_FALSE
val assumeThatStatement = factory.createStatementFromText(
"${AssertJClassNames.ASSUMPTIONS_CLASSNAME}.${MethodNames.ASSUME_THAT}(true).$assumptionExpression();",
ifStatement
)
val assumeThatMethodCall = assumeThatStatement.findStaticMethodCall() ?: return
assumeThatMethodCall.firstArg.replace(condition)
assumeThatMethodCall.resolveMethod()?.addAsStaticImport(ifStatement)
val branchToKeep = (if (removeElse) ifStatement.thenBranch else ifStatement.elseBranch)?.copy()
val parentBlock = ifStatement.parent
if (branchToKeep != null) {
val anchorElement = ifStatement.nextSibling
if (branchToKeep is PsiBlockStatement) {
val codeBlock = branchToKeep.codeBlock
if (codeBlock.statements.isNotEmpty()) {
val hasDeclarations = codeBlock.statements.any { it is PsiDeclarationStatement }
if (hasDeclarations) {
parentBlock.addAfter(branchToKeep, anchorElement)
} else {
parentBlock.addRangeAfter(codeBlock.firstBodyElement, codeBlock.lastBodyElement, anchorElement)
}
}
} else {
parentBlock.addAfter(branchToKeep, anchorElement)
}
}
ifStatement.replace(assumeThatStatement).shortenAndReformat()
}
}
@@ -2,57 +2,56 @@ package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.GUAVA_ASSERTIONS_CLASSNAME
class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val hasExpected: Boolean, private val replacementMethod: String) :
class ReplaceJUnitAssertMethodCallQuickFix(description: String, private val replacementMethod: String, private val noExpectedExpression: Boolean) :
AbstractCommonQuickFix(description) {
companion object {
private const val CONVERT_DESCRIPTION = "Convert JUnit assertions to assertJ"
}
override fun getFamilyName(): String {
return CONVERT_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList
val count = args.expressionCount
val count = args.expressions.size
val actualExpression = args.expressions[count - 1] ?: return
val (expectedExpression, messageExpression) = if (hasExpected) {
val expected = args.expressions[count - 2] ?: return
val message = if (count > 2) args.expressions[0] else null
Pair(expected, message)
val (expectedExpressions, messageExpression) = if (noExpectedExpression) {
val message = args.expressions.getOrNull(count - 2)
emptyArray<PsiExpression>() to message
} else {
val message = if (count > 1) args.expressions[0] else null
Pair(null, message)
val expected = args.expressions[count - 2] ?: return
val message = args.expressions.getOrNull(count - 3)
arrayOf(expected) to message
}
val expectedMethodCall = factory.createExpressionFromText(
"a.${if (hasExpected) replacementMethod.replace("()", "(e)") else replacementMethod}", element
) as PsiMethodCallExpression
if (hasExpected) {
expectedMethodCall.argumentList.expressions[0].replace(expectedExpression!!)
val swapActualAndExpected = ((expectedExpressions.getOrNull(0)?.calculateConstantValue() == null)
&& (actualExpression.calculateConstantValue() != null))
val (expectedMethodCall, newMethodCall) = if (swapActualAndExpected) {
createExpectedMethodCall(element, replacementMethod, actualExpression) to
createAssertThat(element, expectedExpressions.single())
} else {
createExpectedMethodCall(element, replacementMethod, *expectedExpressions) to
createAssertThat(element, actualExpression)
}
val newMethodCall = factory.createExpressionFromText(
"org.assertj.core.api.Assertions.assertThat(a)", element
) as PsiMethodCallExpression
newMethodCall.argumentList.expressions[0].replace(actualExpression)
if (messageExpression != null) {
val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression
asExpression.argumentList.expressions[0].replace(messageExpression)
asExpression.methodExpression.qualifierExpression!!.replace(newMethodCall)
expectedMethodCall.methodExpression.qualifierExpression!!.replace(asExpression)
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)
asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.replaceQualifier(asExpression)
} else {
expectedMethodCall.methodExpression.qualifierExpression!!.replace(newMethodCall)
expectedMethodCall.replaceQualifier(newMethodCall)
}
val assertThatMethod = newMethodCall.resolveMethod() ?: return
addStaticImport(assertThatMethod, element, factory, GUAVA_ASSERTIONS_CLASSNAME)
val codeStyleManager = JavaCodeStyleManager.getInstance(element.project)
val newElement = element.replace(expectedMethodCall)
val shortened = codeStyleManager.shortenClassReferences(newElement)
CodeStyleManager.getInstance(element.project).reformat(shortened)
newMethodCall.resolveMethod()?.addAsStaticImport(element, GUAVA_ASSERTIONS_CLASSNAME)
element.replace(expectedMethodCall).shortenAndReformat()
}
}
@@ -0,0 +1,41 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class ReplaceJUnitAssumeMethodCallQuickFix(description: String, private val replacementMethod: String) :
AbstractCommonQuickFix(description) {
companion object {
private const val CONVERT_DESCRIPTION = "Convert JUnit assumptions to assertJ"
}
override fun getFamilyName(): String {
return CONVERT_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList
val count = args.expressions.size
val actualExpression = args.expressions[count - 1] ?: return
val messageExpression = args.expressions.getOrNull(count - 2)
val expectedMethodCall = createExpectedMethodCall(element, replacementMethod)
val newMethodCall = createAssumeThat(element, actualExpression)
if (messageExpression != null) {
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)
asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.replaceQualifier(asExpression)
} else {
expectedMethodCall.replaceQualifier(newMethodCall)
}
newMethodCall.resolveMethod()?.addAsStaticImport(element)
element.replace(expectedMethodCall).shortenAndReformat()
}
}
@@ -2,59 +2,52 @@ package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import de.platon42.intellij.plugins.cajon.*
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.GUAVA_ASSERTIONS_CLASSNAME
class ReplaceJUnitDeltaAssertMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
companion object {
private const val CONVERT_DESCRIPTION = "Convert JUnit assertions with delta to assertJ"
}
override fun getFamilyName(): String {
return CONVERT_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val args = methodCallExpression.argumentList
val count = args.expressionCount
val actualExpression = args.expressions[count - 2] ?: return
val messageExpression = if (count > 3) args.expressions[0] else null
val count = args.expressions.size
val messageExpression = args.expressions.getOrNull(count - 4)
val expectedExpression = args.expressions[count - 3] ?: return
val actualExpression = args.expressions[count - 2] ?: return
val deltaExpression = args.expressions[count - 1] ?: return
val offsetMethodCall = factory.createExpressionFromText(
"org.assertj.core.data.Offset.offset(c)", element
) as PsiMethodCallExpression
val offsetMethodCall = createMethodCall(element, "org.assertj.core.data.Offset.offset", deltaExpression)
offsetMethodCall.argumentList.expressions[0].replace(deltaExpression)
val expectedMethodCall = factory.createExpressionFromText(
"a.${replacementMethod.removeSuffix("()")}(e, offs)", element
) as PsiMethodCallExpression
expectedMethodCall.argumentList.expressions[0].replace(expectedExpression)
expectedMethodCall.argumentList.expressions[1].replace(offsetMethodCall)
val newMethodCall = factory.createExpressionFromText(
"org.assertj.core.api.Assertions.assertThat(a)", element
) as PsiMethodCallExpression
newMethodCall.argumentList.expressions[0].replace(actualExpression)
val swapActualAndExpected = ((expectedExpression.calculateConstantValue() == null)
&& (actualExpression.calculateConstantValue() != null))
val (expectedMethodCall, newMethodCall) = if (swapActualAndExpected) {
createExpectedMethodCall(element, replacementMethod, actualExpression, offsetMethodCall) to
createAssertThat(element, expectedExpression)
} else {
createExpectedMethodCall(element, replacementMethod, expectedExpression, offsetMethodCall) to
createAssertThat(element, actualExpression)
}
if (messageExpression != null) {
val asExpression = factory.createExpressionFromText("a.as(desc)", element) as PsiMethodCallExpression
asExpression.argumentList.expressions[0].replace(messageExpression)
asExpression.methodExpression.qualifierExpression!!.replace(newMethodCall)
expectedMethodCall.methodExpression.qualifierExpression!!.replace(asExpression)
val asExpression = createExpectedMethodCall(element, MethodNames.AS, messageExpression)
asExpression.replaceQualifier(newMethodCall)
expectedMethodCall.replaceQualifier(asExpression)
} else {
expectedMethodCall.methodExpression.qualifierExpression!!.replace(newMethodCall)
expectedMethodCall.replaceQualifier(newMethodCall)
}
val assertThatMethod = newMethodCall.resolveMethod() ?: return
addStaticImport(assertThatMethod, element, factory, GUAVA_ASSERTIONS_CLASSNAME)
val offsetMethod = offsetMethodCall.resolveMethod() ?: return
addStaticImport(offsetMethod, element, factory)
val codeStyleManager = JavaCodeStyleManager.getInstance(element.project)
val newElement = element.replace(expectedMethodCall)
val shortened = codeStyleManager.shortenClassReferences(newElement)
CodeStyleManager.getInstance(element.project).reformat(shortened)
newMethodCall.resolveMethod()?.addAsStaticImport(element, GUAVA_ASSERTIONS_CLASSNAME)
offsetMethodCall.resolveMethod()?.addAsStaticImport(element)
element.replace(expectedMethodCall).shortenAndReformat()
}
}
@@ -2,19 +2,25 @@ package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class ReplaceSimpleMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
companion object {
private const val REPLACE_DESCRIPTION = "Replace methods by better ones"
}
override fun getFamilyName(): String {
return REPLACE_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val oldQualifier = methodCallExpression.methodExpression.qualifierExpression ?: return
val expectedExpression =
factory.createExpressionFromText("a.$replacementMethod", element) as PsiMethodCallExpression
expectedExpression.methodExpression.qualifierExpression!!.replace(oldQualifier)
val expectedExpression = createExpectedMethodCall(element, replacementMethod)
expectedExpression.replaceQualifierFromMethodCall(methodCallExpression)
element.replace(expectedExpression)
}
}
@@ -2,41 +2,48 @@ package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiReferenceExpression
import de.platon42.intellij.plugins.cajon.*
class ReplaceSizeMethodCallQuickFix(
description: String,
private val replacementMethod: String,
private val noExpectedExpression: Boolean,
private val expectedIsCollection: Boolean
private val noExpectedExpression: Boolean = false,
private val expectedIsCollection: Boolean = false
) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val factory = JavaPsiFacade.getElementFactory(element.project)
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val assertExpression = methodCallExpression.argumentList.expressions[0] ?: return
replaceCollectionSizeOrArrayLength(assertExpression)
val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
val expectedExpression =
factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
if (!noExpectedExpression) {
if (expectedIsCollection) {
replaceCollectionSizeOrArrayLength(oldExpectedExpression.argumentList.expressions[0])
}
expectedExpression.argumentList.expressions[0].replace(oldExpectedExpression.argumentList.expressions[0])
}
expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!)
oldExpectedExpression.replace(expectedExpression)
companion object {
private const val REPLACE_DESCRIPTION = "Replace methods by better ones"
}
private fun replaceCollectionSizeOrArrayLength(assertExpression: PsiExpression) {
override fun getFamilyName(): String {
return REPLACE_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val assertExpression = assertThatMethodCall.firstArg
replaceCollectionAndMapSizeOrArrayLength(assertExpression)
if (expectedIsCollection) {
replaceCollectionAndMapSizeOrArrayLength(outmostCallExpression.firstArg)
}
val args = if (noExpectedExpression) emptyArray() else arrayOf(outmostCallExpression.firstArg)
val expectedExpression = createExpectedMethodCall(outmostCallExpression, replacementMethod, *args)
expectedExpression.replaceQualifierFromMethodCall(outmostCallExpression)
outmostCallExpression.replace(expectedExpression)
}
private fun replaceCollectionAndMapSizeOrArrayLength(assertExpression: PsiExpression) {
assertExpression.replace(
when (assertExpression) {
is PsiReferenceExpression -> assertExpression.qualifierExpression!!
is PsiMethodCallExpression -> assertExpression.methodExpression.qualifierExpression!!
is PsiMethodCallExpression -> assertExpression.qualifierExpression
else -> return
}
)
@@ -0,0 +1,45 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiBinaryExpression
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.*
class SplitBinaryExpressionMethodCallQuickFix(
description: String,
private val replacementMethod: String,
private val pickRightOperand: Boolean = false,
private val noExpectedExpression: Boolean = false
) : AbstractCommonQuickFix(description) {
companion object {
private const val SPLIT_EXPRESSION_DESCRIPTION = "Split binary expressions out of assertThat()"
}
override fun getFamilyName(): String {
return SPLIT_EXPRESSION_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val outmostCallExpression = descriptor.startElement as? PsiMethodCallExpression ?: return
val assertThatMethodCall = outmostCallExpression.findStaticMethodCall() ?: return
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
.filter { it.getExpectedBooleanResult() != null }
.toList()
val binaryExpression = assertThatMethodCall.firstArg as? PsiBinaryExpression ?: return
val expectedArgument = (if (pickRightOperand) binaryExpression.lOperand else binaryExpression.rOperand)?.copy() ?: return
binaryExpression.replace(if (pickRightOperand) binaryExpression.rOperand!! else binaryExpression.lOperand)
val args = if (noExpectedExpression) emptyArray() else arrayOf(expectedArgument)
methodsToFix
.forEach {
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *args)
expectedExpression.replaceQualifierFromMethodCall(it)
it.replace(expectedExpression)
}
}
}
@@ -0,0 +1,34 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import de.platon42.intellij.plugins.cajon.*
class SwapActualAndExpectedExpressionMethodCallQuickFix(
description: String,
private val replacementMethod: String
) : AbstractCommonQuickFix(description) {
companion object {
private const val SPLIT_EXPRESSION_DESCRIPTION = "Swap actual and expected expressions of assertions"
}
override fun getFamilyName(): String {
return SPLIT_EXPRESSION_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val assertThatMethodCall = descriptor.startElement.findStaticMethodCall() ?: return
val methodToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
.filterNot(NOT_ACTUAL_ASSERTIONS::test)
.first()
val oldActualExpression = assertThatMethodCall.firstArg.copy()!!
assertThatMethodCall.firstArg.replace(methodToFix.firstArg)
val expectedExpression = createExpectedMethodCall(methodToFix, replacementMethod, oldActualExpression)
expectedExpression.replaceQualifierFromMethodCall(methodToFix)
methodToFix.replace(expectedExpression)
}
}
@@ -0,0 +1,29 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class UnwrapExpectedStaticMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
companion object {
private const val REMOVE_EXPECTED_OUTMOST_DESCRIPTION = "Unwrap expected expressions and use better assertion"
}
override fun getFamilyName(): String {
return REMOVE_EXPECTED_OUTMOST_DESCRIPTION
}
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val oldExpectedExpression = element.findOutmostMethodCall() ?: return
val expectedMethodCallExpression = oldExpectedExpression.firstArg as? PsiMethodCallExpression ?: return
val expectedExpression = createExpectedMethodCall(element, replacementMethod, expectedMethodCallExpression.firstArg)
expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression)
}
}
@@ -0,0 +1,148 @@
package de.platon42.intellij.plugins.cajon.references
import com.intellij.openapi.util.TextRange
import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.*
import com.intellij.psi.util.PropertyUtilBase
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiTypesUtil
import com.intellij.util.ArrayUtil
import com.intellij.util.ProcessingContext
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.*
class ExtractorReferenceContributor : PsiReferenceContributor() {
companion object {
private val BY_NAME = CallMatcher.staticCall(AssertJClassNames.EXTRACTORS_CLASSNAME, "byName")
private val RESULT_OF = CallMatcher.staticCall(AssertJClassNames.EXTRACTORS_CLASSNAME, "resultOf")
.parameterTypes(CommonClassNames.JAVA_LANG_STRING)!!
private val propertyOrFieldReferenceProvider = PropertyOrFieldReferenceProvider()
private val iterablePropertyOrFieldReferenceProvider = IterablePropertyOrFieldReferenceProvider()
private val iterableResultOfReferenceProvider = IterableResultOfReferenceProvider()
private fun lookupFieldOrProperty(containingClass: PsiClass, path: String, startOffset: Int): List<Pair<TextRange, List<PsiElement>>> {
val partName = path.substring(startOffset).substringBefore(".")
val nextOffset = startOffset + partName.length + 1
val matchedGetter = PropertyUtilBase.findPropertyGetter(containingClass, partName, false, true)
val fieldResult = PropertyUtilBase.findPropertyField(containingClass, partName, false)
val textRange = TextRange(startOffset + 1, nextOffset)
val matchedBareMethod = containingClass.allMethods.find { (it.name == partName) && !it.hasModifierProperty(PsiModifier.STATIC) }
val targets = listOfNotNull<PsiElement>(fieldResult, matchedGetter, matchedBareMethod)
if (targets.isNotEmpty()) {
val results = listOf(textRange to targets)
if (nextOffset >= path.length) {
return results
}
val nextClass = PsiTypesUtil.getPsiClass(matchedGetter?.returnType ?: fieldResult?.type) ?: return results
return listOf(results, lookupFieldOrProperty(nextClass, path, nextOffset)).flatten()
}
return emptyList()
}
private fun lookupMethod(containingClass: PsiClass, methodName: String): List<Pair<TextRange, List<PsiElement>>>? {
val matchedMethod = containingClass.allMethods.find { (it.name == methodName) && !it.hasModifierProperty(PsiModifier.STATIC) } ?: return null
val textRange = TextRange(1, methodName.length + 1)
return listOf(textRange to listOf(matchedMethod))
}
private fun findActualType(element: PsiElement): PsiClassType? {
val assertThatCall = element.findStaticMethodCall()
return assertThatCall?.firstArg?.type as? PsiClassType
}
private fun findAndCreateReferences(element: PsiElement, finder: (PsiLiteralExpression) -> List<Pair<TextRange, List<PsiElement>>>?): Array<PsiReference> {
val literal = element as PsiLiteralExpression
val results = finder(literal) ?: return PsiReference.EMPTY_ARRAY
return results.map { ExtractorReference(literal, it.first, it.second) }.toTypedArray()
}
}
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression::class.java), propertyOrFieldReferenceProvider)
registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression::class.java), iterablePropertyOrFieldReferenceProvider)
registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression::class.java), iterableResultOfReferenceProvider)
}
class ExtractorReference(literal: PsiLiteralExpression, range: TextRange, private val targets: List<PsiElement>) :
PsiPolyVariantReferenceBase<PsiLiteralExpression>(literal, range, true) {
// Do not remove due to compatiblity issue with IDEA <= 2018.2
override fun getVariants(): Array<Any> {
return ArrayUtil.EMPTY_OBJECT_ARRAY
}
override fun resolve(): PsiElement? {
return multiResolve(false).map(ResolveResult::getElement).firstOrNull()
}
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
return PsiElementResolveResult.createResults(targets)
}
}
class PropertyOrFieldReferenceProvider : PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> = findAndCreateReferences(element, ::findReferences)
fun findReferences(element: PsiLiteralExpression): List<Pair<TextRange, List<PsiElement>>>? {
val literal = element.value as? String ?: return null
var methodCallExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression::class.java) ?: return null
var isResultOf = false
if (BY_NAME.test(methodCallExpression)) {
methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression::class.java) ?: return null
} else if (RESULT_OF.test(methodCallExpression)) {
methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression::class.java) ?: return null
isResultOf = true
}
if (!EXTRACTING_FROM_OBJECT.test(methodCallExpression)) {
return emptyList()
}
val containingClass = PsiTypesUtil.getPsiClass(findActualType(methodCallExpression)) ?: return null
return if (isResultOf) lookupMethod(containingClass, literal) else lookupFieldOrProperty(containingClass, literal, 0)
}
}
class IterablePropertyOrFieldReferenceProvider : PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> = findAndCreateReferences(element, ::findReferences)
fun findReferences(element: PsiLiteralExpression): List<Pair<TextRange, List<PsiElement>>>? {
val literal = element.value as? String ?: return null
var methodCallExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression::class.java) ?: return null
var isResultOf = false
if (BY_NAME.test(methodCallExpression)) {
methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression::class.java) ?: return null
} else if (RESULT_OF.test(methodCallExpression)) {
methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression::class.java) ?: return null
isResultOf = true
}
if (!CallMatcher.anyOf(EXTRACTING_FROM_ITERABLE, FLAT_EXTRACTING_FROM_ITERABLE).test(methodCallExpression)) return null
val iterableType = findActualType(methodCallExpression) ?: return null
val innerType = iterableType.resolveGenerics().substitutor.substitute(iterableType.parameters[0])
val containingClass = PsiTypesUtil.getPsiClass(innerType) ?: return null
return if (isResultOf) lookupMethod(containingClass, literal) else lookupFieldOrProperty(containingClass, literal, 0)
}
}
class IterableResultOfReferenceProvider : PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> = findAndCreateReferences(element, ::findReferences)
fun findReferences(element: PsiLiteralExpression): List<Pair<TextRange, List<PsiElement>>>? {
val literal = element.value as? String ?: return null
val methodCallExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression::class.java) ?: return null
if (!EXTRACTING_RESULT_OF_FROM_ITERABLE.test(methodCallExpression)) return null
val iterableType = findActualType(methodCallExpression) ?: return null
val innerType = iterableType.resolveGenerics().substitutor.substitute(iterableType.parameters[0])
val containingClass = PsiTypesUtil.getPsiClass(innerType) ?: return null
return lookupMethod(containingClass, literal)
}
}
}
+56 -20
View File
@@ -1,43 +1,79 @@
<idea-plugin>
<id>de.platon42.cajon</id>
<name>Concise AssertJ Optimizing Nitpicker (Cajon)</name>
<vendor email="chrisly@platon42.de" url="https://github.com/chrisly42/cajon-plugin">Platon42</vendor>
<vendor email="chrisly@platon42.de" url="https://github.com/chrisly42/cajon-plugin">Chris 'platon42' Hodges</vendor>
<description><![CDATA[
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions.
It adds inspections and quick fixes to fully make use of the AssertJ methods
to make the intention clear and concise. It can also convert JUnit 4 assertions to AssertJ.
It adds several <b>inspections and quick fixes</b> to fully use the fluent assertion methods
and thus makes the intention clear and concise, also generating better messages on test failures.
It can also be used to <b>convert JUnit 4 assertions and assumptions to AssertJ</b>.
It supports <b>referencing inside extracting</b>()-methods with strings, adding refactoring safety.
<b>Bogus or twisted assertions</b> are also reported.
<p>
<a href="https://github.com/chrisly42/cajon-plugin/blob/master/README.md">Full documentation here...</a>
]]></description>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="182.0"/>
<idea-version since-build="173.2696.26"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.java</depends>
<extensions defaultExtensionNs="com.intellij">
<localInspection groupPath="Java" shortName="AssertThatObjectIsNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatObjectIsNotNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNotNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatBooleanIsTrueOrFalse" enabledByDefault="true"
level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/>
<psi.referenceContributor implementation="de.platon42.intellij.plugins.cajon.references.ExtractorReferenceContributor"/>
<localInspection groupPath="Java" shortName="AssertThatObjectIsNullOrNotNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullOrNotNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatBooleanCondition" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanConditionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatInvertedBooleanCondition" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatInvertedBooleanConditionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatInstanceOf" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatInstanceOfInspection"/>
<localInspection groupPath="Java" shortName="AssertThatStringIsEmpty" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringIsEmptyInspection"/>
<localInspection groupPath="Java" shortName="AssertThatEnumerableIsEmpty" enabledByDefault="true"
level="WARNING"
<localInspection groupPath="Java" shortName="AssertThatEnumerableIsEmpty" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatEnumerableIsEmptyInspection"/>
<localInspection groupPath="Java" shortName="AssertThatSize" enabledByDefault="true"
level="WARNING"
<localInspection groupPath="Java" shortName="AssertThatSize" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatSizeInspection"/>
<localInspection groupPath="Java" shortName="JUnitAssertToAssertJ" enabledByDefault="true"
level="WARNING"
<localInspection groupPath="Java" shortName="AssertThatBinaryExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatObjectExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatComparable" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatComparableInspection"/>
<localInspection groupPath="Java" shortName="AssertThatStringExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatCollectionOrMapExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatCollectionOrMapExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatFileExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatFileExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatPathExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatPathExpressionInspection"/>
<localInspection groupPath="Java" shortName="JoinAssertThatStatements" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JoinAssertThatStatementsInspection"/>
<localInspection groupPath="Java" shortName="JoinVarArgsContains" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JoinVarArgsContainsInspection"/>
<localInspection groupPath="Java" shortName="AssumeThatInsteadOfReturn" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssumeThatInsteadOfReturnInspection"/>
<localInspection groupPath="Java" shortName="AssertThatJava8Optional" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/>
<localInspection groupPath="Java" shortName="AssertThatGuavaOptional" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatGuavaOptionalInspection"/>
<localInspection groupPath="Java" shortName="ImplicitAssertion" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.ImplicitAssertionInspection"/>
<localInspection groupPath="Java" shortName="TwistedAssertion" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.TwistedAssertionInspection"/>
<localInspection groupPath="Java" shortName="BogusAssertion" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.BogusAssertionInspection"/>
<localInspection groupPath="Java" shortName="JUnitAssertToAssertJ" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JUnitAssertToAssertJInspection"/>
</extensions>
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Foo" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
width="226.77px" height="226.77px" viewBox="0 0 226.77 226.77" enable-background="new 0 0 226.77 226.77" xml:space="preserve">
<g>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="44.1792" y1="72.1201" x2="184.0214" y2="-8.6178">
<stop offset="0.2408" style="stop-color:#FFFFFF"/>
<stop offset="1" style="stop-color:#000000"/>
</linearGradient>
<path fill="url(#SVGID_1_)" stroke="#231F20" stroke-miterlimit="10" d="M210.1,36.551L122.5,7.75
C18.101,26.951,18.101,26.951,18.101,26.951L94.3,55.75L210.1,36.551"/>
<linearGradient id="SVGID_2_" gradientUnits="userSpaceOnUse" x1="-12.9795" y1="80.7827" x2="127.5331" y2="161.9077">
<stop offset="0.1763" style="stop-color:#FFFFFF"/>
<stop offset="0.2617" style="stop-color:#F9F9F9"/>
<stop offset="0.3726" style="stop-color:#E9E9E9"/>
<stop offset="0.4976" style="stop-color:#CFCECE"/>
<stop offset="0.6329" style="stop-color:#AAA9A9"/>
<stop offset="0.7765" style="stop-color:#7B7979"/>
<stop offset="0.9248" style="stop-color:#433F40"/>
<stop offset="1" style="stop-color:#231F20"/>
</linearGradient>
<polygon fill="url(#SVGID_2_)" stroke="#231F20" stroke-miterlimit="10" points="17.5,185.35 94.6,218.95 94.3,55.75
18.101,26.951 "/>
<linearGradient id="SVGID_3_" gradientUnits="userSpaceOnUse" x1="88.6709" y1="122.3887" x2="214.4668" y2="133.3944">
<stop offset="0.1216" style="stop-color:#FFFFFF"/>
<stop offset="1" style="stop-color:#2B2728"/>
<stop offset="1" style="stop-color:#231F20"/>
</linearGradient>
<polygon fill="url(#SVGID_3_)" stroke="#231F20" stroke-miterlimit="10" points="94.5,55.75 94.5,56.35 94.5,55.75 95,219.25
209.5,190.15 209.5,36.65 "/>
</g>
<linearGradient id="SVGID_4_" gradientUnits="userSpaceOnUse" x1="51.6982" y1="79.75" x2="51.6982" y2="79.75">
<stop offset="0.1763" style="stop-color:#FFFFFF"/>
<stop offset="0.2617" style="stop-color:#F9F9F9"/>
<stop offset="0.3726" style="stop-color:#E9E9E9"/>
<stop offset="0.4976" style="stop-color:#CFCECE"/>
<stop offset="0.6329" style="stop-color:#AAA9A9"/>
<stop offset="0.7765" style="stop-color:#7B7979"/>
<stop offset="0.9248" style="stop-color:#433F40"/>
<stop offset="1" style="stop-color:#231F20"/>
</linearGradient>
<path fill="url(#SVGID_4_)" stroke="#231F20" stroke-miterlimit="10" d="M51.699,79.75"/>
<path fill="#231F20" stroke="#231F20" stroke-miterlimit="10" d="M171.307,117.313c0.267,13.237-6.324,27.028-14.723,30.805
s-15.423-3.893-15.69-17.132c-0.267-13.236,6.323-27.029,14.723-30.805C164.015,96.405,171.039,104.076,171.307,117.313z"/>
<polygon fill="#39B54A" stroke="#231F20" stroke-width="0.25" stroke-miterlimit="10" points="175.6,143.05 176.2,99.7 183.55,97
191.95,132.25 193.3,92.95 198.1,91.45 196.3,136.6 188.95,138.85 180.85,103.45 180.7,141.55 "/>
<path fill="#009444" stroke="#231F20" stroke-width="0.25" stroke-miterlimit="10" d="M109.9,113.65c1.8,0,25.35-5.25,25.35-5.25
l1.2,38.7l-6.9,7.5l-11.25,3.15l-5.55-5.25l-1.5-6.75l5.1-1.35l1.95,6.6l3.45,1.05l5.25-1.95l4.5-4.35l-0.6-30.9l-19.2,4.051
L109.9,113.65z"/>
<path fill="#00A79D" stroke="#231F20" stroke-width="0.25" stroke-miterlimit="10" d="M46.15,102.4c0-0.45-2.85-6.45-2.85-6.45
l-6.75-3l-5.7,0.15l-1.916,1.667L26.2,97.15l-2.4,5.1l-0.9,8.55l-0.75,12.15l0.3,12.3l2.85,8.25l8.85,4.95l7.2,0.45l5.55-5.551
l1.8-3.6l-3.15-4.05l-2.85,2.25l-3,3.6l-5.4,1.351l-4.2-1.95l-2.4-5.7l-1.05-5.85l0.6-10.051L28,110.8l0.9-6.75l1.95-3.75l6-0.75
l3.45,1.95l2.25,2.7l1.95,1.5L46.15,102.4z"/>
<polygon fill="#2BB673" stroke="#231F20" stroke-width="0.25" stroke-miterlimit="10" points="52.9,151.9 70.4,101.549
76.1,104.249 85.2,164.05 79.1,160.85 73.1,111.05 71.4,110.149 67,127.15 73.7,129.749 74.5,136.351 65.5,132.7 58.1,154.049 "/>
</svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

@@ -0,0 +1,10 @@
<html>
<body>
Turns a binary expression in the kind of assertThat(actual &lt;operator&gt; expected) with .isTrue()/isFalse()/isEqualTo()/isNotEqualTo()
into assertThat(actual).is&lt;operator&gt;(expected).
<!-- tooltip end -->
There are over 150 combinations that are found with this inspections.
Also works with constant expressions on the expected side.
Swaps actual and expected when actual is a constant expression (correctly transforming the used operator).
</body>
</html>
@@ -2,6 +2,6 @@
<body>
Turns assertThat(condition).isEqualTo(true/false) into assertThat(condition).isTrue()/isFalse().
<!-- tooltip end -->
Also works with constant expressions and Boolean.TRUE/FALSE.
<br>Also works with constant expressions and Boolean.TRUE/FALSE.
</body>
</html>
@@ -0,0 +1,18 @@
<html>
<body>
Turns assertThat(collectionOrMap.someMethod(arg)).isTrue/isFalse() into assertThat(collectionOrMap).someMethod(arg) and
assertThat(map.get(key)).isEqualTo/isNotEqualTo(value) into assertThat(map).containsEntry(key, value).
<!-- tooltip end -->
<br>someMethod() can be isEmpty(), contains(), and containsAll() for collections and
isEmpty(), containsKey(), and containsValue() for maps.
get() may be transformed into containsKey(), doesNotContainKey(), containsEntry() or doesNotContainEntry().
<br>
If you are using degenerated maps in your project that may contain null values (i.e.
map.contains(key) == true AND map.get(key) == null
is valid for some entries in your map), the default behavior of the quickfix
assertThat(map.get(key)).isNull() turning into assertThat(map).doesNotContainKey(key)
is not an equivalent transformation. The settings below can change this behavior to instead transform it into
assertThat(map).containsEntry(key, null) for those cases, create both quickfix choices or simply ignore this
case altogether (if in doubt).
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
Turns assertThat(obj1.compareTo(obj2)) into assertThat(obj1).someMethod(obj2).
</body>
</html>
@@ -0,0 +1,8 @@
<html>
<body>
Operates on assertions on objects of type File. Turns assertThat(file.someMethod(arg)).someAssertion() into assertThat(file).someMethod(arg).
<!-- tooltip end -->
<br>someMethod() can be canRead(), canWrite(), exists(), isAbsolute(), isDirectory(), isFile(),
getName(), getParent(), getParentFile(), list() and listFiles().
</body>
</html>
@@ -0,0 +1,8 @@
<html>
<body>
Looks at expected and actual expression being of Guava Optional type and whether the statement effectively tries to assert the
presence, absence or content and then replaces the statement by isPresent(), isAbsent(), or contains().
<!-- tooltip end -->
<br>Requires AssertJ-Guava to be in classpath.
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Turns assertThat(object instanceof classname).isEqualTo(true/false) into assertThat(object).is(Not)InstanceOf(classname.class).
<!-- tooltip end -->
<br>Also works with constant expressions and Boolean.TRUE/FALSE.
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Turns assertThat(!condition).isEqualTo(true/false) into assertThat(condition).isFalse()/isTrue().
<!-- tooltip end -->
<br>Also works with constant expressions and Boolean.TRUE/FALSE.
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Looks at expected and actual expression being of Java 8 Optional type and whether the statement effectively tries to assert the
presence, absence or content and then replaces the statement by isPresent(), isNotPresent(), contains(), or containsSame().
<!-- tooltip end -->
</body>
</html>
@@ -0,0 +1,9 @@
<html>
<body>
Tries to simplify Object method calls such as toString, hashCode() and equals().
<!-- tooltip end -->
<br>Turns assertThat(object.toString()).isEqualTo(expression) into assertThat(object).hasToString(expression).
<br>Turns assertThat(object.hashCode()).isEqualTo(otherObject.hashCode()) into assertThat(object).hasSameHashCodeAs(otherObject).
<br>Turns assertThat(object.equals(otherObject)).isEqualTo(true) into assertThat(object).isEqualTo(otherObject) (and variations).
</body>
</html>
@@ -1,6 +0,0 @@
<html>
<body>
Turns assertThat(object).isNotEqualTo(null) into assertThat(object).isNotNull().
<!-- tooltip end -->
</body>
</html>
@@ -1,6 +0,0 @@
<html>
<body>
Turns assertThat(object).isEqualTo(null) into assertThat(object).isNull().
<!-- tooltip end -->
</body>
</html>
@@ -0,0 +1,6 @@
<html>
<body>
Turns assertThat(object).is(Not)EqualTo(null) into assertThat(object).is(Not)Null().
<!-- tooltip end -->
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Operates on assertions on objects of type Path. Turns assertThat(file.someMethod(arg)).someAssertion() into assertThat(path).someMethod(arg).
<!-- tooltip end -->
<br>someMethod() can be isAbsolute(), getParent(), startsWith() and endsWith().
</body>
</html>
@@ -1,6 +1,7 @@
<html>
<body>
Makes assertions on sizes of arrays or collections more concise by replacing them with isEmpty(), isNotEmpty(), hasSize(), or hasSameSizeAs().
Makes assertions on sizes of arrays, collections, maps, strings, or CharSequences more concise by replacing them with isEmpty(), isNotEmpty(), hasSize(), or hasSameSizeAs().
<!-- tooltip end -->
<br>Several more conversions are available with AssertJ 3.12.0 or later.
</body>
</html>
@@ -0,0 +1,8 @@
<html>
<body>
Turns assertThat(string.someMethod(arg)).isTrue/isFalse() into assertThat(string).someMethod(arg).
<!-- tooltip end -->
<br>someMethod() can be isEmpty(), equals(), equalsIgnoreCase(), contentEquals(), contains(), startsWith(), endsWith(),
matches(), compareToIgnoreCase(), indexOf(), and trim().
</body>
</html>
@@ -0,0 +1,18 @@
<html>
<body>
Tries to detect bogus uses of return statements in test methods and replaces them by assumeThat() calls.
<!-- tooltip end -->
<br>
Novices will use these to skip test execution by bailing out early on some preconditions not met.
However, this suggests that the test has actually been run and passed instead of showing the test
as being skipped.
<p>Return statements in if statements in main test methods
(must be annotated with JUnit 4 or Jupiter @Test annotations)
will be verified to have at least one assertThat() statement in the code flow.
Method calls within the same class will be examined for assertThat() statements, too.
However, at most 50 statements and down to five recursions will be tolerated before giving up.
</p>
<p>Currently, the quickfix may lose some comments during operation. The other branch of the if statement
will be inlined (blocks with declarations will remain a code block due to variable scope).</p>
</body>
</html>
@@ -0,0 +1,6 @@
<html>
<body>
Finds typical copy and paste errors where the assertion will never fail, such as assertThat(foo).isEqualTo(foo), because actual
and expected expressions are the same.
</body>
</html>
@@ -0,0 +1,8 @@
<html>
<body>
Finds assertion method calls that are already implicitly covered by prior assertions.
<!-- tooltip end -->
This works for isNotNull(), isNotEmpty() and isPresent(). It does not cover all edge cases, such as .isNotNull().isNullOrEmpty()
or comparing against non-null literals and is conservative to not eliminate legitimate assertions.
</body>
</html>
@@ -1,10 +1,12 @@
<html>
<body>
Tries to convert most of the JUnit 4 assertions to AssertJ-Format.
Tries to convert most of the JUnit 4 assertions and assumptions to AssertJ format.
<!-- tooltip end -->
<br>Works for assertTrue(), assertFalse(), assertNull(), assertNotNull(), assertEquals(), assertNotEquals(), assertSame() assertNotSame(), assertArrayEquals().
Copes with variants with message and without, handles special versions for double and float types (including arrays).
<p></p>
Also converts assumeTrue(), assumeFalse(), assumeNotNull(), assumeNoException() to assumeThat().
<p>
Does not support Hamcrest-Matchers. If you need that kind of conversion, you might want to check out the Assertions2AssertJ plugin by Ric Emery.
</p>
</body>
</html>
@@ -0,0 +1,10 @@
<html>
<body>
Joins consecutive assertThat() statements with the same actual expression together.
<!-- tooltip end -->
<br>If the AssertThat()-Statement contains .extracting() methods, they will not be joined.
<br>During joining multiple statements, line breaks may be added to avoid too long lines. The number of statements to join without
adding line breaks can be configured.
<br>Also retains comments during operation and forces a line break for these cases.
</body>
</html>
@@ -0,0 +1,11 @@
<html>
<body>
Finds assertions where multiple .contains(), .containsOnlyOnce() or .doesNotContain() are
used in a single statement that could be joined together.
<!-- tooltip end -->
Only works when variadic arguments are possible and will not be performed on more complex
statements with .extracting() or .as() to avoid changing semantics.
<br>
Note that the quickfix does not handle comments very well and might remove them during the operation.
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Finds assertion method calls that have the expected and actual expressions twisted, such as assertThat(5).isEqualTo(foo).
<!-- tooltip end -->
For some obvious cases, a quickfix to swap the actual and expected expressions is provided.
</body>
</html>
@@ -0,0 +1,24 @@
package de.platon42.intellij.jupiter;
import com.intellij.openapi.roots.DependencyScope;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Repeatable(AddMavenDependencyToModule.List.class)
public @interface AddMavenDependencyToModule {
String value();
boolean includeTransitiveDependencies = false;
DependencyScope scope = DependencyScope.COMPILE;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface List {
AddMavenDependencyToModule[] value();
}
}
@@ -1,32 +1,44 @@
package de.platon42.intellij.jupiter;
import com.intellij.jarRepository.JarRepositoryManager;
import com.intellij.jarRepository.RemoteRepositoryDescription;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.DependencyScope;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.ui.OrderRoot;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.*;
import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy;
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LightCodeInsightExtension implements ParameterResolver, AfterTestExecutionCallback {
public class LightCodeInsightExtension implements ParameterResolver, AfterTestExecutionCallback, InvocationInterceptor {
private static final Logger LOG = Logger.getLogger(LightCodeInsightExtension.class.getName());
@@ -77,7 +89,41 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx
return context.getStore(Namespace.create(LightCodeInsightExtension.class, context.getRequiredTestMethod()));
}
private static class LightCodeInsightFixtureTestCaseWrapper extends LightCodeInsightFixtureTestCase {
@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
Throwable[] throwables = new Throwable[1];
Runnable runnable = () -> {
try {
TestLoggerFactory.onTestStarted();
invocation.proceed();
TestLoggerFactory.onTestFinished(true);
} catch (Throwable e) {
TestLoggerFactory.onTestFinished(false);
throwables[0] = e;
}
};
invokeTestRunnable(runnable);
if (throwables[0] != null) {
throw throwables[0];
}
}
private static void invokeTestRunnable(@NotNull Runnable runnable) {
IdeaTestExecutionPolicy policy = IdeaTestExecutionPolicy.current();
if (policy != null && !policy.runInDispatchThread()) {
runnable.run();
} else {
EdtTestUtilKt.runInEdtAndWait(() -> {
runnable.run();
return null;
});
}
}
private static class LightCodeInsightFixtureTestCaseWrapper extends LightJavaCodeInsightFixtureTestCase {
private final ExtensionContext extensionContext;
private LightCodeInsightFixtureTestCaseWrapper(ExtensionContext extensionContext) {
@@ -116,22 +162,23 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
super.configureModule(module, model, contentEntry);
AddLocalJarToModule methodOrClassAnnotation = getMethodOrClassAnnotation(AddLocalJarToModule.class);
if (methodOrClassAnnotation != null) {
Stream.of(methodOrClassAnnotation.value()).forEach(it -> addJarContaining(model, it));
AddLocalJarToModule localJars = getMethodOrClassAnnotation(AddLocalJarToModule.class);
if (localJars != null) {
Stream.of(localJars.value()).forEach(it -> addJarContaining(model, it));
}
List<AddMavenDependencyToModule> mavenDependencies = getMethodOrClassAnnotations(AddMavenDependencyToModule.class);
mavenDependencies.forEach(it -> addFromMaven(model, it.value(), it.includeTransitiveDependencies, it.scope));
}
};
}
protected void addJarContaining(ModifiableRootModel model, Class clazz) {
void addJarContaining(ModifiableRootModel model, Class clazz) {
try {
Path jarPath = Paths.get(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
VirtualFile jarFile = LocalFileSystem.getInstance().findFileByIoFile(jarPath.toFile());
myFixture.allowTreeAccessForFile(jarFile);
PsiTestUtil.addLibrary(
myFixture.getModule(),
model,
jarPath.getFileName().toString().replace(".jar", ""),
jarPath.getParent().toString(),
@@ -142,6 +189,33 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx
}
}
void addFromMaven(ModifiableRootModel model, String mavenCoordinates,
boolean includeTransitiveDependencies, DependencyScope dependencyScope) {
List<RemoteRepositoryDescription> remoteRepositoryDescriptions = RemoteRepositoryDescription.DEFAULT_REPOSITORIES;
RepositoryLibraryProperties libraryProperties = new RepositoryLibraryProperties(mavenCoordinates, includeTransitiveDependencies);
Collection<OrderRoot> roots =
JarRepositoryManager.loadDependenciesModal(model.getProject(), libraryProperties, false, false, null, remoteRepositoryDescriptions);
LibraryTable.ModifiableModel tableModel = model.getModuleLibraryTable().getModifiableModel();
Library library = tableModel.createLibrary(mavenCoordinates);
Library.ModifiableModel libraryModel = library.getModifiableModel();
if (roots.isEmpty()) {
throw new IllegalStateException(String.format("No roots for '%s'", mavenCoordinates));
}
for (OrderRoot root : roots) {
libraryModel.addRoot(root.getFile(), root.getType());
}
LibraryOrderEntry libraryOrderEntry = model.findLibraryOrderEntry(library);
if (libraryOrderEntry == null) {
throw new IllegalStateException("Unable to find registered library " + mavenCoordinates);
}
libraryOrderEntry.setScope(dependencyScope);
libraryModel.commit();
tableModel.commit();
}
@Override
protected String getTestDataPath() {
TestDataPath testDataPath = getMethodOrClassAnnotation(TestDataPath.class);
@@ -166,5 +240,12 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx
}
return annotation;
}
private <T extends Annotation> List<T> getMethodOrClassAnnotations(Class<T> clazz) {
return Stream.of(extensionContext.getRequiredTestMethod().getAnnotationsByType(clazz),
extensionContext.getRequiredTestClass().getAnnotationsByType(clazz))
.flatMap(Arrays::stream)
.collect(Collectors.toList());
}
}
}
@@ -1,44 +0,0 @@
package de.platon42.intellij.jupiter;
import com.intellij.testFramework.EdtTestUtilKt;
import com.intellij.testFramework.TestLoggerFactory;
import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy;
import org.jetbrains.annotations.NotNull;
// See https://github.com/junit-team/junit5/issues/157
public class WorkaroundUntilJupiterGetsExecutorProvider {
protected static void runTest(Runnable test) throws Throwable {
Throwable[] throwables = new Throwable[1];
Runnable runnable = () -> {
try {
TestLoggerFactory.onTestStarted();
test.run();
TestLoggerFactory.onTestFinished(true);
} catch (Throwable e) {
TestLoggerFactory.onTestFinished(false);
e.fillInStackTrace();
throwables[0] = e;
}
};
invokeTestRunnable(runnable);
if (throwables[0] != null) {
throw throwables[0];
}
}
private static void invokeTestRunnable(@NotNull Runnable runnable) {
IdeaTestExecutionPolicy policy = IdeaTestExecutionPolicy.current();
if (policy != null && !policy.runInDispatchThread()) {
runnable.run();
} else {
EdtTestUtilKt.runInEdtAndWait(() -> {
runnable.run();
return null;
});
}
}
}
@@ -1,188 +0,0 @@
package de.platon42.intellij.playground;
import org.assertj.core.api.ListAssert;
import org.assertj.core.data.Offset;
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Offset.offset;
import static org.junit.Assert.*;
public class Playground {
private void sizeOfList() {
assertThat("string").as("foo").hasSize(0);
assertThat(new StringBuilder()).as("bar").hasSize(0);
ListAssert<String> etc = assertThat(new ArrayList<String>()).as("etc");
etc.hasSize(0);
assertThat(new Long[1]).as("etc").hasSize(0);
assertThat("string").as("foo").isEmpty();
assertThat(new StringBuilder()).as("bar").isEmpty();
assertThat(new ArrayList<Long>()).as("etc").isEmpty();
assertThat(new Long[1]).as("etc").isEmpty();
assertThat(new ArrayList<>().size()).isEqualTo(1);
assertThat(new ArrayList<String>().size()).isEqualTo(1);
assertThat(new ArrayList<String>().size()).isGreaterThanOrEqualTo(1);
assertThat(new ArrayList<String>().size()).isZero();
assertThat(new ArrayList<String>()).hasSameSizeAs(new ArrayList<>());
assertThat(new Long[1]).as("etc").hasSameSizeAs(new Long[2]);
}
private void sizeOfArray() {
assertThat(new String[1].length).isLessThanOrEqualTo(1);
assertThat(new String[1]).hasSameSizeAs(new Object());
assertThat("").isEqualTo(null);
assertThat(true).isTrue();
assertThat(true).isEqualTo(true);
assertThat(Boolean.TRUE).isEqualTo(Boolean.FALSE);
assertThat(Boolean.TRUE).isEqualTo(true);
}
private void booleanIsTrueOrFalse() {
boolean primitive = false;
Boolean object = java.lang.Boolean.TRUE;
assertThat(primitive).isEqualTo(Boolean.TRUE);
assertThat(primitive).isEqualTo(Boolean.FALSE);
assertThat(object).isEqualTo(Boolean.TRUE);
assertThat(object).isEqualTo(Boolean.FALSE);
assertThat(primitive).isEqualTo(true);
assertThat(primitive).isEqualTo(false);
assertThat(object).isEqualTo(true);
assertThat(object).isEqualTo(false);
assertThat(primitive).isNotEqualTo(Boolean.TRUE);
assertThat(primitive).isNotEqualTo(Boolean.FALSE);
assertThat(object).isNotEqualTo(Boolean.TRUE);
assertThat(object).isNotEqualTo(Boolean.FALSE);
assertThat(primitive).isNotEqualTo(true);
assertThat(primitive).isNotEqualTo(false);
assertThat(object).isNotEqualTo(true);
assertThat(object).isNotEqualTo(false);
assertThat(primitive).as("nah").isEqualTo(true && !true);
assertThat(object).isEqualTo(Boolean.TRUE && !Boolean.TRUE);
assertThat("").isEqualTo(Boolean.TRUE);
}
private void stringIsEmpty() {
String foo = "bar";
assertThat(foo).isEqualTo("");
assertThat(foo).hasSize(0);
}
private void junitAssertions() {
assertTrue(true);
assertTrue("message", true);
assertFalse(true);
assertFalse("message", true);
assertEquals(1L, 2L);
assertEquals("message", 1L, 2L);
assertNotEquals(1L, 2L);
assertNotEquals("message", 1L, 2L);
assertEquals(4.0, 4.4, 3.3);
assertThat(3.0).isCloseTo(4.0, Offset.offset(2.3));
assertThat(new int[1]).isEqualTo(new int[2]);
String foo = "foo";
String bar = "bar";
assertTrue(foo == "foo");
assertTrue("oh no!", foo == "foo");
assertFalse(foo == "bar");
assertFalse("boom!", foo == "bar");
assertNull(foo);
assertNull("oh no!", foo);
assertNotNull(foo);
assertNotNull("oh no!", foo);
assertEquals(bar, foo);
assertEquals("equals", bar, foo);
assertNotEquals(bar, foo);
assertNotEquals("equals", bar, foo);
assertSame(bar, foo);
assertSame("same", bar, foo);
assertNotSame(bar, foo);
assertNotSame("same", bar, foo);
assertEquals(1.0, 2.0);
assertEquals(1.0, 2.0, 0.1);
assertEquals("equals", 1.0, 2.0);
assertEquals("equals", 1.0, 2.0, 0.1);
assertEquals(1.0f, 2.0f);
assertEquals(1.0f, 2.0f, 0.1f);
assertEquals("equals", 1.0f, 2.0f);
assertEquals("equals", 1.0f, 2.0f, 0.1f);
assertNotEquals(1.0, 2.0);
assertNotEquals(1.0, 2.0, 0.1);
assertNotEquals("equals", 1.0, 2.0);
assertNotEquals("equals", 1.0, 2.0, 0.1);
assertNotEquals(1.0f, 2.0f);
assertNotEquals(1.0f, 2.0f, 0.1f);
assertNotEquals("equals", 1.0f, 2.0f);
assertNotEquals("equals", 1.0f, 2.0f, 0.1f);
assertArrayEquals(new int[2], new int[1]);
assertArrayEquals("array equals", new int[2], new int[1]);
assertArrayEquals(new double[2], new double[1], 1.0);
assertArrayEquals("array equals", new double[2], new double[1], 1.0);
assertArrayEquals(new float[2], new float[1], 1.0f);
assertArrayEquals("array equals", new float[2], new float[1], 1.0f);
assertThat(foo == "foo").isTrue();
assertThat(foo == "foo").as("oh no!").isTrue();
assertThat(foo == "bar").isFalse();
assertThat(foo == "bar").as("boom!").isFalse();
assertThat(foo).isNull();
assertThat(foo).as("oh no!").isNull();
assertThat(foo).isNotNull();
assertThat(foo).as("oh no!").isNotNull();
assertThat(foo).isEqualTo(bar);
assertThat(foo).as("equals").isEqualTo(bar);
assertThat(foo).isNotEqualTo(bar);
assertThat(foo).as("equals").isNotEqualTo(bar);
assertThat(foo).isSameAs(bar);
assertThat(foo).as("same").isSameAs(bar);
assertThat(foo).isNotSameAs(bar);
assertThat(foo).as("same").isNotSameAs(bar);
assertThat(2.0).isEqualTo(1.0);
assertThat(2.0).isCloseTo(1.0, offset(0.1));
assertThat(2.0).as("equals").isEqualTo(1.0);
assertThat(2.0).as("equals").isCloseTo(1.0, offset(0.1));
assertThat(2.0f).isEqualTo(1.0f);
assertThat(2.0f).isCloseTo(1.0f, offset(0.1f));
assertThat(2.0f).as("equals").isEqualTo(1.0f);
assertThat(2.0f).as("equals").isCloseTo(1.0f, offset(0.1f));
assertThat(2.0).isNotEqualTo(1.0);
assertThat(2.0).isNotCloseTo(1.0, offset(0.1));
assertThat(2.0).as("equals").isNotEqualTo(1.0);
assertThat(2.0).as("equals").isNotCloseTo(1.0, offset(0.1));
assertThat(2.0f).isNotEqualTo(1.0f);
assertThat(2.0f).isNotCloseTo(1.0f, offset(0.1f));
assertThat(2.0f).as("equals").isNotEqualTo(1.0f);
assertThat(2.0f).as("equals").isNotCloseTo(1.0f, offset(0.1f));
assertThat(new int[1]).isEqualTo(new int[2]);
assertThat(new int[1]).as("array equals").isEqualTo(new int[2]);
assertThat(new double[1]).containsExactly(new double[2], offset(1.0));
assertThat(new double[1]).as("array equals").containsExactly(new double[2], offset(1.0));
assertThat(new float[1]).containsExactly(new float[2], offset(1.0f));
assertThat(new float[1]).as("array equals").containsExactly(new float[2], offset(1.0f));
}
}
@@ -1,67 +1,55 @@
package de.platon42.intellij.plugins.cajon
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.pom.java.LanguageLevel
import com.intellij.testFramework.TestLoggerFactory
import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import com.intellij.testFramework.runInEdtAndWait
import de.platon42.intellij.jupiter.AddLocalJarToModule
import de.platon42.intellij.jupiter.AddMavenDependencyToModule
import de.platon42.intellij.jupiter.LightCodeInsightExtension
import de.platon42.intellij.jupiter.TestDataPath
import de.platon42.intellij.jupiter.TestJdk
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayNameGeneration
import org.junit.jupiter.api.DisplayNameGenerator
import org.junit.jupiter.api.extension.ExtendWith
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
@ExtendWith(LightCodeInsightExtension::class)
@TestDataPath("src/test/resources")
@TestJdk(LanguageLevel.JDK_1_8, annotations = true, useInternal = true)
@AddLocalJarToModule(Assertions::class)
//@AddLocalJarToModule(Assertions::class)
@AddMavenDependencyToModule("org.assertj:assertj-core:3.15.0")
@DisplayNameGeneration(AbstractCajonTest.CutOffFixtureDisplayNameGenerator::class)
abstract class AbstractCajonTest {
// See https://github.com/junit-team/junit5/issues/157
protected fun runTest(body: () -> Unit) {
val throwables = arrayOfNulls<Throwable>(1)
invokeTestRunnable {
try {
TestLoggerFactory.onTestStarted()
body()
TestLoggerFactory.onTestFinished(true)
} catch (e: InvocationTargetException) {
TestLoggerFactory.onTestFinished(false)
e.fillInStackTrace()
throwables[0] = e.targetException
} catch (e: IllegalAccessException) {
TestLoggerFactory.onTestFinished(false)
e.fillInStackTrace()
throwables[0] = e
} catch (e: Throwable) {
TestLoggerFactory.onTestFinished(false)
throwables[0] = e
}
}
if (throwables[0] != null) {
throw throwables[0]!!
}
}
private fun invokeTestRunnable(runnable: () -> Unit) {
val policy = IdeaTestExecutionPolicy.current()
if (policy != null && !policy.runInDispatchThread()) {
runnable()
} else {
runInEdtAndWait {
runnable()
}
}
}
protected fun executeQuickFixes(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int) {
val quickfixes = myFixture.getAllQuickFixes().filter { it.familyName.matches(regex) }
assertThat(quickfixes).hasSize(expectedFixes)
val quickfixes = getQuickFixes(myFixture, regex, expectedFixes)
assertThat(quickfixes.groupBy { it.familyName }).hasSize(1)
quickfixes.forEach(myFixture::launchAction)
}
protected fun executeQuickFixesNoFamilyNameCheck(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int) {
val quickfixes = getQuickFixes(myFixture, regex, expectedFixes)
quickfixes.forEach(myFixture::launchAction)
}
protected fun getQuickFixes(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int): List<IntentionAction> {
val quickfixes = myFixture.getAllQuickFixes().filter { it.text.matches(regex) }
assertThat(quickfixes).`as`("Fixes matched by $regex: ${myFixture.getAllQuickFixes().map { it.text }}").hasSize(expectedFixes)
return quickfixes
}
protected fun assertHighlightings(myFixture: JavaCodeInsightTestFixture, count: Int, snippet: String) {
val highlights = myFixture.doHighlighting()
.asSequence()
.filter { it.description?.contains(snippet) ?: false }
.toList()
assertThat(highlights).hasSize(count);
}
class CutOffFixtureDisplayNameGenerator : DisplayNameGenerator.ReplaceUnderscores() {
override fun generateDisplayNameForMethod(testClass: Class<*>?, testMethod: Method?): String {
val nameForMethod = super.generateDisplayNameForMethod(testClass, testMethod)
return nameForMethod.substringBefore("$")
}
}
}
@@ -0,0 +1,19 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatBinaryExpressionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/BinaryExpression")
internal fun assertThat_of_binary_expression_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatBinaryExpressionInspection::class.java)
myFixture.configureByFile("BinaryExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Split binary expression out of assertThat()"), 161)
myFixture.checkResultByFile("BinaryExpressionAfter.java")
}
}
@@ -0,0 +1,22 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatBooleanConditionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/BooleanCondition")
internal fun assertThat_with_isEqualTo_true_or_false_can_use_isTrue_or_isFalse(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatBooleanConditionInspection::class.java)
myFixture.configureByFile("BooleanConditionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isTrue()"), 6)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isFalse()"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isTrue()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isFalse()"), 4)
myFixture.checkResultByFile("BooleanConditionAfter.java")
}
}
@@ -1,21 +0,0 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatBooleanIsTrueOrFalseInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/BooleanIsTrueOrFalse")
internal fun assertThat_with_isEqualTo_true_or_false_can_use_isTrue_or_isFalse(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatBooleanIsTrueOrFalseInspection::class.java)
myFixture.configureByFile("BooleanIsTrueOrFalseBefore.java")
executeQuickFixes(myFixture, Regex("Replace is.*"), 17)
myFixture.checkResultByFile("BooleanIsTrueOrFalseAfter.java")
}
}
}
@@ -0,0 +1,104 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
@TestDataSubPath("inspections/CollectionMapExpression")
internal class AssertThatCollectionOrMapExpressionInspectionTest : AbstractCajonTest() {
@Test
internal fun assertThat_with_certain_Collection_and_Map_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatCollectionOrMapExpressionInspection::class.java)
myFixture.configureByFile("CollectionMapExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isEmpty() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of actual expression and use assertThat().contains() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsAll() of actual expression and use assertThat().containsAll() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsKey() of actual expression and use assertThat().containsKey() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsValue() of actual expression and use assertThat().containsValue() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isNotEmpty() instead"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of actual expression and use assertThat().doesNotContain() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsKey() of actual expression and use assertThat().doesNotContainKey() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsValue() of actual expression and use assertThat().doesNotContainValue() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainEntry() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsKey() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead"), 4)
myFixture.checkResultByFile("CollectionMapExpressionAfter.java")
}
@Test
internal fun assertThat_with_certain_Collection_and_Map_methods_with_Null_values(@MyFixture myFixture: JavaCodeInsightTestFixture) {
val inspection = AssertThatCollectionOrMapExpressionInspection()
inspection.behaviorForMapValueEqualsNull = 3
myFixture.enableInspections(inspection)
myFixture.configureByFile("CollectionMapExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isEmpty() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of actual expression and use assertThat().contains() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsAll() of actual expression and use assertThat().containsAll() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsKey() of actual expression and use assertThat().containsKey() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsValue() of actual expression and use assertThat().containsValue() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of actual expression and use assertThat().isNotEmpty() instead"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of actual expression and use assertThat().doesNotContain() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsKey() of actual expression and use assertThat().doesNotContainKey() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove containsValue() of actual expression and use assertThat().doesNotContainValue() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry() instead"), 6)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainEntry() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsKey() instead"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead"), 0)
myFixture.checkResultByFile("CollectionMapExpressionWithNullValuesAfter.java")
}
@Test
internal fun assertThat_with_certain_Collection_and_Map_methods_with_no_quickfixes_for_get_equals_null(@MyFixture myFixture: JavaCodeInsightTestFixture) {
val inspection = AssertThatCollectionOrMapExpressionInspection()
inspection.behaviorForMapValueEqualsNull = 0
myFixture.enableInspections(inspection)
myFixture.configureByFile("CollectionMapExpressionBefore.java")
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsKey() instead"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead"), 0)
}
@Test
internal fun assertThat_with_certain_Collection_and_Map_methods_with_only_warnings_for_get_equals_null(@MyFixture myFixture: JavaCodeInsightTestFixture) {
val inspection = AssertThatCollectionOrMapExpressionInspection()
inspection.behaviorForMapValueEqualsNull = 1
myFixture.enableInspections(inspection)
myFixture.configureByFile("CollectionMapExpressionBefore.java")
val highlights = myFixture.doHighlighting()
.asSequence()
.filter { it.description == "Moving get() expression out of assertThat() would be more concise" }
.filter {
it.quickFixActionRanges?.any { innerit -> innerit.first.action.text.contains("Inspection 'Asserting a collection or map specific expression") } ?: true
}
.toList()
assertThat(highlights).hasSize(4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsKey() instead"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead"), 0)
}
@Test
internal fun assertThat_with_certain_Collection_and_Map_methods_with_both_quickfixes_for_get_equals_null(@MyFixture myFixture: JavaCodeInsightTestFixture) {
val inspection = AssertThatCollectionOrMapExpressionInspection()
inspection.behaviorForMapValueEqualsNull = 4
myFixture.enableInspections(inspection)
myFixture.configureByFile("CollectionMapExpressionBefore.java")
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead (regular map)"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsEntry(key, null) instead (degenerated map)"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainEntry() instead"), 2)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().containsKey() instead"), 4)
getQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().doesNotContainKey() instead"), 0)
}
}
@@ -0,0 +1,24 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatComparableInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/Comparable")
internal fun assertThat_with_compareTo_method(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatComparableInspection::class.java)
myFixture.configureByFile("ComparableBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isEqualByComparingTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isNotEqualByComparingTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isGreaterThanOrEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isGreaterThan() instead"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isLessThanOrEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove compareTo() of actual expression and use assertThat().isLessThan() instead"), 4)
myFixture.checkResultByFile("ComparableAfter.java")
}
}
@@ -11,11 +11,9 @@ internal class AssertThatEnumerableIsEmptyInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/EnumerableIsEmpty")
internal fun assertThat_with_hasSize_zero_can_use_isEmpty(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatEnumerableIsEmptyInspection::class.java)
myFixture.configureByFile("EnumerableIsEmptyBefore.java")
executeQuickFixes(myFixture, Regex("Replace hasSize.*"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 5)
myFixture.checkResultByFile("EnumerableIsEmptyAfter.java")
}
}
}
@@ -0,0 +1,38 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatFileExpressionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/FileExpression")
internal fun assertThat_with_certain_File_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatFileExpressionInspection::class.java)
myFixture.configureByFile("FileExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove canRead() of actual expression and use assertThat().canRead() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove canWrite() of actual expression and use assertThat().canWrite() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove exists() of actual expression and use assertThat().exists() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove exists() of actual expression and use assertThat().doesNotExist() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isAbsolute() of actual expression and use assertThat().isAbsolute() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isAbsolute() of actual expression and use assertThat().isRelative() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isDirectory() of actual expression and use assertThat().isDirectory() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isFile() of actual expression and use assertThat().isFile() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getName() of actual expression and use assertThat().hasName() instead"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParent() of actual expression and use assertThat().hasNoParent() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParentFile() of actual expression and use assertThat().hasNoParent() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParent() of actual expression and use assertThat().hasParent() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParentFile() of actual expression and use assertThat().hasParent() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove listFiles() of actual expression and use assertThat().isEmptyDirectory() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove listFiles() of actual expression and use assertThat().isNotEmptyDirectory() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove list() of actual expression and use assertThat().isEmptyDirectory() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove list() of actual expression and use assertThat().isNotEmptyDirectory() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove length() of actual expression and use assertThat().isEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove length() of actual expression and use assertThat().isNotEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove length() of actual expression and use assertThat().hasSize() instead"), 1)
myFixture.checkResultByFile("FileExpressionAfter.java")
}
}
@@ -0,0 +1,48 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.AddLocalJarToModule
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
@AddLocalJarToModule(com.google.common.base.Optional::class, org.assertj.guava.api.Assertions::class, Assertions::class)
@TestDataSubPath("inspections/GuavaOptional")
internal class AssertThatGuavaOptionalInspectionTest : AbstractCajonTest() {
@Test
internal fun assertThat_get_or_isPresent_for_Guava_Optional_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatGuavaOptionalInspection::class.java)
myFixture.configureByFile("GuavaOptionalBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isPresent() of actual expression and use assertThat().isPresent() instead"), 6)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isPresent() of actual expression and use assertThat().isAbsent() instead"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove orNull() of actual expression and use assertThat().isPresent() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove orNull() of actual expression and use assertThat().isAbsent() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove get() of actual expression and use assertThat().contains() instead"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap expected expression and replace isEqualTo() with contains()"), 6)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with Guava assertThat().isAbsent()"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with Guava assertThat().isPresent()"), 3)
myFixture.checkResultByFile("GuavaOptionalAfter.java")
}
@Test
internal fun adds_missing_Guava_import_any_order(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatGuavaOptionalInspection::class.java)
myFixture.configureByFile("WithoutPriorGuavaImportBefore.java")
executeQuickFixesNoFamilyNameCheck(myFixture, Regex(".*eplace .* with .*"), 4)
executeQuickFixesNoFamilyNameCheck(myFixture, Regex("Remove .*"), 3)
myFixture.checkResultByFile("WithoutPriorGuavaImportAfter.java")
}
@Test
internal fun adds_missing_Guava_import_isAbsent_first(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatGuavaOptionalInspection::class.java)
myFixture.configureByFile("WithoutPriorGuavaImportBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with Guava assertThat().isAbsent()"), 1)
executeQuickFixesNoFamilyNameCheck(myFixture, Regex(".*eplace .* with .*"), 3)
executeQuickFixesNoFamilyNameCheck(myFixture, Regex("Remove .*"), 3)
myFixture.checkResultByFile("WithoutPriorGuavaImportAfter.java")
}
}
@@ -0,0 +1,20 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatInstanceOfInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/InstanceOf")
internal fun assertThat_with_instanceof_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatInstanceOfInspection::class.java)
myFixture.configureByFile("InstanceOfBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove instanceof in actual expression and use assertThat().isInstanceOf() instead"), 6)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove instanceof in actual expression and use assertThat().isNotInstanceOf() instead"), 6)
myFixture.checkResultByFile("InstanceOfAfter.java")
}
}
@@ -0,0 +1,19 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatInvertedBooleanConditionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/InvertedBooleanCondition")
internal fun assertThat_with_inverted_boolean_condition_can_be_inverted(@MyFixture myFixture: JavaCodeInsightTestFixture) {
myFixture.enableInspections(AssertThatInvertedBooleanConditionInspection::class.java)
myFixture.configureByFile("InvertedBooleanConditionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Invert condition in assertThat()"), 25)
myFixture.checkResultByFile("InvertedBooleanConditionAfter.java")
}
}

Some files were not shown because too many files have changed in this diff Show More