diff --git a/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.java b/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.java new file mode 100644 index 00000000000..9f1b54d8b88 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.java @@ -0,0 +1,26 @@ +/** + * Copyright 2009-2016 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 + * + * 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.builder; + +import org.apache.ibatis.domain.blog.Author; + +public interface CachedAuthorMapper { + Author selectAllAuthors(); + Author selectAuthorWithInlineParams(int id); + void insertAuthor(Author author); + boolean updateAuthor(Author author); + boolean deleteAuthor(int id); +} diff --git a/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.xml b/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.xml index d2e4087ea24..9f0e81f0f75 100644 --- a/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.xml +++ b/src/test/java/org/apache/ibatis/builder/CachedAuthorMapper.xml @@ -20,7 +20,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + diff --git a/src/test/java/org/apache/ibatis/builder/CustomLongTypeHandler.java b/src/test/java/org/apache/ibatis/builder/CustomLongTypeHandler.java new file mode 100644 index 00000000000..601947f42ee --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/CustomLongTypeHandler.java @@ -0,0 +1,50 @@ +/** + * Copyright 2009-2016 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 + * + * 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.builder; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TypeHandler; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@MappedTypes(Long.class) +public class CustomLongTypeHandler implements TypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType) throws SQLException { + ps.setLong(i, parameter); + } + + @Override + public Long getResult(ResultSet rs, String columnName) throws SQLException { + return rs.getLong(columnName); + } + + @Override + public Long getResult(ResultSet rs, int columnIndex) throws SQLException { + return rs.getLong(columnIndex); + } + + @Override + public Long getResult(CallableStatement cs, int columnIndex) throws SQLException { + return cs.getLong(columnIndex); + } + +} diff --git a/src/test/java/org/apache/ibatis/builder/CustomObjectWrapperFactory.java b/src/test/java/org/apache/ibatis/builder/CustomObjectWrapperFactory.java new file mode 100644 index 00000000000..e874ad94df6 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/CustomObjectWrapperFactory.java @@ -0,0 +1,40 @@ +/** + * Copyright 2009-2016 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 + * + * 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.builder; + +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.reflection.factory.DefaultObjectFactory; +import org.apache.ibatis.reflection.wrapper.ObjectWrapper; +import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; + +import java.util.List; +import java.util.Properties; + +public class CustomObjectWrapperFactory implements ObjectWrapperFactory { + + private String option; + + @Override + public boolean hasWrapperFor(Object object) { + return false; + } + + @Override + public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) { + return null; + } + +} diff --git a/src/test/java/org/apache/ibatis/builder/ExampleTypeHandler.java b/src/test/java/org/apache/ibatis/builder/CustomStringTypeHandler.java similarity index 91% rename from src/test/java/org/apache/ibatis/builder/ExampleTypeHandler.java rename to src/test/java/org/apache/ibatis/builder/CustomStringTypeHandler.java index 35d7b3af91d..8ed3cf7440c 100644 --- a/src/test/java/org/apache/ibatis/builder/ExampleTypeHandler.java +++ b/src/test/java/org/apache/ibatis/builder/CustomStringTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2016 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. @@ -23,7 +23,7 @@ import java.sql.ResultSet; import java.sql.SQLException; -public class ExampleTypeHandler implements TypeHandler { +public class CustomStringTypeHandler implements TypeHandler { @Override public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { diff --git a/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml b/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml index 222b000873d..c370e3a9ddd 100644 --- a/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml +++ b/src/test/java/org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml @@ -22,6 +22,10 @@ + + + + @@ -49,4 +53,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/builder/ExampleObjectFactory.java b/src/test/java/org/apache/ibatis/builder/ExampleObjectFactory.java index c5b33e3e91e..7ae89b9491e 100644 --- a/src/test/java/org/apache/ibatis/builder/ExampleObjectFactory.java +++ b/src/test/java/org/apache/ibatis/builder/ExampleObjectFactory.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2016 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. @@ -21,7 +21,7 @@ import java.util.Properties; public class ExampleObjectFactory extends DefaultObjectFactory { - + private Properties properties; @Override public T create(Class type) { return super.create(type); @@ -35,6 +35,11 @@ public T create(Class type, List> constructorArgTypes, List - + diff --git a/src/test/java/org/apache/ibatis/builder/PropertiesUrlMapperConfig.xml b/src/test/java/org/apache/ibatis/builder/PropertiesUrlMapperConfig.xml new file mode 100644 index 00000000000..1971e239b21 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/PropertiesUrlMapperConfig.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java b/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java index 6285c16ddfd..f90f392bc14 100644 --- a/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java +++ b/src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java @@ -23,14 +23,27 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.HashSet; +import java.util.Properties; import java.util.Set; +import org.apache.ibatis.builder.mapper.CustomMapper; +import org.apache.ibatis.builder.typehandler.CustomIntegerTypeHandler; import org.apache.ibatis.builder.xml.XMLConfigBuilder; +import org.apache.ibatis.datasource.pooled.PooledDataSource; +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.domain.blog.Author; +import org.apache.ibatis.domain.blog.Blog; +import org.apache.ibatis.domain.blog.mappers.BlogMapper; +import org.apache.ibatis.domain.blog.mappers.NestedBlogMapper; +import org.apache.ibatis.domain.jpetstore.Cart; import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory; import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory; import org.apache.ibatis.io.JBoss6VFS; import org.apache.ibatis.io.Resources; import org.apache.ibatis.logging.slf4j.Slf4jImpl; +import org.apache.ibatis.mapping.DatabaseIdProvider; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.scripting.defaults.RawLanguageDriver; import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver; import org.apache.ibatis.session.AutoMappingBehavior; @@ -38,11 +51,14 @@ import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.LocalCacheScope; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.hamcrest.core.Is.*; import static org.hamcrest.core.IsInstanceOf.*; @@ -50,6 +66,9 @@ public class XmlConfigBuilderTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception { String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml"; @@ -143,7 +162,9 @@ public void registerJavaTypeInitializingTypeHandler() { public void shouldSuccessfullyLoadXMLConfigFile() throws Exception { String resource = "org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); - XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); + Properties props = new Properties(); + props.put("prop2", "cccc"); + XMLConfigBuilder builder = new XMLConfigBuilder(inputStream, null, props); Configuration config = builder.parse(); assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.NONE)); @@ -171,6 +192,110 @@ public void shouldSuccessfullyLoadXMLConfigFile() throws Exception { assertThat(config.getVfsImpl().getName(), is(JBoss6VFS.class.getName())); assertThat(config.getConfigurationFactory().getName(), is(String.class.getName())); + assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blogauthor").equals(Author.class)); + assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blog").equals(Blog.class)); + assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("cart").equals(Cart.class)); + + assertThat(config.getTypeHandlerRegistry().getTypeHandler(Integer.class), is(instanceOf(CustomIntegerTypeHandler.class))); + assertThat(config.getTypeHandlerRegistry().getTypeHandler(Long.class), is(instanceOf(CustomLongTypeHandler.class))); + assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class), is(instanceOf(CustomStringTypeHandler.class))); + assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR), is(instanceOf(CustomStringTypeHandler.class))); + + ExampleObjectFactory objectFactory = (ExampleObjectFactory)config.getObjectFactory(); + assertThat(objectFactory.getProperties().size(), is(1)); + assertThat(objectFactory.getProperties().getProperty("objectFactoryProperty"), is("100")); + + assertThat(config.getObjectWrapperFactory(), is(instanceOf(CustomObjectWrapperFactory.class))); + + ExamplePlugin plugin = (ExamplePlugin)config.getInterceptors().get(0); + assertThat(plugin.getProperties().size(), is(1)); + assertThat(plugin.getProperties().getProperty("pluginProperty"), is("100")); + + Environment environment = config.getEnvironment(); + assertThat(environment.getId(), is("development")); + assertThat(environment.getDataSource(), is(instanceOf(UnpooledDataSource.class))); + assertThat(environment.getTransactionFactory(), is(instanceOf(JdbcTransactionFactory.class))); + + assertThat(config.getDatabaseId(), is("derby")); + + assertThat(config.getMapperRegistry().getMappers().size(), is(4)); + assertThat(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class), is(true)); + assertThat(config.getMapperRegistry().hasMapper(CustomMapper.class), is(true)); + assertThat(config.getMapperRegistry().hasMapper(BlogMapper.class), is(true)); + assertThat(config.getMapperRegistry().hasMapper(NestedBlogMapper.class), is(true)); + } + @Test + public void shouldSuccessfullyLoadXMLConfigFileWithPropertiesUrl() throws Exception { + String resource = "org/apache/ibatis/builder/PropertiesUrlMapperConfig.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); + Configuration config = builder.parse(); + assertThat(config.getVariables().get("driver").toString(), is("org.apache.derby.jdbc.EmbeddedDriver")); + assertThat(config.getVariables().get("prop1").toString(), is("bbbb")); + + } + + @Test + public void parseIsTwice() throws Exception { + String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); + builder.parse(); + + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Each XMLConfigBuilder can only be used once."); + builder.parse(); + } + + @Test + public void unknownSettings() { + final String MAPPER_CONFIG = "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n"; + + expectedException.expect(BuilderException.class); + expectedException.expectMessage("The setting foo is not known. Make sure you spelled it correctly (case sensitive)."); + + XMLConfigBuilder builder = new XMLConfigBuilder(new StringReader(MAPPER_CONFIG)); + builder.parse(); + } + + @Test + public void unknownJavaTypeOnTypeHandler() { + final String MAPPER_CONFIG = "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n"; + + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Error registering typeAlias for 'null'. Cause: "); + + XMLConfigBuilder builder = new XMLConfigBuilder(new StringReader(MAPPER_CONFIG)); + builder.parse(); + } + + @Test + public void propertiesSpecifyResourceAndUrlAtSameTime() { + final String MAPPER_CONFIG = "\n" + + "\n" + + "\n" + + " \n" + + "\n"; + + expectedException.expect(BuilderException.class); + expectedException.expectMessage("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other."); + + XMLConfigBuilder builder = new XMLConfigBuilder(new StringReader(MAPPER_CONFIG)); + builder.parse(); + } + } diff --git a/src/test/java/org/apache/ibatis/builder/jdbc.properties b/src/test/java/org/apache/ibatis/builder/jdbc.properties new file mode 100644 index 00000000000..79796c40546 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/jdbc.properties @@ -0,0 +1,20 @@ +# +# Copyright 2009-2016 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 +# +# 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. +# + +driver=org.apache.derby.jdbc.EmbeddedDriver +url=jdbc:derby:ibderby;create=true +username= +password= diff --git a/src/test/java/org/apache/ibatis/builder/mapper/CustomMapper.java b/src/test/java/org/apache/ibatis/builder/mapper/CustomMapper.java new file mode 100644 index 00000000000..76b8c993a37 --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/mapper/CustomMapper.java @@ -0,0 +1,19 @@ +/** + * Copyright 2009-2016 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 + * + * 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.builder.mapper; + +public interface CustomMapper { +} diff --git a/src/test/java/org/apache/ibatis/builder/typehandler/CustomIntegerTypeHandler.java b/src/test/java/org/apache/ibatis/builder/typehandler/CustomIntegerTypeHandler.java new file mode 100644 index 00000000000..1d87110918e --- /dev/null +++ b/src/test/java/org/apache/ibatis/builder/typehandler/CustomIntegerTypeHandler.java @@ -0,0 +1,50 @@ +/** + * Copyright 2009-2016 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 + * + * 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.builder.typehandler; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TypeHandler; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@MappedTypes(Integer.class) +public class CustomIntegerTypeHandler implements TypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType) throws SQLException { + ps.setInt(i, parameter); + } + + @Override + public Integer getResult(ResultSet rs, String columnName) throws SQLException { + return rs.getInt(columnName); + } + + @Override + public Integer getResult(ResultSet rs, int columnIndex) throws SQLException { + return rs.getInt(columnIndex); + } + + @Override + public Integer getResult(CallableStatement cs, int columnIndex) throws SQLException { + return cs.getInt(columnIndex); + } + +} diff --git a/src/test/java/org/apache/ibatis/domain/blog/mappers/NestedBlogMapper.java b/src/test/java/org/apache/ibatis/domain/blog/mappers/NestedBlogMapper.java new file mode 100644 index 00000000000..07c53aa49e0 --- /dev/null +++ b/src/test/java/org/apache/ibatis/domain/blog/mappers/NestedBlogMapper.java @@ -0,0 +1,19 @@ +/** + * Copyright 2009-2016 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 + * + * 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.domain.blog.mappers; + +public interface NestedBlogMapper { +} diff --git a/src/test/java/org/apache/ibatis/session/SqlSessionTest.java b/src/test/java/org/apache/ibatis/session/SqlSessionTest.java index 5ca6e5802e9..7214c1cdc16 100644 --- a/src/test/java/org/apache/ibatis/session/SqlSessionTest.java +++ b/src/test/java/org/apache/ibatis/session/SqlSessionTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2016 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. @@ -505,7 +505,7 @@ public void shouldCacheAllAuthors() throws Exception { int second = -1; SqlSession session = sqlMapper.openSession(); try { - List authors = session.selectList("com.domain.CachedAuthorMapper.selectAllAuthors"); + List authors = session.selectList("org.apache.ibatis.builder.CachedAuthorMapper.selectAllAuthors"); first = System.identityHashCode(authors); session.commit(); // commit should not be required for read/only activity. } finally { @@ -513,7 +513,7 @@ public void shouldCacheAllAuthors() throws Exception { } session = sqlMapper.openSession(); try { - List authors = session.selectList("com.domain.CachedAuthorMapper.selectAllAuthors"); + List authors = session.selectList("org.apache.ibatis.builder.CachedAuthorMapper.selectAllAuthors"); second = System.identityHashCode(authors); } finally { session.close();