Skip to content

Commit ec13b6d

Browse files
committed
Add integration test
Issue: #3717 Signed-off-by: yongjunhong <[email protected]>
1 parent 13c1b19 commit ec13b6d

File tree

4 files changed

+110
-10
lines changed

4 files changed

+110
-10
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,6 @@ public final class Constants {
167167
*/
168168
public static final String DEACTIVATE_ALL_CONDITIONS_PATTERN = ClassNamePatternFilterUtils.ALL_PATTERN;
169169

170-
/**
171-
* A blank pattern used for class name filtering: {@value}
172-
*
173-
* <p>This constant is used to represent an empty or blank pattern in class name filtering operations.
174-
*
175-
* @see ClassNamePatternFilterUtils#BLANK
176-
*/
177-
public static final String BLANK = ClassNamePatternFilterUtils.BLANK;
178-
179170
/**
180171
* Property name used to set the default display name generator class name: {@value}
181172
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.engine.extension;
12+
13+
import org.junit.jupiter.api.extension.BeforeAllCallback;
14+
import org.junit.jupiter.api.extension.ExtensionContext;
15+
16+
/**
17+
* Demo extension for auto-detection of extensions loaded via Java's
18+
* {@link java.util.ServiceLoader} mechanism.
19+
*
20+
* @since 5.11
21+
*/
22+
public class ConfigLoaderExtension implements BeforeAllCallback {
23+
24+
@Override
25+
public void beforeAll(ExtensionContext context) {
26+
}
27+
28+
}

jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.api.extension.ParameterResolver;
3232
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
3333
import org.junit.jupiter.engine.config.JupiterConfiguration;
34+
import org.junit.platform.commons.util.ClassNamePatternFilterUtils;
3435

3536
/**
3637
* Tests for the {@link MutableExtensionRegistry}.
@@ -65,13 +66,87 @@ void newRegistryWithoutParentHasDefaultExtensionsPlusAutodetectedExtensionsLoade
6566

6667
List<Extension> extensions = registry.getExtensions(Extension.class);
6768

68-
assertEquals(NUM_DEFAULT_EXTENSIONS + 1, extensions.size());
69+
assertEquals(NUM_DEFAULT_EXTENSIONS + 2, extensions.size());
70+
assertDefaultGlobalExtensionsAreRegistered(4);
71+
72+
assertExtensionRegistered(registry, ServiceLoaderExtension.class);
73+
assertEquals(4, countExtensions(registry, BeforeAllCallback.class));
74+
}
75+
76+
@Test
77+
void registryIncludesAndExcludesSpecificAutoDetectedExtensions() {
78+
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
79+
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
80+
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
81+
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
82+
ClassNamePatternFilterUtils.excludeMatchingClassNames(
83+
"org.junit.jupiter.engine.extension.ConfigLoaderExtension")).test(clazz.getName()));
84+
registry = createRegistryWithDefaultExtensions(configuration);
85+
86+
List<Extension> extensions = registry.getExtensions(Extension.class);
87+
88+
assertEquals(NUM_DEFAULT_EXTENSIONS, extensions.size());
6989
assertDefaultGlobalExtensionsAreRegistered(3);
7090

7191
assertExtensionRegistered(registry, ServiceLoaderExtension.class);
7292
assertEquals(3, countExtensions(registry, BeforeAllCallback.class));
7393
}
7494

95+
@Test
96+
void registryIncludesAllAutoDetectedExtensionsAndExcludesNone() {
97+
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
98+
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
99+
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames("*").and(
100+
ClassNamePatternFilterUtils.excludeMatchingClassNames("")).test(clazz.getName()));
101+
registry = createRegistryWithDefaultExtensions(configuration);
102+
103+
List<Extension> extensions = registry.getExtensions(Extension.class);
104+
105+
assertEquals(NUM_DEFAULT_EXTENSIONS + 2, extensions.size());
106+
assertDefaultGlobalExtensionsAreRegistered(4);
107+
108+
assertExtensionRegistered(registry, ServiceLoaderExtension.class);
109+
assertExtensionRegistered(registry, ConfigLoaderExtension.class);
110+
assertEquals(4, countExtensions(registry, BeforeAllCallback.class));
111+
}
112+
113+
@Test
114+
void registryIncludesSpecificAutoDetectedExtensionsAndExcludesAll() {
115+
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
116+
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
117+
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
118+
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
119+
ClassNamePatternFilterUtils.excludeMatchingClassNames("*")).test(clazz.getName()));
120+
registry = createRegistryWithDefaultExtensions(configuration);
121+
122+
List<Extension> extensions = registry.getExtensions(Extension.class);
123+
124+
assertEquals(NUM_CORE_EXTENSIONS, extensions.size());
125+
assertDefaultGlobalExtensionsAreRegistered(2);
126+
127+
assertExtensionNotRegistered(registry, ServiceLoaderExtension.class);
128+
assertEquals(2, countExtensions(registry, BeforeAllCallback.class));
129+
}
130+
131+
@Test
132+
void registryIncludesAndExcludesSameAutoDetectedExtension() {
133+
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
134+
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
135+
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
136+
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
137+
ClassNamePatternFilterUtils.excludeMatchingClassNames(
138+
"org.junit.jupiter.engine.extension.ServiceLoaderExtension")).test(clazz.getName()));
139+
registry = createRegistryWithDefaultExtensions(configuration);
140+
141+
List<Extension> extensions = registry.getExtensions(Extension.class);
142+
143+
assertEquals(NUM_CORE_EXTENSIONS, extensions.size());
144+
assertDefaultGlobalExtensionsAreRegistered(2);
145+
146+
assertExtensionNotRegistered(registry, ServiceLoaderExtension.class);
147+
assertEquals(2, countExtensions(registry, BeforeAllCallback.class));
148+
}
149+
75150
@Test
76151
void registerExtensionByImplementingClass() {
77152
registry.registerExtension(MyExtension.class);
@@ -157,6 +232,11 @@ private void assertExtensionRegistered(ExtensionRegistry registry, Class<? exten
157232
() -> extensionType.getSimpleName() + " should be present");
158233
}
159234

235+
private void assertExtensionNotRegistered(ExtensionRegistry registry, Class<? extends Extension> extensionType) {
236+
assertTrue(registry.getExtensions(extensionType).isEmpty(),
237+
() -> extensionType.getSimpleName() + " should not be present");
238+
}
239+
160240
private void assertDefaultGlobalExtensionsAreRegistered() {
161241
assertDefaultGlobalExtensionsAreRegistered(2);
162242
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.junit.jupiter.engine.extension.ServiceLoaderExtension
2+
org.junit.jupiter.engine.extension.ConfigLoaderExtension

0 commit comments

Comments
 (0)