Java Code Examples for org.junit.jupiter.api.extension.ExtensionContext#getUniqueId()

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext#getUniqueId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalFlowableFormExtension.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void doFinally(ExtensionContext context, TestInstance.Lifecycle lifecycleForClean) {
    FormEngine formEngine = getFormEngine(context);
    FormEngineConfiguration formEngineConfiguration = formEngine.getFormEngineConfiguration();
    try {
        String annotationDeploymentKey = context.getUniqueId() + ANNOTATION_DEPLOYMENT_ID_KEY;
        String deploymentIdFromDeploymentAnnotation = getStore(context).get(annotationDeploymentKey, String.class);
        if (deploymentIdFromDeploymentAnnotation != null) {
            FormTestHelper.annotationDeploymentTearDown(formEngine, deploymentIdFromDeploymentAnnotation, context.getRequiredTestClass(),
                context.getRequiredTestMethod().getName());
            getStore(context).remove(annotationDeploymentKey);
        }

        AnnotationSupport.findAnnotation(context.getTestMethod(), CleanTest.class)
            .ifPresent(cleanTest -> removeDeployments(formEngine.getFormRepositoryService()));
        if (context.getTestInstanceLifecycle().orElse(TestInstance.Lifecycle.PER_METHOD) == lifecycleForClean) {
            cleanTestAndAssertAndEnsureCleanDb(context, formEngine);
        }

    } finally {
        formEngineConfiguration.getClock().reset();
    }
}
 
Example 2
Source File: SeleniumExtension.java    From selenium-jupiter with Apache License 2.0 5 votes vote down vote up
private DriverHandler getDriverHandler(ParameterContext parameterContext,
        ExtensionContext extensionContext, Parameter parameter,
        Class<?> type, Browser browser, Class<?> constructorClass,
        boolean isRemote) {
    DriverHandler driverHandler = null;
    try {
        driverHandler = getDriverHandler(extensionContext, parameter, type,
                constructorClass, browser, isRemote);

        Optional<DockerBrowser> dockerBrowser = annotationsReader
                .getDocker(parameter);
        String contextId = extensionContext.getUniqueId();
        if (type.equals(RemoteWebDriver.class)
                || type.equals(WebDriver.class) || type.equals(List.class)
                || (dockerBrowser.isPresent()
                        && type.equals(SelenideDriver.class))) {
            initHandlerForDocker(contextId, driverHandler);
        }

        boolean isTemplate = isTestTemplate(extensionContext);
        if (!isTemplate && isGeneric(type) && isRemote) {
            ((RemoteDriverHandler) driverHandler).setParent(this);
            ((RemoteDriverHandler) driverHandler)
                    .setParameterContext(parameterContext);
        }

        putDriverHandlerInMap(extensionContext.getUniqueId(),
                driverHandler);

    } catch (Exception e) {
        handleException(parameter, driverHandler, constructorClass, e);
    }
    return driverHandler;
}
 
Example 3
Source File: MojoExtension.java    From helm-maven-plugin with MIT License 5 votes vote down vote up
private Path getProjectBuildDirectory(ExtensionContext context) {
    String suffix = "";
    if (context.getRequiredTestMethod().isAnnotationPresent(ParameterizedTest.class)) {
        String uniqueId = context.getUniqueId();
        int start = 26 + uniqueId.indexOf("test-template-invocation:#");
        int end = start + uniqueId.substring(start).indexOf("]");
        suffix = "." + uniqueId.substring(start, end);
    }
    return Paths.get("target", "surefire", context.getRequiredTestClass().getSimpleName() + "." + context.getRequiredTestMethod().getName() + suffix);
}
 
