All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wl4g.infra.common.tests.integration.IntegrationTestApplication Maven / Gradle / Ivy

There is a newer version: 3.1.72
Show newest version
/**
 * Copyright 2017 ~ 2025 the original authors James Wong.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ALL_OR KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wl4g.infra.common.tests.integration; import com.wl4g.infra.common.lang.ClassUtils2; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.validation.constraints.NotNull; import static java.lang.System.getProperty; import static java.lang.System.getenv; import static java.util.Locale.US; import static java.util.Objects.isNull; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.StringUtils.replace; /** * The {@link IntegrationTestApplication} * * @author James Wong * @since v1.0 **/ @Slf4j @Getter public abstract class IntegrationTestApplication { public static final String IT_ASSERTION_APPLICATION_CLASS_KEY = "it.assertion.application.class"; public static final String IT_ASSERTION_APPLICATION_CLASS_ENV = replace(IT_ASSERTION_APPLICATION_CLASS_KEY.toLowerCase(US), ".", "_"); private static volatile IntegrationTestApplication INSTANCE; private ConfigurableApplicationContext context; private Runnable startedListener; public static IntegrationTestApplication getInstance() { if (isNull(INSTANCE)) { synchronized (IntegrationTestApplication.class) { if (isNull(INSTANCE)) { String itAppClass = getenv(IT_ASSERTION_APPLICATION_CLASS_ENV); if (isBlank(itAppClass)) { itAppClass = getProperty(IT_ASSERTION_APPLICATION_CLASS_KEY); } if (isBlank(itAppClass)) { throw new IllegalStateException(String.format("Could not create integration test main class instance, please sets JVM property '%s' or environment '%s'", IT_ASSERTION_APPLICATION_CLASS_KEY, IT_ASSERTION_APPLICATION_CLASS_ENV)); } try { INSTANCE = (IntegrationTestApplication) ClassUtils2.forName(itAppClass, Thread.currentThread().getContextClassLoader()) .getConstructor() .newInstance(); } catch (Throwable ex) { throw new IllegalStateException("Could not create it application instance", ex); } } } } return INSTANCE; } public void doMain(String[] args) { log.info("Starting for Integration Test Application ..."); SpringApplication.run(getClass(), args); } public void exit() { SpringApplication.exit(context); } public void setStartedListener(@NotNull Runnable startedListener) { this.startedListener = requireNonNull(startedListener, "startedListener"); } @SuppressWarnings("unused") @Configuration @ConditionalOnProperty(name = IT_ASSERTION_APPLICATION_CLASS_KEY) static class IntegrationTestApplicationConfiguration { @Bean public ApplicationRunner applicationRunner(ConfigurableApplicationContext context) { getInstance().context = context; return args -> { if (getInstance().getStartedListener() != null) { getInstance().getStartedListener().run(); } }; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy