0

I would like to check if a package from tests exists in src. For example if my tests is in my.package.customer I would like to verify that there is a package with that name in src. So far I have tried the following query. That query would return (I hope) all there entries where at least one class a test depends on is from the same package. This would work for me. The remaining problem is that I do not know how to make this work as jqassistant constraint since the goal should fail if the count for this query is 0.

MATCH
    (package:Package)-[:CONTAINS]->(classInPackage:Class),
    (classInPackage:Type)-[:DECLARES]->(aMethod:Method),
    (aMethod)-[:ANNOTATED_BY]->()-[:OF_TYPE]->(testAnnotationType:Type),
    (classInPackage)-[DEPENDS_ON]->(anotherClass:Type:Class),
    (depPackage:Package)-[:CONTAINS]->(anotherClass)                
WHERE
    package.fqn = depPackage.fqn
RETURN
    classInPackage.fqn, anotherClass.fqn, depPackage.fqn
rogergl
  • 3,501
  • 2
  • 30
  • 49

1 Answers1

0

I assume that you're working with rules, i.e. concepts and constraints. Therefore I'd recommend to split things up by using the pre-defined concept "junit4:TestMethod" which adds the labels "Junit4" and "Test" to each method annotated with @Test:

<constraint id="my-rule:EveryClassMustHaveAUnitTest">
  <requiresConcept refId="junit4:TestMethod" />
  <description>For every class there must be at least one unit test.</description>
  <cypher><![CDATA[
  MATCH
    (a:Artifact)-[:CONTAINS]->(c:Type:Class)
  WHERE
    a.type <> "test-jar"
    and not has(c.abstract)
    and not (:Test:Method)-[:INVOKES]->(:Method)<-[:DECLARES]-(c)
  RETURN
    c.fqn

  ]]>
  </cypher>
</constraint>

The query returns each class that

  • is not test code
  • not abstract, i.e. concrete
  • and no test method exists which invokes any method of that class

Not sure if the conditions are sufficient to avoid false positives, give it a try and come back here if we missed something.

Dirk Mahler
  • 1,186
  • 1
  • 6
  • 7