Skip to content

Commit e151deb

Browse files
authored
Merge pull request wildfly-extras#25 from jamezp/domain-refactor
Refactor domain support and use better tag names
2 parents a1e9fb2 + 11bf7b5 commit e151deb

File tree

50 files changed

+428
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+428
-201
lines changed

api/src/main/java/org/wildfly/testing/junit/condition/RequiresModuleExecutionCondition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ private ConditionEvaluationResult checkVersion(final RequiresModule requiresModu
121121
// Likely indicates the version could not be resolved.
122122
if (version.isBlank()) {
123123
return ConditionEvaluationResult
124-
.enabled(String.format("Could not determine version of module %s", moduleDefinition.path));
124+
.enabled("Could not determine version of module %s".formatted(moduleDefinition.path));
125125
}
126126
if (isAtLeastVersion(requiresModule.minVersion(), version)) {
127127
return ConditionEvaluationResult
128-
.enabled(String.format("Found version %s and required a minimum of version %s. Enabling tests.",
128+
.enabled("Found version %s and required a minimum of version %s. Enabling tests.".formatted(
129129
version, requiresModule.minVersion()));
130130
}
131131
return ConditionEvaluationResult
@@ -218,7 +218,7 @@ private static boolean isAtLeastVersion(final String minVersion, final String fo
218218
}
219219

220220
private static String formatReason(final RequiresModule requiresModule, final String fmt, final Object... args) {
221-
String msg = String.format(fmt, args);
221+
String msg = fmt.formatted(args);
222222
if (!requiresModule.issueRef().isBlank()) {
223223
msg = requiresModule.issueRef() + ": " + msg;
224224
}

api/src/test/java/org/wildfly/testing/junit/RequiresModuleTestCase.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public void artifactSkippedMissingModule(@JBossHome final Path jbossHome) {
7070
testEvents.assertStatistics((stats) -> stats.skipped(1L));
7171
testEvents.assertThatEvents().haveExactly(1, EventConditions.event(
7272
EventConditions.skippedWithReason(
73-
String.format(
74-
"Module org.wildfly.testing.junit.test.artifact.invalid not found in %s. Disabling test.",
73+
"Module org.wildfly.testing.junit.test.artifact.invalid not found in %s. Disabling test.".formatted(
7574
jbossHome.resolve("modules")))));
7675
}
7776

@@ -120,9 +119,9 @@ public void resourceRootSkippedMissingModule(@JBossHome final Path jbossHome) {
120119
testEvents.assertStatistics((stats) -> stats.skipped(1L));
121120
testEvents.assertThatEvents().haveExactly(1, EventConditions.event(
122121
EventConditions.skippedWithReason(
123-
String.format(
124-
"Module org.wildfly.testing.junit.test.resource-root.invalid not found in %s. Disabling test.",
125-
jbossHome.resolve("modules")))));
122+
"Module org.wildfly.testing.junit.test.resource-root.invalid not found in %s. Disabling test."
123+
.formatted(
124+
jbossHome.resolve("modules")))));
126125
}
127126

128127
@Test
@@ -269,7 +268,7 @@ private static void createJar(final String moduleName, final Path jbossHome, fin
269268
// Create the JAR with a manifest only
270269
final Path jarPath = jbossHome.resolve(
271270
Path.of("modules", "org", "wildfly", "testing", "junit", "test", moduleName, "main",
272-
String.format("test-%s.jar", version)));
271+
"test-%s.jar".formatted(version)));
273272
final Manifest manifest = new Manifest();
274273
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
275274
manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VERSION, version);

docs/src/main/asciidoc/extension/index.adoc

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ public class MyTest {
8282
}
8383
----
8484

