15
15
16
16
package software .amazon .awssdk .archtests ;
17
17
18
+ import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .classes ;
18
19
import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .methods ;
20
+ import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .noClasses ;
21
+ import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .noFields ;
22
+ import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .noMethods ;
19
23
import static com .tngtech .archunit .library .freeze .FreezingArchRule .freeze ;
20
24
21
25
import com .tngtech .archunit .core .domain .JavaClasses ;
22
26
import com .tngtech .archunit .core .domain .JavaMethod ;
27
+ import com .tngtech .archunit .core .domain .JavaModifier ;
23
28
import com .tngtech .archunit .core .importer .ClassFileImporter ;
24
29
import com .tngtech .archunit .core .importer .ImportOption ;
25
30
import com .tngtech .archunit .junit .ArchTest ;
26
31
import com .tngtech .archunit .lang .ArchCondition ;
27
32
import com .tngtech .archunit .lang .ArchRule ;
28
33
import com .tngtech .archunit .lang .ConditionEvents ;
29
34
import com .tngtech .archunit .lang .SimpleConditionEvent ;
35
+ import java .io .IOException ;
30
36
import java .util .Arrays ;
31
37
import java .util .HashSet ;
38
+ import java .util .Optional ;
32
39
import java .util .Set ;
40
+ import java .util .concurrent .Future ;
33
41
import java .util .regex .Pattern ;
34
42
import org .junit .jupiter .api .Test ;
43
+ import software .amazon .awssdk .annotations .SdkPublicApi ;
35
44
import software .amazon .awssdk .utils .Logger ;
36
45
37
46
/**
@@ -58,41 +67,102 @@ public class CodingConventionWithSuppressionTest {
58
67
*/
59
68
private static final Set <Pattern > ALLOWED_ERROR_LOG_SUPPRESSION = new HashSet <>();
60
69
61
- @ Test
62
- void shouldNotAbuseWarnLog () {
63
- JavaClasses classes = new ClassFileImporter ()
64
- .withImportOptions (Arrays .asList (
65
- location -> ALLOWED_WARN_LOG_SUPPRESSION .stream ().noneMatch (location ::matches ),
66
- new ImportOption .Predefined .DoNotIncludeTests ()))
67
- .importPackages ("software.amazon.awssdk.." );
68
-
69
- ArchRule rule =
70
- freeze (methods ().that ().areDeclaredIn (Logger .class ).and ()
71
- .haveName ("warn" ).should (new MethodBeingUsedByOthers (
72
- "log.warn is detected" )))
73
- .as ("log.warn is detected. Review it with the team. If this is a valid case, add it"
74
- + " to ALLOWED_WARN_LOG_SUPPRESSION allowlist" );
75
-
76
- rule .check (classes );
77
- }
70
+ // @Test
71
+ // void publicApisShouldBeFinal() {
72
+ // JavaClasses classes = new ClassFileImporter()
73
+ // .withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
74
+ // .importPackages("software.amazon.awssdk");
75
+ // freeze(classes().that().areAnnotatedWith(SdkPublicApi.class)
76
+ // .and().areNotInterfaces()
77
+ // .should().haveModifier(JavaModifier.FINAL))
78
+ // .because("public APIs SHOULD be final")
79
+ // .check(classes);
80
+ // }
78
81
79
82
@ Test
80
- void shouldNotAbuseErrorLog () {
83
+ void shouldNotUseFuture () {
81
84
JavaClasses classes = new ClassFileImporter ()
82
- .withImportOptions (Arrays .asList (
83
- location -> ALLOWED_ERROR_LOG_SUPPRESSION .stream ().noneMatch (location ::matches ),
84
- new ImportOption .Predefined .DoNotIncludeTests ()))
85
- .importPackages ("software.amazon.awssdk.." );
86
-
87
- ArchRule rule =
88
- freeze (methods ().that ().areDeclaredIn (Logger .class ).and ()
89
- .haveName ("error" ).should (new MethodBeingUsedByOthers ("log.error is detected" )))
90
- .as ("log.error is detected. Review it with the team. If this is a valid case, add it to "
91
- + "ALLOWED_ERROR_LOG_SUPPRESSION allowlist" );
92
-
93
- rule .check (classes );
85
+ .withImportOptions (Arrays .asList (new ImportOption .Predefined .DoNotIncludeTests ()))
86
+ .importPackages ("software.amazon.awssdk" );
87
+ freeze (noClasses ().should ().dependOnClassesThat ().areAssignableFrom (Future .class )
88
+ .as ("use java.util.concurrent.Future" )
89
+ .because ("Future SHOULD NOT be used, use CompletableFuture instead" ))
90
+ .check (classes );
94
91
}
95
92
93
+ // @Test
94
+ // void shouldNotUseOptionalForFields() {
95
+ // JavaClasses classes = new ClassFileImporter()
96
+ // .withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
97
+ // .importPackages("software.amazon.awssdk");
98
+ // freeze(noFields().should().haveRawType(Optional.class)
99
+ // .as("use Optional for fields")
100
+ // .because("Optional SHOULD NOT be used for method parameters. See "
101
+ // + "https://github.com/aws/aws-sdk-java-v2/blob/master/docs"
102
+ // + "/design/UseOfOptional.md"))
103
+ // .check(classes);
104
+ // }
105
+ //
106
+ // @Test
107
+ // void mustNotUseOptionalForMethodParam() {
108
+ // JavaClasses classes = new ClassFileImporter()
109
+ // .withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
110
+ // .importPackages("software.amazon.awssdk");
111
+ // freeze(noMethods().should().haveRawParameterTypes(Optional.class)
112
+ // .as("use Optional for method parameters")
113
+ // .because("Optional MUST NOT be used for method parameters. See "
114
+ // + "https://github.com/aws/aws-sdk-java-v2/blob/master/docs/design/UseOfOptional.md"))
115
+ // .check(classes);
116
+ // }
117
+ //
118
+ // @Test
119
+ // void publicApisMustNotDeclareThrowableOfCheckedException() {
120
+ // JavaClasses classes = new ClassFileImporter()
121
+ // .withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
122
+ // .importPackages("software.amazon.awssdk");
123
+ // freeze(noMethods().that()
124
+ // .areDeclaredInClassesThat().areAnnotatedWith(SdkPublicApi.class)
125
+ // .should()
126
+ // .declareThrowableOfType(Exception.class).orShould().declareThrowableOfType(IOException.class)
127
+ // .because("public APIs MUST NOT throw checked exception"))
128
+ // .check(classes);
129
+ // }
130
+ //
131
+ // @Test
132
+ // void shouldNotAbuseWarnLog() {
133
+ // JavaClasses classes = new ClassFileImporter()
134
+ // .withImportOptions(Arrays.asList(
135
+ // location -> ALLOWED_WARN_LOG_SUPPRESSION.stream().noneMatch(location::matches),
136
+ // new ImportOption.Predefined.DoNotIncludeTests()))
137
+ // .importPackages("software.amazon.awssdk..");
138
+ //
139
+ // ArchRule rule =
140
+ // freeze(methods().that().areDeclaredIn(Logger.class).and()
141
+ // .haveName("warn").should(new MethodBeingUsedByOthers(
142
+ // "log.warn is detected")))
143
+ // .as("log.warn is detected. Review it with the team. If this is a valid case, add it"
144
+ // + " to ALLOWED_WARN_LOG_SUPPRESSION allowlist");
145
+ //
146
+ // rule.check(classes);
147
+ // }
148
+ //
149
+ // @Test
150
+ // void shouldNotAbuseErrorLog() {
151
+ // JavaClasses classes = new ClassFileImporter()
152
+ // .withImportOptions(Arrays.asList(
153
+ // location -> ALLOWED_ERROR_LOG_SUPPRESSION.stream().noneMatch(location::matches),
154
+ // new ImportOption.Predefined.DoNotIncludeTests()))
155
+ // .importPackages("software.amazon.awssdk..");
156
+ //
157
+ // ArchRule rule =
158
+ // freeze(methods().that().areDeclaredIn(Logger.class).and()
159
+ // .haveName("error").should(new MethodBeingUsedByOthers("log.error is detected")))
160
+ // .as("log.error is detected. Review it with the team. If this is a valid case, add it to "
161
+ // + "ALLOWED_ERROR_LOG_SUPPRESSION allowlist");
162
+ //
163
+ // rule.check(classes);
164
+ // }
165
+
96
166
private static final class MethodBeingUsedByOthers extends ArchCondition <JavaMethod > {
97
167
public MethodBeingUsedByOthers (String description ) {
98
168
super (description );
0 commit comments