diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/fieldtype1/Test1Generator.java b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/ComplexHierarchyGenerator.java similarity index 84% rename from core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/fieldtype1/Test1Generator.java rename to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/ComplexHierarchyGenerator.java index 63374566b9..bbb43af768 100644 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/fieldtype1/Test1Generator.java +++ b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/ComplexHierarchyGenerator.java @@ -13,34 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package mbg.domtest.generators.fieldtype1; +package org.mybatis.generator.api.dom.java; import java.util.ArrayList; import java.util.List; -import org.mybatis.generator.api.dom.java.CompilationUnit; -import org.mybatis.generator.api.dom.java.Field; -import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; -import org.mybatis.generator.api.dom.java.JavaVisibility; -import org.mybatis.generator.api.dom.java.Method; -import org.mybatis.generator.api.dom.java.Parameter; -import org.mybatis.generator.api.dom.java.TopLevelClass; - -import mbg.domtest.CompilationUnitGenerator; - /** - * This class generates a hierarchy with multiple classes that have the same name in - * different packages. It tests the ability of the generator to use fully qualified names + * This test verifies the ability of the generator to use fully qualified names * in code generation when the type is not explicitly imported. * + *

The test generates a hierarchy with multiple classes that have the same name in + * different packages. */ -// @IgnoreDomTest("Ignore until changes for issue #63 are committed") -public class Test1Generator implements CompilationUnitGenerator { +public class ComplexHierarchyGenerator { private static final String BASE_PACKAGE = "mbg.domtest.generators.fieldtype1.output"; - @Override - public List generate() { + public static List generateTestClasses() { FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass"); List answer = new ArrayList<>(); @@ -130,7 +119,7 @@ public List generate() { return answer; } - private TopLevelClass generateFieldTypeMain() { + private static TopLevelClass generateFieldTypeMain() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".FieldType"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); @@ -143,7 +132,7 @@ private TopLevelClass generateFieldTypeMain() { return tlc; } - private TopLevelClass generateFieldTypeSub1() { + private static TopLevelClass generateFieldTypeSub1() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.FieldType"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); @@ -156,7 +145,7 @@ private TopLevelClass generateFieldTypeSub1() { return tlc; } - private TopLevelClass generateTestClassSub1() { + private static TopLevelClass generateTestClassSub1() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.SomeClass"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); @@ -169,7 +158,7 @@ private TopLevelClass generateTestClassSub1() { return tlc; } - private TopLevelClass generateFieldTypeSub2() { + private static TopLevelClass generateFieldTypeSub2() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub2.FieldType"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); diff --git a/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/GeneratedClassCompileTest.java b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/GeneratedClassCompileTest.java new file mode 100644 index 0000000000..d2063914f2 --- /dev/null +++ b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/GeneratedClassCompileTest.java @@ -0,0 +1,109 @@ +/* + * Copyright 2006-2025 the original author or authors. + * + * 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 org.mybatis.generator.api.dom.java; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; +import javax.tools.ToolProvider; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mybatis.generator.api.dom.java.render.TopLevelClassRenderer; +import org.mybatis.generator.api.dom.java.render.TopLevelEnumerationRenderer; +import org.mybatis.generator.api.dom.java.render.TopLevelInterfaceRenderer; + +class GeneratedClassCompileTest { + + @ParameterizedTest + @MethodSource("testVariations") + void testCompile(List testClasses) throws IOException { + DiagnosticCollector diagnostics = new DiagnosticCollector<>(); + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); + fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(Path.of("target").toFile())); + + List files = new SimpleCompilationUnitRenderer().toJavaFileObjects(testClasses); + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, files); + boolean success = task.call(); + assertThat(success).isTrue(); + assertThat(diagnostics.getDiagnostics()).isEmpty(); + } + + private static Stream testVariations() { + return Stream.of( + Arguments.argumentSet("Complex Hierarchy", ComplexHierarchyGenerator.generateTestClasses()), + Arguments.argumentSet("Simple Interface", SimpleInterfaceGenerator.generateTestClasses()), + Arguments.argumentSet("Supers", SupersGenerator.generateTestClasses()) + ); + } + + public static class StringBasedJavaFileObject extends SimpleJavaFileObject { + final String renderedContent; + + public StringBasedJavaFileObject(String name, String renderedContent) { + super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE); + this.renderedContent = renderedContent; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return renderedContent; + } + } + + public static class SimpleCompilationUnitRenderer implements CompilationUnitVisitor { + private final TopLevelClassRenderer tlcRenderer = new TopLevelClassRenderer(); + private final TopLevelInterfaceRenderer tliRenderer = new TopLevelInterfaceRenderer(); + private final TopLevelEnumerationRenderer tleRenderer = new TopLevelEnumerationRenderer(); + + @Override + public String visit(TopLevelClass topLevelClass) { + return tlcRenderer.render(topLevelClass); + } + + @Override + public String visit(TopLevelEnumeration topLevelEnumeration) { + return tleRenderer.render(topLevelEnumeration); + } + + @Override + public String visit(Interface topLevelInterface) { + return tliRenderer.render(topLevelInterface); + } + + private StringBasedJavaFileObject toJavaFileObject(CompilationUnit compilationUnit) { + String source = compilationUnit.accept(this); + return new StringBasedJavaFileObject(compilationUnit.getType().getFullyQualifiedNameWithoutTypeParameters(), source); + } + + public List toJavaFileObjects(List compilationUnits) { + return compilationUnits.stream().map(this::toJavaFileObject).toList(); + } + } +} diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/simple/interfaze/SimpleInterfaceGenerator.java b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SimpleInterfaceGenerator.java similarity index 68% rename from core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/simple/interfaze/SimpleInterfaceGenerator.java rename to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SimpleInterfaceGenerator.java index e159d5db74..f38cf054da 100644 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/simple/interfaze/SimpleInterfaceGenerator.java +++ b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SimpleInterfaceGenerator.java @@ -13,28 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package mbg.domtest.generators.simple.interfaze; +package org.mybatis.generator.api.dom.java; import java.util.ArrayList; import java.util.List; -import org.mybatis.generator.api.dom.java.CompilationUnit; -import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; -import org.mybatis.generator.api.dom.java.Interface; -import org.mybatis.generator.api.dom.java.JavaVisibility; -import org.mybatis.generator.api.dom.java.TopLevelClass; - -import mbg.domtest.CompilationUnitGenerator; - /** - * This generator generates a simple interface and implementing class in different packages. + * This test verifies that a simple interface and implementing class in different packages compile. */ -public class SimpleInterfaceGenerator implements CompilationUnitGenerator { +public class SimpleInterfaceGenerator { private static final String BASE_PACKAGE = "mbg.domtest.generators.simple.interfaze.output"; - @Override - public List generate() { + public static List generateTestClasses() { List answer = new ArrayList<>(); Interface interfaze = generateInterface(); @@ -44,14 +35,14 @@ public List generate() { return answer; } - private Interface generateInterface() { + private static Interface generateInterface() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.SimpleInterface"); Interface interfaze = new Interface(fqjt); interfaze.setVisibility(JavaVisibility.PUBLIC); return interfaze; } - private TopLevelClass generateClass(Interface interfaze) { + private static TopLevelClass generateClass(Interface interfaze) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + "SimpleClass"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/supers/SupersGenerator.java b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SupersGenerator.java similarity index 77% rename from core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/supers/SupersGenerator.java rename to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SupersGenerator.java index 5fc52ccdba..73f79d62a0 100644 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/supers/SupersGenerator.java +++ b/core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SupersGenerator.java @@ -13,25 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package mbg.domtest.generators.supers; +package org.mybatis.generator.api.dom.java; import java.util.ArrayList; import java.util.List; -import org.mybatis.generator.api.dom.java.CompilationUnit; -import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; -import org.mybatis.generator.api.dom.java.Interface; -import org.mybatis.generator.api.dom.java.JavaVisibility; -import org.mybatis.generator.api.dom.java.TopLevelClass; - -import mbg.domtest.CompilationUnitGenerator; - -public class SupersGenerator implements CompilationUnitGenerator { +/** + * This test verifies that implementations can be in different packages from their super classes. + */ +public class SupersGenerator { private static final String BASE_PACKAGE = "mbg.domtest.generators.supers"; - @Override - public List generate() { + public static List generateTestClasses() { List answer = new ArrayList<>(); TopLevelClass baseClass = getBaseClass(); @@ -51,7 +45,7 @@ public List generate() { return answer; } - private TopLevelClass getSuperClass() { + private static TopLevelClass getSuperClass() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub.SuperClass"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); @@ -59,7 +53,7 @@ private TopLevelClass getSuperClass() { return tlc; } - private Interface getSuperInterface() { + private static Interface getSuperInterface() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub.SuperInterface"); Interface ifc = new Interface(fqjt); ifc.setVisibility(JavaVisibility.PUBLIC); @@ -67,7 +61,7 @@ private Interface getSuperInterface() { return ifc; } - private TopLevelClass getBaseClass() { + private static TopLevelClass getBaseClass() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".BaseClass"); TopLevelClass tlc = new TopLevelClass(fqjt); tlc.setVisibility(JavaVisibility.PUBLIC); @@ -75,12 +69,11 @@ private TopLevelClass getBaseClass() { return tlc; } - private Interface getBaseInterface() { + private static Interface getBaseInterface() { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".BaseInterface"); Interface ifc = new Interface(fqjt); ifc.setVisibility(JavaVisibility.PUBLIC); return ifc; } - } diff --git a/core/mybatis-generator-systests-domtests/LICENSE b/core/mybatis-generator-systests-domtests/LICENSE deleted file mode 100644 index 261eeb9e9f..0000000000 --- a/core/mybatis-generator-systests-domtests/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - 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. diff --git a/core/mybatis-generator-systests-domtests/LICENSE_HEADER b/core/mybatis-generator-systests-domtests/LICENSE_HEADER deleted file mode 100644 index a81590a5b5..0000000000 --- a/core/mybatis-generator-systests-domtests/LICENSE_HEADER +++ /dev/null @@ -1,13 +0,0 @@ - Copyright ${license.git.copyrightYears} the original author or authors. - - 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. diff --git a/core/mybatis-generator-systests-domtests/NOTICE b/core/mybatis-generator-systests-domtests/NOTICE deleted file mode 100644 index f26b8a15ff..0000000000 --- a/core/mybatis-generator-systests-domtests/NOTICE +++ /dev/null @@ -1,5 +0,0 @@ -This product includes software developed by -The Apache Software Foundation (http://www.apache.org/). - -This product includes the EqualsUtil and HashCodeUtil classes -from http://www.javapractices.com. diff --git a/core/mybatis-generator-systests-domtests/pom.xml b/core/mybatis-generator-systests-domtests/pom.xml deleted file mode 100644 index 2d22229964..0000000000 --- a/core/mybatis-generator-systests-domtests/pom.xml +++ /dev/null @@ -1,117 +0,0 @@ - - - - 4.0.0 - - org.mybatis.generator - mybatis-generator - 1.4.3-SNAPSHOT - - mybatis-generator-systests-domtests - MyBatis Generator Tests (Java DOM Tests) - Tests for the Java DOM classes. Generates Java classes and fails if the classes won't compile. - - - ${project.build.directory}/generated-test-sources/mybatis-generator-domtest - ${project.basedir}/../checkstyle-override.xml - - - - - org.mybatis.generator - mybatis-generator-core - ${project.version} - - - org.reflections - reflections - - - - - - - org.codehaus.mojo - exec-maven-plugin - - mbg.domtest.GenerateTestSourceFiles - - ${generated.source.directory} - - - - - generate-test-sources - - java - - generate-test-sources - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - add-test-source - - add-test-source - - generate-test-sources - - - ${generated.source.directory} - - - - - - - - org.apache.maven.plugins - maven-deploy-plugin - - true - - - - org.apache.maven.plugins - maven-javadoc-plugin - - true - - - - org.apache.maven.plugins - maven-source-plugin - - true - - - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - - diff --git a/core/mybatis-generator-systests-domtests/readme.md b/core/mybatis-generator-systests-domtests/readme.md deleted file mode 100644 index 8fae866fc0..0000000000 --- a/core/mybatis-generator-systests-domtests/readme.md +++ /dev/null @@ -1,28 +0,0 @@ -# What is this Project? -This project is used to test the MyBatis Generator Java code generator classes. -The basic idea is that we want to use the generator DOM classes to generate Java -code and then try to compile that code as a part of the build. If the code is -generated incorrectly, then the build should fail with compile errors. - -We may or may not write additional test classes to exercise the code that was generated. - -The build will automatically discover code generators that implement the `CompilationUnitGenerator` -interface as long as they are in a sub package of `mbg.domtest.generators`. - -# How to Add a New Test - -1. Add a class in src/main/java. The class should be in a sub-package of `mbg.domtest.generators`. -2. The class must implement `mbg.domtest.CompilationUnitGenerator` -3. During plugin execution, the `generate()` method in your class will be called and in that method - you should use the Java DOM classes to generate any Java code you are interested in. -4. You can use the `mbg.domtest.IgnoreDomTest` annotation to ignore any code generator class (useful - if you are doing TDD and don't want to break the build) - -# How Does this Project Work? -The project hooks a code generation step into the standard Maven lifecycle. We use the exec-maven plugin to hook into the maven build life cycle. We use the build-helper-maven-plugin to add generated source to the test source folders. The basic order of execution is as follows: - -* [compile phase] Your generator classes are compiled along with all the other support classes -* [generate-test-sources phase] Your generator classes are executed and the output is added to the test - source path -* [test-compile phase] The output of your generator is compiled. If it fails, it will break the build. This is the primary test of generated code - that it compiles. Any other tests you add to src/test/java are also compiled -* [test phase] Any tests you create are executed. You can write tests to execute your generated code if you so desire diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/CompilationUnitGenerator.java b/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/CompilationUnitGenerator.java deleted file mode 100644 index 2128592d6a..0000000000 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/CompilationUnitGenerator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2006-2025 the original author or authors. - * - * 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 mbg.domtest; - -import java.util.List; - -import org.mybatis.generator.api.dom.java.CompilationUnit; - -/** - * Implementors of this interface will be called during the build. The expectation is that - * implementors will create hierarchies of classes/interfaces/enums of varying complexity. - * After they are created, the build will attempt to compile everything that was generated. - * If any code produced by the Java code generator is incorrect, the build should fail - * with compile errors in the generated objects. - * - *

