Search in sources :

Example 1 with Logger

use of com.github.nosan.embedded.cassandra.commons.logging.Logger in project embedded-cassandra by nosan.

the class CassandraBuilder method build.

/**
 * Build a new {@link Cassandra} instance.
 *
 * @return a {@link Cassandra} instance.
 */
public Cassandra build() {
    String name = (this.name != null) ? this.name : "cassandra-" + CASSANDRA_ID.getAndIncrement();
    Version version = (this.version != null) ? this.version : DEFAULT_VERSION;
    Path workingDirectory;
    try {
        IOSupplier<? extends Path> workingDirectorySupplier = this.workingDirectorySupplier;
        if (workingDirectorySupplier != null) {
            workingDirectory = workingDirectorySupplier.get();
            Objects.requireNonNull(workingDirectory, "Working Directory must not be null");
        } else {
            workingDirectory = Files.createTempDirectory("");
        }
    } catch (IOException ex) {
        throw new UncheckedIOException("Unable to get a working directory", ex);
    }
    WorkingDirectoryInitializer workingDirectoryInitializer = this.workingDirectoryInitializer;
    if (workingDirectoryInitializer == null) {
        workingDirectoryInitializer = new DefaultWorkingDirectoryInitializer(new WebCassandraDirectoryProvider(new JdkHttpClient(Duration.ofSeconds(30), Duration.ofSeconds(30))));
    }
    WorkingDirectoryDestroyer workingDirectoryDestroyer = this.workingDirectoryDestroyer;
    if (workingDirectoryDestroyer == null) {
        workingDirectoryDestroyer = WorkingDirectoryDestroyer.deleteOnly("bin", "pylib", "lib", "tools", "doc", "javadoc", "interface");
    }
    Duration startupTimeout = this.startupTimeout;
    if (startupTimeout == null) {
        startupTimeout = Duration.ofMinutes(2);
    }
    Logger logger = this.logger;
    if (logger == null) {
        logger = Logger.get(Cassandra.class);
    }
    Map<String, Object> environmentVariables = new LinkedHashMap<>(this.environmentVariables);
    environmentVariables.values().removeIf(Objects::isNull);
    Map<String, Object> systemProperties = new LinkedHashMap<>(this.systemProperties);
    systemProperties.values().removeIf(Objects::isNull);
    Set<String> jvmOptions = new LinkedHashSet<>(this.jvmOptions);
    jvmOptions.removeIf(Objects::isNull);
    Set<WorkingDirectoryCustomizer> workingDirectoryCustomizers = new LinkedHashSet<>(this.workingDirectoryCustomizers);
    workingDirectoryCustomizers.removeIf(Objects::isNull);
    Map<String, Object> configProperties = new LinkedHashMap<>(this.configProperties);
    CassandraDatabaseFactory databaseFactory = new DefaultCassandraDatabaseFactory(name, version, environmentVariables, configProperties, systemProperties, jvmOptions);
    return new DefaultCassandra(name, version, workingDirectory.normalize().toAbsolutePath(), this.registerShutdownHook, workingDirectoryInitializer, workingDirectoryDestroyer, startupTimeout, workingDirectoryCustomizers, databaseFactory, logger);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UncheckedIOException(java.io.UncheckedIOException) Logger(com.github.nosan.embedded.cassandra.commons.logging.Logger) LinkedHashMap(java.util.LinkedHashMap) JdkHttpClient(com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient) Path(java.nio.file.Path) Duration(java.time.Duration) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects)

Example 2 with Logger

use of com.github.nosan.embedded.cassandra.commons.logging.Logger in project embedded-cassandra by nosan.

the class CassandraBuilderTests method logger.

@Test
void logger() {
    Logger logger = Logger.get("TEST");
    assertThat(this.builder.logger(logger).build()).hasFieldOrPropertyWithValue("logger", logger);
}
Also used : Logger(com.github.nosan.embedded.cassandra.commons.logging.Logger) Test(org.junit.jupiter.api.Test)

Example 3 with Logger

use of com.github.nosan.embedded.cassandra.commons.logging.Logger in project embedded-cassandra-spring-boot-starter by nosan.

the class EmbeddedCassandraAutoConfigurationTests method configureProperties.

