Skip to content

Commit

Permalink
GitResolver + Remove checked exceptions in ResourceResolver (#38)
Browse files Browse the repository at this point in the history
* GitResolver + Remove checked exceptions in ResourceResolver

* Move app.properties to test resources

* GitResolver updates and tests

* Fix typo

* Revamp GitResolver APIs

* Javadoc updates

* Cleanup + Fix multi JVM tests

* Fix Gradle test tasks ordering

* Fix getOrDefault in multi-jvm-tests.gradle

* Move InvocationHandlerFactory to internal
  • Loading branch information
joel-jeremy committed Jun 28, 2022
1 parent 523ab91 commit 340f504
Show file tree
Hide file tree
Showing 43 changed files with 1,902 additions and 177 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/gradle-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
# LTS versions + the latest java version
java: [ 8, 11, 17, 18 ]

steps:
- uses: actions/checkout@v3

- name: Set up JDK ${{ matrix.java }}
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java }}
java-version: 11
distribution: 'temurin'

- name: Validate Gradle wrapper
Expand All @@ -24,4 +19,6 @@ jobs:
uses: gradle/gradle-build-action@v2

- name: Build with Gradle
run: ./gradlew build
run: ./gradlew build
env:
ADDITIONAL_TEST_RUNS_ON_JVM_VERSIONS: 17,18
30 changes: 9 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ apply from: "${rootDir}/gradle/reporting.gradle"
apply from: "${rootDir}/gradle/coveralls.gradle"
apply from: "${rootDir}/gradle/snyk.gradle"
apply from: "${rootDir}/gradle/sonarqube.gradle"
apply from: "${rootDir}/gradle/dependency-updates.gradle"

