Skip to content

Commit

Permalink
Deprecate CharStreams and most of Resources (dropwizard#4358)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhowe authored Oct 24, 2021
1 parent 38277c4 commit 3856b1b
Show file tree
Hide file tree
Showing 53 changed files with 269 additions and 340 deletions.
3 changes: 1 addition & 2 deletions docs/source/manual/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,7 @@ assert the expected widget is deserialized based on the ``type`` field.
@Test
public void testBuildAHammer() throws Exception {
final File yml = new File(Resources.getResource("yaml/hammer.yml").toURI());
final WidgetFactory wid = factory.build(yml);
final WidgetFactory wid = factory.build(new ResourceConfigurationSourceProvider(), "yaml/hammer.yml");
assertThat(wid).isInstanceOf(HammerFactory.class);
assertThat(((HammerFactory) wid).createWidget().getWeight()).isEqualTo(10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.dropwizard.servlets.assets.AssetServlet;
import io.dropwizard.servlets.assets.ResourceURL;
import io.dropwizard.setup.Environment;
import io.dropwizard.util.Resources;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -44,7 +43,7 @@ void hasADefaultPath() {
.isEqualTo("index.htm");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("assets"));
.isEqualTo(normalize("/assets"));

assertThat(servlet.getUriPath())
.isEqualTo("/assets");
Expand All @@ -61,7 +60,7 @@ void canHaveCustomPaths() {
.isEqualTo("index.htm");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("json"));
.isEqualTo(normalize("/json"));

assertThat(servlet.getUriPath())
.isEqualTo("/json");
Expand All @@ -78,7 +77,7 @@ void canHaveDifferentUriAndResourcePaths() {
.isEqualTo("index.htm");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("json"));
.isEqualTo(normalize("/json"));

assertThat(servlet.getUriPath())
.isEqualTo("/what");
Expand All @@ -95,7 +94,7 @@ void canSupportDifferentAssetsBundleName() {
.isEqualTo("index.txt");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("json"));
.isEqualTo(normalize("/json"));

assertThat(servlet.getUriPath())
.isEqualTo("/what/new");
Expand All @@ -108,7 +107,7 @@ void canSupportDifferentAssetsBundleName() {
.isEqualTo("index.txt");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("json"));
.isEqualTo(normalize("/json"));

assertThat(servlet.getUriPath())
.isEqualTo("/what/old");
Expand All @@ -125,7 +124,7 @@ void canHaveDifferentUriAndResourcePathsAndIndexFilename() {
.isEqualTo("index.txt");

assertThat(servlet.getResourceURL())
.isEqualTo(normalize("json"));
.isEqualTo(normalize("/json"));

assertThat(servlet.getUriPath())
.isEqualTo("/what");
Expand All @@ -137,13 +136,13 @@ void canHaveDifferentDefaultMediaType() {

assertThat(servletPath).isEqualTo("/assets/*");
assertThat(servlet.getIndexFile()).isEqualTo("index.html");
assertThat(servlet.getResourceURL()).isEqualTo(normalize("assets"));
assertThat(servlet.getResourceURL()).isEqualTo(normalize("/assets"));
assertThat(servlet.getUriPath()).isEqualTo("/assets");
assertThat(servlet.getDefaultMediaType()).isEqualTo("text/plain");
}

private URL normalize(String path) {
return ResourceURL.appendTrailingSlash(Resources.getResource(path));
return ResourceURL.appendTrailingSlash(getClass().getResource(path));
}

private void runBundle(AssetsBundle bundle) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.dropwizard.client;

import io.dropwizard.configuration.ResourceConfigurationSourceProvider;
import io.dropwizard.configuration.YamlConfigurationFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators;
import io.dropwizard.util.Resources;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

class JerseyClientConfigurationTest {
Expand All @@ -16,7 +14,7 @@ class JerseyClientConfigurationTest {
void testBasicJerseyClient() throws Exception {
final JerseyClientConfiguration configuration = new YamlConfigurationFactory<>(JerseyClientConfiguration.class,
Validators.newValidator(), Jackson.newObjectMapper(), "dw")
.build(new File(Resources.getResource("yaml/jersey-client.yml").toURI()));
.build(new ResourceConfigurationSourceProvider(), "yaml/jersey-client.yml");
assertThat(configuration.getMinThreads()).isEqualTo(8);
assertThat(configuration.getMaxThreads()).isEqualTo(64);
assertThat(configuration.getWorkQueueSize()).isEqualTo(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.dropwizard.util.Duration;
import io.dropwizard.util.Resources;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -25,7 +24,7 @@
@ExtendWith(DropwizardExtensionsSupport.class)
class JerseyIgnoreRequestUserAgentHeaderFilterTest {
public static final DropwizardAppExtension<Configuration> APP_RULE =
new DropwizardAppExtension<>(TestApplication.class, Resources.getResource("yaml/jerseyIgnoreRequestUserAgentHeaderFilterTest.yml").getPath());
new DropwizardAppExtension<>(TestApplication.class, JerseyIgnoreRequestUserAgentHeaderFilterTest.class.getResource("/yaml/jerseyIgnoreRequestUserAgentHeaderFilterTest.yml").getPath());

private final URI testUri = URI.create("http://localhost:" + APP_RULE.getLocalPort());
private JerseyClientBuilder clientBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import io.dropwizard.configuration.ConfigurationParsingException;
import io.dropwizard.configuration.ConfigurationValidationException;
import io.dropwizard.configuration.DefaultConfigurationFactoryFactory;
import io.dropwizard.configuration.ResourceConfigurationSourceProvider;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators;
import io.dropwizard.util.Resources;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;

import static java.util.Objects.requireNonNull;
Expand All @@ -29,7 +28,7 @@ private void load(String configLocation) throws Exception {
HttpClientConfiguration.class,
Validators.newValidator(),
objectMapper, "dw"
).build(new File(Resources.getResource(configLocation).toURI()));
).build(new ResourceConfigurationSourceProvider(), configLocation);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.util.Maps;
import io.dropwizard.util.Resources;
import io.dropwizard.validation.BaseValidator;
import org.assertj.core.api.ThrowableAssertAlternative;
import org.assertj.core.data.MapEntry;
Expand All @@ -15,8 +14,6 @@
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -146,18 +143,16 @@ public Example build() {
return new Example();
}
};
protected File malformedFile = new File("/");
protected File emptyFile = new File("/");
protected File invalidFile = new File("/");
protected File validFile = new File("/");
protected File validNoTypeFile = new File("/");
protected File typoFile = new File("/");
protected File wrongTypeFile = new File("/");
protected File malformedAdvancedFile = new File("/");

protected static File resourceFileName(String resourceName) throws URISyntaxException {
return new File(Resources.getResource(resourceName).toURI());
}
protected String malformedFile = "/";
protected String emptyFile = "/";
protected String invalidFile = "/";
protected String validFile = "/";
protected String validNoTypeFile = "/";
protected String typoFile = "/";
protected String wrongTypeFile = "/";
protected String malformedAdvancedFile = "/";

protected ConfigurationSourceProvider configurationSourceProvider = new ResourceConfigurationSourceProvider();

@AfterEach
void resetConfigOverrides() {
Expand All @@ -181,7 +176,7 @@ void usesDefaultedCacheBuilderSpec() throws Exception {

@Test
void loadsValidConfigFiles() throws Exception {
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);

assertThat(example.getName())
.isEqualTo("Coda Hale");
Expand All @@ -205,31 +200,31 @@ void loadsValidConfigFiles() throws Exception {
@Test
void handlesSimpleOverride() throws Exception {
System.setProperty("dw.name", "Coda Hale Overridden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getName())
.isEqualTo("Coda Hale Overridden");
}

@Test
void handlesExistingOverrideWithPeriod() throws Exception {
System.setProperty("dw.my\\.logger.level", "debug");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getLogger())
.containsEntry("level", "debug");
}

@Test
void handlesNewOverrideWithPeriod() throws Exception {
System.setProperty("dw.my\\.logger.com\\.example", "error");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getLogger())
.containsEntry("com.example", "error");
}

@Test
void handlesArrayOverride() throws Exception {
System.setProperty("dw.type", "coder,wizard,overridden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getType().get(2))
.isEqualTo("overridden");
assertThat(example.getType().size())
Expand All @@ -239,7 +234,7 @@ void handlesArrayOverride() throws Exception {
@Test
void handlesArrayOverrideEscaped() throws Exception {
System.setProperty("dw.type", "coder,wizard,overr\\,idden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getType().get(2))
.isEqualTo("overr,idden");
assertThat(example.getType().size())
Expand All @@ -249,7 +244,7 @@ void handlesArrayOverrideEscaped() throws Exception {
@Test
void handlesSingleElementArrayOverride() throws Exception {
System.setProperty("dw.type", "overridden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getType().get(0))
.isEqualTo("overridden");
assertThat(example.getType().size())
Expand All @@ -259,7 +254,7 @@ void handlesSingleElementArrayOverride() throws Exception {
@Test
void handlesArrayOverrideIntoValidNoTypeFile() throws Exception {
System.setProperty("dw.type", "coder,wizard,overridden");
final Example example = factory.build(validNoTypeFile);
final Example example = factory.build(configurationSourceProvider, validNoTypeFile);
assertThat(example.getType().get(2))
.isEqualTo("overridden");
assertThat(example.getType().size())
Expand All @@ -269,7 +264,7 @@ void handlesArrayOverrideIntoValidNoTypeFile() throws Exception {
@Test
void overridesArrayWithIndices() throws Exception {
System.setProperty("dw.type[1]", "overridden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);

assertThat(example.getType().get(0))
.isEqualTo("coder");
Expand All @@ -280,7 +275,7 @@ void overridesArrayWithIndices() throws Exception {
@Test
void overridesArrayWithIndicesReverse() throws Exception {
System.setProperty("dw.type[0]", "overridden");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);

assertThat(example.getType().get(0))
.isEqualTo("overridden");
Expand All @@ -292,7 +287,7 @@ void overridesArrayWithIndicesReverse() throws Exception {
void overridesArrayPropertiesWithIndices() throws Exception {
System.setProperty("dw.servers[0].port", "7000");
System.setProperty("dw.servers[2].port", "9000");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);

assertThat(example.getServers())
.hasSize(3);
Expand All @@ -305,7 +300,7 @@ void overridesArrayPropertiesWithIndices() throws Exception {
@Test
void overrideMapProperty() throws Exception {
System.setProperty("dw.properties.settings.enabled", "true");
final Example example = factory.build(validFile);
final Example example = factory.build(configurationSourceProvider, validFile);
assertThat(example.getProperties())
.contains(MapEntry.entry("debug", "true"),
MapEntry.entry("settings.enabled", "true"));
Expand All @@ -315,55 +310,55 @@ void overrideMapProperty() throws Exception {
void throwsAnExceptionOnUnexpectedArrayOverride() {
System.setProperty("dw.servers.port", "9000");
assertThatIllegalArgumentException()
.isThrownBy(() -> factory.build(validFile))
.isThrownBy(() -> factory.build(configurationSourceProvider, validFile))
.withMessageContaining("target is an array but no index specified");
}

@Test
void throwsAnExceptionOnArrayOverrideWithInvalidType() {
System.setProperty("dw.servers", "one,two");

assertThatExceptionOfType(ConfigurationParsingException.class).isThrownBy(() -> factory.build(validFile));
assertThatExceptionOfType(ConfigurationParsingException.class).isThrownBy(() -> factory.build(configurationSourceProvider, validFile));
}

@Test
void throwsAnExceptionOnOverrideArrayIndexOutOfBounds() {
System.setProperty("dw.type[2]", "invalid");
assertThatExceptionOfType(ArrayIndexOutOfBoundsException.class)
.isThrownBy(() -> factory.build(validFile))
.isThrownBy(() -> factory.build(configurationSourceProvider, validFile))
.withMessageContaining("index is greater than size of array");
}

@Test
void throwsAnExceptionOnOverrideArrayPropertyIndexOutOfBounds() {
System.setProperty("dw.servers[4].port", "9000");
assertThatExceptionOfType(ArrayIndexOutOfBoundsException.class)
.isThrownBy(() -> factory.build(validFile))
.isThrownBy(() -> factory.build(configurationSourceProvider, validFile))
.withMessageContaining("index is greater than size of array");
}

@Test
void throwsAnExceptionOnMalformedFiles() {
assertThatExceptionOfType(ConfigurationParsingException.class)
.isThrownBy(() -> factory.build(malformedFile));
.isThrownBy(() -> factory.build(configurationSourceProvider, malformedFile));
}

@Test
void throwsAnExceptionOnEmptyFiles() {
assertThatExceptionOfType(ConfigurationParsingException.class)
.isThrownBy(() -> factory.build(emptyFile))
.isThrownBy(() -> factory.build(configurationSourceProvider, emptyFile))
.withMessageContaining(" * Configuration at " + emptyFile.toString() + " must not be empty");
}

@Test
void throwsAnExceptionOnInvalidFiles() {
ThrowableAssertAlternative<ConfigurationValidationException> t = assertThatExceptionOfType(ConfigurationValidationException.class)
.isThrownBy(() -> factory.build(invalidFile));
.isThrownBy(() -> factory.build(configurationSourceProvider, invalidFile));

if ("en".equals(Locale.getDefault().getLanguage())) {
t.withMessageEndingWith(String.format(
"%s has an error:%n * name must match \"[\\w]+[\\s]+[\\w]+([\\s][\\w]+)?\"%n",
invalidFile.getName()));
invalidFile));
}
}

Expand Down Expand Up @@ -415,13 +410,13 @@ void throwsAnExceptionIfDefaultConfigurationCantBeInstantiated() {
@Test
void incorrectTypeIsFound() {
assertThatExceptionOfType(ConfigurationParsingException.class)
.isThrownBy(() -> factory.build(wrongTypeFile))
.isThrownBy(() -> factory.build(configurationSourceProvider, wrongTypeFile))
.withMessage(String.format("%s has an error:" + NEWLINE +
" * Incorrect type of value at: age; is of type: String, expected: int" + NEWLINE, wrongTypeFile));
}

@Test
void printsDetailedInformationOnMalformedContent() throws Exception {
factory.build(malformedAdvancedFile);
factory.build(configurationSourceProvider, malformedAdvancedFile);
}
}
Loading

0 comments on commit 3856b1b

Please sign in to comment.