@Test
void configureProperties() {
    this.runner.withUserConfiguration(ExcludeCassandraBeanDefinitionRegistryPostProcessor.class).withPropertyValues("cassandra.embedded.config-file=classpath:cassandra.yaml", "cassandra.embedded.config-properties.start_rpc=true", "cassandra.embedded.environment-variables.JVM_OPTS=-Xmx512m", "cassandra.embedded.jvm-options=-Xmx256m", "cassandra.embedded.logger=MyLogger", "cassandra.embedded.version=3.11.3", "cassandra.embedded.name=MyCassandra", "cassandra.embedded.register-shutdown-hook=false", "cassandra.embedded.system-properties.cassandra.start_rpc=true", "cassandra.embedded.startup-timeout=1m", "cassandra.embedded.working-directory-resources.[conf/cassandra.yaml]=classpath:cassandra.yaml", "cassandra.embedded.working-directory=target/embeddedCassandra").withBean(CassandraBuilderConfigurator.class, () -> (builder) -> builder.addJvmOptions("-Xmx1024m")).run(context -> {
        assertThat(context).hasSingleBean(CassandraBuilder.class);
        Cassandra cassandra = context.getBean(CassandraBuilder.class).build();
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.jvmOptions", new LinkedHashSet<>(Arrays.asList("-Xmx256m", "-Xmx1024m")));
        assertThat(cassandra).hasFieldOrPropertyWithValue("logger", Logger.get("MyLogger"));
        assertThat(cassandra).hasFieldOrPropertyWithValue("name", "MyCassandra");
        assertThat(cassandra).hasFieldOrPropertyWithValue("version", Version.parse("3.11.3"));
        assertThat(cassandra).hasFieldOrPropertyWithValue("registerShutdownHook", false);
        assertThat(cassandra).hasFieldOrPropertyWithValue("startupTimeout", Duration.ofMinutes(1));
        assertThat(cassandra).hasFieldOrPropertyWithValue("workingDirectory", Paths.get("target/embeddedCassandra").toAbsolutePath());
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.environmentVariables", Collections.singletonMap("JVM_OPTS", "-Xmx512m"));
        Map<String, Object> systemProperties = new LinkedHashMap<>();
        systemProperties.put("cassandra.start_rpc", "true");
        systemProperties.put("cassandra.config", new UrlResource(new ClassPathResource("cassandra.yaml").getURL()));
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.systemProperties", systemProperties);
        assertThat((Collection<?>) ReflectionTestUtils.getField(cassandra, "workingDirectoryCustomizers")).hasSize(1);
    });
}
Also used : CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) Arrays(java.util.Arrays) DriverConfigLoaderBuilderCustomizer(org.springframework.boot.autoconfigure.cassandra.DriverConfigLoaderBuilderCustomizer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) ClassPathResource(org.springframework.core.io.ClassPathResource) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) CassandraBuilderConfigurator(com.github.nosan.embedded.cassandra.CassandraBuilderConfigurator) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Version(com.github.nosan.embedded.cassandra.Version) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LinkedHashMap(java.util.LinkedHashMap) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlScript(com.github.nosan.embedded.cassandra.cql.CqlScript) Duration(java.time.Duration) Map(java.util.Map) UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) LinkedHashSet(java.util.LinkedHashSet) Logger(com.github.nosan.embedded.cassandra.commons.logging.Logger) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) OS(org.junit.jupiter.api.condition.OS) CassandraAutoConfiguration(org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration) Collection(java.util.Collection) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) BeansException(org.springframework.beans.BeansException) BeanDefinitionRegistryPostProcessor(org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor) Test(org.junit.jupiter.api.Test) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Configuration(org.springframework.context.annotation.Configuration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) Paths(java.nio.file.Paths) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) CqlSessionFactoryBean(org.springframework.data.cassandra.config.CqlSessionFactoryBean) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) CassandraBuilderConfigurator(com.github.nosan.embedded.cassandra.CassandraBuilderConfigurator) ClassPathResource(org.springframework.core.io.ClassPathResource) LinkedHashMap(java.util.LinkedHashMap) UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Aggregations

Logger (com.github.nosan.embedded.cassandra.commons.logging.Logger)3 Duration (java.time.Duration)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.jupiter.api.Test)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 CqlSessionBuilder (com.datastax.oss.driver.api.core.CqlSessionBuilder)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)1 Cassandra (com.github.nosan.embedded.cassandra.Cassandra)1 CassandraBuilder (com.github.nosan.embedded.cassandra.CassandraBuilder)1 CassandraBuilderConfigurator (com.github.nosan.embedded.cassandra.CassandraBuilderConfigurator)1 Version (com.github.nosan.embedded.cassandra.Version)1 UrlResource (com.github.nosan.embedded.cassandra.commons.UrlResource)1 JdkHttpClient (com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient)1 CqlScript (com.github.nosan.embedded.cassandra.cql.CqlScript)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1