Skip to content

Commit 30f00a3

Browse files
BAEL-65 - moving from sout to logger
1 parent 5733676 commit 30f00a3

1 file changed

Lines changed: 98 additions & 100 deletions

File tree

‎spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooStoredProceduresIntegrationTest.java‎

Lines changed: 98 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.junit.Before;
1515
import org.junit.Test;
1616
import org.junit.runner.RunWith;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1719
import org.springframework.beans.factory.annotation.Autowired;
1820
import org.springframework.test.context.ContextConfiguration;
1921
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -23,106 +25,102 @@
2325
import com.baeldung.spring.PersistenceConfig;
2426

2527
@RunWith(SpringJUnit4ClassRunner.class)
26-
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
28+
@ContextConfiguration(classes = {PersistenceConfig.class}, loader = AnnotationConfigContextLoader.class)
2729
public class FooStoredProceduresIntegrationTest {
2830

29-
@Autowired
30-
private SessionFactory sessionFactory;
31-
32-
@Autowired
33-
private IFooService fooService;
34-
35-
private Session session;
36-
37-
@Before
38-
public final void before() {
39-
session = sessionFactory.openSession();
40-
Assume.assumeTrue(getAllFoosExists());
41-
Assume.assumeTrue(getFoosByNameExists());
42-
}
43-
44-
private boolean getFoosByNameExists() {
45-
try {
46-
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
47-
.addEntity(Foo.class);
48-
sqlQuery.list();
49-
return true;
50-
} catch (SQLGrammarException e) {
51-
System.out
52-
.println("WARNING : GetFoosByName() Procedure is may be missing ");
53-
return false;
54-
}
55-
}
56-
57-
private boolean getAllFoosExists() {
58-
try {
59-
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
60-
.addEntity(Foo.class);
61-
sqlQuery.list();
62-
return true;
63-
} catch (SQLGrammarException e) {
64-
System.out
65-
.println("WARNING : GetAllFoos() Procedure is may be missing ");
66-
return false;
67-
}
68-
}
69-
70-
@After
71-
public final void after() {
72-
session.close();
73-
}
74-
75-
@Test
76-
public final void getAllFoosUsingStoredProcedures() {
77-
78-
fooService.create(new Foo(randomAlphabetic(6)));
79-
80-
// Stored procedure getAllFoos using createSQLQuery
81-
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
82-
Foo.class);
83-
@SuppressWarnings("unchecked")
84-
List<Foo> allFoos = sqlQuery.list();
85-
for (Foo foo : allFoos) {
86-
System.out.println("getAllFoos() SQL Query result : "
87-
+ foo.getName());
88-
}
89-
assertEquals(allFoos.size(), fooService.findAll().size());
90-
91-
// Stored procedure getAllFoos using a Named Query
92-
Query namedQuery = session.getNamedQuery("callGetAllFoos");
93-
@SuppressWarnings("unchecked")
94-
List<Foo> allFoos2 = namedQuery.list();
95-
for (Foo foo : allFoos2) {
96-
System.out.println("getAllFoos() NamedQuery result : "
97-
+ foo.getName());
98-
}
99-
assertEquals(allFoos2.size(), fooService.findAll().size());
100-
}
101-
102-
@Test
103-
public final void getFoosByNameUsingStoredProcedures() {
104-
105-
fooService.create(new Foo("NewFooName"));
106-
107-
// Stored procedure getFoosByName using createSQLQuery()
108-
Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
109-
.addEntity(Foo.class).setParameter("fooName", "NewFooName");
110-
@SuppressWarnings("unchecked")
111-
List<Foo> allFoosByName = sqlQuery.list();
112-
for (Foo foo : allFoosByName) {
113-
System.out.println("getFoosByName() using SQL Query : found => "
114-
+ foo.toString());
115-
}
116-
117-
// Stored procedure getFoosByName using getNamedQuery()
118-
Query namedQuery = session.getNamedQuery("callGetFoosByName")
119-
.setParameter("fooName", "NewFooName");
120-
@SuppressWarnings("unchecked")
121-
List<Foo> allFoosByName2 = namedQuery.list();
122-
for (Foo foo : allFoosByName2) {
123-
System.out.println("getFoosByName() using Native Query : found => "
124-
+ foo.toString());
125-
}
126-
127-
}
31+
private static final Logger LOGGER = LoggerFactory.getLogger(FooStoredProceduresIntegrationTest.class);
32+
33+
@Autowired
34+
private SessionFactory sessionFactory;
35+
36+
@Autowired
37+
private IFooService fooService;
38+
39+
private Session session;
40+
41+
@Before
42+
public final void before() {
43+
session = sessionFactory.openSession();
44+
Assume.assumeTrue(getAllFoosExists());
45+
Assume.assumeTrue(getFoosByNameExists());
46+
}
47+
48+
private boolean getFoosByNameExists() {
49+
try {
50+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
51+
.addEntity(Foo.class);
52+
sqlQuery.list();
53+
return true;
54+
} catch (SQLGrammarException e) {
55+
LOGGER.error("WARNING : GetFoosByName() Procedure is may be missing ", e);
56+
return false;
57+
}
58+
}
59+
60+
private boolean getAllFoosExists() {
61+
try {
62+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
63+
.addEntity(Foo.class);
64+
sqlQuery.list();
65+
return true;
66+
} catch (SQLGrammarException e) {
67+
LOGGER.error("WARNING : GetAllFoos() Procedure is may be missing ", e);
68+
return false;
69+
}
70+
}
71+
72+
@After
73+
public final void after() {
74+
session.close();
75+
}
76+
77+
@Test
78+
public final void getAllFoosUsingStoredProcedures() {
79+
80+
fooService.create(new Foo(randomAlphabetic(6)));
81+
82+
// Stored procedure getAllFoos using createSQLQuery
83+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
84+
Foo.class);
85+
@SuppressWarnings("unchecked")
86+
List<Foo> allFoos = sqlQuery.list();
87+
for (Foo foo : allFoos) {
88+
LOGGER.info("getAllFoos() SQL Query result : {}", foo.getName());
89+
}
90+
assertEquals(allFoos.size(), fooService.findAll().size());
91+
92+
// Stored procedure getAllFoos using a Named Query
93+
Query namedQuery = session.getNamedQuery("callGetAllFoos");
94+
@SuppressWarnings("unchecked")
95+
List<Foo> allFoos2 = namedQuery.list();
96+
for (Foo foo : allFoos2) {
97+
LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName());
98+
}
99+
assertEquals(allFoos2.size(), fooService.findAll().size());
100+
}
101+
102+
@Test
103+
public final void getFoosByNameUsingStoredProcedures() {
104+
105+
fooService.create(new Foo("NewFooName"));
106+
107+
// Stored procedure getFoosByName using createSQLQuery()
108+
Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
109+
.addEntity(Foo.class).setParameter("fooName", "NewFooName");
110+
@SuppressWarnings("unchecked")
111+
List<Foo> allFoosByName = sqlQuery.list();
112+
for (Foo foo : allFoosByName) {
113+
LOGGER.info("getFoosByName() using SQL Query : found => {}", foo.toString());
114+
}
115+
116+
// Stored procedure getFoosByName using getNamedQuery()
117+
Query namedQuery = session.getNamedQuery("callGetFoosByName")
118+
.setParameter("fooName", "NewFooName");
119+
@SuppressWarnings("unchecked")
120+
List<Foo> allFoosByName2 = namedQuery.list();
121+
for (Foo foo : allFoosByName2) {
122+
LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString());
123+
}
124+
125+
}
128126
}

0 commit comments

Comments
 (0)