JAVA-8435: reducing logging for tutorials-integration job

This commit is contained in:
chaos2418
2021-11-20 09:45:50 +05:30
parent a0dbe854b7
commit fa1f32f186
53 changed files with 300 additions and 122 deletions

View File

@@ -123,24 +123,39 @@ public class EnversFooBarAuditIntegrationTest {
assertNotNull(barRevisionList);
assertEquals(4, barRevisionList.size());
assertEquals("BAR", barRevisionList.get(0).getName());
assertEquals("BAR", barRevisionList.get(1).getName());
assertEquals("BAR1", barRevisionList.get(2).getName());
assertEquals("BAR1", barRevisionList.get(3).getName());
assertEquals("BAR", barRevisionList.get(0)
.getName());
assertEquals("BAR", barRevisionList.get(1)
.getName());
assertEquals("BAR1", barRevisionList.get(2)
.getName());
assertEquals("BAR1", barRevisionList.get(3)
.getName());
assertEquals(1, barRevisionList.get(0).getFooSet().size());
assertEquals(2, barRevisionList.get(1).getFooSet().size());
assertEquals(2, barRevisionList.get(2).getFooSet().size());
assertEquals(3, barRevisionList.get(3).getFooSet().size());
assertEquals(1, barRevisionList.get(0)
.getFooSet()
.size());
assertEquals(2, barRevisionList.get(1)
.getFooSet()
.size());
assertEquals(2, barRevisionList.get(2)
.getFooSet()
.size());
assertEquals(3, barRevisionList.get(3)
.getFooSet()
.size());
// test Foo revisions
fooRevisionList = fooService.getRevisions();
assertNotNull(fooRevisionList);
assertEquals(3, fooRevisionList.size());
assertEquals("FOO1", fooRevisionList.get(0).getName());
assertEquals("FOO2", fooRevisionList.get(1).getName());
assertEquals("FOO3", fooRevisionList.get(2).getName());
assertEquals("FOO1", fooRevisionList.get(0)
.getName());
assertEquals("FOO2", fooRevisionList.get(1)
.getName());
assertEquals("FOO3", fooRevisionList.get(2)
.getName());
}
}

View File

@@ -10,8 +10,13 @@ import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FooFixtures {
private static final Logger LOGGER = LoggerFactory.getLogger(FooFixtures.class);
private SessionFactory sessionFactory;
public FooFixtures(final SessionFactory sessionFactory) {
@@ -36,26 +41,30 @@ public class FooFixtures {
foo.setBar(bar);
session.save(foo);
final Foo foo2 = new Foo(null);
if (i % 2 == 0)
if (i % 2 == 0) {
foo2.setName("LuckyFoo" + (i + 120));
}
foo2.setBar(bar);
session.save(foo2);
bar.getFooSet().add(foo);
bar.getFooSet().add(foo2);
bar.getFooSet()
.add(foo);
bar.getFooSet()
.add(foo2);
session.merge(bar);
}
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
if (tx != null) {
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
}
LOGGER.error("Not able to open session", he);
} catch (final Exception e) {
e.printStackTrace();
LOGGER.error(e.getLocalizedMessage(), e);
} finally {
if (session != null)
if (session != null) {
session.close();
}
}
}
@@ -71,7 +80,8 @@ public class FooFixtures {
final Foo foo = new Foo();
foo.setName("Foo_" + (i + 120));
final Bar bar = new Bar("bar_" + i);
bar.getFooSet().add(foo);
bar.getFooSet()
.add(foo);
foo.setBar(bar);
fooList.add(foo);
@@ -86,15 +96,16 @@ public class FooFixtures {
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
if (tx != null) {
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
}
LOGGER.error("Not able to open session", he);
} catch (final Exception e) {
e.printStackTrace();
LOGGER.error(e.getLocalizedMessage(), e);
} finally {
if (session != null)
if (session != null) {
session.close();
}
}
}

View File

