Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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<CompilationUnit> generate() {
public static List<CompilationUnit> generateTestClasses() {
FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass");

List<CompilationUnit> answer = new ArrayList<>();
Expand Down Expand Up @@ -130,7 +119,7 @@ public List<CompilationUnit> 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CompilationUnit> testClasses) throws IOException {
DiagnosticCollector<JavaFileObject> 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<StringBasedJavaFileObject> 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<Arguments> 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<String> {
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<StringBasedJavaFileObject> toJavaFileObjects(List<CompilationUnit> compilationUnits) {
return compilationUnits.stream().map(this::toJavaFileObject).toList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompilationUnit> generate() {
public static List<CompilationUnit> generateTestClasses() {
List<CompilationUnit> answer = new ArrayList<>();

Interface interfaze = generateInterface();
Expand All @@ -44,14 +35,14 @@ public List<CompilationUnit> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompilationUnit> generate() {
public static List<CompilationUnit> generateTestClasses() {
List<CompilationUnit> answer = new ArrayList<>();

TopLevelClass baseClass = getBaseClass();
Expand All @@ -51,36 +45,35 @@ public List<CompilationUnit> 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);

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);

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);

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;
}

}
Loading