BAEL-65 - moving from sout to logger

This commit is contained in:
Slavisa Baeldung 2016-07-07 15:42:13 +02:00
parent 5733676887
commit 30f00a34af

View File

@ -14,6 +14,8 @@ import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -23,106 +25,102 @@ import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig; import com.baeldung.spring.PersistenceConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = {PersistenceConfig.class}, loader = AnnotationConfigContextLoader.class)
public class FooStoredProceduresIntegrationTest { public class FooStoredProceduresIntegrationTest {
@Autowired private static final Logger LOGGER = LoggerFactory.getLogger(FooStoredProceduresIntegrationTest.class);
private SessionFactory sessionFactory;
@Autowired @Autowired
private IFooService fooService; private SessionFactory sessionFactory;
private Session session; @Autowired
private IFooService fooService;
@Before private Session session;
public final void before() {
session = sessionFactory.openSession();
Assume.assumeTrue(getAllFoosExists());
Assume.assumeTrue(getFoosByNameExists());
}
private boolean getFoosByNameExists() { @Before
try { public final void before() {
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()") session = sessionFactory.openSession();
.addEntity(Foo.class); Assume.assumeTrue(getAllFoosExists());
sqlQuery.list(); Assume.assumeTrue(getFoosByNameExists());
return true; }
} catch (SQLGrammarException e) {
System.out
.println("WARNING : GetFoosByName() Procedure is may be missing ");
return false;
}
}
private boolean getAllFoosExists() { private boolean getFoosByNameExists() {
try { try {
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()") Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
.addEntity(Foo.class); .addEntity(Foo.class);
sqlQuery.list(); sqlQuery.list();
return true; return true;
} catch (SQLGrammarException e) { } catch (SQLGrammarException e) {
System.out LOGGER.error("WARNING : GetFoosByName() Procedure is may be missing ", e);
.println("WARNING : GetAllFoos() Procedure is may be missing "); return false;
return false; }
} }
}
@After private boolean getAllFoosExists() {
public final void after() { try {
session.close(); Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
} .addEntity(Foo.class);
sqlQuery.list();
return true;
} catch (SQLGrammarException e) {
LOGGER.error("WARNING : GetAllFoos() Procedure is may be missing ", e);
return false;
}
}
@Test @After
public final void getAllFoosUsingStoredProcedures() { public final void after() {
session.close();
}
fooService.create(new Foo(randomAlphabetic(6))); @Test
public final void getAllFoosUsingStoredProcedures() {
// Stored procedure getAllFoos using createSQLQuery fooService.create(new Foo(randomAlphabetic(6)));
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
Foo.class);
@SuppressWarnings("unchecked")
List<Foo> allFoos = sqlQuery.list();
for (Foo foo : allFoos) {
System.out.println("getAllFoos() SQL Query result : "
+ foo.getName());
}
assertEquals(allFoos.size(), fooService.findAll().size());
// Stored procedure getAllFoos using a Named Query // Stored procedure getAllFoos using createSQLQuery
Query namedQuery = session.getNamedQuery("callGetAllFoos"); Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
@SuppressWarnings("unchecked") Foo.class);
List<Foo> allFoos2 = namedQuery.list(); @SuppressWarnings("unchecked")
for (Foo foo : allFoos2) { List<Foo> allFoos = sqlQuery.list();
System.out.println("getAllFoos() NamedQuery result : " for (Foo foo : allFoos) {
+ foo.getName()); LOGGER.info("getAllFoos() SQL Query result : {}", foo.getName());
} }
assertEquals(allFoos2.size(), fooService.findAll().size()); assertEquals(allFoos.size(), fooService.findAll().size());
}
@Test // Stored procedure getAllFoos using a Named Query
public final void getFoosByNameUsingStoredProcedures() { Query namedQuery = session.getNamedQuery("callGetAllFoos");
@SuppressWarnings("unchecked")
List<Foo> allFoos2 = namedQuery.list();
for (Foo foo : allFoos2) {
LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName());
}
assertEquals(allFoos2.size(), fooService.findAll().size());
}
fooService.create(new Foo("NewFooName")); @Test
public final void getFoosByNameUsingStoredProcedures() {
// Stored procedure getFoosByName using createSQLQuery() fooService.create(new Foo("NewFooName"));
Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
.addEntity(Foo.class).setParameter("fooName", "NewFooName");
@SuppressWarnings("unchecked")
List<Foo> allFoosByName = sqlQuery.list();
for (Foo foo : allFoosByName) {
System.out.println("getFoosByName() using SQL Query : found => "
+ foo.toString());
}
// Stored procedure getFoosByName using getNamedQuery() // Stored procedure getFoosByName using createSQLQuery()
Query namedQuery = session.getNamedQuery("callGetFoosByName") Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
.setParameter("fooName", "NewFooName"); .addEntity(Foo.class).setParameter("fooName", "NewFooName");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Foo> allFoosByName2 = namedQuery.list(); List<Foo> allFoosByName = sqlQuery.list();
for (Foo foo : allFoosByName2) { for (Foo foo : allFoosByName) {
System.out.println("getFoosByName() using Native Query : found => " LOGGER.info("getFoosByName() using SQL Query : found => {}", foo.toString());
+ foo.toString()); }
}
} // Stored procedure getFoosByName using getNamedQuery()
Query namedQuery = session.getNamedQuery("callGetFoosByName")
.setParameter("fooName", "NewFooName");
@SuppressWarnings("unchecked")
List<Foo> allFoosByName2 = namedQuery.list();
for (Foo foo : allFoosByName2) {
LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString());
}
}
} }