Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
Issue: #3717
Signed-off-by: yongjunhong <kevin0928@naver.com>
  • Loading branch information
YongGoose committed Dec 14, 2024
1 parent 13c1b19 commit ec13b6d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,6 @@ public final class Constants {
*/
public static final String DEACTIVATE_ALL_CONDITIONS_PATTERN = ClassNamePatternFilterUtils.ALL_PATTERN;

/**
* A blank pattern used for class name filtering: {@value}
*
* <p>This constant is used to represent an empty or blank pattern in class name filtering operations.
*
* @see ClassNamePatternFilterUtils#BLANK
*/
public static final String BLANK = ClassNamePatternFilterUtils.BLANK;

/**
* Property name used to set the default display name generator class name: {@value}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.engine.extension;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* Demo extension for auto-detection of extensions loaded via Java's
* {@link java.util.ServiceLoader} mechanism.
*
* @since 5.11
*/
public class ConfigLoaderExtension implements BeforeAllCallback {

@Override
public void beforeAll(ExtensionContext context) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.platform.commons.util.ClassNamePatternFilterUtils;

/**
* Tests for the {@link MutableExtensionRegistry}.
Expand Down Expand Up @@ -65,13 +66,87 @@ void newRegistryWithoutParentHasDefaultExtensionsPlusAutodetectedExtensionsLoade

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

assertEquals(NUM_DEFAULT_EXTENSIONS + 1, extensions.size());
assertEquals(NUM_DEFAULT_EXTENSIONS + 2, extensions.size());
assertDefaultGlobalExtensionsAreRegistered(4);

assertExtensionRegistered(registry, ServiceLoaderExtension.class);
assertEquals(4, countExtensions(registry, BeforeAllCallback.class));
}

@Test
void registryIncludesAndExcludesSpecificAutoDetectedExtensions() {
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
ClassNamePatternFilterUtils.excludeMatchingClassNames(
"org.junit.jupiter.engine.extension.ConfigLoaderExtension")).test(clazz.getName()));
registry = createRegistryWithDefaultExtensions(configuration);

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

assertEquals(NUM_DEFAULT_EXTENSIONS, extensions.size());
assertDefaultGlobalExtensionsAreRegistered(3);

assertExtensionRegistered(registry, ServiceLoaderExtension.class);
assertEquals(3, countExtensions(registry, BeforeAllCallback.class));
}

@Test
void registryIncludesAllAutoDetectedExtensionsAndExcludesNone() {
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames("*").and(
ClassNamePatternFilterUtils.excludeMatchingClassNames("")).test(clazz.getName()));
registry = createRegistryWithDefaultExtensions(configuration);

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

assertEquals(NUM_DEFAULT_EXTENSIONS + 2, extensions.size());
assertDefaultGlobalExtensionsAreRegistered(4);

assertExtensionRegistered(registry, ServiceLoaderExtension.class);
assertExtensionRegistered(registry, ConfigLoaderExtension.class);
assertEquals(4, countExtensions(registry, BeforeAllCallback.class));
}

@Test
void registryIncludesSpecificAutoDetectedExtensionsAndExcludesAll() {
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
ClassNamePatternFilterUtils.excludeMatchingClassNames("*")).test(clazz.getName()));
registry = createRegistryWithDefaultExtensions(configuration);

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

assertEquals(NUM_CORE_EXTENSIONS, extensions.size());
assertDefaultGlobalExtensionsAreRegistered(2);

assertExtensionNotRegistered(registry, ServiceLoaderExtension.class);
assertEquals(2, countExtensions(registry, BeforeAllCallback.class));
}

@Test
void registryIncludesAndExcludesSameAutoDetectedExtension() {
when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
when(configuration.getFilterForAutoDetectedExtensions()).thenReturn(
clazz -> ClassNamePatternFilterUtils.includeMatchingClassNames(
"org.junit.jupiter.engine.extension.ServiceLoaderExtension").and(
ClassNamePatternFilterUtils.excludeMatchingClassNames(
"org.junit.jupiter.engine.extension.ServiceLoaderExtension")).test(clazz.getName()));
registry = createRegistryWithDefaultExtensions(configuration);

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

assertEquals(NUM_CORE_EXTENSIONS, extensions.size());
assertDefaultGlobalExtensionsAreRegistered(2);

assertExtensionNotRegistered(registry, ServiceLoaderExtension.class);
assertEquals(2, countExtensions(registry, BeforeAllCallback.class));
}

@Test
void registerExtensionByImplementingClass() {
registry.registerExtension(MyExtension.class);
Expand Down Expand Up @@ -157,6 +232,11 @@ private void assertExtensionRegistered(ExtensionRegistry registry, Class<? exten
() -> extensionType.getSimpleName() + " should be present");
}

private void assertExtensionNotRegistered(ExtensionRegistry registry, Class<? extends Extension> extensionType) {
assertTrue(registry.getExtensions(extensionType).isEmpty(),
() -> extensionType.getSimpleName() + " should not be present");
}

private void assertDefaultGlobalExtensionsAreRegistered() {
assertDefaultGlobalExtensionsAreRegistered(2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.junit.jupiter.engine.extension.ServiceLoaderExtension
org.junit.jupiter.engine.extension.ConfigLoaderExtension

0 comments on commit ec13b6d

Please # to comment.