85+
=== Test Tags
86+
87+
Tests are automatically tagged to allow selective execution:
88+
89+
* `@WildFlyTest` - tagged with `standalone`
90+
* `@WildFlyDomainTest` - tagged with `domain`
91+
* `@ManualMode` - tagged with `manual`
92+
93+
Examples:
94+
95+
[source,bash]
96+
----
97+
# Run only standalone tests
98+
mvn test -Dgroups=standalone
99+
100+
# Run only domain tests
101+
mvn test -Dgroups=domain
102+
103+
# Run only manual mode tests
104+
mvn test -Dgroups=manual
105+
106+
# Run standalone automatic mode tests (not manual)
107+
mvn test -Dgroups='standalone & !manual'
108+
109+
# Run all non-domain tests
110+
mvn test -Dgroups='!domain'
111+
----
112+
85113
== Creating Deployments
86114

87115
=== @DeploymentProducer
@@ -219,11 +247,11 @@ Test deployments on a domain controller:
219247

220248
[source,java]
221249
----
222-
@WildFlyTest
223-
@Domain("main-server-group")
250+
@WildFlyDomainTest
224251
public class DomainTest {
225252
226253
@DeploymentProducer
254+
@ServerGroup("main-server-group")
227255
public static WebArchive deployment() {
228256
return ShrinkWrap.create(WebArchive.class)
229257
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
@@ -267,10 +295,11 @@ To use domain mode testing:
267295
----
268296

269297
3. **Use the correct annotations**:
270-
- `@Domain("server-group-name")` on the test class (you can specify multiple server groups: `@Domain({"group1", "group2"})`)
298+
- `@WildFlyDomainTest` on the test class
299+
- `@ServerGroup("server-group-name")` on the deployment method (you can specify multiple server groups: `@ServerGroup({"group1", "group2"})`)
271300
- `@DomainServer("server-name")` on `@ServerResource` fields to target specific servers
272301

273-
NOTE: Domain tests are automatically tagged with `@Tag("domain-test")`. You can use this to run domain tests separately: `mvn test -Dgroups=domain-test`
302+
NOTE: Domain tests are automatically tagged with `@Tag("domain")`. You can use this to run domain tests separately: `mvn test -Dgroups=domain`
274303

275304
== ServerManager API
276305

docs/src/main/asciidoc/extension/troubleshooting.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,15 @@ war.addClass(SpecificClass1.class)
288288

289289
1. **Server Group Doesn't Exist**
290290
+
291-
Verify the server group name matches your domain configuration:
291+
Verify the server group name on your deployment method matches your domain configuration:
292292
+
293293
[source,java]
294294
----
295-
@Domain("main-server-group") // Must match domain.xml
295+
@DeploymentProducer
296+
@ServerGroup("main-server-group") // Must match domain.xml
297+
public static WebArchive deployment() {
298+
return ShrinkWrap.create(WebArchive.class);
299+
}
296300
----
297301

298302
2. **Server Not in Group**

extension/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<goal>verify</goal>
117117
</goals>
118118
<configuration>
119-
<excludedGroups>domain-test</excludedGroups>
119+
<excludedGroups>domain</excludedGroups>
120120
</configuration>
121121
</execution>
122122
<execution>
@@ -126,7 +126,7 @@
126126
<goal>verify</goal>
127127
</goals>
128128
<configuration>
129-
<groups>domain-test</groups>
129+
<groups>domain</groups>
130130
</configuration>
131131
</execution>
132132
</executions>

extension/src/main/java/org/wildfly/testing/junit/extension/DeploymentManagerProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Object produce(final ExtensionContext context, final Class<?> clazz, fina
3030
throws IllegalArgumentException {
3131
if (!DeploymentManager.class.isAssignableFrom(clazz)) {
3232
throw new IllegalArgumentException(
33-
String.format("Type %s is not assignable to %s", clazz.getName(), DeploymentManager.class.getName()));
33+
"Type %s is not assignable to %s".formatted(clazz.getName(), DeploymentManager.class.getName()));
3434
}
3535

3636
// Get the server from WildFlyExtension and return as managed which will not allow shutting down the server

extension/src/main/java/org/wildfly/testing/junit/extension/ModelControllerClientProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Object produce(final ExtensionContext context, final Class<?> clazz, fina
3030
throws IllegalArgumentException {
3131
if (!ModelControllerClient.class.isAssignableFrom(clazz)) {
3232
throw new IllegalArgumentException(
33-
String.format("Type %s is not assignable to %s", clazz.getName(), ModelControllerClient.class.getName()));
33+
"Type %s is not assignable to %s".formatted(clazz.getName(), ModelControllerClient.class.getName()));
3434
}
3535

3636
// Get the server from WildFlyExtension and return as managed which will not allow shutting down the server

extension/src/main/java/org/wildfly/testing/junit/extension/ServerManagerProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Object produce(final ExtensionContext context, final Class<?> clazz, fina
3030
throws IllegalArgumentException {
3131
if (!ServerManager.class.isAssignableFrom(clazz)) {
3232
throw new IllegalArgumentException(
33-
String.format("Type %s is not assignable to %s", clazz.getName(), ServerManager.class.getName()));
33+
"Type %s is not assignable to %s".formatted(clazz.getName(), ServerManager.class.getName()));
3434
}
3535

3636
// Get the server from WildFlyExtension and return as managed which will not allow shutting down the server

extension/src/main/java/org/wildfly/testing/junit/extension/ServerResourceExtension.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Object resolveParameter(final ParameterContext parameterContext, final Ex
8787
.getType(), parameterContext.getParameter().getAnnotations());
8888
} catch (Throwable e) {
8989
throw new ParameterResolutionException(
90-
String.format("Failed to resolve parameter '%s'.", parameterContext.getParameter()), e);
90+
"Failed to resolve parameter '%s'.".formatted(parameterContext.getParameter()), e);
9191
}
9292
}
9393

@@ -105,7 +105,7 @@ private void injectFields(final ExtensionContext context, final Object testInsta
105105
AnnotationSupport.findAnnotatedFields(testClass, ServerResource.class, predicate).forEach(field -> {
106106
if (Modifier.isFinal(field.getModifiers())) {
107107
throw new ExtensionConfigurationException(
108-
String.format("Field '%s' cannot be final for injection.", field));
108+
"Field '%s' cannot be final for injection.".formatted(field));
109109
}
110110
// Find the producer which can provide this parameter
111111
ServerResourceProducer injectionProducer = null;
@@ -117,7 +117,7 @@ private void injectFields(final ExtensionContext context, final Object testInsta
117117
}
118118
if (injectionProducer == null) {
119119
throw new ExtensionConfigurationException(
120-
String.format("Could not find InjectionProducer for field '%s' of type %s.", field, field.getType()
120+
"Could not find InjectionProducer for field '%s' of type %s.".formatted(field, field.getType()
121121
.getName()));
122122
}
123123
try {
@@ -126,14 +126,14 @@ private void injectFields(final ExtensionContext context, final Object testInsta
126126
field.set(testInstance, value);
127127
} else {
128128
throw new ParameterResolutionException(
129-
String.format("Could not make field %s accessible for injection.", field));
129+
"Could not make field %s accessible for injection.".formatted(field));
130130
}
131131
} catch (Throwable e) {
132-
if (e instanceof ParameterResolutionException) {
133-
throw (ParameterResolutionException) e;
132+
if (e instanceof ParameterResolutionException pre) {
133+
throw pre;
134134
}
135135
throw new ParameterResolutionException(
136-
String.format("Could not make field %s accessible for injection.", field), e);
136+
"Could not make field %s accessible for injection.".formatted(field), e);
137137
}
138138
});
139139
}

extension/src/main/java/org/wildfly/testing/junit/extension/TestSupport.java

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,41 @@ static Optional<Archive<?>> findDeploymentMethod(final ExtensionContext context)
7070
final var method = validate(testClass, methods);
7171
// This must be a void return type
7272
if (!method.getReturnType().equals(Void.TYPE)) {
73-
throw new JUnitException(String.format("Method '%s' must return void", method));
73+
throw new JUnitException("Method '%s' must return void".formatted(method));
7474
}
7575
final int count = method.getParameterCount();
7676
if (count > 2) {
7777
throw new JUnitException(
78-
String.format("Method %s has too many parameters. Only two parameters are allowed.", method));
78+
"Method %s has too many parameters. Only two parameters are allowed.".formatted(method));
7979
}
8080
if (count == 0) {
81-
throw new JUnitException(String.format("Method '%s' must have at least one parameter.", method));
81+
throw new JUnitException("Method '%s' must have at least one parameter.".formatted(method));
8282
}
8383
final var testDeployment = method.getAnnotation(GenerateDeployment.class);
8484
final var parameterTypes = method.getParameterTypes();
8585
final var deploymentType = testDeployment.value() == GenerateDeployment.DeploymentType.INFER
86-
? inferredType(parameterTypes[0]).orElseThrow(() -> new JUnitException(String
87-
.format("Could not infer the type to create for argument %s on method %s. " +
88-
"If this is a custom %s type, please consider using a @DeploymentProducer", parameterTypes[0],
89-
method, Archive.class.getName())))
86+
? inferredType(parameterTypes[0]).orElseThrow(() -> new JUnitException(
87+
("Could not infer the type to create for argument %s on method %s. " +
88+
"If this is a custom %s type, please consider using a @DeploymentProducer")
89+
.formatted(parameterTypes[0], method, Archive.class.getName())))
9090
: testDeployment.value();
9191
// Check that first parameter is an Archive type
9292
if (!Archive.class.isAssignableFrom(parameterTypes[0])) {
93-
throw new JUnitException(String.format(
94-
"@GenerateDeployment method %s must have an Archive type as the first parameter, but was %s",
95-
method.getName(), parameterTypes[0].getName()));
93+
throw new JUnitException(
94+
"@GenerateDeployment method %s must have an Archive type as the first parameter, but was %s"
95+
.formatted(method.getName(), parameterTypes[0].getName()));
9696
}
9797
// Validate the parameter type is assignable from the deployment type
9898
if (!deploymentType.archiveType().isAssignableFrom(parameterTypes[0])) {
99-
throw new JUnitException(String.format("Parameter '%s' must be assignable from '%s'", parameterTypes[0],
99+
throw new JUnitException("Parameter '%s' must be assignable from '%s'".formatted(parameterTypes[0],
100100
deploymentType.archiveType()));
101101
}
102102
final TestInfo testInfo;
103103
// Check the second parameter if applicable
104104
if (parameterTypes.length == 2) {
105105
if (!TestInfo.class.isAssignableFrom(parameterTypes[1])) {
106106
throw new JUnitException(
107-
String.format("Parameter '%s' must be assignable from '%s'", parameterTypes[1], TestInfo.class));
107+
"Parameter '%s' must be assignable from '%s'".formatted(parameterTypes[1], TestInfo.class));
108108
}
109109
testInfo = new ParameterTestInfo(context);
110110
} else {
@@ -121,7 +121,7 @@ static Optional<Archive<?>> findDeploymentMethod(final ExtensionContext context)
121121

122122
return Optional.of(archive);
123123
} catch (Exception e) {
124-
throw new JUnitException(String.format("Failed to execute deployment method in %s: %s", method.getName(), method),
124+
throw new JUnitException("Failed to execute deployment method in %s: %s".formatted(method.getName(), method),
125125
e);
126126
}
127127
}
@@ -138,17 +138,18 @@ static Optional<Archive<?>> findDeploymentProducerMethod(final ExtensionContext
138138
// The return type must be an Archive<?> of some type
139139
if (!Archive.class.isAssignableFrom(method.getReturnType())) {
140140
throw new JUnitException(
141-
String.format("Method '%s' must return assignable from %s", method, Archive.class.getName()));
141+
"Method '%s' must return assignable from %s".formatted(method, Archive.class.getName()));
142142
}
143143
// A single parameter of type TestInfo is allowed, but not required
144144
final var parameterTypes = method.getParameterTypes();
145145
if (parameterTypes.length > 1) {
146-
throw new JUnitException(String.format(
147-
"Method %s has too many parameters. Only one parameter of type %s is allowed.", method, TestInfo.class));
146+
throw new JUnitException(
147+
"Method %s has too many parameters. Only one parameter of type %s is allowed.".formatted(method,
148+
TestInfo.class));
148149
}
149150
if (parameterTypes.length == 1 && !TestInfo.class.isAssignableFrom(parameterTypes[0])) {
150-
throw new JUnitException(String.format(
151-
"Method %s parameter must be of type %s, but was %s.", method, TestInfo.class, parameterTypes[0]));
151+
throw new JUnitException(
152+
"Method %s parameter must be of type %s, but was %s.".formatted(method, TestInfo.class, parameterTypes[0]));
152153
}
153154

154155
final TestInfo testInfo;
@@ -167,7 +168,7 @@ static Optional<Archive<?>> findDeploymentProducerMethod(final ExtensionContext
167168
}
168169
return Optional.of(archive);
169170
} catch (Exception e) {
170-
throw new JUnitException(String.format("Failed to execute deployment method in %s: %s", method.getName(), method),
171+
throw new JUnitException("Failed to execute deployment method in %s: %s".formatted(method.getName(), method),
171172
e);
172173
}
173174
}
@@ -176,15 +177,15 @@ private static Method validate(final Class<?> testClass, final List<Method> meth
176177
// Only one deployment method is allowed
177178
if (methods.size() > 1) {
178179
throw new JUnitException(
179-
String.format("Found more than one deployment method in %s: %s", testClass.getName(), methods));
180+
"Found more than one deployment method in %s: %s".formatted(testClass.getName(), methods));
180181
}
181182
final var method = methods.get(0);
182183
if (!Modifier.isStatic(method.getModifiers())) {
183184
throw new JUnitException(
184-
String.format("Deployment method %s in type %s must be static.", testClass.getName(), method));
185+
"Deployment method %s in type %s must be static.".formatted(testClass.getName(), method));
185186
}
186187
if (!method.trySetAccessible()) {
187-
throw new JUnitException(String.format("Method '%s' is not accessible", method));
188+
throw new JUnitException("Method '%s' is not accessible".formatted(method));
188189
}
189190
return method;
190191
}
@@ -229,6 +230,34 @@ public String toString() {
229230

230231
}
231232

