Commit 1f57ac8c authored by Romain F. T's avatar Romain F. T
Browse files

fix #837

parent 837aeb13
<?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="j-unit-static-suite">
<ownedElements name="(default package)">
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest837_a">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="suite">
<modifier visibility="public"/>
<modifier static="true"/>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest837_b">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="suite">
<modifier visibility="public"/>
<modifier static="false"/>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest837_c">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="suite">
<modifier visibility="private"/>
<modifier static="true"/>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest837_d">
<modifier visibility="public"/>
<superClass type="//@ownedElements.1/@ownedPackages.0/@ownedElements.0"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="suite">
<modifier visibility="private"/>
<modifier static="false"/>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
<ownedElements xsi:type="java:ClassDeclaration" originalCompilationUnit="//@compilationUnits.0" name="FooTest837_2">
<modifier visibility="public"/>
<bodyDeclarations xsi:type="java:MethodDeclaration" originalCompilationUnit="//@compilationUnits.0" name="suite">
<modifier visibility="private"/>
<modifier static="false"/>
<body originalCompilationUnit="//@compilationUnits.0"/>
<returnType type="//@orphanTypes.5"/>
</bodyDeclarations>
</ownedElements>
</ownedElements>
<ownedElements name="junit" proxy="true">
<ownedPackages name="framework" proxy="true">
<ownedElements xsi:type="java:ClassDeclaration" name="TestCase" proxy="true" usagesInTypeAccess="//@ownedElements.0/@ownedElements.0/@superClass">
</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"/>
<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"/>
<compilationUnits name="FooTest.java" originalFilePath="/home/roschan/Projets/Eclipse/eclipse-workspace3/ATL/j-unit-static-suite/src/FooTest.java" types="//@ownedElements.0/@ownedElements.0"/>
</java:Model>
......@@ -136,6 +136,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) =
thisModule.finalizeShouldBeProtected(),
thisModule.importFromSamePackage(),
thisModule.junitSpelling(),
thisModule.jUnitStaticSuite(),
thisModule.missingBreakInSwitch(),
thisModule.missingSerialVersionUID(),
thisModule.nonCaseLabelInSwitchStatement(),
......@@ -1380,7 +1381,7 @@ rule MeasureJUnitTestsShouldIncludeAssert(m: java!MethodDeclaration, c: java!Cla
}
}
----------------------- JUnitTestContainsTooManyAsserts ------------------------
-- -------------------- JUnitTestContainsTooManyAsserts ------------------------
-- A measure instance if a JUnit test class violates the rule JUnitTestContainsTooManyAsserts
rule MeasureJUnitTestContainsTooManyAsserts(m: java!MethodDeclaration, c: java!ClassDeclaration, max: Integer) {
to
......@@ -1400,6 +1401,27 @@ rule MeasureJUnitTestContainsTooManyAsserts(m: java!MethodDeclaration, c: java!C
}
}
-- ---------------------------- JUnitStaticSuite -------------------------------
--- A measure instance if a JUnit test class violates the rule JUnitStaticSuite,
--- aka if it contains a method named "suite" which is not public or not static.
rule MeasureJUnitStaticSuite(c: java!ClassDeclaration) {
to
om: smm!ObservedMeasure (
measure <- noc,
measurements <- measurement
),
noc: smm!DimensionalMeasure (
name <- 'JUnitStaticSuite',
shortDescription <- 'The suite() method in a JUnit test needs to be both public and static.'
),
measurement: smm!DirectMeasurement (
error<- 'The suite() method in the class ' + c.name + ' should be both public and static.'
)
do {
noc;
}
}
-- ------------------------------------- LongInstantiation ---------------------------------------
--- A Measure instance if the class violates the rule 'LongInstantiation'.
rule MeasureLongInstantiation(variable : java!CompilationUnit) {
......@@ -1421,7 +1443,7 @@ rule MeasureLongInstantiation(variable : java!CompilationUnit) {
}
}
-- ------------------------------------- LongVariable ---------------------------------------
-- ------------------------------------- LongVariable ---------------------------------------
--- A Measure instance if the variable violates the rule LongVariable.
rule MeasureLongVariable(variable : java!VariableDeclarationFragment) {
to
......@@ -1441,7 +1463,7 @@ rule MeasureLongVariable(variable : java!VariableDeclarationFragment) {
}
}
-- -----------------------------------------MethodWithSameNameAsEnclosingClass----------------------------------------
-- -------------------------------- MethodWithSameNameAsEnclosingClass ------------------------------------
--- A Measure instance if the class violate the rule MethodWithSameNameAsEnclosingClass
rule MeasureMethodWithSameNameAsEnclosingClass(method : java!MethodDeclaration) {
to
......@@ -1461,7 +1483,7 @@ rule MeasureMethodWithSameNameAsEnclosingClass(method : java!MethodDeclaration)
}
}
-- -----------------------------------------MisLeadingVariableName----------------------------------------
-- ----------------------------------- MisLeadingVariableName ---------------------------------------
--- A Measure instance if the class violate the rule MisLeadingVariableName
rule MeasureMisLeadingVariableName(variable :java!VariableDeclarationFragment) {
to
......@@ -1709,7 +1731,7 @@ rule MeasureSimplifyConditional(class : String, method: String) {
}
-- ----------------------------------------- start with ------------------------------------------
-- -------------------------------------- StartWith ------------------------------------------
--- TODO: Add comment
rule MeasureStartWith(w: java!MethodInvocation) {
to
......
......@@ -137,11 +137,13 @@ helper def: jUnitTestsShouldIncludeAssert() : Set(smm!Measure) =
);
-- counts the number of assertions in a method declaration
helper context java!MethodDeclaration def: numberOfAsserts() : Integer =
helper context java!MethodDeclaration def: numberOfAsserts() : Integer =
if self.getBody() = OclUndefined then 0 else
self.getBody().statements
-> select(sttm | sttm <> OclUndefined) -- utile?
-> select(sttm | sttm.oclIsTypeOf(java!AssertStatement))
-> size();
-> size()
endif;
-- tells if a method is a test (assuming its class is a TestCase)
-- XXX we maybe could check the annotation too?
......
......@@ -15,7 +15,6 @@ helper def: switchStatementContainsMissingBreaks(ss: java!SwitchStatement): Bool
-- the number of break statements => we don't have a break statement for every switch case
-- in that case, return true, else false
(thisModule.nbBranchesOfASwitchStatement(ss) - thisModule.nbEmptySwitchCasesOfASwitchStatement(ss)) > thisModule.nbBreakStatementOfASwitchStatement(ss)
;
--------------------------------------------- DoNotExtendJavaLangThrowable ---------------------------------------------
......@@ -31,6 +30,7 @@ helper def: doNotExtendJavaLangThrowable() : Set(smm!Measure) =
-- collect all results and send an error message
->collect(it4|thisModule.MeasureDoNotExtendJavaLangThrowable(it4))
;
--------------------------------------------- SuspiciousEqualsMethodName---------------------------------------------
--Detects the declaration of methods whose name and parameters looks like the equals(Object) method, which denote an intention to override the equals(Object) method.
......@@ -64,10 +64,7 @@ helper def: isWrongEqualsDeclaration(method : java!MethodDeclaration) : Boolean
method.parameters.first().type.type.name = 'Object' and
method.returnType.type.name = 'boolean'
);
--Allows to have the signature of a method as a String.
helper context java!MethodDeclaration def: toString() : String =
if self.hasReturnType() then
......@@ -143,10 +140,8 @@ helper context java!VariableDeclarationFragment def: variableIsObject() : Boolea
self.variablesContainer.type.type.oclIsTypeOf(java!ClassDeclaration);
----------
--------------------------------------------------------------------------------------------------------
------------------------------------------- AvoidCallingFinalize----------------------------------------------
--------------------------------------------------------------------------------
----------------------------- AvoidCallingFinalize -----------------------------
-- Rule for metrics AvoidCallingFinalize
helper def: avoidCallingFinalize() : Set(smm!Measure) =
java!MethodInvocation.allInstances()
......@@ -167,7 +162,8 @@ helper def: emptyWhileStmt() : Set(smm!Measure) =
->select( w | w.body.oclIsTypeOf(java!Block))
->select(w | w.body.statements.isEmpty())
->collect(w | thisModule.MeasureEmptyWhileStmt(w));
------------------------------------------- MeasureDontImportSun----------------------------------------------
--------------------------------- MeasureDontImportSun -------------------------------------
-- Rule for metrics DontImportSun
helper def: dontImportSun() : Set(smm!Measure) =
java!Package.allInstances()
......@@ -182,7 +178,6 @@ helper def: finalizeShouldBeProtected() : Set(smm!Measure) =
->select(m | m.name = 'finalize' and m.modifier.visibility.toString() <> 'protected')
->collect(m | thisModule.MesureFinalizeShouldBeProtected(m));
--------------------------------------------------------------------------------------------------------------
--------------------------------------------DontUseFloatTypeForLoopIndices------------------------------------
......@@ -191,11 +186,7 @@ helper def: dontUseFloatTypeForLoopIndices() : Set(smm!Measure) =
-> select(loop | loop.initializers.type.type = '//@orphanType.2')
-> collect(loop | thisModule.MeasureDontUseFloatTypeForLoopIndices())
;
--------------------------------------------- EmptySwitchStatement ---------------------------------------------
......@@ -207,13 +198,12 @@ helper def: emptySwitchStatement() : Set(smm!Measure) =
------------------------------------------ EmptySynchronizedBlock ---------------------------------------------
--Detects the use of empty synchronized block. These blocks serve no purpose and should be removed
helper def: emptySynchronizedBlock() : Set(smm!Measure) =
java!SynchronizedStatement.allInstances()
->select(synchronizedBlock | synchronizedBlock.body.statements.isEmpty() )
->collect(synchronizedBlock | thisModule.MeasureEmptySynchronizedBlock(synchronizedBlock));
---------------------------------------- EmptyFinallyBlock----------------------------------------------
-- Rule for metrics EmptyFinallyBlock
helper def: emptyFinallyBlock() : Set(smm!Measure) =
......@@ -229,9 +219,6 @@ helper def: emptyfinalizeMethod() : Set(smm!Measure) =
->select( finalizeMethod | finalizeMethod.name = 'finalize' and finalizeMethod.body.statements.isEmpty())
->collect(finalizeMethod| thisModule. MeasureEmptyFinalizer(finalizeMethod));
--- Issues #780, #784, #799, #819, #862, #871
--- helper AvoidCatchingNPE
......@@ -311,28 +298,28 @@ helper def: UseProperClassLoader() : Set(smm!Measure) =
->collect(w | thisModule.MeasureUseProperClassLoader(w));
-----------------------------------------avoidCatchingThrowable----------------------------------------------
---------------------------------- avoidCatchingThrowable -----------------------------------------
-- Rule for metrics avoidCatchingThrowable
helper def: avoidCatchingThrowable() : Set(smm!Measure) =
java!CatchClause.allInstances()
->select(catch | catch.exception.type.type.name = 'Throwable')
->collect(catch | thisModule.MeasureAvoidCatchingThrowable(catch));
-----------------------------------------DoNotCallGarbageCollectionExplicitly----------------------------------------------
-------------------------- DoNotCallGarbageCollectionExplicitly --------------------------------
-- Rule for metrics DoNotCallGarbageCollectionExplicitly
helper def: doNotCallGarbageCollectionExplicitly() : Set(smm!Measure) =
java!MethodInvocation.allInstances()
->select(method | method.method.name = 'gc' and method.method.proxy)
->collect(method | thisModule.MeasureDoNotCallGarbageCollectionExplicitly(method));
-----------------------------------------EmptyCatchBlock----------------------------------------------
------------------------------- EmptyCatchBlock -------------------------------------
-- Rule for metrics EmptyCatchBlock
helper def: emptyCatchBlock() : Set(smm!Measure) =
java!CatchClause.allInstances()
->select(catch | catch.body.statements.size() = 0)
->collect(catch | thisModule.MeasureEmptyCatchBlock(catch));
-----------------------------------------ImportFromSamePackage----------------------------------------------
------------------------------- ImportFromSamePackage -----------------------------------
-- Rule for metrics ImportFromSamePackage
helper def: importFromSamePackage() : Set(smm!Measure) =
java!ImportDeclaration.allInstances()
......@@ -341,20 +328,30 @@ helper def: importFromSamePackage() : Set(smm!Measure) =
-- ------------------------------------------- JUnitSpelling ---------------------------------------------
--- Helper for issue JUnitSpelling : Some JUnit framework methods are easy to misspell !("tearDown", "setUp").
helper def: junitSpelling() : Set(smm!Measure) =
helper def: junitSpelling() : Set(smm!Measure) =
java!MethodDeclaration.allInstances()
-> select(i | ((i.name <> 'tearDown' and i.name <> 'setUp') and (i.name.toLower() = 'teardown' or i.name.toLower() = 'setup')))
-> collect(i | thisModule.MeasureJUnitSpelling(i));
-----------------------------------------MethodWithSameNameAsEnclosingClass----------------------------------------------
-- --------------------------- JUnitStaticSuite --------------------------------
--- Helper for issue JUnitStaticSuite : in JUnit TestCase, the method "suite" has to be both public and static
helper def: jUnitStaticSuite() : Set(smm!Measure) =
java!ClassDeclaration.allInstances()
-> select(cl | cl.isTestClass())
-> iterate(i; res : Set(smm!Measure) = Set{} | i.bodyDeclarations
-> select(mtd | mtd.oclIsTypeOf(java!MethodDeclaration) and mtd.name = 'suite')
-> reject(mtd | mtd.modifier.visibility.toString() = 'public' and mtd.modifier.static = false)
-> collect(mtd | thisModule.MeasureJUnitStaticSuite(i))
);
-------------------------------- MethodWithSameNameAsEnclosingClass ----------------------------------
-- Rule for metrics MethodWithSameNameAsEnclosingClass
helper def: methodWithSameNameAsEnclosingClass() : Set(smm!Measure) =
java!MethodDeclaration.allInstances()
->select(method | method.name = method.abstractTypeDeclaration.name)
->collect(method | thisModule.MeasureMethodWithSameNameAsEnclosingClass(method));
------------------------------------------- testClassWithoutTest ------------------------------------------------------------------------------------
-------------------------- testClassWithoutTest --------------------------------
helper def: testClassWithoutTest() : Set(smm!Measure) =
java!ClassDeclaration.allInstances()
......@@ -370,7 +367,7 @@ helper context java!ClassDeclaration def: nbTestMethod() : Integer =
-> select(bodyDeclaration | bodyDeclaration.name.size() > 4)
-> select(each | each.name.substring(1, 4) = 'test')
-> size();
--------------------------------------------- DoNotCallSystemExit ---------------------------------------------
-- Detects if the method "exit" of the System or Runtime class is called
......@@ -444,7 +441,7 @@ helper context java!CompilationUnit def: hasJunitAnnotation() : Boolean =
-> select(it | it.importedElement.package.package.package.name = 'junit')
;
--------------------------------------------- SuspiciousHashcodeMethodName ---------------------------------------------
-------------------------------- SuspiciousHashcodeMethodName --------------------------------------
helper def: suspiciousHashcodeMethodName(): Set(smm!Measure) =
java!MethodDeclaration.allInstances()
......@@ -514,13 +511,13 @@ helper context java!SingleVariableAccess def : isString() : Boolean =
--- Called if an operand is a string literal
--- Always return true
helper context java!StringLiteral def : isString() : Boolean =
helper context java!StringLiteral def : isString() : Boolean =
true;
--- Called if the operand is 'this'
--- Return true : the expression is of type String
--- return false otherwise
helper context java!ThisExpression def: isString() : Boolean =
helper context java!ThisExpression def: isString() : Boolean =
self.originalCompilationUnit.types
->exists(name | name = 'String');
......@@ -534,7 +531,7 @@ helper context java!Expression def: isString() : Boolean =
--- Param variable : A java declaration of a variable
--- Return true : variable is a string
--- return false otherwise
helper def: variableIsString(variable : java!VariableDeclaration) : Boolean =
helper def: variableIsString(variable : java!VariableDeclaration) : Boolean =
variable.variableIsString();
--- Called when the context is MethodDeclaration
......@@ -554,7 +551,7 @@ helper context java!SingleVariableAccess def: variableIsString() : Boolean =
--- Called when the context is a SingleVariableDeclaration
--- See : variableIsString(variable : java!VariableDeclaration)
helper context java!SingleVariableDeclaration def: variableIsString() : Boolean =
helper context java!SingleVariableDeclaration def: variableIsString() : Boolean =
self.type.type.name = 'String';
--- Called when the context is AbstractMethodDeclaration
......@@ -609,7 +606,7 @@ helper def: nonStaticInitializer() : Set(smm!Measure) =
-> select(i | i.modifier.static = false)
-> collect (i | thisModule.MeasureNonStaticInitializer(i));
--------------------------------------------- SimpleDateFormatNeedsLocale ---------------------------------------------
---------------------------------- SimpleDateFormatNeedsLocale -----------------------------------
--- Indicates if a SimpleDateFormat variable has no Locale parameter
helper def: simpleDateFormatNeedsLocale() : Set(smm!Measure) =
......@@ -618,7 +615,7 @@ helper def: simpleDateFormatNeedsLocale() : Set(smm!Measure) =
-> select(i | i.initializer <> OclUndefined) -> select(i | i.initializer.arguments.size() <= 1 and i.initializer.type.type.name = 'SimpleDateFormat')
-> collect (i | thisModule.MeasureSimpleDateFormatNeedsLocale(i));
--------------------------------------------- NonCaseLabelInSwitchStatement ---------------------------------------------
----------------------------------- NonCaseLabelInSwitchStatement --------------------------------------
--- Indicates if a non-case label is present in a switch statement.
helper def: nonCaseLabelInSwitchStatement() : Set(smm!Measure) =
......@@ -626,3 +623,6 @@ helper def: nonCaseLabelInSwitchStatement() : Set(smm!Measure) =
-> select(i | i.name <> OclUndefined)
-> select(i | i.name <> 'case :' and i.name <> 'default:')
-> collect (i | thisModule.MeasureNonCaseLabelInSwitchStatement(i));
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