Skip to content

Commit 2739f2c

Browse files
authored
Merge pull request #1389 from jeffgbutler/remove-confusing-domtests-project
Remove Confusing domtests Module
2 parents d07fc28 + b27b7f0 commit 2739f2c

File tree

14 files changed

+135
-644
lines changed

14 files changed

+135
-644
lines changed

core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/fieldtype1/Test1Generator.java renamed to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/ComplexHierarchyGenerator.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package mbg.domtest.generators.fieldtype1;
16+
package org.mybatis.generator.api.dom.java;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
import org.mybatis.generator.api.dom.java.CompilationUnit;
22-
import org.mybatis.generator.api.dom.java.Field;
23-
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24-
import org.mybatis.generator.api.dom.java.JavaVisibility;
25-
import org.mybatis.generator.api.dom.java.Method;
26-
import org.mybatis.generator.api.dom.java.Parameter;
27-
import org.mybatis.generator.api.dom.java.TopLevelClass;
28-
29-
import mbg.domtest.CompilationUnitGenerator;
30-
3121
/**
32-
* This class generates a hierarchy with multiple classes that have the same name in
33-
* different packages. It tests the ability of the generator to use fully qualified names
22+
* This test verifies the ability of the generator to use fully qualified names
3423
* in code generation when the type is not explicitly imported.
3524
*
25+
* <p>The test generates a hierarchy with multiple classes that have the same name in
26+
* different packages.
3627
*/
37-
// @IgnoreDomTest("Ignore until changes for issue #63 are committed")
38-
public class Test1Generator implements CompilationUnitGenerator {
28+
public class ComplexHierarchyGenerator {
3929

4030
private static final String BASE_PACKAGE = "mbg.domtest.generators.fieldtype1.output";
4131

42-
@Override
43-
public List<CompilationUnit> generate() {
32+
public static List<CompilationUnit> generateTestClasses() {
4433
FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass");
4534

4635
List<CompilationUnit> answer = new ArrayList<>();
@@ -130,7 +119,7 @@ public List<CompilationUnit> generate() {
130119
return answer;
131120
}
132121

133-
private TopLevelClass generateFieldTypeMain() {
122+
private static TopLevelClass generateFieldTypeMain() {
134123
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".FieldType");
135124
TopLevelClass tlc = new TopLevelClass(fqjt);
136125
tlc.setVisibility(JavaVisibility.PUBLIC);
@@ -143,7 +132,7 @@ private TopLevelClass generateFieldTypeMain() {
143132
return tlc;
144133
}
145134

146-
private TopLevelClass generateFieldTypeSub1() {
135+
private static TopLevelClass generateFieldTypeSub1() {
147136
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.FieldType");
148137
TopLevelClass tlc = new TopLevelClass(fqjt);
149138
tlc.setVisibility(JavaVisibility.PUBLIC);
@@ -156,7 +145,7 @@ private TopLevelClass generateFieldTypeSub1() {
156145
return tlc;
157146
}
158147

159-
private TopLevelClass generateTestClassSub1() {
148+
private static TopLevelClass generateTestClassSub1() {
160149
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.SomeClass");
161150
TopLevelClass tlc = new TopLevelClass(fqjt);
162151
tlc.setVisibility(JavaVisibility.PUBLIC);
@@ -169,7 +158,7 @@ private TopLevelClass generateTestClassSub1() {
169158
return tlc;
170159
}
171160

172-
private TopLevelClass generateFieldTypeSub2() {
161+
private static TopLevelClass generateFieldTypeSub2() {
173162
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub2.FieldType");
174163
TopLevelClass tlc = new TopLevelClass(fqjt);
175164
tlc.setVisibility(JavaVisibility.PUBLIC);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/simple/interfaze/SimpleInterfaceGenerator.java renamed to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SimpleInterfaceGenerator.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package mbg.domtest.generators.simple.interfaze;
16+
package org.mybatis.generator.api.dom.java;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
import org.mybatis.generator.api.dom.java.CompilationUnit;
22-
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
23-
import org.mybatis.generator.api.dom.java.Interface;
24-
import org.mybatis.generator.api.dom.java.JavaVisibility;
25-
import org.mybatis.generator.api.dom.java.TopLevelClass;
26-
27-
import mbg.domtest.CompilationUnitGenerator;
28-
2921
/**
30-
* This generator generates a simple interface and implementing class in different packages.
22+
* This test verifies that a simple interface and implementing class in different packages compile.
3123
*/
32-
public class SimpleInterfaceGenerator implements CompilationUnitGenerator {
24+
public class SimpleInterfaceGenerator {
3325

3426
private static final String BASE_PACKAGE = "mbg.domtest.generators.simple.interfaze.output";
3527

36-
@Override
37-
public List<CompilationUnit> generate() {
28+
public static List<CompilationUnit> generateTestClasses() {
3829
List<CompilationUnit> answer = new ArrayList<>();
3930

4031
Interface interfaze = generateInterface();
@@ -44,14 +35,14 @@ public List<CompilationUnit> generate() {
4435
return answer;
4536
}
4637

47-
private Interface generateInterface() {
38+
private static Interface generateInterface() {
4839
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub1.SimpleInterface");
4940
Interface interfaze = new Interface(fqjt);
5041
interfaze.setVisibility(JavaVisibility.PUBLIC);
5142
return interfaze;
5243
}
5344

54-
private TopLevelClass generateClass(Interface interfaze) {
45+
private static TopLevelClass generateClass(Interface interfaze) {
5546
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + "SimpleClass");
5647
TopLevelClass tlc = new TopLevelClass(fqjt);
5748
tlc.setVisibility(JavaVisibility.PUBLIC);

core/mybatis-generator-systests-domtests/src/main/java/mbg/domtest/generators/supers/SupersGenerator.java renamed to core/mybatis-generator-core/src/test/java/org/mybatis/generator/api/dom/java/SupersGenerator.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package mbg.domtest.generators.supers;
16+
package org.mybatis.generator.api.dom.java;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
import org.mybatis.generator.api.dom.java.CompilationUnit;
22-
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
23-
import org.mybatis.generator.api.dom.java.Interface;
24-
import org.mybatis.generator.api.dom.java.JavaVisibility;
25-
import org.mybatis.generator.api.dom.java.TopLevelClass;
26-
27-
import mbg.domtest.CompilationUnitGenerator;
28-
29-
public class SupersGenerator implements CompilationUnitGenerator {
21+
/**
22+
* This test verifies that implementations can be in different packages from their super classes.
23+
*/
24+
public class SupersGenerator {
3025

3126
private static final String BASE_PACKAGE = "mbg.domtest.generators.supers";
3227

33-
@Override
34-
public List<CompilationUnit> generate() {
28+
public static List<CompilationUnit> generateTestClasses() {
3529
List<CompilationUnit> answer = new ArrayList<>();
3630

3731
TopLevelClass baseClass = getBaseClass();
@@ -51,36 +45,35 @@ public List<CompilationUnit> generate() {
5145
return answer;
5246
}
5347

54-
private TopLevelClass getSuperClass() {
48+
private static TopLevelClass getSuperClass() {
5549
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub.SuperClass");
5650
TopLevelClass tlc = new TopLevelClass(fqjt);
5751
tlc.setVisibility(JavaVisibility.PUBLIC);
5852

5953
return tlc;
6054
}
6155

62-
private Interface getSuperInterface() {
56+
private static Interface getSuperInterface() {
6357
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".sub.SuperInterface");
6458
Interface ifc = new Interface(fqjt);
6559
ifc.setVisibility(JavaVisibility.PUBLIC);
6660

6761
return ifc;
6862
}
6963

70-
private TopLevelClass getBaseClass() {
64+
private static TopLevelClass getBaseClass() {
7165
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".BaseClass");
7266
TopLevelClass tlc = new TopLevelClass(fqjt);
7367
tlc.setVisibility(JavaVisibility.PUBLIC);
7468

7569
return tlc;
7670
}
7771

78-
private Interface getBaseInterface() {
72+
private static Interface getBaseInterface() {
7973
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(BASE_PACKAGE + ".BaseInterface");
8074
Interface ifc = new Interface(fqjt);
8175
ifc.setVisibility(JavaVisibility.PUBLIC);
8276

8377
return ifc;
8478
}
85-
8679
}

0 commit comments

Comments
 (0)