Skip to content

Commit 7f6ec0c

Browse files
committed
feat: enable targeting a test process by its process identifier
1 parent fd2adaf commit 7f6ec0c

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

testprocesses-core/src/main/java/io/github/netmikey/testprocesses/TestProcessDefinitionBy.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class TestProcessDefinitionBy<T extends TestProcessDefinition> {
3535

3636
private Optional<String> beanName = Optional.empty();
3737

38+
private Optional<String> processIdentifier = Optional.empty();
39+
3840
/**
3941
* Address a test process definition by its concrete type.
4042
*
@@ -82,6 +84,23 @@ public static TestProcessDefinitionBy<? extends TestProcessDefinition> beanName(
8284
return result;
8385
}
8486

87+
/**
88+
* Address a test process definition by its process identifier.
89+
*
90+
* @param testProcessIdentifier
91+
* The identifier returned by
92+
* {@link TestProcessDefinition#getProcessIdentifier()}.
93+
* @return The {@link TestProcessDefinitionBy} reference to the test process
94+
* with the specified testProcessIdentifier.
95+
*/
96+
public static TestProcessDefinitionBy<? extends TestProcessDefinition> processIdentifier(
97+
String testProcessIdentifier) {
98+
99+
TestProcessDefinitionBy<? extends TestProcessDefinition> result = new TestProcessDefinitionBy<>();
100+
result.processIdentifier = Optional.of(testProcessIdentifier);
101+
return result;
102+
}
103+
85104
/**
86105
* Get the clazz.
87106
*
@@ -109,4 +128,12 @@ public Optional<String> getBeanName() {
109128
return beanName;
110129
}
111130

131+
/**
132+
* Get the processIdentifier.
133+
*
134+
* @return Returns the processIdentifier.
135+
*/
136+
public Optional<String> getProcessIdentifier() {
137+
return processIdentifier;
138+
}
112139
}

testprocesses-core/src/main/java/io/github/netmikey/testprocesses/TestProcessesRegistry.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,60 @@ private <T extends TestProcessDefinition> T retrieve(TestProcessDefinitionBy<T>
352352
+ matchingBeans.keySet().stream().collect(Collectors.joining(", ")));
353353
}
354354

355+
result = matchingBeans.values()
356+
.stream()
357+
.findFirst()
358+
.get();
359+
}
360+
} else if (testProcessDefinitionBy.getProcessIdentifier().isPresent()) {
361+
String processIdentifier = testProcessDefinitionBy.getProcessIdentifier().get();
362+
363+
// First, try to find a match within the currently running processes
364+
Map<String, T> matchingRunning = runningProcesses.entrySet().stream()
365+
.filter(p -> processIdentifier.equals(p.getValue().getDefinition().getProcessIdentifier()))
366+
.collect(Collectors.toMap(
367+
e -> "instance " + e.getValue().getDefinition() + " with identifier "
368+
+ e.getValue().getDefinition().getProcessIdentifier(),
369+
e -> (T) e.getValue().getDefinition()));
370+
371+
// Note that this shouldn't happen as only one process with a given
372+
// processIdentifier should run at any time, but check for good
373+
// measure...
374+
if (matchingRunning.size() > 1) {
375+
throw new TooManyTestProcessDefinitionsException("More than one TestProcessDefinition bean with "
376+
+ "identifier " + processIdentifier + " were found to be running. Consider referencing your "
377+
+ "test process using the class, instance or bean name rather than the process identifier. "
378+
+ "Found beans were: " + matchingRunning.keySet().stream().collect(Collectors.joining(", ")));
379+
}
380+
381+
if (matchingRunning.size() == 1) {
382+
result = matchingRunning.values()
383+
.stream()
384+
.findFirst()
385+
.get();
386+
} else {
387+
// If no matching definition is running, try to find a matching
388+
// Spring bean
389+
390+
Map<String, T> matchingBeans = testProcessDefinitionBeans.entrySet().stream()
391+
.filter(b -> processIdentifier.equals(b.getValue().getProcessIdentifier()))
392+
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue()));
393+
394+
if (matchingBeans.size() == 0) {
395+
throw new UnknownTestProcessDefinitionException(
396+
"No " + TestProcessDefinition.class.getSimpleName() + " with identifier "
397+
+ processIdentifier + " could be found. Make sure you have a bean of type "
398+
+ TestProcessDefinition.class.getSimpleName()
399+
+ "that uses this identifier in your Spring Test-Context.");
400+
}
401+
402+
if (matchingBeans.size() > 1) {
403+
throw new TooManyTestProcessDefinitionsException("More than one TestProcessDefinition "
404+
+ "bean with identifier " + processIdentifier + " were found. Consider referencing it "
405+
+ "using its class, the instance or bean name rather than the process identifier. "
406+
+ "Found beans were: " + matchingBeans.keySet().stream().collect(Collectors.joining(", ")));
407+
}
408+
355409
result = matchingBeans.values()
356410
.stream()
357411
.findFirst()

