Skip to content

Tests for Issue #324 #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions src/test/java/issues/gh324/Issue324Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2016-2021 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 issues.gh324;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.mapping.Environment;
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.transaction.jdbc.JdbcTransactionFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public class Issue324Test {
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";

private SqlSessionFactory sqlSessionFactory;

@BeforeEach
void setup() throws Exception {
Class.forName(JDBC_DRIVER);
InputStream is = getClass().getResourceAsStream("/issues/gh324/CreateDB.sql");
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
ScriptRunner sr = new ScriptRunner(connection);
sr.setLogWriter(null);
sr.runScript(new InputStreamReader(is));
}

UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
Configuration config = new Configuration(environment);
config.addMapper(NameTableMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

@Test
void testCacheWithAutoCommitOnUpdate() {
insertRecord();
Optional<NameRecord> returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Fred");
});

updateRecordWithAutoCommit();
returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Barney");
});
}

@Test
void testCacheWithNoAutoCommitOnUpdateAndNoExplicitCommit() {
insertRecord();
Optional<NameRecord> returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Fred");
});

// the update should rollback
updateRecordWithoutAutoCommitAndNoExplicitCommit();
returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Fred");
});
}

@Test
void testCacheWithNoAutoCommitOnUpdateAndExplicitCommit() {
insertRecord();
Optional<NameRecord> returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Fred");
});

updateRecordWithoutAutoCommitAndExplicitCommit();
returnedRecord = getRecord();
assertThat(returnedRecord).hasValueSatisfying(rr -> {
assertThat(rr.getId()).isEqualTo(1);
assertThat(rr.getName()).isEqualTo("Barney");
});
}

private void insertRecord() {
try (SqlSession session = sqlSessionFactory.openSession(true)) {
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
NameRecord record = new NameRecord();
record.setId(1);
record.setName("Fred");
mapper.insert(record);
}
}

private void updateRecordWithAutoCommit() {
try (SqlSession session = sqlSessionFactory.openSession(true)) {
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
NameRecord record = new NameRecord();
record.setId(1);
record.setName("Barney");
mapper.updateByPrimaryKey(record);
}
}

private void updateRecordWithoutAutoCommitAndNoExplicitCommit() {
// this should rollback
try (SqlSession session = sqlSessionFactory.openSession()) {
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
NameRecord record = new NameRecord();
record.setId(1);
record.setName("Barney");
mapper.updateByPrimaryKey(record);
}
}

private void updateRecordWithoutAutoCommitAndExplicitCommit() {
try (SqlSession session = sqlSessionFactory.openSession()) {
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
NameRecord record = new NameRecord();
record.setId(1);
record.setName("Barney");
mapper.updateByPrimaryKey(record);
session.commit();
}
}

private Optional<NameRecord> getRecord() {
try (SqlSession session = sqlSessionFactory.openSession()) {
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
return mapper.selectByPrimaryKey(1);
}
}
}
39 changes: 39 additions & 0 deletions src/test/java/issues/gh324/NameRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2016-2021 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 issues.gh324;

import java.io.Serializable;

public class NameRecord implements Serializable {
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;
}
}
35 changes: 35 additions & 0 deletions src/test/java/issues/gh324/NameTableDynamicSqlSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016-2021 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 issues.gh324;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;

import java.sql.JDBCType;

public class NameTableDynamicSqlSupport {
public static final NameTable nameTable = new NameTable();
public static final SqlColumn<Integer> id = nameTable.id;
public static final SqlColumn<String> name = nameTable.name;

public static final class NameTable extends SqlTable {
public NameTable() {
super("NameTable");
}
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
public final SqlColumn<String> name = column("name", JDBCType.VARCHAR);
}
}
74 changes: 74 additions & 0 deletions src/test/java/issues/gh324/NameTableMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2016-2021 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 issues.gh324;

import static issues.gh324.NameTableDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

import org.apache.ibatis.annotations.*;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.*;

import java.util.List;
import java.util.Optional;

@CacheNamespace
public interface NameTableMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<NameRecord>, CommonUpdateMapper {
@SelectProvider(type=SqlProviderAdapter.class, method="select")
@Results(id="NameTableResult", value={
@Result(column="id", property="id", id=true),
@Result(column="name", property="name")
})
List<NameRecord> selectMany(SelectStatementProvider selectStatement);

@SelectProvider(type=SqlProviderAdapter.class, method="select")
@ResultMap("NameTableResult")
Optional<NameRecord> selectOne(SelectStatementProvider selectStatement);

BasicColumn[] selectList = BasicColumn.columnList(id, name);

default Optional<NameRecord> selectOne(SelectDSLCompleter completer) {
return MyBatis3Utils.selectOne(this::selectOne, selectList, nameTable, completer);
}

default Optional<NameRecord> selectByPrimaryKey(Integer id_) {
return selectOne(c ->
c.where(id, isEqualTo(id_))
);
}

default int insert(NameRecord record) {
return MyBatis3Utils.insert(this::insert, record, nameTable, c ->
c.map(id).toProperty("id")
.map(name).toProperty("name")
);
}

default int update(UpdateDSLCompleter completer) {
return MyBatis3Utils.update(this::update, nameTable, completer);
}

default int updateByPrimaryKey(NameRecord record) {
return update(c ->
c.set(name).equalTo(record::getName)
.where(id, isEqualTo(record::getId))
);
}
}
Loading