|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Salesforce.com, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +package com.salesforce.jprotoc; |
| 9 | + |
| 10 | +import com.google.common.io.Files; |
| 11 | +import com.google.protobuf.ExtensionRegistry; |
| 12 | +import com.google.protobuf.GeneratedMessage; |
| 13 | +import com.google.protobuf.compiler.PluginProtos; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static com.google.common.base.Preconditions.checkArgument; |
| 22 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * This method is designed to be used by unit tests. Unit testing typically works like this: |
| 26 | + * 1. Add the Dump protoc plugin to your test code generation build phase, to write out a proto dump file. |
| 27 | + * 2. Locate the generated dump file in a unit test. |
| 28 | + * 3. Call this method, with the path of the dump file from a test. |
| 29 | + * 4. Inspect the generated output. |
| 30 | + */ |
| 31 | +public final class ProtocPluginTesting { |
| 32 | + private ProtocPluginTesting() { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public static final String MAVEN_DUMP_PATH = "target/generated-test-sources/protobuf/dump/descriptor_dump"; |
| 37 | + |
| 38 | + /** |
| 39 | + * Debug a single generator using the parsed proto descriptor. |
| 40 | + * @param generator The generator to run. |
| 41 | + * @param dumpPath The path to a descriptor dump on the filesystem. |
| 42 | + * @return The compiled output from the protoc plugin |
| 43 | + */ |
| 44 | + public static PluginProtos.CodeGeneratorResponse test(@Nonnull Generator generator, @Nonnull String dumpPath) throws IOException { |
| 45 | + checkNotNull(generator, "generator"); |
| 46 | + return test(Collections.singletonList(generator), dumpPath); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Debug multiple generators using the parsed proto descriptor, aggregating their results. |
| 51 | + * @param generators The list of generators to run. |
| 52 | + * @param dumpPath The path to a descriptor dump on the filesystem. |
| 53 | + * @return The compiled output from the protoc plugin |
| 54 | + */ |
| 55 | + public static PluginProtos.CodeGeneratorResponse test(@Nonnull List<Generator> generators, @Nonnull String dumpPath) throws IOException { |
| 56 | + return test(generators, Collections.emptyList(), dumpPath); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test multiple generators using the parsed proto descriptor, aggregating their results. |
| 61 | + * Also register the given extensions so they may be processed by the generator. |
| 62 | + * |
| 63 | + * @param generators The list of generators to run. |
| 64 | + * @param extensions The list of extensions to register. |
| 65 | + * @param dumpPath The path to a descriptor dump on the filesystem. |
| 66 | + * @return The compiled output from the protoc plugin |
| 67 | + */ |
| 68 | + public static PluginProtos.CodeGeneratorResponse test( |
| 69 | + @Nonnull List<Generator> generators, |
| 70 | + List<GeneratedMessage.GeneratedExtension> extensions, |
| 71 | + @Nonnull String dumpPath) throws IOException { |
| 72 | + checkNotNull(generators, "generators"); |
| 73 | + checkArgument(!generators.isEmpty(), "generators.isEmpty()"); |
| 74 | + checkNotNull(extensions, "extensions"); |
| 75 | + checkNotNull(dumpPath, "dumpPath"); |
| 76 | + |
| 77 | + // As per https://developers.google.com/protocol-buffers/docs/reference/java-generated#extension, |
| 78 | + // extensions must be registered in order to be processed. |
| 79 | + ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); |
| 80 | + for (GeneratedMessage.GeneratedExtension extension : extensions) { |
| 81 | + extensionRegistry.add(extension); |
| 82 | + } |
| 83 | + |
| 84 | + byte[] generatorRequestBytes = Files.toByteArray(new File(dumpPath)); |
| 85 | + PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.parseFrom( |
| 86 | + generatorRequestBytes, extensionRegistry); |
| 87 | + |
| 88 | + return ProtocPlugin.generate(generators, request); |
| 89 | + } |
| 90 | +} |
0 commit comments