BAEL-788 Added the review comment changes (#1672)
* BAEL-788 Added java config * BAEL-788 Removed XML config * added .gitignore * amoved unused imports * renamed test
This commit is contained in:
parent
33fef603b3
commit
dc561548b3
|
@ -0,0 +1,16 @@
|
|||
*.class
|
||||
|
||||
.settings
|
||||
.project
|
||||
*.log
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
/testdb1
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -3,7 +3,6 @@ package com.baeldung.mybatis.utils;
|
|||
import com.baeldung.mybatis.mapper.AddressMapper;
|
||||
import com.baeldung.mybatis.mapper.PersonMapper;
|
||||
import org.apache.ibatis.datasource.pooled.PooledDataSource;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.jdbc.SQL;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
@ -12,8 +11,6 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
|||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class MyBatisUtil {
|
||||
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
|
||||
|
|
|
@ -1,145 +0,0 @@
|
|||
package com.baeldung.mybatis.mapper;
|
||||
|
||||
import com.baeldung.mybatis.model.Address;
|
||||
import com.baeldung.mybatis.model.Person;
|
||||
import com.baeldung.mybatis.utils.MyBatisUtil;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class PersonMapperTest {
|
||||
|
||||
SqlSession session;
|
||||
|
||||
@Before
|
||||
public void setup() throws SQLException {
|
||||
|
||||
session = MyBatisUtil.buildqlSessionFactory().openSession();
|
||||
createTables(session);
|
||||
|
||||
}
|
||||
|
||||
private void createTables(SqlSession session) throws SQLException {
|
||||
|
||||
String createPersonTable = "create table person (" + "personId integer not null generated always as"
|
||||
+ " identity (start with 1, increment by 1), " + "name varchar(30) not null, "
|
||||
+ "constraint primary_key_person primary key (personId))";
|
||||
|
||||
String createAddressTable = "create table address (" + "addressId integer not null generated always as"
|
||||
+ " identity (start with 1, increment by 1), " + "streetAddress varchar(300), personId integer, "
|
||||
+ "constraint primary_key_address primary key (addressId))";
|
||||
|
||||
String alterTable = "ALTER TABLE "
|
||||
+ " address ADD CONSTRAINT fk_person FOREIGN KEY (personId) REFERENCES person (personId)";
|
||||
|
||||
session.getConnection().createStatement().execute(createPersonTable);
|
||||
session.getConnection().createStatement().execute(createAddressTable);
|
||||
session.getConnection().createStatement().execute(alterTable);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonAdressSaved_ThenPersonAddressCanBeQueried() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
addressMapper.saveAddress(address);
|
||||
|
||||
Person returnedPerson = personMapper.getPersonById(id);
|
||||
assertEquals("Baljeet S", returnedPerson.getName());
|
||||
assertEquals("Pune", returnedPerson.getAddresses().get(0).getStreetAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonSaved_ThenPersonCanBeQueried() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
addressMapper.saveAddress(address);
|
||||
|
||||
Person returnedPerson = personMapper.getPerson(id);
|
||||
assertEquals("Baljeet S", returnedPerson.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonUpdated_ThenPersonIsChanged() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
addressMapper.saveAddress(address);
|
||||
|
||||
personMapper.updatePerson(new Person(id, "Baljeet1"));
|
||||
Person returnedPerson = personMapper.getPerson(id);
|
||||
assertEquals("Baljeet1", returnedPerson.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersoSaved_ThenMapIsReturned() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
addressMapper.saveAddress(address);
|
||||
|
||||
Map<Integer, Person> returnedPerson = personMapper.getAllPerson();
|
||||
assertEquals(1, returnedPerson.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonSearched_ThenResultIsReturned() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
addressMapper.saveAddress(address);
|
||||
|
||||
Person returnedPerson = personMapper.getPersonByName("Baljeet S");
|
||||
assertEquals("Baljeet S", returnedPerson.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddressSearched_ThenResultIsReturned() {
|
||||
Person person = new Person("Baljeet S");
|
||||
Address address = new Address("Pune");
|
||||
PersonMapper personMapper = session.getMapper(PersonMapper.class);
|
||||
Integer id = personMapper.save(person);
|
||||
address.setPersonId(id);
|
||||
AddressMapper addressMapper = session.getMapper(AddressMapper.class);
|
||||
Integer addressId = addressMapper.saveAddress(address);
|
||||
Address returnedAddress = addressMapper.getAddresses(addressId);
|
||||
|
||||
assertEquals("Pune", returnedAddress.getStreetAddress());
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() throws SQLException {
|
||||
session.getConnection().createStatement().execute("drop table address");
|
||||
session.getConnection().createStatement().execute("drop table person");
|
||||
|
||||
session.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue