Skip to content

Commit

Permalink
Demonstrating the issue reported in mybatis#902 .
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Jun 13, 2017
1 parent de59d27 commit b93072c
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@
<version>9.1-901-1.jdbc4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.embed</groupId>
<artifactId>postgresql-embedded</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--
-- Copyright 2009-2017 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.
--

CREATE SCHEMA mbtest;

CREATE TABLE mbtest.users (
user_id serial PRIMARY KEY,
name character varying(30)
);

INSERT INTO mbtest.users (name) values
('Jimmy');


CREATE TABLE mbtest.sections (
section_id int PRIMARY KEY,
name character varying(30)
);

INSERT INTO mbtest.sections (section_id, name) values
(1, 'Section 1');
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2009-2017 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.submitted.usesjava8.postgres_genkeys;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

public interface Mapper {
@Update("update mbtest.users set name = #{name} where user_id = #{userId}")
int updateUserById(@Param("userId") Integer userId, @Param("name") String name);

@Update("update mbtest.sections set name = #{name} where section_id = #{sectionId}")
int updateSectionById(@Param("sectionId") Integer sectionId, @Param("name") String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2017 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.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="useGeneratedKeys" value="true" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql:postgres_genkeys" />
<property name="username" value="postgres" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper
class="org.apache.ibatis.submitted.usesjava8.postgres_genkeys.Mapper" />
</mappers>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright 2009-2017 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.submitted.usesjava8.postgres_genkeys;

import static org.junit.Assert.*;

import java.io.Reader;
import java.nio.file.Paths;
import java.sql.Connection;
import java.util.Collections;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;

public class PostgresGeneratedKeysTest {

private static final EmbeddedPostgres postgres = new EmbeddedPostgres();

private static SqlSessionFactory sqlSessionFactory;

@BeforeClass
public static void setUp() throws Exception {
// Launch PostgreSQL server. Download / unarchive if necessary.
postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get("target/postgres")), "localhost", 5432, "postgres_genkeys", "postgres", "root", Collections.emptyList());

Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/MapperConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();

SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);
reader.close();
session.close();
}

@AfterClass
public static void tearDown() {
postgres.stop();
}

@Test
public void shouldDefaultKeyPropertyNotCauseException() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Mapper mapper = sqlSession.getMapper(Mapper.class);
int result = mapper.updateUserById(1, "Ethan");
assertEquals(1, result);
} finally {
sqlSession.close();
}
}

@Test
public void shouldDefaultKeyPropertyNotCauseException2() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Mapper mapper = sqlSession.getMapper(Mapper.class);
int result = mapper.updateSectionById(1, "IMF");
assertEquals(1, result);
} finally {
sqlSession.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2009-2017 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.submitted.usesjava8.postgres_genkeys;

public class User {
private Integer id;

private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit b93072c

Please sign in to comment.