allprojects {
apply from: "${rootDir}/gradle/dependencies.gradle"
apply from: "${rootDir}/gradle/dependency-versions.gradle"

group = "io.github.joeljeremy7.externalizedproperties"

Expand All @@ -36,6 +37,7 @@ configure(javaProjects) {

apply from: "${rootDir}/gradle/publications.gradle"
apply from: "${rootDir}/gradle/code-quality.gradle"
apply from: "${rootDir}/gradle/multi-jvm-tests.gradle"

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:${versions.junitJupiter}"
Expand All @@ -44,7 +46,7 @@ configure(javaProjects) {
testing {
suites {
test {
useJUnitJupiter()
useJUnitJupiter()
}
integrationTest(JvmTestSuite) {
testType = TestSuiteType.INTEGRATION_TEST
Expand All @@ -59,18 +61,18 @@ configure(javaProjects) {
}
}

compileJava {
options.release = 11
}

java {
archivesBaseName = rootProject.relativeProjectPath(project.path).replace(":", "-")

toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}

compileJava {
options.release = 8
}

javadoc {
configure(options) {
tags(
Expand All @@ -93,18 +95,4 @@ if (project.hasProperty("ossrh")) {
}
}
}
}

def isNonStable = { String version ->
def nonStableKeyword = ["PREVIEW","ALPHA","BETA", "SNAPSHOT"].any {
it -> version.toUpperCase().contains(it)
}
return nonStableKeyword
}
tasks.named("dependencyUpdates").configure {
rejectVersionIf {
isNonStable(it.candidate.version) && !isNonStable(it.currentVersion)
}
checkForGradleUpdate = true
outputFormatter = "html"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.joeljeremy7.externalizedproperties.core.conversion.converters.DefaultConverter;
import io.github.joeljeremy7.externalizedproperties.core.internal.CachingExternalizedProperties;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationCacheKey;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.SystemExternalizedProperties;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.ExpiringCacheStrategy;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.WeakConcurrentHashMapCacheStrategy;
Expand All @@ -13,7 +14,6 @@
import io.github.joeljeremy7.externalizedproperties.core.internal.proxy.EagerLoadingInvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.proxy.ExternalizedPropertiesInvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.resolvers.RootResolver;
import io.github.joeljeremy7.externalizedproperties.core.proxy.InvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.resolvers.DefaultResolver;
import io.github.joeljeremy7.externalizedproperties.core.resolvers.VariableExpandingResolver;
import io.github.joeljeremy7.externalizedproperties.core.variableexpansion.NoOpVariableExpander;
Expand Down Expand Up @@ -90,7 +90,7 @@ static Builder builder() {
}

/**
* Builder for {@link ExternalizedProperties}.
* The builder for {@link ExternalizedProperties}.
*/
static class Builder implements BuilderConfiguration {
private final List<ProfilesBuilder> profilesBuilders = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public static String requireNonNullOrEmpty(@Nullable String arg, String argName)
return arg;
}

/**
* Require argument to not be {@code null} or an empty {@link String}.
*
* @param arg The {@link String} argument.
* @param argName The name of the {@link String} argument to be used in building the
* {@link IllegalArgumentException} message if the argument failed validation.
* @return The {@link String} argument.
*/
public static String requireNonNullOrBlank(@Nullable String arg, String argName) {
if (arg == null || arg.chars().allMatch(Character::isWhitespace)) {
throw new IllegalArgumentException(argName + " must not be null or empty.");
}
return arg;
}

/**
* Require argument to not be {@code null} or an empty {@link Collection}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.github.joeljeremy7.externalizedproperties.core.proxy;
package io.github.joeljeremy7.externalizedproperties.core.internal;

import io.github.joeljeremy7.externalizedproperties.core.Converter;
import io.github.joeljeremy7.externalizedproperties.core.InvocationContext;
import io.github.joeljeremy7.externalizedproperties.core.Resolver;
import io.github.joeljeremy7.externalizedproperties.core.VariableExpander;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationContextFactory;

import java.lang.reflect.InvocationHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.github.joeljeremy7.externalizedproperties.core.TypeReference;
import io.github.joeljeremy7.externalizedproperties.core.VariableExpander;
import io.github.joeljeremy7.externalizedproperties.core.VariableExpanderFacade;
import io.github.joeljeremy7.externalizedproperties.core.proxy.InvocationHandlerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.github.joeljeremy7.externalizedproperties.core.VariableExpander;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationCacheKey;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationContextFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.WeakConcurrentHashMapCacheStrategy;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.WeakHashMapCacheStrategy;
import io.github.joeljeremy7.externalizedproperties.core.proxy.InvocationHandlerFactory;

import java.lang.reflect.Method;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.github.joeljeremy7.externalizedproperties.core.VariableExpander;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationCacheKey;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationContextFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.WeakConcurrentHashMapCacheStrategy;
import io.github.joeljeremy7.externalizedproperties.core.internal.caching.WeakHashMapCacheStrategy;
import io.github.joeljeremy7.externalizedproperties.core.proxy.InvocationHandlerFactory;

import java.lang.reflect.Method;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ else if ("hashCode".equals(method.getName())) {
}
} else if (method.getParameterCount() == 1) {
if ("equals".equals(method.getName()) &&
method.getParameterTypes()[0].equals(Object.class)) {
Object.class.equals(method.getParameterTypes()[0])) {
return proxyEquals(proxy, args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.joeljeremy7.externalizedproperties.core.Resolver;
import io.github.joeljeremy7.externalizedproperties.core.VariableExpander;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationContextFactory;
import io.github.joeljeremy7.externalizedproperties.core.proxy.InvocationHandlerFactory;
import io.github.joeljeremy7.externalizedproperties.core.internal.InvocationHandlerFactory;

/**
* The factory for {@link ExternalizedPropertiesInvocationHandler}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.joeljeremy7.externalizedproperties.core.resolvers;

import io.github.joeljeremy7.externalizedproperties.core.ExternalizedPropertiesException;
import io.github.joeljeremy7.externalizedproperties.core.Resolver;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -63,9 +66,8 @@ public class ResourceResolver extends MapResolver {
* </pre></blockquote>
*
* @param url The URL resource to read the properties from.
* @throws IOException if an I/O exception occurs.
*/
private ResourceResolver(URL url) throws IOException {
private ResourceResolver(URL url) {
this(url, new PropertiesReader());
}

Expand All @@ -75,10 +77,9 @@ private ResourceResolver(URL url) throws IOException {
* @param url The URL resource to read the properties from.
* @param reader The reader which reads/parses properties from the URL into
* a {@link Map} instance.
* @throws IOException if an I/O exception occurs.
*/
private ResourceResolver(URL url, ResourceReader reader) throws IOException {
super(readFromUrl(
private ResourceResolver(URL url, ResourceReader reader) {
super(readMapFromUrl(
requireNonNull(url, "url"),
requireNonNull(reader, "reader")
));
Expand Down Expand Up @@ -106,9 +107,8 @@ private ResourceResolver(URL url, ResourceReader reader) throws IOException {
* @param url The URL resource to read properties from.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link URL}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromUrl(URL url) throws IOException {
public static ResourceResolver fromUrl(URL url) {
return new ResourceResolver(url);
}

Expand All @@ -130,9 +130,8 @@ public static ResourceResolver fromUrl(URL url) throws IOException {
* a {@link Map} instance.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link URL}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromUrl(URL url, ResourceReader reader) throws IOException {
public static ResourceResolver fromUrl(URL url, ResourceReader reader) {
return new ResourceResolver(url, reader);
}

Expand Down Expand Up @@ -162,10 +161,9 @@ public static ResourceResolver fromUrl(URL url, ResourceReader reader) throws IO
* @param uri The URI resource to read properties from.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link URL}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromUri(URI uri) throws IOException {
return fromUrl(requireNonNull(uri, "uri").toURL());
public static ResourceResolver fromUri(URI uri) {
return fromUrl(toUrl(requireNonNull(uri, "uri")));
}

/**
Expand All @@ -191,10 +189,9 @@ public static ResourceResolver fromUri(URI uri) throws IOException {
* a {@link Map} instance.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link URL}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromUri(URI uri, ResourceReader reader) throws IOException {
return fromUrl(requireNonNull(uri, "uri").toURL(), reader);
public static ResourceResolver fromUri(URI uri, ResourceReader reader) {
return fromUrl(toUrl(requireNonNull(uri, "uri")), reader);
}

/**
Expand Down Expand Up @@ -223,10 +220,9 @@ public static ResourceResolver fromUri(URI uri, ResourceReader reader) throws IO
* @param path The path resource to read properties from.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link URL}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromPath(Path path) throws IOException {
return fromUri(requireNonNull(path, "path").toUri());
public static ResourceResolver fromPath(Path path) {
return fromUrl(toUrl(requireNonNull(path, "path")));
}

/**
Expand All @@ -252,22 +248,29 @@ public static ResourceResolver fromPath(Path path) throws IOException {
* a {@link Map} instance.
* @return The {@link ResourceResolver} which reads and resolves properties from the
* given {@link Path}.
* @throws IOException if an I/O exception occurs.
*/
public static ResourceResolver fromPath(Path path, ResourceReader reader) throws IOException {
return fromUri(requireNonNull(path, "path").toUri(), reader);
public static ResourceResolver fromPath(Path path, ResourceReader reader) {
return fromUrl(toUrl(requireNonNull(path, "path")), reader);
}

private static Map<String, String> readFromUrl(URL url, ResourceReader reader)
throws IOException
{
private static Map<String, String> readMapFromUrl(URL url, ResourceReader reader) {
Map<String, String> result = new LinkedHashMap<>();
String resourceContent = readString(url.openStream());
// Add the raw resource String as property.
result.put(url.toString(), resourceContent);
Map<String, Object> properties = reader.read(resourceContent);
// Flatten the properties.
result.putAll(flattenMap(properties));
try {
String resourceContent = readString(url.openStream());
// Add the raw resource String as property.
result.put(url.toString(), resourceContent);
Map<String, Object> properties = reader.read(resourceContent);
// Flatten the properties.
result.putAll(flattenMap(properties));
} catch (IOException e) {
throw new ExternalizedPropertiesException(
String.format(
"An exception occurred while trying to read resource: %s",
url.toString()
),
e
);
}
return result;
}

Expand Down Expand Up @@ -394,6 +397,18 @@ private static String determineKey(
return key;
}

private static URL toUrl(URI uri) {
try {
return uri.toURL();
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}
}

private static URL toUrl(Path path) {
return toUrl(path.toUri());
}

/**
* API for reading properties from a resource based on the format supported by the
* {@link ResourceReader} implementation.
Expand Down

0 comments on commit 340f504

Please sign in to comment.