1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: kenton@google.com (Kenton Varda)
32//
33// TODO(kenton):  Share code with the versions of this test in other languages?
34//   It seemed like parameterizing it would add more complexity than it is
35//   worth.
36
37#include <memory>
38#ifndef _SHARED_PTR_H
39#include <google/protobuf/stubs/shared_ptr.h>
40#endif
41
42#include <google/protobuf/compiler/java/java_generator.h>
43#include <google/protobuf/compiler/command_line_interface.h>
44#include <google/protobuf/io/zero_copy_stream.h>
45#include <google/protobuf/io/printer.h>
46
47#include <google/protobuf/testing/file.h>
48#include <google/protobuf/testing/file.h>
49#include <google/protobuf/testing/googletest.h>
50#include <gtest/gtest.h>
51
52namespace google {
53namespace protobuf {
54namespace compiler {
55namespace java {
56namespace {
57
58class TestGenerator : public CodeGenerator {
59 public:
60  TestGenerator() {}
61  ~TestGenerator() {}
62
63  virtual bool Generate(const FileDescriptor* file,
64                        const string& parameter,
65                        GeneratorContext* context,
66                        string* error) const {
67    string filename = "Test.java";
68    TryInsert(filename, "outer_class_scope", context);
69    TryInsert(filename, "class_scope:foo.Bar", context);
70    TryInsert(filename, "class_scope:foo.Bar.Baz", context);
71    TryInsert(filename, "builder_scope:foo.Bar", context);
72    TryInsert(filename, "builder_scope:foo.Bar.Baz", context);
73    TryInsert(filename, "enum_scope:foo.Qux", context);
74    return true;
75  }
76
77  void TryInsert(const string& filename, const string& insertion_point,
78                 GeneratorContext* context) const {
79    google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
80        context->OpenForInsert(filename, insertion_point));
81    io::Printer printer(output.get(), '$');
82    printer.Print("// inserted $name$\n", "name", insertion_point);
83  }
84};
85
86// This test verifies that all the expected insertion points exist.  It does
87// not verify that they are correctly-placed; that would require actually
88// compiling the output which is a bit more than I care to do for this test.
89TEST(JavaPluginTest, PluginTest) {
90  GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
91                             "syntax = \"proto2\";\n"
92                             "package foo;\n"
93                             "option java_package = \"\";\n"
94                             "option java_outer_classname = \"Test\";\n"
95                             "message Bar {\n"
96                             "  message Baz {}\n"
97                             "}\n"
98                             "enum Qux { BLAH = 1; }\n",
99                             true));
100
101  google::protobuf::compiler::CommandLineInterface cli;
102  cli.SetInputsAreProtoPathRelative(true);
103
104  JavaGenerator java_generator;
105  TestGenerator test_generator;
106  cli.RegisterGenerator("--java_out", &java_generator, "");
107  cli.RegisterGenerator("--test_out", &test_generator, "");
108
109  string proto_path = "-I" + TestTempDir();
110  string java_out = "--java_out=" + TestTempDir();
111  string test_out = "--test_out=" + TestTempDir();
112
113  const char* argv[] = {
114    "protoc",
115    proto_path.c_str(),
116    java_out.c_str(),
117    test_out.c_str(),
118    "test.proto"
119  };
120
121  EXPECT_EQ(0, cli.Run(5, argv));
122}
123
124}  // namespace
125}  // namespace java
126}  // namespace compiler
127}  // namespace protobuf
128}  // namespace google
129