diff --git a/src/test/java/com/google/api/generator/gapic/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/BUILD.bazel index ef1035d943..7a9cbd0da5 100644 --- a/src/test/java/com/google/api/generator/gapic/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/BUILD.bazel @@ -4,6 +4,7 @@ filegroup( name = "gapic_files", srcs = [ "//src/test/java/com/google/api/generator/gapic/composer:composer_files", + "//src/test/java/com/google/api/generator/gapic/dummy:dummy_files", "//src/test/java/com/google/api/generator/gapic/model:model_files", "//src/test/java/com/google/api/generator/gapic/protoparser:protoparser_files", ], diff --git a/src/test/java/com/google/api/generator/gapic/dummy/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/dummy/BUILD.bazel new file mode 100644 index 0000000000..84e4febc7a --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/dummy/BUILD.bazel @@ -0,0 +1,21 @@ +package(default_visibility = ["//visibility:public"]) + +TESTS = [ + "FileDiffInfraDummyTest", +] + +filegroup( + name = "dummy_files", + srcs = ["{0}.java".format(f) for f in TESTS], +) + +[java_test( + name = test_name, + srcs = ["{0}.java".format(test_name)], + test_class = "com.google.api.generator.gapic.dummy.{0}".format(test_name), + deps = [ + "//src/main/java/com/google/api/generator/engine/ast", + "//src/main/java/com/google/api/generator/engine/writer", + "@junit_junit//jar", + ], +) for test_name in TESTS] diff --git a/src/test/java/com/google/api/generator/gapic/dummy/FileDiffInfraDummyTest.java b/src/test/java/com/google/api/generator/gapic/dummy/FileDiffInfraDummyTest.java new file mode 100644 index 0000000000..9dcc0ce252 --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/dummy/FileDiffInfraDummyTest.java @@ -0,0 +1,104 @@ +// Copyright 2020 Google LLC +// +// 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 com.google.api.generator.gapic.dummy; + +import static junit.framework.Assert.assertEquals; + +import com.google.api.generator.engine.ast.BlockComment; +import com.google.api.generator.engine.ast.ClassDefinition; +import com.google.api.generator.engine.ast.CommentStatement; +import com.google.api.generator.engine.ast.LineComment; +import com.google.api.generator.engine.ast.ScopeNode; +import com.google.api.generator.engine.writer.JavaWriterVisitor; +import java.util.Arrays; +import org.junit.Test; + +public class FileDiffInfraDummyTest { + // Add two simple tests for testing the file-diff infra. + // 1. The two unit tests create simple java classes. + // 2. Two unit tests are created because we have the case of two expected strings comparison + // in one test class e.g. ResourceNameHelperClassComposer. In that case, two golden files will be + // created. + // + // TODO(xiaozhenliu): remove this test class once the file-diff infra is in place and well-tested. + @Test + public void simpleClass() { + ClassDefinition classDef = + ClassDefinition.builder() + .setHeaderCommentStatements( + Arrays.asList( + CommentStatement.withComment( + LineComment.withComment("This is a test class for file-diff infra")))) + .setPackageString("com.google.showcase.v1beta1.stub") + .setName("EchoStubSettings") + .setScope(ScopeNode.PUBLIC) + .build(); + JavaWriterVisitor visitor = new JavaWriterVisitor(); + classDef.accept(visitor); + assertEquals(visitor.write(), EXPECTED_CLASS_STRING_SIMPLE); + } + + @Test + public void classWithHeader() { + ClassDefinition classDef = + ClassDefinition.builder() + .setFileHeader( + Arrays.asList( + CommentStatement.withComment(BlockComment.withComment(APACHE_LICENSE_STRING)))) + .setPackageString("com.google.showcase.v1beta1.stub") + .setName("EchoStubSettings") + .setScope(ScopeNode.PUBLIC) + .build(); + JavaWriterVisitor visitor = new JavaWriterVisitor(); + classDef.accept(visitor); + assertEquals(visitor.write(), EXPECTED_CLASS_STRING_WITH_HEADER); + } + + private static final String APACHE_LICENSE_STRING = + "Copyright 2020 Google LLC\n\n" + + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "you may not use this file except in compliance with the License.\n" + + "You may obtain a copy of the License at\n\n" + + " https://www.apache.org/licenses/LICENSE-2.0\n\n" + + "Unless required by applicable law or agreed to in writing, software\n" + + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "See the License for the specific language governing permissions and\n" + + "limitations under the License."; + + private static final String EXPECTED_CLASS_STRING_SIMPLE = + "package com.google.showcase.v1beta1.stub;\n\n" + + "// This is a test class for file-diff infra\n" + + "public class EchoStubSettings {}\n"; + + private static final String EXPECTED_CLASS_STRING_WITH_HEADER = + "/*\n" + + " * Copyright 2020 Google LLC\n" + + " *\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + + " *\n" + + " * https://www.apache.org/licenses/LICENSE-2.0\n" + + " *\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + + " */\n\n" + + "package com.google.showcase.v1beta1.stub;\n\n" + + "public class EchoStubSettings {}\n"; +} diff --git a/src/test/java/com/google/api/generator/gapic/dummy/goldens/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/dummy/goldens/BUILD.bazel new file mode 100644 index 0000000000..85ac1a519e --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/dummy/goldens/BUILD.bazel @@ -0,0 +1,6 @@ +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "goldens_files", + srcs = glob(["*.golden"]), +) diff --git a/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestClassWithHeader.golden b/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestClassWithHeader.golden new file mode 100644 index 0000000000..ad531d6d09 --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestClassWithHeader.golden @@ -0,0 +1,19 @@ +/* + * Copyright 2020 Google LLC + * + * 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 + * + * https://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 com.google.showcase.v1beta1.stub; + +public class EchoStubSettings {} diff --git a/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestSimpleClass.golden b/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestSimpleClass.golden new file mode 100644 index 0000000000..82eb2e6b92 --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/dummy/goldens/FileDiffInfraDummyTestSimpleClass.golden @@ -0,0 +1,4 @@ +package com.google.showcase.v1beta1.stub; + +// This is a test class for file-diff infra +public class EchoStubSettings {}