Skip to content

Commit

Permalink
Add test case that exercises treeAsTokens (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
chokoswitch authored Feb 7, 2024
1 parent da14b7f commit e37583c
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 54 deletions.
41 changes: 3 additions & 38 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import nebula.plugin.release.git.opinion.Strategies
import net.ltgt.gradle.errorprone.errorprone

plugins {
jacoco

id("org.curioswitch.curiostack.java-library")
id("org.curioswitch.curiostack.publishing")

id("org.curioswitch.gradle-protobuf-plugin")

id("com.github.ben-manes.versions")
id("io.github.gradle-nexus.publish-plugin")
id("nebula.release")
Expand Down Expand Up @@ -44,37 +41,20 @@ dependencies {
// Used by byte-buddy but not brought in as a transitive dependency.
compileOnly("com.google.code.findbugs:annotations")

testImplementation(project(":testing"))
testImplementation("com.google.protobuf:protobuf-java-util")
}

protobuf {
protoc { artifact.set("com.google.protobuf:protoc:3.22.3") }

descriptorSetOptions.enabled.set(false)
descriptorSetOptions.path.set(file("build/unused-descriptor-set"))
}

testing {
suites {
register<JvmTestSuite>("testDatabind") {
dependencies {
implementation(project)
implementation(project())
implementation(project(":testing"))

implementation("com.fasterxml.jackson.core:jackson-databind")
implementation("com.google.protobuf:protobuf-java-util")
}
sources {
java {
srcDir("build/generated/proto/test")
}
}
targets {
all {
testTask.configure {
dependsOn("generateTestProto")
}
}
}
}
}
}
Expand All @@ -83,28 +63,13 @@ tasks {
withType<JavaCompile>().configureEach {
with(options) {
release.set(8)

errorprone {
excludedPaths.set(
".*com.google.protobuf.util.*|.*org.curioswitch.common.protobuf.json.test.*",
)
}

// protoc generates code deprecated code so disable the lint.
if (name.contains("Test")) {
compilerArgs.add("-Xlint:-deprecation")
}
}
}

check {
dependsOn(testing.suites.named("testDatabind"))
}

spotlessJava {
dependsOn(named("generateTestProto"), named("generateTestdatabindProto"))
}

named("release") {
mustRunAfter("snapshotSetup", "finalSetup")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ dependencies {
testing {
suites.withType(JvmTestSuite::class).configureEach {
dependencies {
implementation(project)

implementation("org.assertj:assertj-core")
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.junit.jupiter:junit-jupiter-params")
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=b586e04868a22fd817c8971330fec37e298f3242eb85c374181b12d637f80302
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStorePath=wrapper/dists
18 changes: 14 additions & 4 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,10 +80,10 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
Expand Down Expand Up @@ -143,12 +143,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -205,6 +209,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
15 changes: 9 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,8 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dependencyResolutionManagement {
}
}

include(":dependencyManagement")
include(":dependencyManagement")
include(":testing")
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

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

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonTestProto;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -115,4 +118,28 @@ void doesNotCloseJsonParser() throws Exception {
marshaller.mergeValue(parser, JsonTestProto.TestAllTypes.newBuilder());
assertThat(parser.isClosed()).isFalse();
}

@Test
@SuppressWarnings("deprecation") // Test old API.
void treeAsTokens() throws Exception {
JsonTestProto.TestAllTypes message =
JsonTestProto.TestAllTypes.newBuilder().setOptionalInt32(10).build();

MessageMarshaller marshaller =
MessageMarshaller.builder()
.register(JsonTestProto.TestAllTypes.getDefaultInstance())
.build();

ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonFactory.Feature.INTERN_FIELD_NAMES, false);
mapper.registerModule(MessageMarshallerModule.of(marshaller));

String json = JsonFormat.printer().print(message);
JsonNode tree = mapper.readTree(json);
JsonParser parser = mapper.treeAsTokens(tree);

JsonTestProto.TestAllTypes.Builder builder = JsonTestProto.TestAllTypes.newBuilder();
marshaller.mergeValue(parser, builder);
assertThat(builder.build().getOptionalInt32()).isEqualTo(10);
}
}
39 changes: 39 additions & 0 deletions testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import net.ltgt.gradle.errorprone.errorprone

plugins {
id("org.curioswitch.curiostack.java-library")

id("org.curioswitch.gradle-protobuf-plugin")
}

dependencies {
api("com.google.protobuf:protobuf-java")
}

protobuf {
protoc { artifact.set("com.google.protobuf:protoc:3.22.3") }

descriptorSetOptions.enabled.set(false)
descriptorSetOptions.path.set(file("build/unused-descriptor-set"))
}

tasks {
withType<JavaCompile>().configureEach {
with(options) {
release.set(8)

errorprone {
excludedPaths.set(
".*com.google.protobuf.util.*|.*org.curioswitch.common.protobuf.json.test.*",
)
}

// protoc generates code deprecated code so disable the lint.
compilerArgs.add("-Xlint:-deprecation")
}
}

spotlessJavaCheck {
enabled = false
}
}

0 comments on commit e37583c

Please sign in to comment.