Example 4
Source File: FilesystemFriendlyNameGenerator.java    From testcontainers-java with MIT License 5 votes vote down vote up
static String filesystemFriendlyNameOf(ExtensionContext context) {
    String contextId = context.getUniqueId();
    try {
        return (isBlank(contextId))
            ? UNKNOWN_NAME
            : URLEncoder.encode(contextId, UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        return UNKNOWN_NAME;
    }
}
 
Example 5
Source File: SeleniumExtension.java    From selenium-jupiter with Apache License 2.0 4 votes vote down vote up
@Override
public Object resolveParameter(ParameterContext parameterContext,
        ExtensionContext extensionContext) {
    String contextId = extensionContext.getUniqueId();
    Parameter parameter = parameterContext.getParameter();
    int index = parameterContext.getIndex();

    log.trace("Context id {}", contextId);
    if (isSingleSession(extensionContext)
            && driverHandlerMap.containsKey(contextId)) {
        List<DriverHandler> list = driverHandlerMap.get(contextId);
        if (index < list.size()) {
            Object obj = list.get(index).getObject();
            if (obj != null) {
                log.trace("Returning index {}: {}", index, obj);
                return obj;
            }
        }
    }

    Class<?> type = parameter.getType();
    String url = null;
    Browser browser = null;

    // Check template
    if (isGeneric(type) && !browserListMap.isEmpty()) {
        browser = getBrowser(contextId, index);
    }
    Optional<String> urlFromAnnotation = getUrlFromAnnotation(parameter,
            extensionContext);
    if (urlFromAnnotation.isPresent() && browser != null) {
        browser.setUrl(urlFromAnnotation.get());
    }
    if (browser != null) {
        type = templateHandlerMap.get(browser.getType());
        url = browser.getUrl();
    }

    // Handler
    Class<?> constructorClass = handlerMap.containsKey(type.getName())
            ? handlerMap.get(type.getName())
            : OtherDriverHandler.class;
    boolean isRemote = constructorClass.equals(RemoteDriverHandler.class);
    if (url != null && !url.isEmpty()) {
        constructorClass = RemoteDriverHandler.class;
        isRemote = true;
    }

    // WebDriverManager
    runWebDriverManagerIfNeded(type, isRemote);

    DriverHandler driverHandler = getDriverHandler(parameterContext,
            extensionContext, parameter, type, browser, constructorClass,
            isRemote);
    return resolveHandler(parameter, driverHandler);
}
 
Example 6
Source File: SeleniumExtension.java    From selenium-jupiter with Apache License 2.0 4 votes vote down vote up
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
        ExtensionContext extensionContext) {
    String contextId = extensionContext.getUniqueId();
    try {
        // 1. By JSON content
        String browserJsonContent = getConfig()
                .getBrowserTemplateJsonContent();
        if (browserJsonContent.isEmpty()) {
            // 2. By JSON file
            String browserJsonFile = getConfig()
                    .getBrowserTemplateJsonFile();
            if (browserJsonFile.startsWith(CLASSPATH_PREFIX)) {
                String browserJsonInClasspath = browserJsonFile
                        .substring(CLASSPATH_PREFIX.length());
                InputStream resourceAsStream = this.getClass()
                        .getResourceAsStream("/" + browserJsonInClasspath);

                if (resourceAsStream != null) {
                    browserJsonContent = IOUtils.toString(resourceAsStream,
                            defaultCharset());
                }

            } else {
                browserJsonContent = new String(
                        readAllBytes(get(browserJsonFile)));
            }
        }
        if (!browserJsonContent.isEmpty()) {
            return new Gson()
                    .fromJson(browserJsonContent, BrowsersTemplate.class)
                    .getStream().map(b -> invocationContext(b, this));
        }

        // 3. By setter
        if (browserListList != null) {
            return browserListList.stream()
                    .map(b -> invocationContext(b, this));
        }
        if (browserListMap != null) {
            return Stream.of(
                    invocationContext(browserListMap.get(contextId), this));
        }

    } catch (IOException e) {
        throw new SeleniumJupiterException(e);
    }

    throw new SeleniumJupiterException(
            "No browser scenario registered for test template");
}
 
Example 7
Source File: InternalFlowableExtension.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected void doFinally(ExtensionContext context, TestInstance.Lifecycle lifecycleForClean) {
    ProcessEngine processEngine = getProcessEngine(context);
    ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
    boolean isAsyncHistoryEnabled = processEngineConfiguration.isAsyncHistoryEnabled();

    if (isAsyncHistoryEnabled) {
        ManagementService managementService = processEngine.getManagementService();
        List<HistoryJob> jobs = managementService.createHistoryJobQuery().list();
        for (HistoryJob job : jobs) {
            managementService.deleteHistoryJob(job.getId());
        }
    }

    HistoryManager asyncHistoryManager = null;
    try {
        if (isAsyncHistoryEnabled) {
            processEngineConfiguration.setAsyncHistoryEnabled(false);
            asyncHistoryManager = processEngineConfiguration.getHistoryManager();
            processEngineConfiguration
                .setHistoryManager(new DefaultHistoryManager(processEngineConfiguration, 
                        processEngineConfiguration.getHistoryLevel(), processEngineConfiguration.isUsePrefixId()));
        }

        String annotationDeploymentKey = context.getUniqueId() + ANNOTATION_DEPLOYMENT_ID_KEY;
        String deploymentIdFromDeploymentAnnotation = getStore(context).get(annotationDeploymentKey, String.class);
        if (deploymentIdFromDeploymentAnnotation != null) {
            TestHelper.annotationDeploymentTearDown(processEngine, deploymentIdFromDeploymentAnnotation, context.getRequiredTestClass(),
                context.getRequiredTestMethod().getName());
            getStore(context).remove(annotationDeploymentKey);
        }

        AnnotationSupport.findAnnotation(context.getTestMethod(), CleanTest.class)
            .ifPresent(cleanTest -> removeDeployments(processEngine.getRepositoryService()));

        AbstractFlowableTestCase.cleanDeployments(processEngine);
        
        if (context.getTestInstanceLifecycle().orElse(TestInstance.Lifecycle.PER_METHOD) == lifecycleForClean
                && processEngineConfiguration.isUsingRelationalDatabase()) { // the logic only is applicable to a relational database with tables
            cleanTestAndAssertAndEnsureCleanDb(context, processEngine);
        }

    } finally {

        if (isAsyncHistoryEnabled) {
            processEngineConfiguration.setAsyncHistoryEnabled(true);
            processEngineConfiguration.setHistoryManager(asyncHistoryManager);
        }

        processEngineConfiguration.getClock().reset();
    }
}
 
Example 8
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 4 votes vote down vote up
private TestDescription testDescriptionFrom(ExtensionContext context) {
    return new TestcontainersTestDescription(
        context.getUniqueId(),
        filesystemFriendlyNameOf(context)
    );
}