Commit 9221087a authored by Alexandre MELO's avatar Alexandre MELO Committed by Gerson SUNYE
Browse files

UseassertTrueinsteadofassertEqual

parent d21816e8
<?xml version="1.0" encoding="ASCII"?>
<java:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://www.eclipse.org/MoDisco/Java/0.2.incubation/java" name="test">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="test">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="main">
<annotations originalCompilationUnit="//@compilationUnits.0">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<values>
<value xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;restriction&quot;"/>
</values>
</annotations>
<modifier visibility="public" static="true"/>
<body originalCompilationUnit="//@compilationUnits.0">
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<type type="//@orphanTypes.4"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="myVar" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression/@arguments.1 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.5/@expression/@leftHandSide //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.6/@expression/@arguments.2 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.7/@expression/@arguments.1">
<initializer xsi:type="java:BooleanLiteral" originalCompilationUnit="//@compilationUnits.0" value="true"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// Ok" prefixOfParent="true"/>
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@unresolvedItems.2">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;myVar is true&quot;"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
</expression>
</statements>
<statements xsi:type="java:VariableDeclarationStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// Bad" prefixOfParent="true"/>
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="teststring" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.3/@expression/@arguments.2">
<initializer xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;ere&quot;"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@unresolvedItems.3">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;myVar is true&quot;"/>
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;ere&quot;"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@fragments.0"/>
</expression>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// Bad" prefixOfParent="true"/>
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@unresolvedItems.3">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;myVar is true&quot;"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.1/@ownedPackages.0/@ownedElements.4/@bodyDeclarations.0/@fragments.0">
<qualifier xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
</arguments>
<arguments xsi:type="java:BooleanLiteral" originalCompilationUnit="//@compilationUnits.0" value="true"/>
</expression>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// Bad" prefixOfParent="true"/>
<expression xsi:type="java:Assignment" originalCompilationUnit="//@compilationUnits.0">
<leftHandSide xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
<rightHandSide xsi:type="java:BooleanLiteral" originalCompilationUnit="//@compilationUnits.0"/>
</expression>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@unresolvedItems.3">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;myVar is false&quot;"/>
<arguments xsi:type="java:BooleanLiteral" originalCompilationUnit="//@compilationUnits.0"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
</expression>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// Bad" prefixOfParent="true"/>
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@unresolvedItems.3">
<arguments xsi:type="java:StringLiteral" originalCompilationUnit="//@compilationUnits.0" escapedValue="&quot;myVar is false&quot;"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.1/@ownedPackages.0/@ownedElements.4/@bodyDeclarations.1/@fragments.0">
<qualifier xsi:type="java:TypeAccess" type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
</arguments>
</expression>
</statements>
</body>
<parameters originalCompilationUnit="//@compilationUnits.0" name="args">
<modifier/>
<type type="//@orphanTypes.9"/>
</parameters>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="java" proxy="true">
<ownedPackages name="lang" proxy="true">
<ownedElements xsi:type="java:AnnotationTypeDeclaration" name="SuppressWarnings" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@annotations.0/@type"/>
<ownedElements xsi:type="java:ClassDeclaration" name="String" proxy="true" usagesInTypeAccess="//@orphanTypes.9/@elementType //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@type">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.1 //@ownedElements.1/@ownedPackages.0/@ownedElements.4/@superInterfaces.1">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="CharSequence" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.2"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Boolean" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.4/@expression/@arguments.1/@qualifier //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.7/@expression/@arguments.2/@qualifier">
<bodyDeclarations xsi:type="java:FieldDeclaration" proxy="true">
<fragments name="TRUE" proxy="true" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.4/@expression/@arguments.1"/>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:FieldDeclaration" proxy="true">
<fragments name="FALSE" proxy="true" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.7/@expression/@arguments.2"/>
</bodyDeclarations>
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.1/@superInterfaces.0 //@ownedElements.1/@ownedPackages.0/@ownedElements.4/@superInterfaces.0"/>
</ownedPackages>
</ownedElements>
<orphanTypes xsi:type="java:PrimitiveTypeInt" name="int"/>
<orphanTypes xsi:type="java:PrimitiveTypeLong" name="long"/>
<orphanTypes xsi:type="java:PrimitiveTypeFloat" name="float"/>
<orphanTypes xsi:type="java:PrimitiveTypeDouble" name="double"/>
<orphanTypes xsi:type="java:PrimitiveTypeBoolean" name="boolean" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@type"/>
<orphanTypes xsi:type="java:PrimitiveTypeVoid" name="void" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@returnType"/>
<orphanTypes xsi:type="java:PrimitiveTypeChar" name="char"/>
<orphanTypes xsi:type="java:PrimitiveTypeShort" name="short"/>
<orphanTypes xsi:type="java:PrimitiveTypeByte" name="byte"/>
<orphanTypes xsi:type="java:ArrayType" name="java.lang.String[]" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@parameters.0/@type" dimensions="1">
<elementType type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
</orphanTypes>
<unresolvedItems name="org.junit.Assert.assertEquals" proxy="true" usagesInImports="//@compilationUnits.0/@imports.0"/>
<unresolvedItems name="org.junit.Assert.assertTrue" proxy="true" usagesInImports="//@compilationUnits.0/@imports.1"/>
<unresolvedItems xsi:type="java:UnresolvedMethodDeclaration" name="assertTrue" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression"/>
<unresolvedItems xsi:type="java:UnresolvedMethodDeclaration" name="assertEquals" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.3/@expression //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.4/@expression //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.6/@expression //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.7/@expression"/>
<compilationUnits name="test.java" originalFilePath="C:\Users\knowi\Documents\M2\Modele\ProjetATL2LeRetour\test\src\test\test.java" commentList="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.4/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.5/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.7/@comments.0" types="//@ownedElements.0/@ownedElements.0">
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@unresolvedItems.0"/>
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@unresolvedItems.1"/>
</compilationUnits>
</java:Model>
......@@ -92,12 +92,14 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.emptyTryBlock(),
thisModule.emptyWhileStmt(),
thisModule.dontImportSun(),
thisModule.finalizeShouldBeProtected(),
--thisModule.finalizeShouldBeProtected(),
-- Best practices rules
thisModule.avoidThrowingNewInstanceOfSameException(),
thisModule.switchDensity(),
thisModule.avoidStringBufferField()
thisModule.useAssertTrueInsteadOfAssertEquals(),
thisModule.switchDensity()
--thisModule.avoidStringBufferField()
};
......@@ -868,6 +870,27 @@ rule MeasureCompareObjectsWithEquals(expression : java!InfixExpression) {
}
--------------------------------------------- UseAssertTrueInsteadOfAssertEquals ---------------------------------------------
-- A Measure instance if the class violates the rule UseAssertTrueInsteadOfAssertEquals.
rule MesureUseAssertTrueInsteadOfAssertEquals(class : java!MethodInvocation) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'UseAssertTrueInsteadOfAssertEquals',
shortDescription <- 'Use AssertTrue instead of AssertEquals'
),
measurement: smm!DirectMeasurement (
error <- class.originalCompilationUnit.name + ' Use AssertTrue instead of AssertEquals '
)
do {
noc;
}
}
--------------------------------------------- AvoidRethrowingException ---------------------------------------------
--A Measure instance if the class violates the rule AvoidRethrowingException.
rule MeasureAvoidRethrowingException(statement : java!TryStatement) {
......@@ -899,7 +922,6 @@ rule MeasureIntegerInstantiation(variable : java!CompilationUnit) {
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'IntegerInstantiation',
shortDescription <- 'Calling new Integer() causes memory allocation that can be avoided by the static Integer.valueOf(). It makes use of an internal cache that recycles earlier instances making it more memory efficient. Note that new Integer() is deprecated since JDK 9 for that reason.'
),
......@@ -1076,3 +1098,4 @@ rule MesureFinalizeShouldBeProtected(m : java!MethodDeclaration) {
......@@ -61,11 +61,36 @@ helper def: switchDensity() : Set (smm!Measure) =
->select(switchStatement | switchStatement.statements.size() -1 >= 10)
->collect(switchStatement | thisModule.MeasureSwitchDensity(switchCase))
);
--------------------------------------------- useAssertTrueInsteadOfAssertEquals ---------------------------------------------
helper def: useAssertTrueInsteadOfAssertEquals() : Set(smm!Measure) =
-- Browse through all methods use
java!MethodInvocation.allInstances()
--Check if the method use is assertEqual and a Boolean is used
->select(assert | thisModule.isWrongUseAssertTrueInsteadOfAssertEquals(assert) and assert.method.name = 'assertEquals')
->collect(assert | thisModule.MesureUseAssertTrueInsteadOfAssertEquals(assert));
helper def: isWrongUseAssertTrueInsteadOfAssertEquals(m:java!MethodInvocation) : Boolean =
--Shorten the path m.arguments.last() to lastArgument and check if it's a boolean
let lastArgument : java!Expression = m.arguments.last() in lastArgument.isBoolean();
--Checks if it's a BooleanLiteral
helper context java!BooleanLiteral def: isBoolean() : Boolean = true;
--Checks if the type of a Expression is Boolean
helper context java!Expression def: isBoolean() : Boolean =
self.variable.variablesContainer.isBoolean();
--Checks if the type of a VariableDeclarationStatement is Boolean
helper context java!VariableDeclarationStatement def: isBoolean() : Boolean =
self.type.type.name= 'boolean';
--Use in case of Boolean.TRUE or Boolean.FALSE
helper context java!FieldDeclaration def: isBoolean() : Boolean =
self.abstractTypeDeclaration.name = 'Boolean';
--------------------------------------------- AvoidStringBufferField ---------------------------------------------
-- Rule for metrics AvoidStringBufferField
helper def: avoidStringBufferField() : Set(smm!Measure) =
java!FieldDeclaration.allInstances()
->select(dec | dec.type.type.name = 'StringBuffer' or dec.type.type.name = 'StringBuilder')
->collect(dec | thisModule.MeasureAvoidStringBufferField(dec));
\ No newline at end of file
->collect(dec | thisModule.MeasureAvoidStringBufferField(dec));
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment