diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java index 0d973749c7..66feca7d26 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryActionRoutingParityTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.core.common; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -49,7 +50,7 @@ class AwsQueryActionRoutingParityTest { } } - private static final String HANDLERS_ROOT = "src/main/java/io/github/hectorvent/floci/services/"; + private static final String HANDLERS_ROOT = MainSources.BASE_DIR.resolve("services") + "/"; /** * Every Query handler in the tree, with the service its actions must reach. diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java index 0e2029b58c..50a9325fab 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.core.common; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -28,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class PartitionSmokeInventoryTest { - private static final Path SERVICES = Path.of("src/main/java/io/github/hectorvent/floci/services"); + private static final Path SERVICES = MainSources.BASE_DIR.resolve("services"); private static final Path EXEMPTIONS = Path.of("src/test/resources/partition/smoke-exemptions.tsv"); private static final Pattern MINTS = Pattern.compile( "buildArn\\(|buildGlobalArn\\(|\\bArn\\.of\\(|\\bArn\\.global\\(|\\bnew (AwsArnUtils\\.)?Arn\\(" diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/PersistedModelReflectionTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/PersistedModelReflectionTest.java index bdd4019894..9e865f3372 100644 --- a/src/test/java/io/github/hectorvent/floci/core/storage/PersistedModelReflectionTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/storage/PersistedModelReflectionTest.java @@ -2,9 +2,11 @@ package io.github.hectorvent.floci.core.storage; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.testing.MainSources; import io.quarkus.runtime.annotations.RegisterForReflection; import org.junit.jupiter.api.Test; +import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.GenericArrayType; @@ -42,7 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class PersistedModelReflectionTest { - private static final String BASE_PACKAGE = "io.github.hectorvent.floci"; + private static final String BASE_PACKAGE = MainSources.BASE_PACKAGE; @Test void persistedTypesAreRegisteredForReflection() throws Exception { @@ -75,7 +77,7 @@ class PersistedModelReflectionTest { paths.filter(p -> p.toString().endsWith(".class")) .forEach(p -> { String name = classesDir.relativize(p).toString() - .replace(java.io.File.separatorChar, '.') + .replace(File.separatorChar, '.') .replaceAll("\\.class$", ""); if (!name.startsWith(BASE_PACKAGE)) { return; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java index a0e28bb9b8..a8af1a9802 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixtureTest.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.github.hectorvent.floci.services.scheduler.SchedulerService; import io.github.hectorvent.floci.services.sns.SnsService; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import java.lang.reflect.Method; @@ -114,8 +115,7 @@ class CfnProvisionerFixtureTest { */ @Test void everyProvisionerIsReachableThroughThePublicBuilderOrExplicitlyExempt() throws Exception { - Path provisioners = Path.of( - "src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners"); + Path provisioners = MainSources.packageDir(CfnResourceProvisioner.class); Set onDisk; try (Stream files = Files.list(provisioners)) { onDisk = files.map(f -> f.getFileName().toString()) diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java index 2a515c1404..28f5b55376 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnSchemaCoverageTest.java @@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.cloudformation; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResourceProvisioner; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -43,8 +45,7 @@ class CfnSchemaCoverageTest { private static final Path GAPS = Path.of("src/test/resources/cloudformation/getatt-attribute-gaps.tsv"); private static final Path SCHEMA_DIR = Path.of("local/aws/cfn-resource-schemas/us-east-1"); - private static final Path PROVISIONERS = Path.of( - "src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners"); + private static final Path PROVISIONERS = MainSources.packageDir(CfnResourceProvisioner.class); private static final Pattern PUT_ATTRIBUTE = Pattern.compile("getAttributes\\(\\)\\.put\\(\\s*\"([^\"]+)\""); diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/SecurityMaterialRandomnessTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/SecurityMaterialRandomnessTest.java index d96141ec59..9c349296d1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/SecurityMaterialRandomnessTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/SecurityMaterialRandomnessTest.java @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.services.iam; +import io.github.hectorvent.floci.services.kms.KmsService; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -18,12 +20,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class SecurityMaterialRandomnessTest { - private static final Path IAM_SERVICE = - Path.of("src/main/java/io/github/hectorvent/floci/services/iam/IamService.java"); - private static final Path STS_HANDLER = - Path.of("src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java"); - private static final Path KMS_SERVICE = - Path.of("src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java"); + private static final Path IAM_SERVICE = MainSources.sourceFile(IamService.class); + private static final Path STS_HANDLER = MainSources.sourceFile(StsQueryHandler.class); + private static final Path KMS_SERVICE = MainSources.sourceFile(KmsService.class); private static final Pattern RANDOM_SECRET_DECLARATION = Pattern.compile("private\\s+String\\s+randomSecret\\s*\\(int\\s+length\\)"); diff --git a/src/test/java/io/github/hectorvent/floci/testing/MainSources.java b/src/test/java/io/github/hectorvent/floci/testing/MainSources.java new file mode 100644 index 0000000000..79727d97b8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/MainSources.java @@ -0,0 +1,37 @@ +package io.github.hectorvent.floci.testing; + +import java.nio.file.Path; + +/** + * Paths into {@code src/main/java} for tests that read Floci's own sources. They are derived from + * classes rather than written out, so they keep pointing at the right files when the package moves. + */ +public final class MainSources { + + /** Floci's root package: the package this class sits in, minus {@code .testing}. */ + public static final String BASE_PACKAGE = + MainSources.class.getPackageName().substring(0, MainSources.class.getPackageName().lastIndexOf('.')); + + /** The source directory of {@link #BASE_PACKAGE}. */ + public static final Path BASE_DIR = packageDir(BASE_PACKAGE); + + private MainSources() { + } + + /** The source directory of the given class's package. */ + public static Path packageDir(Class type) { + return packageDir(type.getPackageName()); + } + + /** The source file of a top-level class. */ + public static Path sourceFile(Class type) { + if (type.getEnclosingClass() != null) { + throw new IllegalArgumentException(type.getName() + " is nested; pass its top-level class"); + } + return packageDir(type).resolve(type.getSimpleName() + ".java"); + } + + private static Path packageDir(String packageName) { + return Path.of("src/main/java", packageName.replace('.', '/')); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/testing/MainSourcesTest.java b/src/test/java/io/github/hectorvent/floci/testing/MainSourcesTest.java new file mode 100644 index 0000000000..ce16aa1604 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/MainSourcesTest.java @@ -0,0 +1,31 @@ +package io.github.hectorvent.floci.testing; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MainSourcesTest { + + @Test + void basePackageIsTheParentOfConfig() { + assertEquals(EmulatorConfig.class.getPackageName(), MainSources.BASE_PACKAGE + ".config"); + assertTrue(Files.isDirectory(MainSources.BASE_DIR.resolve("services")), MainSources.BASE_DIR.toString()); + } + + @Test + void sourceFileOfATopLevelClassExists() { + assertTrue(Files.isRegularFile(MainSources.sourceFile(EmulatorConfig.class)), + MainSources.sourceFile(EmulatorConfig.class).toString()); + assertEquals(MainSources.BASE_DIR.resolve("config"), MainSources.packageDir(EmulatorConfig.class)); + } + + @Test + void sourceFileRejectsANestedClass() { + assertThrows(IllegalArgumentException.class, () -> MainSources.sourceFile(EmulatorConfig.ServicesConfig.class)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java b/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java index 9e2a6d3c8f..2c0f958eb9 100644 --- a/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java +++ b/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.tools.ami; import io.github.hectorvent.floci.core.common.Digests; +import io.github.hectorvent.floci.testing.MainSources; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -149,7 +150,7 @@ class AmiImageToolTest { @Test void toolTreeDoesNotContainShellScripts() throws Exception { - Path tools = Path.of("src/main/java/io/github/hectorvent/floci/tools/ami"); + Path tools = MainSources.packageDir(AmiImageTool.class); if (Files.isDirectory(tools)) { try (Stream stream = Files.walk(tools)) { assertFalse(stream.anyMatch(path -> path.getFileName().toString().endsWith(".sh")));