Search in sources :

Example 1 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteWithDefaultValue.

@Test
void shouldSubstituteWithDefaultValue() throws IOException {
    StringLookup dummyLookup = (x) -> null;
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
    assertThat(provider.open("foo: ${bar:-default}")).hasSameContentAs(new ByteArrayInputStream("foo: default".getBytes(StandardCharsets.UTF_8)));
}
Also used : Test(org.junit.jupiter.api.Test) StringLookup(org.apache.commons.text.lookup.StringLookup) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) StandardCharsets(java.nio.charset.StandardCharsets) InputStream(java.io.InputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ByteArrayInputStream(java.io.ByteArrayInputStream) StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.jupiter.api.Test)

Example 2 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteOnlyExistingVariables.

@Test
void shouldSubstituteOnlyExistingVariables() throws IOException {
    StringLookup dummyLookup = (x) -> null;
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
    assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: ${bar}".getBytes(StandardCharsets.UTF_8)));
}
Also used : Test(org.junit.jupiter.api.Test) StringLookup(org.apache.commons.text.lookup.StringLookup) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) StandardCharsets(java.nio.charset.StandardCharsets) InputStream(java.io.InputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ByteArrayInputStream(java.io.ByteArrayInputStream) StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.jupiter.api.Test)

Example 3 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dropwizard by dropwizard.

the class DefaultLoggingFactoryTest method testConfigure.

@Test
void testConfigure(@TempDir Path tempDir) throws Exception {
    final StringSubstitutor substitutor = new StringSubstitutor(Maps.of("new_app", tempDir.resolve("example-new-app").toFile().getAbsolutePath(), "new_app_not_additive", tempDir.resolve("example-new-app-not-additive").toFile().getAbsolutePath(), "default", tempDir.resolve("example").toFile().getAbsolutePath()));
    DefaultLoggingFactory config = null;
    try {
        config = factory.build(new SubstitutingSourceProvider(configurationSourceProvider, substitutor), "yaml/logging_advanced.yml");
        config.configure(new MetricRegistry(), "test-logger");
        LoggerFactory.getLogger("com.example.app").debug("Application debug log");
        LoggerFactory.getLogger("com.example.app").info("Application log");
        LoggerFactory.getLogger("com.example.newApp").debug("New application debug log");
        LoggerFactory.getLogger("com.example.newApp").info("New application info log");
        LoggerFactory.getLogger("com.example.legacyApp").debug("Legacy application debug log");
        LoggerFactory.getLogger("com.example.legacyApp").info("Legacy application info log");
        LoggerFactory.getLogger("com.example.notAdditive").debug("Not additive application debug log");
        LoggerFactory.getLogger("com.example.notAdditive").info("Not additive application info log");
        config.stop();
        config.reset();
        assertThat(Files.readAllLines(tempDir.resolve("example.log"))).containsOnly("INFO  com.example.app: Application log", "DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log", "DEBUG com.example.legacyApp: Legacy application debug log", "INFO  com.example.legacyApp: Legacy application info log");
        assertThat(Files.readAllLines(tempDir.resolve("example-new-app.log"))).containsOnly("DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log");
        assertThat(Files.readAllLines(tempDir.resolve("example-new-app-not-additive.log"))).containsOnly("DEBUG com.example.notAdditive: Not additive application debug log", "INFO  com.example.notAdditive: Not additive application info log");
    } finally {
        if (config != null) {
            config.reset();
        }
    }
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) StringSubstitutor(org.apache.commons.text.StringSubstitutor) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.jupiter.api.Test)

Example 4 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dropwizard by dropwizard.

the class TcpSocketAppenderFactoryTest method testTestTcpLogging.

@Test
void testTestTcpLogging() throws Exception {
    try (ServerSocket serverSocket = createServerSocket();
        TcpServer tcpServer = new TcpServer(serverSocket)) {
        Future<List<String>> receivedMessages = tcpServer.receive();
        DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider(new ResourceConfigurationSourceProvider(), new StringSubstitutor(Collections.singletonMap("tcp.server.port", serverSocket.getLocalPort()))), "yaml/logging-tcp.yml");
        loggingFactory.configure(new MetricRegistry(), "tcp-test");
        List<String> loggedMessages = generateLogs(LoggerFactory.getLogger("com.example.app"));
        loggingFactory.reset();
        assertThat(receivedMessages.get(1, TimeUnit.MINUTES)).hasSize(100).allSatisfy(s -> assertThat(s).startsWith("INFO")).extracting(s -> s.substring(s.lastIndexOf("com.example.app: ") + "com.example.app: ".length())).containsExactlyElementsOf(loggedMessages);
    }
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) ResourceConfigurationSourceProvider(io.dropwizard.configuration.ResourceConfigurationSourceProvider) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Duration(io.dropwizard.util.Duration) StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test) ServerSocket(java.net.ServerSocket) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) DataSize(io.dropwizard.util.DataSize) Jackson(io.dropwizard.jackson.Jackson) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) BaseValidator(io.dropwizard.validation.BaseValidator) Collections(java.util.Collections) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ResourceConfigurationSourceProvider(io.dropwizard.configuration.ResourceConfigurationSourceProvider) MetricRegistry(com.codahale.metrics.MetricRegistry) ServerSocket(java.net.ServerSocket) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 5 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project OpenRefine by OpenRefine.

the class DBExtensionTestUtils method getJDBCUrl.

public static String getJDBCUrl(DatabaseConfiguration dbConfig) {
    Map<String, Object> substitutes = new HashMap<String, Object>();
    substitutes.put("dbType", dbConfig.getDatabaseType());
    substitutes.put("host", dbConfig.getDatabaseHost());
    substitutes.put("port", "" + dbConfig.getDatabasePort());
    substitutes.put("dbName", dbConfig.getDatabaseName());
    substitutes.put("useSSL", dbConfig.isUseSSL());
    String urlTemplate = "jdbc:${dbType}://${host}:${port}/${dbName}?useSSL=${useSSL}";
    StringSubstitutor strSub = new StringSubstitutor(substitutes);
    return strSub.replace(urlTemplate);
}
Also used : HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4