chore(test): derive main source paths from classes (#6592)

Seven tests read Floci's own sources through paths written out as
src/main/java/io/github/hectorvent/floci/..., and
PersistedModelReflectionTest named the root package as a string. A new
MainSources helper derives them from classes instead (the root package
from its own package), so they keep pointing at the right files when
the package moves to io.floci.aws. PersistedModelReflectionTest also
imports File instead of qualifying it inline.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-26 06:23:13 -04:00
1 parent 51ce672218
commit e8adca56e6
9 files changed
+88 -15

No files matched your search

@@ -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.
@@ -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\\("
@@ -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;
@@ -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<String> onDisk;
try (Stream<Path> files = Files.list(provisioners)) {
onDisk = files.map(f -> f.getFileName().toString())
@@ -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*\"([^\"]+)\"");
@@ -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\\)");
@@ -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('.', '/'));
}
}
@@ -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));
}
}
@@ -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<Path> stream = Files.walk(tools)) {
assertFalse(stream.anyMatch(path -> path.getFileName().toString().endsWith(".sh")));