|
| 1 | +/* |
| 2 | + * Copyright 2006-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mybatis.generator.api.dom.java; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import javax.tools.DiagnosticCollector; |
| 21 | +import javax.tools.JavaCompiler; |
| 22 | +import javax.tools.JavaFileObject; |
| 23 | +import javax.tools.SimpleJavaFileObject; |
| 24 | +import javax.tools.StandardJavaFileManager; |
| 25 | +import javax.tools.StandardLocation; |
| 26 | +import javax.tools.ToolProvider; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URI; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.List; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.Arguments; |
| 35 | +import org.junit.jupiter.params.provider.MethodSource; |
| 36 | +import org.mybatis.generator.api.dom.java.render.TopLevelClassRenderer; |
| 37 | +import org.mybatis.generator.api.dom.java.render.TopLevelEnumerationRenderer; |
| 38 | +import org.mybatis.generator.api.dom.java.render.TopLevelInterfaceRenderer; |
| 39 | + |
| 40 | +class GeneratedClassCompileTest { |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @MethodSource("testVariations") |
| 44 | + void testCompile(List<CompilationUnit> testClasses) throws IOException { |
| 45 | + DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
| 46 | + |
| 47 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 48 | + StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); |
| 49 | + fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(Path.of("target").toFile())); |
| 50 | + |
| 51 | + List<StringBasedJavaFileObject> files = new SimpleCompilationUnitRenderer().toJavaFileObjects(testClasses); |
| 52 | + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, files); |
| 53 | + boolean success = task.call(); |
| 54 | + assertThat(success).isTrue(); |
| 55 | + assertThat(diagnostics.getDiagnostics()).isEmpty(); |
| 56 | + } |
| 57 | + |
| 58 | + private static Stream<Arguments> testVariations() { |
| 59 | + return Stream.of( |
| 60 | + Arguments.argumentSet("Complex Hierarchy", ComplexHierarchyGenerator.generateTestClasses()), |
| 61 | + Arguments.argumentSet("Simple Interface", SimpleInterfaceGenerator.generateTestClasses()), |
| 62 | + Arguments.argumentSet("Supers", SupersGenerator.generateTestClasses()) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public static class StringBasedJavaFileObject extends SimpleJavaFileObject { |
| 67 | + final String renderedContent; |
| 68 | + |
| 69 | + public StringBasedJavaFileObject(String name, String renderedContent) { |
| 70 | + super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE); |
| 71 | + this.renderedContent = renderedContent; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
| 76 | + return renderedContent; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static class SimpleCompilationUnitRenderer implements CompilationUnitVisitor<String> { |
| 81 | + private final TopLevelClassRenderer tlcRenderer = new TopLevelClassRenderer(); |
| 82 | + private final TopLevelInterfaceRenderer tliRenderer = new TopLevelInterfaceRenderer(); |
| 83 | + private final TopLevelEnumerationRenderer tleRenderer = new TopLevelEnumerationRenderer(); |
| 84 | + |
| 85 | + @Override |
| 86 | + public String visit(TopLevelClass topLevelClass) { |
| 87 | + return tlcRenderer.render(topLevelClass); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String visit(TopLevelEnumeration topLevelEnumeration) { |
| 92 | + return tleRenderer.render(topLevelEnumeration); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String visit(Interface topLevelInterface) { |
| 97 | + return tliRenderer.render(topLevelInterface); |
| 98 | + } |
| 99 | + |
| 100 | + private StringBasedJavaFileObject toJavaFileObject(CompilationUnit compilationUnit) { |
| 101 | + String source = compilationUnit.accept(this); |
| 102 | + return new StringBasedJavaFileObject(compilationUnit.getType().getFullyQualifiedNameWithoutTypeParameters(), source); |
| 103 | + } |
| 104 | + |
| 105 | + public List<StringBasedJavaFileObject> toJavaFileObjects(List<CompilationUnit> compilationUnits) { |
| 106 | + return compilationUnits.stream().map(this::toJavaFileObject).toList(); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments