Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Intellij Community
Commits
a90c9f96
Commit
a90c9f96
authored
8 years ago
by
Anna.Kozlova
Browse files
Options
Download
Email Patches
Plain Diff
constant assertion inspection for testng
parent
dcd5dd41
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/ConstantAssertArgumentInspectionBase.java
+77
-0
.../siyeh/ig/junit/ConstantAssertArgumentInspectionBase.java
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/ConstantJUnitAssertArgumentInspection.java
+4
-62
...siyeh/ig/junit/ConstantJUnitAssertArgumentInspection.java
plugins/testng/src/META-INF/plugin.xml
+2
-0
plugins/testng/src/META-INF/plugin.xml
plugins/testng/src/com/theoryinpractice/testng/inspection/ConstantTestNGAssertArgumentInspection.java
+34
-0
...ng/inspection/ConstantTestNGAssertArgumentInspection.java
plugins/testng/src/inspectionDescriptions/ConstantTestNGAssertArgument.html
+9
-0
.../inspectionDescriptions/ConstantTestNGAssertArgument.html
with
126 additions
and
62 deletions
+126
-62
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/ConstantAssertArgumentInspectionBase.java
0 → 100644
+
77
-
0
View file @
a90c9f96
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com.siyeh.ig.junit
;
import
com.intellij.psi.PsiExpression
;
import
com.intellij.psi.PsiExpressionList
;
import
com.intellij.psi.PsiMethodCallExpression
;
import
com.intellij.psi.util.PsiUtil
;
import
com.siyeh.InspectionGadgetsBundle
;
import
com.siyeh.ig.BaseInspection
;
import
com.siyeh.ig.BaseInspectionVisitor
;
import
org.jetbrains.annotations.NonNls
;
import
org.jetbrains.annotations.NotNull
;
import
java.util.HashSet
;
import
java.util.Set
;
public
abstract
class
ConstantAssertArgumentInspectionBase
extends
BaseInspection
{
@NonNls
private
static
final
Set
<
String
>
ASSERT_METHODS
=
new
HashSet
<>();
static
{
ASSERT_METHODS
.
add
(
"assertTrue"
);
ASSERT_METHODS
.
add
(
"assertFalse"
);
ASSERT_METHODS
.
add
(
"assertNull"
);
ASSERT_METHODS
.
add
(
"assertNotNull"
);
}
@Override
@NotNull
protected
String
buildErrorString
(
Object
...
infos
)
{
return
InspectionGadgetsBundle
.
message
(
"constant.junit.assert.argument.problem.descriptor"
);
}
protected
abstract
boolean
checkTestNG
();
@Override
public
BaseInspectionVisitor
buildVisitor
()
{
return
new
ConstantAssertArgumentVisitor
();
}
private
class
ConstantAssertArgumentVisitor
extends
BaseInspectionVisitor
{
@Override
public
void
visitMethodCallExpression
(
PsiMethodCallExpression
expression
)
{
AssertHint
assertHint
=
AssertHint
.
create
(
expression
,
methodName
->
ASSERT_METHODS
.
contains
(
methodName
)
?
1
:
null
,
checkTestNG
());
if
(
assertHint
==
null
)
{
return
;
}
final
PsiExpressionList
argumentList
=
expression
.
getArgumentList
();
final
PsiExpression
[]
arguments
=
argumentList
.
getExpressions
();
if
(
arguments
.
length
==
0
)
{
return
;
}
final
PsiExpression
argument
=
arguments
[
assertHint
.
getArgIndex
()];
if
(!
PsiUtil
.
isConstantExpression
(
argument
))
{
return
;
}
registerError
(
argument
);
}
}
}
This diff is collapsed.
Click to expand it.
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/ConstantJUnitAssertArgumentInspection.java
+
4
-
62
View file @
a90c9f96
...
...
@@ -15,79 +15,21 @@
*/
package
com.siyeh.ig.junit
;
import
com.intellij.psi.*
;
import
com.intellij.psi.util.PsiUtil
;
import
com.siyeh.InspectionGadgetsBundle
;
import
com.siyeh.ig.BaseInspection
;
import
com.siyeh.ig.BaseInspectionVisitor
;
import
org.jetbrains.annotations.Nls
;
import
org.jetbrains.annotations.NonNls
;
import
org.jetbrains.annotations.NotNull
;
import
java.util.HashSet
;
import
java.util.Set
;
public
class
ConstantJUnitAssertArgumentInspection
extends
BaseInspection
{
@NonNls
private
static
final
Set
<
String
>
ASSERT_METHODS
=
new
HashSet
<>();
static
{
ASSERT_METHODS
.
add
(
"assertTrue"
);
ASSERT_METHODS
.
add
(
"assertFalse"
);
ASSERT_METHODS
.
add
(
"assertNull"
);
ASSERT_METHODS
.
add
(
"assertNotNull"
);
}
public
class
ConstantJUnitAssertArgumentInspection
extends
ConstantAssertArgumentInspectionBase
{
@Override
@Nls
@NotNull
public
String
getDisplayName
()
{
return
InspectionGadgetsBundle
.
message
(
"constant.junit.assert.argument.display.name"
);
}
@Override
@NotNull
protected
String
buildErrorString
(
Object
...
infos
)
{
return
InspectionGadgetsBundle
.
message
(
"constant.junit.assert.argument.problem.descriptor"
);
return
InspectionGadgetsBundle
.
message
(
"constant.junit.assert.argument.display.name"
);
}
@Override
public
BaseInspectionVisitor
buildVisitor
()
{
return
new
ConstantJUnitAssertArgumentVisitor
();
}
private
static
class
ConstantJUnitAssertArgumentVisitor
extends
BaseInspectionVisitor
{
@Override
public
void
visitMethodCallExpression
(
PsiMethodCallExpression
expression
)
{
final
PsiReferenceExpression
methodExpression
=
expression
.
getMethodExpression
();
@NonNls
final
String
methodName
=
methodExpression
.
getReferenceName
();
if
(!
ASSERT_METHODS
.
contains
(
methodName
))
{
return
;
}
final
PsiMethod
method
=
expression
.
resolveMethod
();
if
(
method
==
null
)
{
return
;
}
final
boolean
messageOnFirstPosition
=
AssertHint
.
isMessageOnFirstPosition
(
method
,
false
);
final
boolean
messageOnLastPosition
=
AssertHint
.
isMessageOnLastPosition
(
method
,
false
);
if
(!
messageOnFirstPosition
&&
!
messageOnLastPosition
)
{
return
;
}
final
PsiExpressionList
argumentList
=
expression
.
getArgumentList
();
final
PsiExpression
[]
arguments
=
argumentList
.
getExpressions
();
if
(
arguments
.
length
==
0
)
{
return
;
}
final
PsiExpression
argument
=
arguments
[
messageOnFirstPosition
?
arguments
.
length
-
1
:
0
];
if
(!
PsiUtil
.
isConstantExpression
(
argument
))
{
return
;
}
registerError
(
argument
);
}
protected
boolean
checkTestNG
()
{
return
false
;
}
}
This diff is collapsed.
Click to expand it.
plugins/testng/src/META-INF/plugin.xml
+
2
-
0
View file @
a90c9f96
...
...
@@ -76,6 +76,8 @@
groupPath=
"Java"
language=
"JAVA"
shortName=
"SimplifiedTestNGAssertion"
bundle=
"com.siyeh.InspectionGadgetsBundle"
key=
"simplifiable.testng.assertion.display.name"
groupName=
"TestNG"
enabledByDefault=
"false"
level=
"WARNING"
/>
<localInspection
groupPath=
"Java"
language=
"JAVA"
shortName=
"ConstantTestNGAssertArgument"
displayName=
"Constant TestNG assert argument"
groupName=
"TestNG"
enabledByDefault=
"false"
level=
"WARNING"
implementationClass=
"com.theoryinpractice.testng.inspection.ConstantTestNGAssertArgumentInspection"
/>
</extensions>
<extensions
defaultExtensionNs=
"com.theoryinpractice.testng"
>
<listener
implementation=
"org.testng.TestNGTestDiscoveryListener"
/>
...
...
This diff is collapsed.
Click to expand it.
plugins/testng/src/com/theoryinpractice/testng/inspection/ConstantTestNGAssertArgumentInspection.java
0 → 100644
+
34
-
0
View file @
a90c9f96
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com.theoryinpractice.testng.inspection
;
import
com.siyeh.ig.junit.ConstantAssertArgumentInspectionBase
;
import
org.jetbrains.annotations.Nls
;
import
org.jetbrains.annotations.NotNull
;
public
class
ConstantTestNGAssertArgumentInspection
extends
ConstantAssertArgumentInspectionBase
{
@Override
protected
boolean
checkTestNG
()
{
return
true
;
}
@Nls
@NotNull
@Override
public
String
getDisplayName
()
{
return
"Constant TestNG assert argument"
;
}
}
This diff is collapsed.
Click to expand it.
plugins/testng/src/inspectionDescriptions/ConstantTestNGAssertArgument.html
0 → 100644
+
9
-
0
View file @
a90c9f96
<html>
<body>
Reports constant arguments to TestNG assertTrue, assertFalse,
assertNull and assertNotNull method calls. Calls to these methods with such
constant arguments will either always fail or always succeed.
Such statements can easily be left over after refactoring and are probably not intended.
<small>
New in 2017.1
</small>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help