From a2c7bc22d266db46d34b26b723777b13eabc4680 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Wed, 2 Mar 2022 18:31:29 -0500 Subject: [PATCH 1/5] Failing tests for #742 --- .../src/test/resources/scripts/CreateDB.sql | 9 +++- .../scripts/generatorConfig-kotlin.xml | 4 ++ .../scripts/generatorConfig_Dsql.xml | 4 ++ .../mbg/test/common/scripts/CreateDB.sql | 9 +++- .../src/main/resources/generatorConfig.xml | 4 ++ .../AbstractAnnotatedMiscellaneousTest.kt | 3 +- .../kotlin/miscellaneous/MiscellaneousTest.kt | 50 ++++++++++++++++++ .../src/main/resources/generatorConfig.xml | 4 ++ .../AbstractAnnotatedMiscellaneousTest.java | 4 +- .../dsql/miscellaneous/MiscellaneousTest.java | 52 ++++++++++++++++++- 10 files changed, 138 insertions(+), 5 deletions(-) diff --git a/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql b/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql index 88a2ab7805..721cfd6573 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql +++ b/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql @@ -1,5 +1,5 @@ -- --- Copyright 2006-2018 the original author or authors. +-- Copyright 2006-2022 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. @@ -30,6 +30,7 @@ drop table mbgtest.Translations if exists; drop table CompoundKey if exists; drop schema mbgtest if exists; drop table EnumTest if exists; +drop table EnumOrdinalTest if exists; drop table GeneratedAlwaysTest if exists; drop table GeneratedAlwaysTestNoUpdates if exists; drop table IgnoreManyColumns if exists; @@ -155,6 +156,12 @@ create table EnumTest ( primary key(id) ); +create table EnumOrdinalTest ( + id int not null, + enumOrdinal int not null, + primary key(id) +); + create table GeneratedAlwaysTest ( id int not null, name varchar(20) not null, diff --git a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml index 5e1a912659..2d6307756e 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml +++ b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml @@ -158,6 +158,10 @@
+ + +
diff --git a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig_Dsql.xml b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig_Dsql.xml index fd07a419a4..07e80f0b18 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig_Dsql.xml +++ b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig_Dsql.xml @@ -164,6 +164,10 @@
+ + +
diff --git a/core/mybatis-generator-systests-common/src/main/resources/mbg/test/common/scripts/CreateDB.sql b/core/mybatis-generator-systests-common/src/main/resources/mbg/test/common/scripts/CreateDB.sql index 88a2ab7805..721cfd6573 100644 --- a/core/mybatis-generator-systests-common/src/main/resources/mbg/test/common/scripts/CreateDB.sql +++ b/core/mybatis-generator-systests-common/src/main/resources/mbg/test/common/scripts/CreateDB.sql @@ -1,5 +1,5 @@ -- --- Copyright 2006-2018 the original author or authors. +-- Copyright 2006-2022 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. @@ -30,6 +30,7 @@ drop table mbgtest.Translations if exists; drop table CompoundKey if exists; drop schema mbgtest if exists; drop table EnumTest if exists; +drop table EnumOrdinalTest if exists; drop table GeneratedAlwaysTest if exists; drop table GeneratedAlwaysTestNoUpdates if exists; drop table IgnoreManyColumns if exists; @@ -155,6 +156,12 @@ create table EnumTest ( primary key(id) ); +create table EnumOrdinalTest ( + id int not null, + enumOrdinal int not null, + primary key(id) +); + create table GeneratedAlwaysTest ( id int not null, name varchar(20) not null, diff --git a/core/mybatis-generator-systests-kotlin/src/main/resources/generatorConfig.xml b/core/mybatis-generator-systests-kotlin/src/main/resources/generatorConfig.xml index 5e1a912659..2d6307756e 100644 --- a/core/mybatis-generator-systests-kotlin/src/main/resources/generatorConfig.xml +++ b/core/mybatis-generator-systests-kotlin/src/main/resources/generatorConfig.xml @@ -158,6 +158,10 @@
+ + +
diff --git a/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/AbstractAnnotatedMiscellaneousTest.kt b/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/AbstractAnnotatedMiscellaneousTest.kt index b72f856960..01ea1575f2 100644 --- a/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/AbstractAnnotatedMiscellaneousTest.kt +++ b/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/AbstractAnnotatedMiscellaneousTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 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. @@ -34,6 +34,7 @@ abstract class AbstractAnnotatedMiscellaneousTest { val environment = Environment("test", JdbcTransactionFactory(), ds) val config = Configuration(environment) config.addMapper(EnumtestMapper::class.java) + config.addMapper(EnumordinaltestMapper::class.java) config.addMapper(GeneratedalwaystestMapper::class.java) config.addMapper(GeneratedalwaystestnoupdatesMapper::class.java) config.addMapper(MyObjectMapper::class.java) diff --git a/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/MiscellaneousTest.kt b/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/MiscellaneousTest.kt index 4ab25c941e..a656f46444 100644 --- a/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/MiscellaneousTest.kt +++ b/core/mybatis-generator-systests-kotlin/src/test/kotlin/mbg/test/mb3/dsql/kotlin/miscellaneous/MiscellaneousTest.kt @@ -22,6 +22,7 @@ import mbg.test.mb3.common.TestEnum import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.mapper.MyObjectMapper import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.mapper.* import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.mapper.MyObjectDynamicSqlSupport.myObject +import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.model.Enumordinaltest import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.model.Enumtest import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.model.MyObject import mbg.test.mb3.generated.dsql.kotlin.miscellaneous.model.Regexrename @@ -970,4 +971,53 @@ class MiscellaneousTest : AbstractAnnotatedMiscellaneousTest() { assertEquals(TestEnum.FRED, returnedRecord.name) } } + + @Test + fun testEnumOrdinal() { + openSession().use { sqlSession -> + val mapper = sqlSession.getMapper(EnumordinaltestMapper::class.java) + + + val enumTest = Enumordinaltest() + enumTest.id = 1 + enumTest.enumordinal = TestEnum.FRED + + val rows = mapper.insert(enumTest) + assertEquals(1, rows) + + val returnedRecords = mapper.select { allRows() } + assertEquals(1, returnedRecords.size) + + val returnedRecord = returnedRecords[0] + assertEquals(1, returnedRecord.id) + assertEquals(TestEnum.FRED, returnedRecord.enumordinal) + } + } + + @Test + fun testEnumOrdinalInsertMultiple() { + openSession().use { sqlSession -> + val mapper = sqlSession.getMapper(EnumordinaltestMapper::class.java) + val records = listOf( + Enumordinaltest().apply { + id = 1 + enumordinal = TestEnum.FRED + }, + Enumordinaltest().apply { + id = 2 + enumordinal = TestEnum.BARNEY + } + ) + + val rows = mapper.insertMultiple(records) + assertEquals(2, rows) + + val returnedRecords = mapper.select { allRows() } + assertEquals(2, returnedRecords.size) + + val returnedRecord = returnedRecords[0] + assertEquals(1, returnedRecord.id) + assertEquals(TestEnum.FRED, returnedRecord.enumordinal) + } + } } diff --git a/core/mybatis-generator-systests-mybatis3-java8/src/main/resources/generatorConfig.xml b/core/mybatis-generator-systests-mybatis3-java8/src/main/resources/generatorConfig.xml index fd07a419a4..07e80f0b18 100644 --- a/core/mybatis-generator-systests-mybatis3-java8/src/main/resources/generatorConfig.xml +++ b/core/mybatis-generator-systests-mybatis3-java8/src/main/resources/generatorConfig.xml @@ -164,6 +164,10 @@
+ + +
diff --git a/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/AbstractAnnotatedMiscellaneousTest.java b/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/AbstractAnnotatedMiscellaneousTest.java index 96ecb12b4c..307986634b 100644 --- a/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/AbstractAnnotatedMiscellaneousTest.java +++ b/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/AbstractAnnotatedMiscellaneousTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 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. @@ -17,6 +17,7 @@ import static mbg.test.common.util.TestUtilities.createDatabase; +import mbg.test.mb3.generated.dsql.miscellaneous.mapper.EnumordinaltestMapper; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; @@ -46,6 +47,7 @@ public void setUp() throws Exception { Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(EnumtestMapper.class); + config.addMapper(EnumordinaltestMapper.class); config.addMapper(GeneratedalwaystestMapper.class); config.addMapper(GeneratedalwaystestnoupdatesMapper.class); config.addMapper(MyObjectMapper.class); diff --git a/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/MiscellaneousTest.java b/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/MiscellaneousTest.java index 042bb68135..0b4c36f4a4 100644 --- a/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/MiscellaneousTest.java +++ b/core/mybatis-generator-systests-mybatis3-java8/src/test/java/mbg/test/mb3/dsql/miscellaneous/MiscellaneousTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 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. @@ -27,6 +27,8 @@ import java.util.List; import java.util.Optional; +import mbg.test.mb3.generated.dsql.miscellaneous.mapper.EnumordinaltestMapper; +import mbg.test.mb3.generated.dsql.miscellaneous.model.Enumordinaltest; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.Test; @@ -998,4 +1000,52 @@ public void testEnumInsertMultiple() { assertEquals(TestEnum.FRED, returnedRecord.getName()); } } + + @Test + public void testEnumOrdinal() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + EnumordinaltestMapper mapper = sqlSession.getMapper(EnumordinaltestMapper.class); + + Enumordinaltest enumTest = new Enumordinaltest(); + enumTest.setId(1); + enumTest.setEnumordinal(TestEnum.FRED); + int rows = mapper.insert(enumTest); + assertEquals(1, rows); + + List returnedRecords = mapper.select(SelectDSLCompleter.allRows()); + assertEquals(1, returnedRecords.size()); + + Enumordinaltest returnedRecord = returnedRecords.get(0); + assertEquals(1, returnedRecord.getId().intValue()); + assertEquals(TestEnum.FRED, returnedRecord.getEnumordinal()); + } + } + + @Test + public void testEnumOrdinalInsertMultiple() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + EnumordinaltestMapper mapper = sqlSession.getMapper(EnumordinaltestMapper.class); + List records = new ArrayList<>(); + + Enumordinaltest enumTest = new Enumordinaltest(); + enumTest.setId(1); + enumTest.setEnumordinal(TestEnum.FRED); + records.add(enumTest); + + enumTest = new Enumordinaltest(); + enumTest.setId(2); + enumTest.setEnumordinal(TestEnum.BARNEY); + records.add(enumTest); + + int rows = mapper.insertMultiple(records); + assertEquals(2, rows); + + List returnedRecords = mapper.select(SelectDSLCompleter.allRows()); + assertEquals(2, returnedRecords.size()); + + Enumordinaltest returnedRecord = returnedRecords.get(0); + assertEquals(1, returnedRecord.getId().intValue()); + assertEquals(TestEnum.FRED, returnedRecord.getEnumordinal()); + } + } } From 77a76d1a049b18d93332b8b6c7b52a9877e2c7e6 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Thu, 3 Mar 2022 14:23:29 -0500 Subject: [PATCH 2/5] Upgrade to release version of MyBatis Dynamic SQL --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index 2db1698f17..c908389648 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -273,7 +273,7 @@ org.mybatis.dynamic-sql mybatis-dynamic-sql - 1.4.0-SNAPSHOT + 1.4.0 org.jetbrains.kotlin From bc754b226d1e78d740cbaf54254f47495751cee8 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Thu, 3 Mar 2022 14:52:32 -0500 Subject: [PATCH 3/5] Calm down the modernizer --- .../main/java/org/mybatis/generator/api/MyBatisGenerator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java index 11d8264a3a..248a539733 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java @@ -23,6 +23,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; +import java.nio.charset.Charset; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashSet; @@ -427,7 +428,7 @@ private void writeFile(File file, String content, String fileEncoding) throws IO if (fileEncoding == null) { osw = new OutputStreamWriter(fos); } else { - osw = new OutputStreamWriter(fos, fileEncoding); + osw = new OutputStreamWriter(fos, Charset.forName(fileEncoding)); } try (BufferedWriter bw = new BufferedWriter(osw)) { From 7a61388c54bdacc4a9f2310854b251c4a12bc3b0 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Thu, 3 Mar 2022 14:55:00 -0500 Subject: [PATCH 4/5] More tests --- .../src/test/resources/scripts/CreateDB.sql | 2 +- .../scripts/generatorConfig-kotlin.xml | 2 +- .../resources/scripts/generatorConfig.xml | 10 ++++++- .../scripts/generatorConfig_Dsql.xml | 2 +- .../mbg/test/common/scripts/CreateDB.sql | 2 +- .../src/main/resources/generatorConfig.xml | 2 +- .../kotlin/miscellaneous/MiscellaneousTest.kt | 10 +++---- .../src/main/resources/generatorConfig.xml | 2 +- .../dsql/miscellaneous/MiscellaneousTest.java | 10 +++---- .../src/main/resources/generatorConfig.xml | 10 ++++++- .../AbstractAnnotatedMiscellaneousTest.java | 4 ++- .../miscellaneous/MiscellaneousTest.java | 28 ++++++++++++++++++- .../mb3/miscellaneous/MiscellaneousTest.java | 28 ++++++++++++++++++- .../test/mb3/miscellaneous/MapperConfig.xml | 3 +- 14 files changed, 93 insertions(+), 22 deletions(-) diff --git a/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql b/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql index 721cfd6573..2c57b7bdfa 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql +++ b/core/mybatis-generator-core/src/test/resources/scripts/CreateDB.sql @@ -158,7 +158,7 @@ create table EnumTest ( create table EnumOrdinalTest ( id int not null, - enumOrdinal int not null, + name int not null, primary key(id) ); diff --git a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml index 2d6307756e..4f8a0849be 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml +++ b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig-kotlin.xml @@ -159,7 +159,7 @@
-
diff --git a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig.xml b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig.xml index cc24a6d015..b31d5d595c 100644 --- a/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig.xml +++ b/core/mybatis-generator-core/src/test/resources/scripts/generatorConfig.xml @@ -1,7 +1,7 @@