|
14 | 14 |
|
15 | 15 | package com.google.api.generator.engine; |
16 | 16 |
|
17 | | -import static junit.framework.Assert.assertEquals; |
18 | | - |
19 | 17 | import com.google.api.generator.engine.ast.AnnotationNode; |
20 | 18 | import com.google.api.generator.engine.ast.AnonymousClassExpr; |
21 | 19 | import com.google.api.generator.engine.ast.AssignmentExpr; |
|
51 | 49 | import com.google.api.generator.engine.ast.VariableExpr; |
52 | 50 | import com.google.api.generator.engine.ast.WhileStatement; |
53 | 51 | import com.google.api.generator.engine.writer.JavaWriterVisitor; |
| 52 | +import com.google.api.generator.test.framework.Assert; |
54 | 53 | import java.io.FileWriter; |
55 | 54 | import java.io.IOException; |
| 55 | +import java.nio.file.Path; |
| 56 | +import java.nio.file.Paths; |
56 | 57 | import java.util.ArrayList; |
57 | 58 | import java.util.Arrays; |
58 | 59 | import java.util.HashMap; |
|
61 | 62 | import org.junit.Test; |
62 | 63 |
|
63 | 64 | public class JavaCodeGeneratorTest { |
| 65 | + private static final String GOLDENFILES_DIRECTORY = |
| 66 | + "src/test/java/com/google/api/generator/engine/goldens/"; |
64 | 67 | // Create shared VaporReferences. |
65 | 68 | private static final VaporReference stubRef = createVaporReference("com.google.gax.grpc", "Stub"); |
66 | 69 | private static final VaporReference bookKindRef = |
@@ -169,7 +172,8 @@ public void validJavaClass() { |
169 | 172 | .build(); |
170 | 173 | JavaWriterVisitor javaWriterVisitor = new JavaWriterVisitor(); |
171 | 174 | libraryServiceStubClass.accept(javaWriterVisitor); |
172 | | - assertEquals(javaWriterVisitor.write(), EXPECTED_CLASS_STRING); |
| 175 | + Path goldenFilePath = Paths.get(GOLDENFILES_DIRECTORY, "JavaCodeGeneratorTest.golden"); |
| 176 | + Assert.assertCodeEquals(goldenFilePath, javaWriterVisitor.write()); |
173 | 177 | } |
174 | 178 |
|
175 | 179 | // Private helpers. |
@@ -744,156 +748,4 @@ private ClassDefinition createNestedClassNovel() { |
744 | 748 | Arrays.asList(createOverrideCreateBookMethod(novelClassRef, bookKindNovelEnumExpr))) |
745 | 749 | .build(); |
746 | 750 | } |
747 | | - |
748 | | - private static final String EXPECTED_CLASS_STRING = |
749 | | - "/*\n" |
750 | | - + " * Copyright 2020 Gagpic-generator-java\n" |
751 | | - + " *\n" |
752 | | - + " * Licensed description and license version 2.0 (the \"License\");\n" |
753 | | - + " *\n" |
754 | | - + " * https://www.foo.bar/licenses/LICENSE-2.0\n" |
755 | | - + " *\n" |
756 | | - + " * Software distributed under the License is distributed on an \"AS IS\" BASIS.\n" |
757 | | - + " * See the License for the specific language governing permissions and\n" |
758 | | - + " * limitations under the License.\n" |
759 | | - + " */\n" |
760 | | - + "\n" |
761 | | - + "package com.google.example.library.core;\n" |
762 | | - + "\n" |
763 | | - + "import com.google.exmaple.library.LibraryService;\n" |
764 | | - + "import com.google.exmaple.library.core.LibraryServiceStub;\n" |
765 | | - + "import com.google.exmaple.library.v1.BookKind;\n" |
766 | | - + "import com.google.gax.grpc.Stub;\n" |
767 | | - + "import java.io.FileWriter;\n" |
768 | | - + "import java.io.IOException;\n" |
769 | | - + "import java.util.ArrayList;\n" |
770 | | - + "import java.util.HashMap;\n" |
771 | | - + "import java.util.List;\n" |
772 | | - + "import java.util.Stack;\n" |
773 | | - + "\n" |
774 | | - + "/**\n" |
775 | | - + " * Service Description: This is a test comment.\n" |
776 | | - + " *\n" |
777 | | - + " * <pre><code>\n" |
778 | | - + " * LibraryServiceStub libServiceStub = new LibraryServiceStub()\n" |
779 | | - + " * </code></pre>\n" |
780 | | - + " *\n" |
781 | | - + " * <ol>\n" |
782 | | - + " * <li>A \"flattened\" method.\n" |
783 | | - + " * <li>A \"request object\" method.\n" |
784 | | - + " * <li>A \"callable\" method.\n" |
785 | | - + " * </ol>\n" |
786 | | - + " *\n" |
787 | | - + " * @deprecated This is a deprecated message.\n" |
788 | | - + " */\n" |
789 | | - + "@SuppressWarnings(\"all\")\n" |
790 | | - + "@Deprecated\n" |
791 | | - + "@Override\n" |
792 | | - + "public class LibraryServiceStub extends Stub implements LibraryService {\n" |
793 | | - + " private static final String serviceName = \"LibraryServiceStub\";\n" |
794 | | - + " protected List<Shelf> shelfList;\n" |
795 | | - + " public static HashMap<String, Shelf> shelfMap;\n" |
796 | | - + "\n" |
797 | | - + " public LibraryServiceStub() {\n" |
798 | | - + " super();\n" |
799 | | - + " this.shelfList = new ArrayList<>();\n" |
800 | | - + " shelfMap = new HashMap<>();\n" |
801 | | - + " }\n" |
802 | | - + "\n" |
803 | | - + " @Override\n" |
804 | | - + " public String addShelf(String name, double seriesDoubleNum) {\n" |
805 | | - + " int seriesNum = ((int) seriesDoubleNum);\n" |
806 | | - + " if (condition) {\n" |
807 | | - + " return \"Series number equals to max int value.\";\n" |
808 | | - + " }\n" |
809 | | - + " shelfList.add(new Shelf(name, seriesNum));\n" |
810 | | - + " if (shelfMap.containsKey(name)) {\n" |
811 | | - + " return \"Shelf is already existing in the map.\";\n" |
812 | | - + " }\n" |
813 | | - + " shelfMap.put(name, new Shelf(name, seriesNum));\n" |
814 | | - + " return \"Shelf added.\";\n" |
815 | | - + " }\n" |
816 | | - + "\n" |
817 | | - + " public void updateShelfMap(Shelf newShelf) throws Exception {\n" |
818 | | - + " if (shelfMap.containsKey(newShelf.shelfName)) {\n" |
819 | | - + " shelfMap.put(newShelf.shelfName, newShelf);\n" |
820 | | - + " } else {\n" |
821 | | - + " throw new Exception(\"Updating shelf is not existing in the map\");\n" |
822 | | - + " }\n" |
823 | | - + " }\n" |
824 | | - + "\n" |
825 | | - + " public void printShelfListToFile(String fileName) {\n" |
826 | | - + " StringBuilder sb = new StringBuilder();\n" |
827 | | - + " try {\n" |
828 | | - + " FileWriter fileWriter = new FileWriter(fileName);\n" |
829 | | - + " for (Shelf s : shelfList) {\n" |
830 | | - + " sb.append(s.shelfName).append(s.seriesNum);\n" |
831 | | - + " }\n" |
832 | | - + " fileName.write(sb.toString());\n" |
833 | | - + " fileName.close();\n" |
834 | | - + " } catch (IOException e) {\n" |
835 | | - + " e.printStackTrace();\n" |
836 | | - + " }\n" |
837 | | - + " }\n" |
838 | | - + "\n" |
839 | | - + " /**\n" |
840 | | - + " * Add books to Shelf and check if there is a novel, return string message as" |
841 | | - + " whether novel books\n" |
842 | | - + " * are added to the shelf.\n" |
843 | | - + " *\n" |
844 | | - + " * @param shelf The Shelf object to which books will put.\n" |
845 | | - + " * @param stack The Stack of the BookKinds.\n" |
846 | | - + " */\n" |
847 | | - + " public String addBooksContainsNovel(Shelf shelf, Stack<BookKind> stack) {\n" |
848 | | - + " boolean containsNovel = false;\n" |
849 | | - + " while (stack.isEmpty()) {\n" |
850 | | - + " Book addedBook = addBookToShelf(stack.pop(), shelf);\n" |
851 | | - + " if (addedBook instanceof Novel) {\n" |
852 | | - + " containsNovel = true;\n" |
853 | | - + " }\n" |
854 | | - + " }\n" |
855 | | - + " return containsNovel ? \"Added novels\" : \"No novels added\";\n" |
856 | | - + " }\n" |
857 | | - + "\n" |
858 | | - + " // Private helper.\n" |
859 | | - + " private Book addBookToShelf(BookKind bookKind, Shelf shelf) {\n" |
860 | | - + " Book book =\n" |
861 | | - + " new Book() {\n" |
862 | | - + " @Override\n" |
863 | | - + " public void createBook(int seriesNum, BookKind bookKind) {\n" |
864 | | - + " this.seriesNum = seriesNum;\n" |
865 | | - + " this.bookKind = bookKind;\n" |
866 | | - + " }\n" |
867 | | - + " };\n" |
868 | | - + " return book;\n" |
869 | | - + " }\n" |
870 | | - + "\n" |
871 | | - + " public class Shelf {\n" |
872 | | - + " public String shelfName;\n" |
873 | | - + " public int seriesNum;\n" |
874 | | - + " public String shelfServiceName = serviceName;\n" |
875 | | - + "\n" |
876 | | - + " public Shelf(String shelfName, int seriesNum) {\n" |
877 | | - + " this.shelfName = shelfName;\n" |
878 | | - + " this.seriesNum = seriesNum;\n" |
879 | | - + " }\n" |
880 | | - + " }\n" |
881 | | - + "\n" |
882 | | - + " // Test nested abstract class and abstract method.\n" |
883 | | - + " public abstract class Book {\n" |
884 | | - + " public BookKind bookKind;\n" |
885 | | - + " public int seriesNum;\n" |
886 | | - + "\n" |
887 | | - + " public abstract void createBook(int seriesNum, BookKind bookKind);\n" |
888 | | - + " }\n" |
889 | | - + "\n" |
890 | | - + " public class Novel extends Book {\n" |
891 | | - + "\n" |
892 | | - + " @Override\n" |
893 | | - + " public void createBook(int seriesNum, BookKind bookKind) {\n" |
894 | | - + " this.seriesNum = seriesNum;\n" |
895 | | - + " this.bookKind = BookKind.NOVEL;\n" |
896 | | - + " }\n" |
897 | | - + " }\n" |
898 | | - + "}\n"; |
899 | 751 | } |
0 commit comments