Use the IgnoreDomTest annotation to skip a generator class (for example, - * when doing TDD to fix an error in the code generator, and you want to commit the - * test before committing the fix) - * - */ -public interface CompilationUnitGenerator { - List generate(); -} diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/GenerateTestSourceFiles.java b/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/GenerateTestSourceFiles.java deleted file mode 100644 index e3a6c93123..0000000000 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/GenerateTestSourceFiles.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2006-2025 the original author or authors. - * - * 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 mbg.domtest; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.StringTokenizer; - -import org.mybatis.generator.api.dom.DefaultJavaFormatter; -import org.mybatis.generator.api.dom.java.CompilationUnit; -import org.mybatis.generator.internal.util.StringUtility; -import org.reflections.Reflections; - -public class GenerateTestSourceFiles { - - public static void main(String[] args) { - if (args.length < 1 || !StringUtility.stringHasValue(args[0])) { - throw new RuntimeException("This class requres one argument which is the location of the output directory"); - } - - String outputDirectory = args[0]; - - GenerateTestSourceFiles app = new GenerateTestSourceFiles(); - - try { - app.run(Path.of(outputDirectory).toFile()); - } catch (Exception e) { - throw new RuntimeException("Exception creating test classes", e); - } - } - - private void gatherGenerators(List generators) throws ReflectiveOperationException { - Reflections reflections = new Reflections("mbg.domtest.generators"); - Set> classes = reflections.getSubTypesOf(CompilationUnitGenerator.class); - - for (Class clazz : classes) { - if (clazz.getAnnotation(IgnoreDomTest.class) == null) { - generators.add(clazz.getDeclaredConstructor().newInstance()); - } else { - System.out.println("Generator " + clazz.getName() + " ignored"); - } - } - } - - private void run(File outputDirectory) throws IOException, ReflectiveOperationException { - setupOutputDirectry(outputDirectory); - - List generators = new ArrayList<>(); - gatherGenerators(generators); - - List cus = new ArrayList<>(); - - for (CompilationUnitGenerator generator : generators) { - cus.addAll(generator.generate()); - } - - for (CompilationUnit cu : cus) { - writeCompilationUnit(outputDirectory, cu); - } - } - - private void setupOutputDirectry(File outputDirectory) throws IOException { - if (!outputDirectory.exists()) { - outputDirectory.mkdirs(); - } - - if (!outputDirectory.isDirectory()) { - throw new IOException("can't create output directory"); - } - } - - private void writeCompilationUnit(File rootDirectory, CompilationUnit cu) throws IOException { - String _package = cu.getType().getPackageName(); - - StringBuilder sb = new StringBuilder(); - StringTokenizer st = new StringTokenizer(_package, "."); - while (st.hasMoreTokens()) { - sb.append(st.nextToken()); - sb.append(File.separatorChar); - } - - Path directory = rootDirectory.toPath().resolve(sb.toString()); - - if (!Files.isDirectory(directory)) { - try { - Files.createDirectories(directory); - } catch (IOException e) { - throw new IOException("can't create package directory"); - } - } - - String fileName = cu.getType().getShortName() + ".java"; - Path targetFile = directory.resolve(fileName); - - DefaultJavaFormatter formatter = new DefaultJavaFormatter(); - writeFile(targetFile.toFile(), formatter.getFormattedContent(cu)); - } - - private void writeFile(File file, String content) throws IOException { - OutputStream fos = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); - OutputStreamWriter osw = new OutputStreamWriter(fos); - - try (BufferedWriter bw = new BufferedWriter(osw)) { - bw.write(content); - } - } -} diff --git a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/IgnoreDomTest.java b/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/IgnoreDomTest.java deleted file mode 100644 index 15e9741cd9..0000000000 --- a/core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/IgnoreDomTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2006-2025 the original author or authors. - * - * 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 mbg.domtest; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Add this annotation to a generator class to ignore the class. This aids in - * developing tests for new features so we don't break the build when working on - * the test first. - * - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -public @interface IgnoreDomTest { - /** - * The reason why the test is ignored (optional) - */ - String value() default ""; -} diff --git a/core/mybatis-generator-systests-domtests/src/site/site.xml b/core/mybatis-generator-systests-domtests/src/site/site.xml deleted file mode 100644 index e4caabc0f4..0000000000 --- a/core/mybatis-generator-systests-domtests/src/site/site.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - -

- - diff --git a/core/pom.xml b/core/pom.xml index 74499adcd1..7050056c7c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -40,7 +40,6 @@ mybatis-generator-core - mybatis-generator-systests-domtests mybatis-generator-maven-plugin mybatis-generator-systests-common mybatis-generator-systests-mybatis3