Skip to content

Commit

Permalink
More converters + cleanup (#53)
Browse files Browse the repository at this point in the history
* More converters + cleanup

* Set additionalTestRunsOnJvmVersions in gradle.properties

* Cleanup GitHub actions

* Docs update

* Do a clean on gradle build

* Fix additional test runs on JVM versions
  • Loading branch information
joel-jeremy committed Aug 19, 2022
1 parent 57e1bc5 commit be677a8
Show file tree
Hide file tree
Showing 64 changed files with 2,076 additions and 485 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/gradle-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
uses: actions/setup-java@v3
with:
java-version: 11
distribution: 'temurin'
distribution: temurin

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

- name: Build with Gradle
run: ./gradlew build
env:
ADDITIONAL_TEST_RUNS_ON_JVM_VERSIONS: 17,18
run: ./gradlew build
6 changes: 3 additions & 3 deletions .github/workflows/publish-to-github-packages.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Publish Snapshot to GitHub Packages
on:
workflow_run:
workflows: ["Scans"]
workflows: [Scans]
branches: [main]
types: [completed]
jobs:
Expand All @@ -16,8 +16,8 @@ jobs:

- uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
java-version: 11
distribution: temurin

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish-to-ossrh.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Publish Snapshot to OSSRH
on:
workflow_run:
workflows: ["Scans"]
workflows: [Scans]
branches: [main]
types: [completed]
jobs:
Expand All @@ -16,8 +16,8 @@ jobs:

- uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
java-version: 11
distribution: temurin

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-to-github-packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:

- uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
java-version: 11
distribution: temurin

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-to-ossrh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:

- uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
java-version: 11
distribution: temurin

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
97 changes: 97 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Contributing Guidelines

First of all, thank you for having the interest in contributing to this project!

## Found a bug?

Please create an issue describing the following:

- The version the bug was discovered.
- A scenario to reproduce the bug.

## Enhancement ideas?

Got an idea to enhance the library? Please feel free to create an issue describing the feature proposal. Any ideas are welcome! :)

## Build

The project uses Java 11 as runtime for Gradle but compiles source code to Java 8.

To build the project, run the command:

```sh
./gradlew clean build
```

To create reports, run the commands:

```sh
./gradlew clean build testAggregateTestReport
```

```sh
./gradlew clean build testCodeCoverageReport
```

Tests are run in multiple JVM runtimes. By default, it is run in LTS versions (succeeding the version used in source compilation) + the latest released non-LTS version. Test runtimes are overrideable by setting the `ADDITIONAL_TEST_RUNS_ON_JVM_VERSIONS` environment variable or `additionalTestRunsOnJvmVersions` system property e.g. `ADDITIONAL_TEST_RUNS_ON_JVM_VERSIONS=8,17,18` / `additionalTestRunsOnJvmVersions=8,17,18`.

## Development Guidelines

### Unit Test Structure

Unit tests in this project follow a specific structure.

- Classes must have a corresponding test class i.e. `MapResolver` -> `MapResolverTests`. The test class must be in the exact same java package as the class it corresponds to.
- Test classes are nested in structure. Each method in the class under test must have a corresponding `@Nested` test class. Each `@Nested` test class must test scenarios that is supported by the method it corresponds to.

```java
// Class under test: io.github.joeljeremy7.externalizedproperties.resolver.my.MyResolver
class MyResolver implements Resolver {
public MyResolver(...) {
...
}

public Optional<String> resolve(InvocationContext context, String propertyName) {
...
}

public String someOtherMethod(...) {
...
}

public static class Builder {
...
public MyResolver build() {
...
}
}
}

// Test class: io.github.joeljeremy7.externalizedproperties.resolver.my.MyResolverTests
class MyResolverTests {
@Nested
class Constructor {
// @Test methods here...
}

@Nested
class ResolveMethod {
// @Test methods here...
}

@Nested
class SomeOtherMethod {
// @Test methods here...
}

// Nested class must also have corresponding test classes
@Nested
class BuilderTests {
...
@Nested
class BuildMethod {
// @Test methods here...
}
}
}
```
21 changes: 9 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,8 @@ configure(javaProjects) {
apply from: "${rootDir}/gradle/code-quality.gradle"
apply from: "${rootDir}/gradle/multi-jvm-tests.gradle"

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:${versions.junitJupiter}"
}

