forked from devork/flit
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from github/gorzell/multi-file-java
Fix a number of cases that were not handled in the TypeMapper.
- Loading branch information
Showing
4 changed files
with
269 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=1.0.0 | ||
version=1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
plugin/src/test/java/com/flit/protoc/gen/server/TypeMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package com.flit.protoc.gen.server; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import com.google.protobuf.DescriptorProtos; | ||
import com.squareup.javapoet.ClassName; | ||
import org.junit.Test; | ||
|
||
public class TypeMapperTest { | ||
|
||
private static final String PROTO_PACKAGE = "flit.test"; | ||
private static final String JAVA_PACKAGE = "com.flit.test"; | ||
|
||
private static final DescriptorProtos.DescriptorProto MAP_MESSAGE = DescriptorProtos.DescriptorProto | ||
.newBuilder() | ||
.setName("Map") | ||
.build(); | ||
|
||
private static final DescriptorProtos.DescriptorProto MAPPER_MESSAGE = DescriptorProtos.DescriptorProto | ||
.newBuilder() | ||
.setName("Mapper") | ||
.build(); | ||
|
||
@Test | ||
public void protoPackage() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Map"); | ||
assertEquals(PROTO_PACKAGE, result.packageName()); | ||
assertEquals("Mapper", result.enclosingClassName().simpleName()); | ||
assertEquals("Map", result.simpleName()); | ||
} | ||
|
||
@Test | ||
public void protoPackageNameCollision() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAPPER_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Mapper"); | ||
assertEquals(PROTO_PACKAGE, result.packageName()); | ||
assertEquals("MapperOuterClass", result.enclosingClassName().simpleName()); | ||
assertEquals("Mapper", result.simpleName()); | ||
} | ||
|
||
@Test | ||
public void protoPackageWithOuterClass() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.setJavaOuterClassname("Mapper") | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Map"); | ||
assertEquals(PROTO_PACKAGE, result.packageName()); | ||
assertEquals("Mapper", result.enclosingClassName().simpleName()); | ||
assertEquals("Map", result.simpleName()); | ||
} | ||
|
||
@Test | ||
public void javaPackage() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.setJavaPackage(JAVA_PACKAGE) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Map"); | ||
assertEquals(JAVA_PACKAGE, result.packageName()); | ||
assertEquals("Mapper", result.enclosingClassName().simpleName()); | ||
assertEquals("Map", result.simpleName()); | ||
} | ||
|
||
@Test | ||
public void javaPackageNameCollision() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.setJavaPackage(JAVA_PACKAGE) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAPPER_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Mapper"); | ||
assertEquals(JAVA_PACKAGE, result.packageName()); | ||
assertEquals("MapperOuterClass", result.enclosingClassName().simpleName()); | ||
assertEquals("Mapper", result.simpleName()); | ||
} | ||
|
||
@Test | ||
public void javaPackageWithOuterClass() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.setJavaOuterClassname("Mapper") | ||
.setJavaPackage(JAVA_PACKAGE) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Map"); | ||
assertEquals(JAVA_PACKAGE, result.packageName()); | ||
assertEquals("Mapper", result.enclosingClassName().simpleName()); | ||
assertEquals("Map", result.simpleName()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void javaPackageWithOuterClassEmpty() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(false) | ||
.setJavaOuterClassname("") | ||
.setJavaPackage(JAVA_PACKAGE) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
mapper.get(".flit.test.Map"); | ||
} | ||
|
||
@Test | ||
public void javaPackageWithOuterClassMultiFile() { | ||
DescriptorProtos.FileOptions options = DescriptorProtos.FileOptions.newBuilder() | ||
.setJavaMultipleFiles(true) | ||
.setJavaOuterClassname("Mapper") | ||
.setJavaPackage(JAVA_PACKAGE) | ||
.build(); | ||
|
||
DescriptorProtos.FileDescriptorProto proto = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setPackage(PROTO_PACKAGE) | ||
.setName("mapper.proto") | ||
.setOptions(options) | ||
.addMessageType(MAP_MESSAGE) | ||
.build(); | ||
|
||
TypeMapper mapper = new TypeMapper(); | ||
mapper.add(proto); | ||
|
||
ClassName result = mapper.get(".flit.test.Map"); | ||
assertEquals(JAVA_PACKAGE, result.packageName()); | ||
assertNull(result.enclosingClassName()); | ||
assertEquals("Map", result.simpleName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version=1.0.0 |