testprocesses-core/src/test/java/io/github/netmikey/testprocesses/functional/AnnotatedTestClassTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void oneTestWereTestProcessShouldBeRunning() {
3434
* should be running. The execution order may vary and is not important.
3535
*/
3636
@Test
37-
public void anotherTestWereTestProcessShouldBeRunning() {
37+
public void anotherTestWhereTestProcessShouldBeRunning() {
3838
assertEchoRunningByClass(registry);
39+
assertEchoRunningByProcessIdentifier(registry);
3940
}
4041
}

testprocesses-core/src/test/java/io/github/netmikey/testprocesses/functional/testfixtures/EchoTestProcess.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
@Component
1515
public class EchoTestProcess extends AbstractTestProcessDefinition {
1616

17+
/**
18+
* The process identifier.
19+
*/
20+
public static String PROCESS_IDENTIFIER = "echo-process";
21+
1722
/**
1823
* Default constructor.
1924
*/
@@ -29,6 +34,6 @@ protected void buildProcess(ProcessBuilder builder) {
2934

3035
@Override
3136
public String getProcessIdentifier() {
32-
return "echo-process";
37+
return PROCESS_IDENTIFIER;
3338
}
3439
}

testprocesses-core/src/test/java/io/github/netmikey/testprocesses/functional/testfixtures/TestHelper.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ public static void assertEchoRunningByClass(TestProcessesRegistry registry) {
8686
assertRunningByClass(registry, EchoTestProcess.class);
8787
}
8888

89+
/**
90+
* Assert the {@link EchoTestProcess} is currently running using its process
91+
* identifier for the lookup.
92+
*
93+
* @param registry
94+
* The registry on which the lookup should be performed.
95+
*/
96+
public static void assertEchoRunningByProcessIdentifier(TestProcessesRegistry registry) {
97+
assertRunningByProcessIdentifier(registry, EchoTestProcess.PROCESS_IDENTIFIER);
98+
}
99+
89100
/**
90101
* Assert the {@link TestProcessDefinition} is currently running using its
91102
* class for the lookup.
@@ -103,6 +114,23 @@ public static void assertRunningByClass(TestProcessesRegistry registry,
103114
Assertions.assertEquals(TestProcessState.STARTED, runningEchoProcess.getDefinition().getActualState());
104115
}
105116

117+
/**
118+
* Assert the {@link TestProcessDefinition} is currently running using its
119+
* process identifier for the lookup.
120+
*
121+
* @param registry
122+
* The registry on which the lookup should be performed.
123+
* @param processIdentifier
124+
* The process identifier to be looked up.
125+
*/
126+
public static void assertRunningByProcessIdentifier(TestProcessesRegistry registry, String processIdentifier) {
127+
128+
RunningTestProcess<?> runningEchoProcess = registry.retrieveRunningProcess(processIdentifier(processIdentifier))
129+
.orElseThrow(() -> new AssertionFailedError(
130+
"process with identifier `" + processIdentifier + "` should have been started"));
131+
Assertions.assertEquals(TestProcessState.STARTED, runningEchoProcess.getDefinition().getActualState());
132+
}
133+
106134
/**
107135
* Assert the {@link EchoTestProcess} is currently running using its
108136
* {@link EchoTestProcess} instance for the lookup.

0 commit comments

Comments
 (0)