@@ -15,6 +15,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -29,6 +31,8 @@ import com.baeldung.spring.config.PersistenceTestConfig;
@SuppressWarnings("unchecked")
public class FooSortingPersistenceIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(FooSortingPersistenceIntegrationTest.class);
@Autowired
private SessionFactory sessionFactory;
@@ -46,7 +50,8 @@ public class FooSortingPersistenceIntegrationTest {
@After
public void after() {
session.getTransaction().commit();
session.getTransaction()
.commit();
session.close();
}
@@ -56,7 +61,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {} , Id: {}", foo.getName(), foo.getId());
}
}
@@ -66,9 +71,10 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
assertNull(fooList.get(fooList.toArray().length - 1).getName());
assertNull(fooList.get(fooList.toArray().length - 1)
.getName());
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -77,9 +83,10 @@ public class FooSortingPersistenceIntegrationTest {
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
assertNull(fooList.get(0).getName());
assertNull(fooList.get(0)
.getName());
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName());
LOGGER.debug("Name: {}", foo.getName());
}
}
@@ -90,7 +97,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -100,7 +107,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -110,7 +117,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -120,7 +127,7 @@ public class FooSortingPersistenceIntegrationTest {
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -131,29 +138,33 @@ public class FooSortingPersistenceIntegrationTest {
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@Test
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
final Criteria criteria = session.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
criteria.addOrder(Order.asc("name")
.nulls(NullPrecedence.LAST));
final List<Foo> fooList = criteria.list();
assertNull(fooList.get(fooList.toArray().length - 1).getName());
assertNull(fooList.get(fooList.toArray().length - 1)
.getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@Test
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
final Criteria criteria = session.createCriteria(Foo.class, "FOO");
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
criteria.addOrder(Order.desc("name")
.nulls(NullPrecedence.FIRST));
final List<Foo> fooList = criteria.list();
assertNull(fooList.get(0).getName());
assertNull(fooList.get(0)
.getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -164,9 +175,9 @@ public class FooSortingPersistenceIntegrationTest {
final List<Bar> barList = query.list();
for (final Bar bar : barList) {
final Set<Foo> fooSet = bar.getFooSet();
System.out.println("Bar Id:" + bar.getId());
LOGGER.debug("Bar Id:{}", bar.getId());
for (final Foo foo : fooSet) {
System.out.println("FooName:" + foo.getName());
LOGGER.debug("FooName:{}", foo.getName());
}
}
}

View File

@@ -1,7 +1,10 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.hibernate.FooSortingPersistenceIntegrationTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
@@ -16,6 +19,8 @@ import com.baeldung.spring.config.PersistenceTestConfig;
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ParentServicePersistenceIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ParentServicePersistenceIntegrationTest.class);
@Autowired
private IParentService service;
@@ -37,11 +42,11 @@ public class ParentServicePersistenceIntegrationTest {
final Parent parentEntity = new Parent(childEntity);
service.create(parentEntity);
System.out.println("Child = " + childService.findOne(childEntity.getId()));
System.out.println("Child - parent = " + childService.findOne(childEntity.getId()).getParent());
LOGGER.debug("Child = {}", childService.findOne(childEntity.getId()));
LOGGER.debug("Child - parent = {}", childService.findOne(childEntity.getId()).getParent());
System.out.println("Parent = " + service.findOne(parentEntity.getId()));
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
LOGGER.debug("Parent = {}", service.findOne(parentEntity.getId()));
LOGGER.debug("Parent - child = {}", service.findOne(parentEntity.getId()).getChild());
}
@Test(expected = DataIntegrityViolationException.class)

View File

@@ -166,7 +166,7 @@ public class PersistenceTestConfig {
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", "true");
hibernateProperties.setProperty("hibernate.show_sql", "false");
// hibernateProperties.setProperty("hibernate.format_sql", "true");
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");

View File

@@ -19,7 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@RunWith(SpringRunner.class)
@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:insert_users.sql")
@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:insert_users.sql", showSql = false)
public class UserRepositoryIntegrationTest {
@Autowired
@@ -40,46 +40,42 @@ public class UserRepositoryIntegrationTest {
@Test
public void whenFindAllSortedByNameThenAllSorted() {
List<User> allUsersSortedByName = userRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
assertThat(allUsersSortedByName)
.extracting("name")
assertThat(allUsersSortedByName).extracting("name")
.containsSequence("Bob", "Cindy", "John");
}
@Test
public void whenFindAllSortedByNameLengthThenException() {
assertThatThrownBy(() -> userRepository.findAll(Sort.by("LENGTH(name)")))
.isInstanceOf(PropertyReferenceException.class);
assertThatThrownBy(() -> userRepository.findAll(Sort.by("LENGTH(name)"))).isInstanceOf(PropertyReferenceException.class);
}
@Test
public void whenFindAllUsersSortedByNameThenAllSorted() {
List<User> allUsersSortedByName = userRepository.findAllUsers(Sort.by(Sort.Direction.ASC, "name"));
assertThat(allUsersSortedByName)
.extracting("name")
assertThat(allUsersSortedByName).extracting("name")
.containsSequence("Bob", "Cindy", "John");
}
@Test
public void whenFindAllUsersSortedByNameLengthThenAllSorted() {
List<User> allUsersSortedByName = userRepository.findAllUsers(JpaSort.unsafe("LENGTH(name)"));
assertThat(allUsersSortedByName)
.extracting("name")
assertThat(allUsersSortedByName).extracting("name")
.containsSequence("Bob", "John", "Cindy");
}
@Test
public void whenFindAllUsersWithPaginationThenPaginated() {
Page<User> page = userRepository.findAllUsersWithPagination(PageRequest.of(0, 1));
assertThat(page.stream().map(User::getId))
.hasSize(1)
assertThat(page.stream()
.map(User::getId)).hasSize(1)
.containsOnly(1);
}
@Test
public void whenFindAllUsersWithPaginationNativeThenPaginated() {
Page<User> page = userRepository.findAllUsersWithPaginationNative(PageRequest.of(1, 1));
assertThat(page.stream().map(User::getId))
.hasSize(1)
assertThat(page.stream()
.map(User::getId)).hasSize(1)
.containsOnly(2);
}
@@ -126,8 +122,7 @@ public class UserRepositoryIntegrationTest {
@Test
public void whenFindUserByNameListThenAllFound() {
List<User> users = userRepository.findUserByNameList(Arrays.asList("Bob", "Cindy"));
assertThat(users)
.extracting("name")
assertThat(users).extracting("name")
.containsOnly("Bob", "Cindy");
}

View File

@@ -17,7 +17,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql")
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql", showSql = false)
public class ArticleRepositoryIntegrationTest {
@Autowired

View File

@@ -11,7 +11,7 @@
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserEager" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />

View File

@@ -11,7 +11,7 @@
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserLazy" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />