[BAEL-4635] Fixed tests and formatted per feedback
This commit is contained in:
parent
400cbb845d
commit
3686fa1cfb
|
@ -13,27 +13,24 @@ public class HibernateConfig {
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
public static SessionFactory getSessionFactory() {
|
||||||
if (sessionFactory == null) {
|
if (sessionFactory == null) {
|
||||||
try {
|
Configuration configuration = new Configuration();
|
||||||
Configuration configuration = new Configuration();
|
|
||||||
|
|
||||||
Properties settings = new Properties();
|
Properties settings = new Properties();
|
||||||
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
||||||
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
||||||
settings.put(Environment.USER, "root");
|
settings.put(Environment.USER, "root");
|
||||||
settings.put(Environment.PASS, "password");
|
settings.put(Environment.PASS, "password");
|
||||||
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
||||||
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||||
configuration.setProperties(settings);
|
configuration.setProperties(settings);
|
||||||
|
|
||||||
configuration.addAnnotatedClass(User.class);
|
configuration.addAnnotatedClass(User.class);
|
||||||
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||||
.build();
|
.applySettings(configuration.getProperties())
|
||||||
|
.build();
|
||||||
|
|
||||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,10 +62,10 @@ public class User implements Serializable {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new StringJoiner(", ", User.class.getSimpleName() + "[", "]").add("id=" + id)
|
return new StringJoiner(", ", User.class.getSimpleName() + "[", "]").add("id=" + id)
|
||||||
.add("email='" + email + "'")
|
.add("email='" + email + "'")
|
||||||
.add("password='" + password + "'")
|
.add("password='" + password + "'")
|
||||||
.add("currentDevice='" + currentDevice + "'")
|
.add("currentDevice='" + currentDevice + "'")
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -9,13 +9,11 @@ public class UserDao {
|
||||||
|
|
||||||
public void saveUser(User user) {
|
public void saveUser(User user) {
|
||||||
Transaction transaction = null;
|
Transaction transaction = null;
|
||||||
try (Session session = HibernateConfig.getSessionFactory()
|
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
|
||||||
.openSession()) {
|
|
||||||
transaction = session.beginTransaction();
|
transaction = session.beginTransaction();
|
||||||
session.save(user);
|
session.save(user);
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
if (transaction != null) {
|
if (transaction != null) {
|
||||||
transaction.rollback();
|
transaction.rollback();
|
||||||
}
|
}
|
||||||
|
@ -23,10 +21,8 @@ public class UserDao {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<User> getUsers() {
|
public List<User> getUsers() {
|
||||||
try (Session session = HibernateConfig.getSessionFactory()
|
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
|
||||||
.openSession()) {
|
return session.createQuery("from User", User.class).list();
|
||||||
return session.createQuery("from User", User.class)
|
|
||||||
.list();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
||||||
|
|
||||||
public class TransientFieldTest {
|
public class TransientFieldUnitTest {
|
||||||
|
|
||||||
private final UserDao userDao = new UserDao();
|
private final UserDao userDao = new UserDao();
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ public class TransientFieldTest {
|
||||||
private final User user = new User("user" + randInt + "@bar.com", "hunter2", "MacOSX");
|
private final User user = new User("user" + randInt + "@bar.com", "hunter2", "MacOSX");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void JPA_UserWithTransientAnnotation_FieldNotPersisted() {
|
public void givenFieldWithTransientAnnotation_whenSavingViaJPA_thenFieldIgnored() {
|
||||||
userDao.saveUser(user);
|
userDao.saveUser(user);
|
||||||
List<User> allUsers = userDao.getUsers();
|
List<User> allUsers = userDao.getUsers();
|
||||||
User savedUser = allUsers.get(allUsers.indexOf(user));
|
User savedUser = allUsers.get(allUsers.indexOf(user));
|
||||||
|
@ -32,7 +32,7 @@ public class TransientFieldTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void JavaSerialization_UserWithTransientAnnotation_FieldSerialized() throws IOException, ClassNotFoundException {
|
public void givenFieldWithTransientAnnotation_whenSerializingObject_thenFieldSerialized() throws IOException, ClassNotFoundException {
|
||||||
|
|
||||||
FileOutputStream fout = new FileOutputStream("test.obj");
|
FileOutputStream fout = new FileOutputStream("test.obj");
|
||||||
ObjectOutputStream out = new ObjectOutputStream(fout);
|
ObjectOutputStream out = new ObjectOutputStream(fout);
|
||||||
|
@ -49,7 +49,7 @@ public class TransientFieldTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void JSONSerialization_UserWithTransientAnnotation_FieldSerialized() throws JsonProcessingException {
|
public void givenFieldWithTransientAnnotation_whenSerializingToJSON_thenFieldSerialized() throws JsonProcessingException {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
String json = objectMapper.writeValueAsString(user);
|
String json = objectMapper.writeValueAsString(user);
|
||||||
User savedUser = objectMapper.readValue(json, User.class);
|
User savedUser = objectMapper.readValue(json, User.class);
|
||||||
|
@ -58,7 +58,7 @@ public class TransientFieldTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void JSONSerialization_JacksonUsingHibernate5Module_FieldNotSerialized() throws JsonProcessingException {
|
public void givenJacksonHibernate5Module_whenSerializingTransientAnnotation_thenFieldIgnored() throws JsonProcessingException {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.registerModule(new Hibernate5Module());
|
objectMapper.registerModule(new Hibernate5Module());
|
||||||
String json = objectMapper.writeValueAsString(user);
|
String json = objectMapper.writeValueAsString(user);
|
||||||
|
@ -68,7 +68,7 @@ public class TransientFieldTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void JSONSerialization_MapperPropagatesTransientMarker_FieldSerialized() throws JsonProcessingException {
|
public void givenPropagateTransientFieldFlag_whenSerializingTransientAnnotation_thenFieldSerialized() throws JsonProcessingException {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
|
objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
|
||||||
String json = objectMapper.writeValueAsString(user);
|
String json = objectMapper.writeValueAsString(user);
|
Loading…
Reference in New Issue