Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Jan 14, 2025
1 parent ab70247 commit c1ad5a0
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions docs/spring/cse48cc092-d218-11ef-85e3-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.submitted.global_variables_defaults;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Property;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.type.JdbcType;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

class CustomizationTest {

@Test
void applyDefaultValueWhenCustomizeDefaultValueSeparator() throws IOException {

Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");

Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
Configuration configuration = factory.getConfiguration();
configuration.addMapper(CustomDefaultValueSeparatorMapper.class);

SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));

Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.NULL);
Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
.isEqualTo("jdbc:hsqldb:mem:global_variables_defaults");
Assertions.assertThat(configuration.getDatabaseId()).isEqualTo("hsql");
Assertions.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
.isEqualTo("default");
Assertions.assertThat(cache.getName()).isEqualTo("default");

try (SqlSession sqlSession = factory.openSession()) {
CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
Assertions.assertThat(mapper.selectValue(null)).isEqualTo("default");
}

}

@Test
void applyPropertyValueWhenCustomizeDefaultValueSeparator() throws IOException {

Properties props = new Properties();
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
props.setProperty("settings:jdbcTypeForNull", JdbcType.CHAR.name());
props.setProperty("db:name", "global_variables_defaults_custom");
props.setProperty("productName:hsql", "Hsql");
props.setProperty("objectFactory:name", "customObjectFactory");
props.setProperty("cache:name", "customCache");

Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
Configuration configuration = factory.getConfiguration();
configuration.addMapper(CustomDefaultValueSeparatorMapper.class);

SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));

Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.CHAR);
Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
.isEqualTo("jdbc:hsqldb:mem:global_variables_defaults_custom");
Assertions.assertThat(configuration.getDatabaseId()).isNull();
Assertions.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
.isEqualTo("customObjectFactory");
Assertions.assertThat(cache.getName()).isEqualTo("customCache");

try (SqlSession sqlSession = factory.openSession()) {
CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
Assertions.assertThat(mapper.selectValue("3333")).isEqualTo("3333");
}

}

@CacheNamespace(implementation = SupportClasses.CustomCache.class, properties = {
@Property(name = "name", value = "${cache:name?:default}")
})
private interface CustomDefaultValueSeparatorMapper {
@Select("SELECT '${val != null ? val : 'default'}' FROM INFORMATION_SCHEMA.SYSTEM_USERS")
String selectValue(@Param("val") String val);
}

}

0 comments on commit c1ad5a0

Please sign in to comment.