Skip to content

Commit

Permalink
Use junit directly
Browse files Browse the repository at this point in the history
  • Loading branch information
zoewangg committed Dec 12, 2024
1 parent 20366bb commit 1e6dfc2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

@AnalyzeClasses(packages = "software.amazon.awssdk..",
importOptions = ImportOption.DoNotIncludeTests.class)
@ArchIgnore
public class CodingConventionTest {

@ArchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@

package software.amazon.awssdk.archtests;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noFields;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noMethods;
import static com.tngtech.archunit.library.freeze.FreezingArchRule.freeze;

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.Logger;

/**
Expand All @@ -58,6 +67,67 @@ public class CodingConventionWithSuppressionTest {
*/
private static final Set<Pattern> ALLOWED_ERROR_LOG_SUPPRESSION = new HashSet<>();

@Test
void publicApisShouldBeFinal() {
JavaClasses classes = new ClassFileImporter()
.withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
.importPackages("software.amazon.awssdk");
freeze(classes().that().areAnnotatedWith(SdkPublicApi.class)
.and().areNotInterfaces()
.should().haveModifier(JavaModifier.FINAL))
.because("public APIs SHOULD be final")
.check(classes);
}

@Test
void shouldNotUseFuture() {
JavaClasses classes = new ClassFileImporter()
.withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
.importPackages("software.amazon.awssdk");
freeze(noClasses().should().dependOnClassesThat().areAssignableFrom(Future.class)
.as("use java.util.concurrent.Future")
.because("Future SHOULD NOT be used, use CompletableFuture instead"))
.check(classes);
}

@Test
void shouldNotUseOptionalForFields() {
JavaClasses classes = new ClassFileImporter()
.withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
.importPackages("software.amazon.awssdk");
freeze(noFields().should().haveRawType(Optional.class)
.as("use Optional for fields")
.because("Optional SHOULD NOT be used for method parameters. See "
+ "https://github.com/aws/aws-sdk-java-v2/blob/master/docs"
+ "/design/UseOfOptional.md"))
.check(classes);
}

@Test
void mustNotUseOptionalForMethodParam() {
JavaClasses classes = new ClassFileImporter()
.withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
.importPackages("software.amazon.awssdk");
freeze(noMethods().should().haveRawParameterTypes(Optional.class)
.as("use Optional for method parameters")
.because("Optional MUST NOT be used for method parameters. See "
+ "https://github.com/aws/aws-sdk-java-v2/blob/master/docs/design/UseOfOptional.md"))
.check(classes);
}

@Test
void publicApisMustNotDeclareThrowableOfCheckedException() {
JavaClasses classes = new ClassFileImporter()
.withImportOptions(Arrays.asList(new ImportOption.Predefined.DoNotIncludeTests()))
.importPackages("software.amazon.awssdk");
freeze(noMethods().that()
.areDeclaredInClassesThat().areAnnotatedWith(SdkPublicApi.class)
.should()
.declareThrowableOfType(Exception.class).orShould().declareThrowableOfType(IOException.class)
.because("public APIs MUST NOT throw checked exception"))
.check(classes);
}

@Test
void shouldNotAbuseWarnLog() {
JavaClasses classes = new ClassFileImporter()
Expand Down

0 comments on commit 1e6dfc2

Please sign in to comment.