Commit 60bac72f authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'useAssertSameInsteadOfAssertTrue' into 'master'

fix issue #655 UseAssertSameInsteadOfAssertTrue

Closes #655

See merge request naomod/mde/projet-2019!101
parents 2f25ac2c b37a9df6
<?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="useAssertSameInsteadOfAssertTrue">
<ownedElements name="useAssertSameInsteadOfAssertTrue">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="TestuseAssertSameInsteadOfAssertTrue">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="//import org.junit.;" prefixOfParent="true"/>
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="main">
<annotations originalCompilationUnit="//@compilationUnits.0">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.4"/>
<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="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
<fragments originalCompilationUnit="//@compilationUnits.0" name="a" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression/@arguments.0/@leftOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@expression/@arguments.0">
<initializer xsi:type="java:NullLiteral" originalCompilationUnit="//@compilationUnits.0"/>
</fragments>
<fragments originalCompilationUnit="//@compilationUnits.0" name="b" usageInVariableAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression/@arguments.0/@rightOperand //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@expression/@arguments.1">
<initializer xsi:type="java:NullLiteral" originalCompilationUnit="//@compilationUnits.0"/>
</fragments>
<modifier/>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// bad usage"/>
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.4">
<arguments xsi:type="java:InfixExpression" originalCompilationUnit="//@compilationUnits.0" operator="==">
<rightOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.1"/>
<leftOperand xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
</arguments>
</expression>
</statements>
<statements xsi:type="java:ExpressionStatement" originalCompilationUnit="//@compilationUnits.0">
<comments xsi:type="java:LineComment" originalCompilationUnit="//@compilationUnits.0" content="// good usage"/>
<expression xsi:type="java:MethodInvocation" originalCompilationUnit="//@compilationUnits.0" method="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.5">
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.0"/>
<arguments xsi:type="java:SingleVariableAccess" variable="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@fragments.1"/>
</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:ClassDeclaration" name="String" proxy="true" usagesInTypeAccess="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@parameters.0/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.1/@parameters.0/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.2/@parameters.0/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.3/@parameters.0/@type //@orphanTypes.9/@elementType">
<superInterfaces type="//@ownedElements.1/@ownedPackages.1/@ownedElements.0"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.1"/>
<superInterfaces type="//@ownedElements.1/@ownedPackages.0/@ownedElements.2"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="Comparable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.1">
<typeParameters name="T" proxy="true"/>
</ownedElements>
<ownedElements xsi:type="java:InterfaceDeclaration" name="CharSequence" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.2"/>
<ownedElements xsi:type="java:ClassDeclaration" name="Object" proxy="true" usagesInTypeAccess="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0/@parameters.1/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.1/@parameters.1/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.2/@parameters.1/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.2/@parameters.2/@type //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.0/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.5/@parameters.0/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.5/@parameters.1/@type"/>
<ownedElements xsi:type="java:AnnotationTypeDeclaration" name="SuppressWarnings" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@annotations.0/@type"/>
</ownedPackages>
<ownedPackages name="io" proxy="true">
<ownedElements xsi:type="java:InterfaceDeclaration" name="Serializable" proxy="true" usagesInTypeAccess="//@ownedElements.1/@ownedPackages.0/@ownedElements.0/@superInterfaces.0"/>
</ownedPackages>
</ownedElements>
<ownedElements name="org" proxy="true">
<ownedPackages name="junit" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="Assert" proxy="true">
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertNotNull" proxy="true" usagesInImports="//@compilationUnits.0/@imports.0">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</parameters>
<parameters name="arg1" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertNull" proxy="true" usagesInImports="//@compilationUnits.0/@imports.1">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</parameters>
<parameters name="arg1" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertSame" proxy="true" usagesInImports="//@compilationUnits.0/@imports.2">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</parameters>
<parameters name="arg1" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
<parameters name="arg2" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertTrue" proxy="true" usagesInImports="//@compilationUnits.0/@imports.3">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
</parameters>
<parameters name="arg1" proxy="true">
<type type="//@orphanTypes.4"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertTrue" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@expression">
<parameters name="arg0" proxy="true">
<type type="//@orphanTypes.4"/>
</parameters>
</bodyDeclarations>
<bodyDeclarations xsi:type="java:MethodDeclaration" name="assertSame" proxy="true" usages="//@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@expression">
<parameters name="arg0" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
<parameters name="arg1" proxy="true">
<type type="//@ownedElements.1/@ownedPackages.0/@ownedElements.3"/>
</parameters>
</bodyDeclarations>
</ownedElements>
</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.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.3/@parameters.1/@type //@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.4/@parameters.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.0"/>
</orphanTypes>
<compilationUnits name="TestuseAssertSameInsteadOfAssertTrue.java" originalFilePath="C:\Users\knowi\Documents\M2\Modele\ProjetATL2LeRetour\useAssertSameInsteadOfAssertTrue\src\useAssertSameInsteadOfAssertTrue\TestuseAssertSameInsteadOfAssertTrue.java" commentList="//@ownedElements.0/@ownedElements.0/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.1/@comments.0 //@ownedElements.0/@ownedElements.0/@bodyDeclarations.0/@body/@statements.2/@comments.0" package="//@ownedElements.0" types="//@ownedElements.0/@ownedElements.0">
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.0"/>
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.1"/>
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.2"/>
<imports originalCompilationUnit="//@compilationUnits.0" static="true" importedElement="//@ownedElements.2/@ownedPackages.0/@ownedElements.0/@bodyDeclarations.3"/>
</compilationUnits>
</java:Model>
......@@ -98,6 +98,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
-- Best practices rules
thisModule.avoidThrowingNewInstanceOfSameException(),
thisModule.useAssertTrueInsteadOfAssertEquals(),
thisModule.useAssertSameInsteadOfAssertTrue(),
thisModule.switchDensity()
--thisModule.avoidStringBufferField()
......@@ -1115,5 +1116,24 @@ rule RuleUseArrayListInsteadOfVector(tab :java!Package) {
}
}
-------------------------------------------UseAssertSameInsteadOfAssertTrue----------------------------------------------
-- A Measure instance if the class violate the rule AssertSameInsteadOfAssertTrue
rule MesureUseAssertSameInsteadOfAssertTrue(tab :java!Package) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'Use AssertSame Instead Of AssertTrue',
shortDescription <- 'Use AssertSame Instead Of AssertTrue'
),
measurement: smm!DirectMeasurement (
error<- 'Use AssertSame Instead Of AssertTrue.'
)
do {
noc;
}
}
......@@ -67,7 +67,8 @@ 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')
->select(assert | assert.method.name = 'assertEquals')
->select(assert | thisModule.isWrongUseAssertTrueInsteadOfAssertEquals(assert))
->collect(assert | thisModule.MesureUseAssertTrueInsteadOfAssertEquals(assert));
......@@ -94,3 +95,17 @@ 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));
---------------------------------------------UseAssertSameInsteadOfAssertTrue---------------------------------------------
helper def: useAssertSameInsteadOfAssertTrue() : Set(smm!Measure) =
-- Browse through all methods use
java!MethodInvocation.allInstances()
--Check if the method use is assertTrue and check if == is used
->select(assert | assert.method.name = 'assertTrue')
->select(assert | thisModule.isWrongUseAssertSameInsteadOfAssertTrue(assert))
->collect(assert | thisModule.MesureUseAssertSameInsteadOfAssertTrue(assert));
helper def: isWrongUseAssertSameInsteadOfAssertTrue(m:java!MethodInvocation) : Boolean =
--Browse through all arguments of the method assertTrue and see if == is used
(m.arguments->select(t|t.oclIsTypeOf(java!InfixExpression))->select(t|t.operator.toString() = '==')->collect(t|t).notEmpty());
\ No newline at end of file
Markdown is supported
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