testing {
suites {
test {
useJUnitJupiter()
}
suites {
integrationTest(JvmTestSuite) {
testType = TestSuiteType.INTEGRATION_TEST
targets {
Expand All @@ -59,10 +52,9 @@ configure(javaProjects) {
}
}
}
}

compileJava {
options.release = 11
suites.configureEach {
useJUnitJupiter("${versions.junitJupiter}")
}
}

java {
Expand All @@ -73,6 +65,11 @@ configure(javaProjects) {
}
}

tasks.withType(JavaCompile).configureEach {
// For some reason, options.release flag doesn't work with compileJava.
options.release = 8
}

javadoc {
configure(options) {
tags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public T value() {
return value;
}

/** {@inheritDoc}} */
/** {@inheritDoc} */
@Override
public int hashCode() {
return Objects.hashCode(value);
}

/** {@inheritDoc}} */
/** {@inheritDoc} */
@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ default ConversionResult<T> convert(
*
* @param context The invocation context.
* @param valueToConvert The value to convert.
* @param targetType The target type of the conversion.
* @param targetType The target type of the conversion. In the case of converter facades
* (see {@link ConverterFacade}), target type may be different from the invoked proxy
* method's return type.
* @return The result of conversion to the target type or {@link ConversionResult#skip()}
* if the converter cannot handle conversion to the target type and that the conversion
* process should skip/move to the next registered converter in the conversion pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private OrdinalResolver(int ordinal, Resolver decorated) {
this.decorated = requireNonNull(decorated, "decorated");
}

/** {@inheritDoc}} */
/** {@inheritDoc} */
@Override
public Optional<String> resolve(InvocationContext context, String propertyName) {
return decorated.resolve(context, propertyName);
Expand Down Expand Up @@ -212,13 +212,13 @@ public OrdinalConverter(int ordinal, Converter<T> decorated) {
this.decorated = requireNonNull(decorated, "decorated");;
}

/** {@inheritDoc}} */
/** {@inheritDoc} */
@Override
public boolean canConvertTo(Class<?> targetType) {
return decorated.canConvertTo(targetType);
}

/** {@inheritDoc}} */
/** {@inheritDoc} */
@Override
public ConversionResult<T> convert(
InvocationContext context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.joeljeremy7.externalizedproperties.core.conversion.converters;

import io.github.joeljeremy7.externalizedproperties.core.ConversionResult;
import io.github.joeljeremy7.externalizedproperties.core.Converter;
import io.github.joeljeremy7.externalizedproperties.core.InvocationContext;

import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
* Supports conversion of values to a {@link BigDecimal}.
*/
public class BigDecimalConverter implements Converter<BigDecimal> {

/** {@inheritDoc} */
@Override
public boolean canConvertTo(Class<?> targetType) {
return BigDecimal.class.equals(targetType);
}

/** {@inheritDoc} */
@Override
public ConversionResult<BigDecimal> convert(
InvocationContext context,
String valueToConvert,
Type targetType
) {
return ConversionResult.of(new BigDecimal(valueToConvert));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.joeljeremy7.externalizedproperties.core.conversion.converters;

import io.github.joeljeremy7.externalizedproperties.core.ConversionResult;
import io.github.joeljeremy7.externalizedproperties.core.Converter;
import io.github.joeljeremy7.externalizedproperties.core.InvocationContext;

import java.lang.reflect.Type;
import java.math.BigInteger;

/**
* Supports conversion of values to a {@link BigInteger}.
*/
public class BigIntegerConverter implements Converter<BigInteger> {

/** {@inheritDoc} */
@Override
public boolean canConvertTo(Class<?> targetType) {
return BigInteger.class.equals(targetType);
}

/** {@inheritDoc} */
@Override
public ConversionResult<BigInteger> convert(
InvocationContext context,
String valueToConvert,
Type targetType
) {
return ConversionResult.of(new BigInteger(valueToConvert));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.joeljeremy7.externalizedproperties.core.conversion.converters;

import io.github.joeljeremy7.externalizedproperties.core.ConversionResult;
import io.github.joeljeremy7.externalizedproperties.core.Converter;
import io.github.joeljeremy7.externalizedproperties.core.InvocationContext;
import io.github.joeljeremy7.externalizedproperties.core.conversion.ConversionException;

import java.lang.reflect.Type;

/**
* Supports conversion of values to a {@link Class}.
*/
public class ClassConverter implements Converter<Class<?>> {

/** {@inheritDoc} */
@Override
public boolean canConvertTo(Class<?> targetType) {
return Class.class.equals(targetType);
}

/** {@inheritDoc} */
@Override
public ConversionResult<Class<?>> convert(
InvocationContext context,
String valueToConvert,
Type targetType
) {
try {
return ConversionResult.of(Class.forName(valueToConvert));
} catch (ClassNotFoundException e) {
throw new ConversionException(
String.format("Failed to load as Class: %s", valueToConvert),
e
);
}
}
}

0 comments on commit be677a8

Please sign in to comment.