Skip to content

Commit

Permalink
Merge pull request #30 from jqassistant-plugin/29_fix_combination_con…
Browse files Browse the repository at this point in the history
…traint

#29 Refactor: Fix jee-injection:NoCombinationOfBeanProducersAndApplicationCode
  • Loading branch information
DirkMahler authored Feb 20, 2025
2 parents 3e22a87 + 5af8a1f commit b7e2425
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
(t)-[:DECLARES]->(otherPublicMethod:Method {visibility: "public"})
WHERE
NOT otherPublicMethod:BeanProducer
AND NOT otherPublicMethod:Constructor
RETURN DISTINCT
t AS `Java-Klasse`
]]></cypher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@
import java.util.ArrayList;
import java.util.List;

import static com.buschmais.jqassistant.core.report.api.model.Result.Status.FAILURE;
import static com.buschmais.jqassistant.core.report.api.model.Result.Status.SUCCESS;
import static com.buschmais.jqassistant.core.test.matcher.ConstraintMatcher.constraint;
import static com.buschmais.jqassistant.core.test.matcher.ResultMatcher.result;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;

public class InjectionIT extends AbstractJavaPluginIT {

/**
* Verifies the concept "jee-injection:InjectionPoint".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void injectionPointIdentification() throws Exception {
scanClasses(BeanProducer.class);
scanClasses(BeanProducerWithConstraintViolations.class);
Result<Concept> conceptResult = applyConcept("jee-injection:InjectionPoint");
store.beginTransaction();
assertThat(conceptResult.getStatus(), equalTo(Result.Status.SUCCESS));
Expand All @@ -47,30 +44,28 @@ void injectionPointIdentification() throws Exception {
/**
* Verifies the constraint "jee-injection:BeanProducerMustNotBeInvokedDirectly".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void beanProducerAccess() throws Exception {
scanClasses(BeanProducer.class);
scanClasses(BeanProducerWithConstraintViolations.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:BeanProducerMustNotBeInvokedDirectly");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
assertThat(constraintResult.getRows().size(), equalTo(1));
assertThat(constraintResult.getRows().get(0).getColumns().get("Type").getLabel(), endsWith("test.set.BeanProducer"));
assertThat(constraintResult.getRows().get(0).getColumns().get("Type").getLabel(), endsWith("test.set.BeanProducerWithConstraintViolations"));
assertThat(constraintResult.getRows().get(0).getColumns().get("Invocation").getLabel(), endsWith("void beanProducerAccessor()"));
store.commitTransaction();
}

/**
* Verifies the constraint "jee-injection:FieldsOfInjectablesMustNotBeManipulated".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void injectableFieldManipulation() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, InjectableB.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, InjectableB.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:FieldsOfInjectablesMustNotBeManipulated");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -87,12 +82,11 @@ void injectableFieldManipulation() throws Exception {
/**
* Verifies the constraint "jee-injection:InjectablesShouldBeHeldInFinalFields".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void finalFieldsForInjectables() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, InjectableB.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, InjectableB.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:InjectablesShouldBeHeldInFinalFields");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -107,12 +101,11 @@ void finalFieldsForInjectables() throws Exception {
/**
* Verifies the constraint "jee-injection:InjectablesMustNotBeAccessedStatically".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void staticAccessOfInjectables() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, InjectableB.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, InjectableB.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:InjectablesMustNotBeAccessedStatically");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -126,12 +119,11 @@ void staticAccessOfInjectables() throws Exception {
/**
* Verifies the constraint "jee-injection:InjectablesMustNotBeHeldInStaticVariables".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void staticVariablesForInjectables() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, InjectableB.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, InjectableB.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:InjectablesMustNotBeHeldInStaticVariables");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -144,12 +136,11 @@ void staticVariablesForInjectables() throws Exception {
/**
* Verifies the constraint "jee-injection:InjectablesMustNotBeInstantiated".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void injectableInstantiation() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, InjectableB.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, InjectableB.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:InjectablesMustNotBeInstantiated");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -163,12 +154,11 @@ void injectableInstantiation() throws Exception {
/**
* Verifies the constraint "jee-injection:InjectablesMustOnlyBeHeldInInjectables".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void injectableOfNonInjectable() throws Exception {
scanClasses(BeanProducer.class, InjectableA.class, NonInjectableType.class);
scanClasses(BeanProducerWithConstraintViolations.class, InjectableA.class, NonInjectableType.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:InjectablesMustOnlyBeHeldInInjectables");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -181,12 +171,11 @@ void injectableOfNonInjectable() throws Exception {
/**
* Verifies the constraint "jee-injection:JdkClassesMustNotBeInjectables".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void jdkClassesAsInjectables() throws Exception {
scanClasses(BeanProducer.class);
scanClasses(BeanProducerWithConstraintViolations.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:JdkClassesMustNotBeInjectables");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
Expand All @@ -196,36 +185,49 @@ void jdkClassesAsInjectables() throws Exception {
}

/**
* Verifies the constraint "jee-injection:NoCombinationOfBeanProducersAndApplicationCode".
* Verifies the constraint "jee-injection:NoCombinationOfBeanProducersAndApplicationCode" with a test class with violations.
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void combinationOfBeanProducersAndApplicationCode() throws Exception {
scanClasses(BeanProducer.class);
void combinationOfBeanProducersAndApplicationCodeWithViolation() throws Exception {
scanClasses(BeanProducerWithConstraintViolations.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:NoCombinationOfBeanProducersAndApplicationCode");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
assertThat(constraintResult.getRows().size(), equalTo(1));
assertThat(constraintResult.getRows().get(0).getColumns().get("Java-Klasse").getLabel(), endsWith("test.set.BeanProducer"));
assertThat(constraintResult.getRows().get(0).getColumns().get("Java-Klasse").getLabel(), endsWith("test.set.BeanProducerWithConstraintViolations"));
store.commitTransaction();
}

/**
* Verifies the constraint "jee-injection:NoCombinationOfBeanProducersAndApplicationCode" with a test class without violations.
*
* @throws IOException If the test fails.
*/
@Test
void combinationOfBeanProducersAndApplicationCodeWithoutViolation() throws Exception {
scanClasses(BeanProducerWithoutConstraintViolations.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:NoCombinationOfBeanProducersAndApplicationCode");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(SUCCESS));
assertThat(constraintResult.getRows().size(), equalTo(0));
store.commitTransaction();
}

/**
* Verifies the constraint "jee-injection:BeansMustNotUseFieldInjectionExceptEJBs".
*
* @throws IOException
* If the test fails.
* @throws IOException If the test fails.
*/
@Test
void fieldInjectionWithEjb_Violation() throws Exception {
scanClasses(BeanProducer.class, LocalEjb.class);
scanClasses(BeanProducerWithConstraintViolations.class, LocalEjb.class);
Result<Constraint> constraintResult = validateConstraint("jee-injection:BeansMustNotUseFieldInjectionExceptEJBs");
store.beginTransaction();
assertThat(constraintResult.getStatus(), equalTo(Result.Status.FAILURE));
assertThat(constraintResult.getRows().size(), equalTo(1));
assertThat(constraintResult.getRows().get(0).getColumns().get("Type").getLabel(), endsWith("test.set.BeanProducer"));
assertThat(constraintResult.getRows().get(0).getColumns().get("Type").getLabel(), endsWith("test.set.BeanProducerWithConstraintViolations"));
assertThat(constraintResult.getRows().get(0).getColumns().get("Field").getLabel(), endsWith("Object injectionPointField"));
store.commitTransaction();
}
Expand All @@ -234,8 +236,7 @@ void fieldInjectionWithEjb_Violation() throws Exception {
* Verifies the constraint "jee-injection:BeansMustNotUseFieldInjectionExceptEJBs" results in no
* violations when applied to beans with setter or constructor injection.
*
* @throws java.io.IOException
* If the test fails.
* @throws java.io.IOException If the test fails.
*/
@Test
void fieldInjectionWithEjb_No_Violation() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

public class BeanProducer {
public class BeanProducerWithConstraintViolations {

@Inject
private Object injectionPointField;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jqassistant.plugin.jee.injection.test.set;

import javax.enterprise.inject.Produces;

public class BeanProducerWithoutConstraintViolations {

@Produces
public InjectableA beanProducerA(){
return new InjectableA();
}

}

0 comments on commit b7e2425

Please sign in to comment.