org.junit.jupiter.api.extension.ExtensionContext#getUniqueId()源码实例Demo

下面列出了org.junit.jupiter.api.extension.ExtensionContext#getUniqueId() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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();
    }
}
 
源代码2 项目: selenium-jupiter   文件: SeleniumExtension.java
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;
}
 
源代码3 项目: helm-maven-plugin   文件: MojoExtension.java
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);
}
 
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;
    }
}
 
源代码5 项目: selenium-jupiter   文件: SeleniumExtension.java
@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);
}
 
源代码6 项目: selenium-jupiter   文件: SeleniumExtension.java
@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");
}
 
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();
    }
}
 
private TestDescription testDescriptionFrom(ExtensionContext context) {
    return new TestcontainersTestDescription(
        context.getUniqueId(),
        filesystemFriendlyNameOf(context)
    );
}