233+
/**
234+
* Finds the deployment method (either {@link GenerateDeployment} or {@link DeploymentProducer})
235+
* and returns the Method reference without invoking it.
236+
*
237+
* @param context the extension context
238+
*
239+
* @return the deployment method, or empty if no deployment method exists
240+
*/
241+
static Optional<Method> findDeploymentMethodReference(final ExtensionContext context) {
242+
final var testClass = context.getRequiredTestClass();
243+
244+
// Check for @GenerateDeployment
245+
final var generateMethods = AnnotationSupport.findAnnotatedMethods(testClass, GenerateDeployment.class,
246+
HierarchyTraversalMode.BOTTOM_UP);
247+
if (!generateMethods.isEmpty()) {
248+
return Optional.of(validate(testClass, generateMethods));
249+
}
250+
251+
// Check for @DeploymentProducer
252+
final var producerMethods = AnnotationSupport.findAnnotatedMethods(testClass, DeploymentProducer.class,
253+
HierarchyTraversalMode.BOTTOM_UP);
254+
if (!producerMethods.isEmpty()) {
255+
return Optional.of(validate(testClass, producerMethods));
256+
}
257+
258+
return Optional.empty();
259+
}
260+
232261
private static Optional<GenerateDeployment.DeploymentType> inferredType(final Class<?> methodParameterType) {
233262
for (final var type : GenerateDeployment.DeploymentType.values()) {
234263
if (type == GenerateDeployment.DeploymentType.INFER) {

0 commit comments

Comments
 (0)