Resource naming gives temporary external resources structured identities instead of anonymous random names. In integration tests, that structure preserves isolation while making stale queues, buckets, and other resources discoverable for cleanup. section 12.6
A practical naming scheme combines:
- A stable root identifying the integration-test namespace. section 12.6
- Owner, suite, and test identifiers that support cleanup at increasingly narrow scopes. section 12.6
- A random suffix that prevents collisions and isolates each test run from prior failures. section 12.6
Listing 12.15 An example approach to name spacing resources
record Namespace(String owner, String suite, String testName){ #A
static String ROOT = "integ"; #B
}
class Scope {
public static String everything(Namespace ns) { #C
return Namespace.ROOT;
}
public static String ownedBy(Namespace ns) { #C
return String.join("-", Namespace.ROOT, ns.owner);
}
public static String thisSuite(Namespace ns) { #C
return String.join("-", Namespace.ROOT, ns.owner, ns.suite);
}
public static String thisTest(Namespace ns) { #C
return String.join("-", Namespace.ROOT, ns.owner, ns.suite, ns.testName);
}
}
String randomName(Namespace namespace) { #D
return format(
"%s-%s",
Scope.thisTest(namespace),
UUID.randomUUID().toString().substring(0, 8)
);
}The Namespace and Scope types make cleanup boundaries explicit, while randomName combines the test scope with a short UUID suffix. section 12.6
Listing 12.16 Supporting code
class InfraTooling {
private CloudSDK cloudSDK;
private Namespace namespace;
Queue createQueue() {
return cloudSDK.createQueue(randomName(namespace));
}
Bucket createBucket(){
return cloudSDK.createBucket(randomName(namespace));
}
void cleanup(Function<Namespace, String> prefix) {
deleteQueues(prefix);
deleteBuckets(prefix);
deleteWhatever(prefix);
}
void deleteQueues(Function<Namespace, String> strat) {
for (String name : cloudSDK.listQueues()) {
if (name.startsWith(strat.apply(namespace))) {
cloudSDK.deleteQueue(name);
}
}
}
}
@Test
void myCoolIntegrationTest() {
String owner = myDependencyInjector.environment();
Namespace namespace = new Namespace(
owner,
"Chapter12",
"myCoolIntegrationTest");
InfraTooling infra = new InfraTooling(cloudSDK, namespace);
Queue inputQueue = infra.createQueue();
Queue outputQueue = infra.createQueue();
Bucket bucket = infra.createBucket();
// test code here
infra.cleanup(Scope::thisTest);
}Support code centralizes resource creation and deletes resources whose names begin with a selected namespace prefix. section 12.6
