Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
#32 added concept "java:TypeArgumentDeclaredByTypeParameter"
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jun 18, 2021
1 parent bbce3c0 commit f8e971b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/resources/META-INF/jqassistant-rules/java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,25 @@
</verify>
</concept>

<concept id="java:TypeArgumentDeclaredByTypeParameter">
<description>Creates a `DECLARED_BY` relation between a type argument of a parameterized type to the according type parameter of the declaring type.
</description>
<cypher><![CDATA[
MATCH
(parameterizedType:ParameterizedType)-[:OF_RAW_TYPE]->(rawType:Type),
(parameterizedType)-[hasActualTypeArgument:HAS_ACTUAL_TYPE_ARGUMENT]->(typeArgument),
(rawType)-[declaresTypeParameter:DECLARES_TYPE_PARAMETER]->(typeParameter)
WHERE
hasActualTypeArgument.index = declaresTypeParameter.index
MERGE
(typeArgument)-[:DECLARED_BY]->(typeParameter)
RETURN
count(*) as TypeParameterDeclarations
]]></cypher>
<verify>
<aggregation/>
</verify>
</concept>

</jqassistant-rules>

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.buschmais.jqassistant.plugin.java.test.rules;

import java.util.List;
import java.util.Map;

import com.buschmais.jqassistant.core.report.api.model.Result;
import com.buschmais.jqassistant.core.rule.api.model.Concept;
import com.buschmais.jqassistant.core.rule.api.model.RuleException;
import com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor;
import com.buschmais.jqassistant.plugin.java.test.AbstractJavaPluginIT;
Expand All @@ -10,15 +13,20 @@

import org.junit.jupiter.api.Test;

import static com.buschmais.jqassistant.core.report.api.model.Result.Status.SUCCESS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.HamcrestCondition.matching;

public class TypeParameterIT extends AbstractJavaPluginIT {

@Test
void outerClassTypeParameters() throws RuleException {
void innerTypeParameterDeclaredByOuterType() throws RuleException {
scanClasses(GenericTypeDeclarations.class, GenericTypeDeclarations.Inner.class);
applyConcept("java:InnerTypeParameterDeclaredByOuterType");
Result<Concept> conceptResult = applyConcept("java:InnerTypeParameterDeclaredByOuterType");
assertThat(conceptResult.getStatus()).isEqualTo(SUCCESS);
List<Map<String, Object>> rows = conceptResult.getRows();
assertThat(rows).hasSize(1);
assertThat(rows.get(0).get("OuterTypeDeclarations")).isEqualTo(1l);
store.beginTransaction();
List<TypeDescriptor> outerTypes = query(
"MATCH (:Type{name:'GenericTypeDeclarations$Inner'})-[:REQUIRES_TYPE_PARAMETER]->(:TypeVariable)-[:DECLARED_BY]->(:TypeVariable)<-[:DECLARES_TYPE_PARAMETER]-(outer:Type) RETURN outer")
Expand All @@ -27,4 +35,23 @@ void outerClassTypeParameters() throws RuleException {
assertThat(outerTypes.get(0)).is(matching(TypeDescriptorMatcher.typeDescriptor(GenericTypeDeclarations.class)));
store.commitTransaction();
}

@Test
void typeArgumentDeclaredByTypeParameter() throws RuleException {
scanClasses(GenericTypeDeclarations.class, GenericTypeDeclarations.Inner.class);
Result<Concept> conceptResult = applyConcept("java:TypeArgumentDeclaredByTypeParameter");
assertThat(conceptResult.getStatus()).isEqualTo(SUCCESS);
List<Map<String, Object>> rows = conceptResult.getRows();
assertThat(rows).hasSize(1);
assertThat(rows.get(0).get("TypeParameterDeclarations")).isEqualTo(2l);
store.beginTransaction();
List<String> typeVariables = query( //
"MATCH (:Type{name:'GenericTypeDeclarations$Inner'})-[:EXTENDS_GENERIC]->(parameterizedType:ParameterizedType)," + //
"(:Type{name:'GenericTypeDeclarations'})-[dtp:DECLARES_TYPE_PARAMETER]->(typeVariable:TypeVariable)," + //
"(parameterizedType)-[:HAS_ACTUAL_TYPE_ARGUMENT]->(:TypeVariable)-[:DECLARED_BY]->(typeVariable:TypeVariable) " + //
"RETURN typeVariable.name as typeVariableName ORDER BY dtp.index").getColumn("typeVariableName");
assertThat(typeVariables).hasSize(2);
assertThat(typeVariables).containsExactly("X", "Y");
store.commitTransaction();
}
}

0 comments on commit f8e971b

Please sign in to comment.