Skip to content

Commit

Permalink
Test and fix Kotlin source generation to work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Nov 25, 2023
1 parent 72ce3c5 commit e79d563
Show file tree
Hide file tree
Showing 20 changed files with 527 additions and 228 deletions.
2 changes: 1 addition & 1 deletion src/it/java-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate-java</goal>
<goal>generate</goal>
</goals>
</execution>
</executions>
Expand Down
12 changes: 6 additions & 6 deletions src/it/java-simple/test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import java.nio.file.Path
import static org.assertj.core.api.Assertions.assertThat

Path baseDirectory = basedir.toPath().toAbsolutePath()
def generatedSourcesPath = baseDirectory.resolve("target/generated-sources/protoc-java")
def classesPath = baseDirectory.resolve("target/classes")
def generatedSourcesDir = baseDirectory.resolve("target/generated-sources/protobuf")
def classesDir = baseDirectory.resolve("target/classes")
def expectedGeneratedFiles = [
"org/example/helloworld/Helloworld",
"org/example/helloworld/GreetingRequest",
"org/example/helloworld/GreetingRequestOrBuilder",
]

assertThat(generatedSourcesPath).isDirectory()
assertThat(generatedSourcesDir).isDirectory()

assertThat(classesPath).isDirectory()
assertThat(classesDir).isDirectory()

expectedGeneratedFiles.forEach {
assertThat(generatedSourcesPath.resolve("${it}.java"))
assertThat(generatedSourcesDir.resolve("${it}.java"))
.exists()
.isNotEmptyFile()
assertThat(classesPath.resolve("${it}.class"))
assertThat(classesDir.resolve("${it}.class"))
.exists()
.isNotEmptyFile()

Expand Down
2 changes: 1 addition & 1 deletion src/it/java-test-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>generate-test-java</goal>
<goal>generate-test</goal>
</goals>
</execution>
</executions>
Expand Down
13 changes: 6 additions & 7 deletions src/it/java-test-simple/test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ import java.nio.file.Path
import static org.assertj.core.api.Assertions.assertThat

Path baseDirectory = basedir.toPath().toAbsolutePath()
def generatedSourcesPath = baseDirectory.resolve("target/generated-test-sources/protoc-java")
def classesPath = baseDirectory.resolve("target/test-classes")
def generatedSourcesDir = baseDirectory.resolve("target/generated-test-sources/protobuf")
def classesDir = baseDirectory.resolve("target/test-classes")
def expectedGeneratedFiles = [
"org/example/helloworld/Helloworld",
"org/example/helloworld/GreetingRequest",
"org/example/helloworld/GreetingRequestOrBuilder",
]

assertThat(generatedSourcesPath).isDirectory()
assertThat(generatedSourcesDir).isDirectory()

assertThat(classesPath).isDirectory()
assertThat(classesDir).isDirectory()

expectedGeneratedFiles.forEach {
assertThat(generatedSourcesPath.resolve("${it}.java"))
assertThat(generatedSourcesDir.resolve("${it}.java"))
.exists()
.isNotEmptyFile()
assertThat(classesPath.resolve("${it}.class"))
assertThat(classesDir.resolve("${it}.class"))
.exists()
.isNotEmptyFile()

}
2 changes: 2 additions & 0 deletions src/it/kotlin-simple/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = clean package
invoker.quiet = false
113 changes: 113 additions & 0 deletions src/it/kotlin-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2023, Ashley Scopes.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<junit.version>5.10.1</junit.version>
<kotlin.version>1.9.21</kotlin.version>
<protobuf.version>3.25.0</protobuf.version>

<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-kotlin</artifactId>
<version>${protobuf.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>

<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>

<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>${plugin-group-id}</groupId>
<artifactId>${plugin-artifact-id}</artifactId>
<version>${plugin-version}</version>

<configuration>
<generateKotlinWrappers>true</generateKotlinWrappers>
<version>${protobuf.version}</version>
</configuration>

<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
27 changes: 27 additions & 0 deletions src/it/kotlin-simple/src/main/protobuf/helloworld.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (C) 2023, Ashley Scopes.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

option java_multiple_files = true;
option java_package = "org.example.helloworld";

package org.example.helloworld;

message GreetingRequest {
string name = 1;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023, Ashley Scopes.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.example.helloworld

import java.io.ByteArrayOutputStream
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Test

class ProtobufTest {

@Test
fun generatedProtobufSourcesAreValid() {
// Given
val expectedGreetingRequest = greetingRequest {
name = "Ashley"
}

// When
val baos = ByteArrayOutputStream()
expectedGreetingRequest.writeTo(baos);
val actualGreetingRequest = GreetingRequest.parseFrom(baos.toByteArray())

assertNotEquals(0, baos.toByteArray().size)

// Then
assertEquals(expectedGreetingRequest.name, actualGreetingRequest.name)
}
}
51 changes: 51 additions & 0 deletions src/it/kotlin-simple/test.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2023, Ashley Scopes.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.nio.file.Path
import static org.assertj.core.api.Assertions.assertThat

Path baseDirectory = basedir.toPath().toAbsolutePath()
def generatedSourcesDir = baseDirectory.resolve("target/generated-sources/protobuf")
def classesDir = baseDirectory.resolve("target/classes")
def expectedGeneratedJavaFiles = [
"org/example/helloworld/Helloworld",
"org/example/helloworld/GreetingRequest",
"org/example/helloworld/GreetingRequestOrBuilder",
]
def expectedGeneratedKotlinFiles = [
"org/example/helloworld/GreetingRequestKt",
]

assertThat(generatedSourcesDir).isDirectory()

assertThat(classesDir).isDirectory()

expectedGeneratedJavaFiles.forEach {
assertThat(generatedSourcesDir.resolve("${it}.java"))
.exists()
.isNotEmptyFile()
assertThat(classesDir.resolve("${it}.class"))
.exists()
.isNotEmptyFile()
}

expectedGeneratedKotlinFiles.forEach {
assertThat(generatedSourcesDir.resolve("${it}.kt"))
.exists()
.isNotEmptyFile()
assertThat(classesDir.resolve("${it}.class"))
.exists()
.isNotEmptyFile()
}
2 changes: 2 additions & 0 deletions src/it/kotlin-test-simple/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = clean package
invoker.quiet = false
Loading

0 comments on commit e79d563

Please sign in to comment.