forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GeneralCodingRule to discourage use of field injection
Resolves TNG#288
- Loading branch information
1 parent
2e6f178
commit 8bfe5a8
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
archunit/src/test/java/com/tngtech/archunit/library/GeneralCodingRulesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.tngtech.archunit.library; | ||
|
||
import javax.annotation.Resource; | ||
import javax.inject.Inject; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import static com.tngtech.archunit.core.domain.TestUtils.importClasses; | ||
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_FIELD_INJECTION; | ||
import static com.tngtech.archunit.testutil.Assertions.assertThat; | ||
|
||
public class GeneralCodingRulesTest { | ||
|
||
@Test | ||
public void classesShouldNotUseFieldInjection_ClassWithoutFieldInjection() { | ||
JavaClasses classes = importClasses(ClassWithoutFieldInjection.class); | ||
assertThat(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).checking(classes).hasNoViolation(); | ||
} | ||
|
||
@Test | ||
public void classesShouldNotUseFieldInjection_ClassWithSpringAutowiredFieldInjection() { | ||
JavaClasses classes = importClasses(ClassWithSpringAutowiredFieldInjection.class); | ||
assertThat(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).checking(classes).hasViolations(1); | ||
} | ||
|
||
@Test | ||
public void classesShouldNotUseFieldInjection_ClassWithSpringValueFieldInjection() { | ||
JavaClasses classes = importClasses(ClassWithSpringValueFieldInjection.class); | ||
assertThat(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).checking(classes).hasViolations(1); | ||
} | ||
|
||
@Test | ||
public void classesShouldNotUseFieldInjection_ClassWithJakartaInjectFieldInjection() { | ||
JavaClasses classes = importClasses(ClassWithJakartaInjectFieldInjection.class); | ||
assertThat(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).checking(classes).hasViolations(1); | ||
} | ||
|
||
@Test | ||
public void classesShouldNotUseFieldInjection_ClassWithJakartaResourceFieldInjection() { | ||
JavaClasses classes = importClasses(ClassWithJakartaResourceFieldInjection.class); | ||
assertThat(NO_CLASSES_SHOULD_USE_FIELD_INJECTION).checking(classes).hasViolations(1); | ||
} | ||
|
||
private static class ClassWithoutFieldInjection { | ||
@SuppressWarnings("unused") | ||
private String value; | ||
} | ||
|
||
private static class ClassWithSpringAutowiredFieldInjection { | ||
@Autowired | ||
@SuppressWarnings("unused") | ||
private String value; | ||
} | ||
|
||
private static class ClassWithSpringValueFieldInjection { | ||
@Value("${name}") | ||
@SuppressWarnings("unused") | ||
private String value; | ||
} | ||
|
||
private static class ClassWithJakartaInjectFieldInjection { | ||
@Inject | ||
@SuppressWarnings("unused") | ||
private String value; | ||
} | ||
|
||
private static class ClassWithJakartaResourceFieldInjection { | ||
@Resource | ||
@SuppressWarnings("unused") | ||
private String value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters