Author: jgbutler Date: Thu Nov 12 23:00:33 2009 New Revision: 835626 URL: http://svn.apache.org/viewvc?rev=835626&view=rev Log: [ibator] a new test for enum, and a plugin for ibatis3 mapperconfig files
Added: ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/plugins/MapperConfigPlugin.java ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/java/ibatortest/execute/miscellaneous/TestEnum.java Modified: ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/generator/XmlConstants.java ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/CreateDB.sql ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/ibatorConfig.xml ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/java/ibatortest/execute/miscellaneous/MiscellaneousTest.java ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/resources/ibatortest/MapperConfig.xml Modified: ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/generator/XmlConstants.java URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/generator/XmlConstants.java?rev=835626&r1=835625&r2=835626&view=diff ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/generator/XmlConstants.java (original) +++ ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/generator/XmlConstants.java Thu Nov 12 23:00:33 2009 @@ -40,6 +40,10 @@ public static final String IBATIS3_MAPPER_PUBLIC_ID = "-//ibatis.apache.org//DTD Mapper 3.0//EN"; //$NON-NLS-1$ + public static final String IBATIS3_MAPPER_CONFIG_SYSTEM_ID = "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"; //$NON-NLS-1$ + + public static final String IBATIS3_MAPPER_CONFIG_PUBLIC_ID = "-//ibatis.apache.org//DTD Config 3.0//EN"; //$NON-NLS-1$ + public static final String IBATOR_CONFIG_SYSTEM_ID = "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd"; //$NON-NLS-1$ public static final String IBATOR_CONFIG_PUBLIC_ID = "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"; //$NON-NLS-1$ Added: ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/plugins/MapperConfigPlugin.java URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/plugins/MapperConfigPlugin.java?rev=835626&view=auto ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/plugins/MapperConfigPlugin.java (added) +++ ibatis/java/ibator/trunk/core/ibator-core/src/main/java/org/apache/ibatis/ibator/plugins/MapperConfigPlugin.java Thu Nov 12 23:00:33 2009 @@ -0,0 +1,146 @@ +/* + * Copyright 2009 The Apache Software Foundation + * + * 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. + */ +package org.apache.ibatis.ibator.plugins; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.ibatis.ibator.api.GeneratedXmlFile; +import org.apache.ibatis.ibator.api.IbatorPluginAdapter; +import org.apache.ibatis.ibator.api.IntrospectedTable; +import org.apache.ibatis.ibator.api.dom.xml.Attribute; +import org.apache.ibatis.ibator.api.dom.xml.Document; +import org.apache.ibatis.ibator.api.dom.xml.TextElement; +import org.apache.ibatis.ibator.api.dom.xml.XmlElement; +import org.apache.ibatis.ibator.generator.XmlConstants; +import org.apache.ibatis.ibator.internal.util.StringUtility; +import org.apache.ibatis.ibator.internal.util.messages.Messages; + +/** + * This plugin generates a MapperConfig file containing mapper entries for + * SQL maps generated by Ibator for iBATIS3. This demonstrates hooking into + * the Ibator code generation lifecycle and generating additional XML files. + * <p> + * This plugin accepts three properties: + * <ul> + * <li><tt>fileName</tt> (optional) the name of the generated file. + * this defaults to "SqlMapConfig.xml" if not specified.</li> + * <li><tt>targetPackage</tt> (required) the name of the package where the + * file should be placed. Specified like "com.mycompany.sql".</li> + * <li><tt>targetProject</tt> (required) the name of the project where the + * file should be placed.</li> + * </ul> + * + * Note: targetPackage and targetProject follow the same rules as the + * targetPackage and targetProject values on the sqlMapGenerator + * configuration element. + * + * @author Jeff Butler + * + */ +public class MapperConfigPlugin extends IbatorPluginAdapter { + + private List<String> mapperFiles; + + public MapperConfigPlugin() { + mapperFiles = new ArrayList<String>(); + } + + public boolean validate(List<String> warnings) { + boolean valid = true; + + if (!StringUtility.stringHasValue(properties.getProperty("targetProject"))) { //$NON-NLS-1$ + warnings.add(Messages.getString("ValidationError.18", //$NON-NLS-1$ + "MapperConfigPlugin", //$NON-NLS-1$ + "targetProject")); //$NON-NLS-1$ + valid = false; + } + + if (!StringUtility.stringHasValue(properties.getProperty("targetPackage"))) { //$NON-NLS-1$ + warnings.add(Messages.getString("ValidationError.18", //$NON-NLS-1$ + "MapperConfigPlugin", //$NON-NLS-1$ + "targetPackage")); //$NON-NLS-1$ + valid = false; + } + + return valid; + } + + @Override + public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() { + Document document = new Document(XmlConstants.IBATIS3_MAPPER_CONFIG_PUBLIC_ID, + XmlConstants.IBATIS3_MAPPER_CONFIG_SYSTEM_ID); + + XmlElement root = new XmlElement("configuration"); //$NON-NLS-1$ + document.setRootElement(root); + + root.addElement(new TextElement("<!--")); //$NON-NLS-1$ + root.addElement(new TextElement( + " This file is generated by Apache iBATIS ibator.")); //$NON-NLS-1$ + root.addElement(new TextElement( + " This file is the shell of a Mapper Config file - in many cases you will need to add")); //$NON-NLS-1$ + root.addElement(new TextElement( + " to this file before it is usable by iBATIS.")); //$NON-NLS-1$ + + StringBuilder sb = new StringBuilder(); + sb.append(" This file was generated on "); //$NON-NLS-1$ + sb.append(new Date()); + sb.append('.'); + root.addElement(new TextElement(sb.toString())); + + root.addElement(new TextElement("-->")); //$NON-NLS-1$ + + XmlElement mappers = new XmlElement("mappers"); //$NON-NLS-1$ + root.addElement(mappers); + + XmlElement mapper; + for (String mapperFile : mapperFiles) { + mapper = new XmlElement("mapper"); //$NON-NLS-1$ + mapper.addAttribute(new Attribute("resource", mapperFile)); //$NON-NLS-1$ + mappers.addElement(mapper); + } + + GeneratedXmlFile gxf = new GeneratedXmlFile(document, + properties.getProperty("fileName", "MapperConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$ + properties.getProperty("targetPackage"), //$NON-NLS-1$ + properties.getProperty("targetProject"), //$NON-NLS-1$ + false); + + List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1); + answer.add(gxf); + + return answer; + } + + /* + * This method collects the name of every SqlMap file generated + * by Ibator in this context. + */ + @Override + public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) { + StringBuilder sb = new StringBuilder(); + sb.append(sqlMap.getTargetPackage()); + sb.append('.'); + String temp = sb.toString(); + sb.setLength(0); + sb.append(temp.replace('.', '/')); + sb.append(sqlMap.getFileName()); + mapperFiles.add(sb.toString()); + + return true; + } +} Added: ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/java/ibatortest/execute/miscellaneous/TestEnum.java URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/java/ibatortest/execute/miscellaneous/TestEnum.java?rev=835626&view=auto ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/java/ibatortest/execute/miscellaneous/TestEnum.java (added) +++ ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/java/ibatortest/execute/miscellaneous/TestEnum.java Thu Nov 12 23:00:33 2009 @@ -0,0 +1,28 @@ +/* + * Copyright 2009 The Apache Software Foundation + * + * 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. + */ + +package ibatortest.execute.miscellaneous; + +/** + * @author Jeff Butler + * + */ +public enum TestEnum { + FRED, + WILMA, + BARNEY, + BETTY +} Modified: ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/CreateDB.sql URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/CreateDB.sql?rev=835626&r1=835625&r2=835626&view=diff ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/CreateDB.sql (original) +++ ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/CreateDB.sql Thu Nov 12 23:00:33 2009 @@ -8,6 +8,7 @@ drop table BlobsOnly if exists; drop table RegexRename if exists; drop table AnotherAwfulTable if exists; +drop table EnumTest if exists; create table FieldsOnly ( IntegerField int, @@ -101,3 +102,9 @@ "delete" varchar(30), primary key(id) ); + +create table EnumTest ( + id int not null, + name varchar(20) not null, + primary key(id) +); Modified: ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/ibatorConfig.xml URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/ibatorConfig.xml?rev=835626&r1=835625&r2=835626&view=diff ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/ibatorConfig.xml (original) +++ ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/main/resources/ibatorConfig.xml Thu Nov 12 23:00:33 2009 @@ -165,6 +165,9 @@ <table tableName="RegexRename"> <columnRenamingRule searchString="^CUST" /> </table> + <table tableName="EnumTest"> + <columnOverride column="name" javaType="ibatortest.execute.miscellaneous.TestEnum"/> + </table> </ibatorContext> <ibatorContext id="miscellaneousTests_NoDAO" targetRuntime="Ibatis3"> Modified: ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/java/ibatortest/execute/miscellaneous/MiscellaneousTest.java URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/java/ibatortest/execute/miscellaneous/MiscellaneousTest.java?rev=835626&r1=835625&r2=835626&view=diff ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/java/ibatortest/execute/miscellaneous/MiscellaneousTest.java (original) +++ ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/java/ibatortest/execute/miscellaneous/MiscellaneousTest.java Thu Nov 12 23:00:33 2009 @@ -17,9 +17,11 @@ import static org.junit.Assert.*; import ibatortest.AbstractTest; +import ibatortest.generated.miscellaneous.dao.EnumtestMapper; import ibatortest.generated.miscellaneous.dao.MyObjectMapper; import ibatortest.generated.miscellaneous.dao.RegexrenameMapper; import ibatortest.generated.miscellaneous.model.Anotherawfultable; +import ibatortest.generated.miscellaneous.model.Enumtest; import ibatortest.generated.miscellaneous.model.MyObject; import ibatortest.generated.miscellaneous.model.MyObjectCriteria; import ibatortest.generated.miscellaneous.model.MyObjectKey; @@ -1017,4 +1019,28 @@ sqlSession.close(); } } + + @Test + public void testEnum() { + SqlSession sqlSession = sqlSessionFactory.openSession(); + + try { + EnumtestMapper mapper = sqlSession.getMapper(EnumtestMapper.class); + + Enumtest enumTest = new Enumtest(); + enumTest.setId(1); + enumTest.setName(TestEnum.FRED); + int rows = mapper.insert(enumTest); + assertEquals(1, rows); + + List<Enumtest> returnedRecords = mapper.selectByExample(null); + assertEquals(1, returnedRecords.size()); + + Enumtest returnedRecord = returnedRecords.get(0); + assertEquals(1, returnedRecord.getId().intValue()); + assertEquals(TestEnum.FRED, returnedRecord.getName()); + } finally { + sqlSession.close(); + } + } } Modified: ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/resources/ibatortest/MapperConfig.xml URL: http://svn.apache.org/viewvc/ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/resources/ibatortest/MapperConfig.xml?rev=835626&r1=835625&r2=835626&view=diff ============================================================================== --- ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/resources/ibatortest/MapperConfig.xml (original) +++ ibatis/java/ibator/trunk/core/ibator-systests-ibatis3/src/test/resources/ibatortest/MapperConfig.xml Thu Nov 12 23:00:33 2009 @@ -53,6 +53,7 @@ <mapper resource="ibatortest/generated/miscellaneous/xml/AnotherawfultableMapper.xml" /> <mapper resource="ibatortest/generated/miscellaneous/xml/MyObjectMapper.xml" /> <mapper resource="ibatortest/generated/miscellaneous/xml/RegexrenameMapper.xml" /> + <mapper resource="ibatortest/generated/miscellaneous/xml/EnumtestMapper.xml" /> </mappers> </configuration>