
com.jpattern.orm.BaseTestShared Maven / Gradle / Ivy
package com.jpattern.orm;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.math.BigDecimal;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import com.jpattern.logger.ILogger;
import com.jpattern.orm.connection.ConnectionWrapper;
import com.jpattern.orm.logger.OrmLogger;
import com.jpattern.orm.test.IJpOrmTestsConstants;
import com.jpattern.orm.test.SpringSkipTestRunner;
import com.jpattern.shared.util.Chronometer;
/**
*
* @author Francesco Cina
*
* 20/mag/2011
*/
@RunWith(SpringSkipTestRunner.class)
@ContextConfiguration(locations = { "classpath:"
+ IJpOrmTestsConstants.SPRING_XML_FILE })
public abstract class BaseTestShared {
@Rule
public final TestName name = new TestName();
private final Chronometer chronometer = new Chronometer();
private ILogger logger;
@Resource
private JPOrmTestData jpOrmTestData;
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUpBeforeTest() throws Exception {
if (!this.jpOrmTestData.isDbAvailable()) {
Assume.assumeTrue(true);
}
this.chronometer.restart();
this.logger = OrmLogger.getOrmLogger(getClass());
this.logger.info("setUpBeforeTest",
"===================================================================");
this.logger.info("setUpBeforeTest", "BEGIN TEST " + this.name.getMethodName());
this.logger.info("setUpBeforeTest", "OPEN_CONNECTIONS: "
+ ConnectionWrapper.OPEN_CONNECTIONS);
this.logger.info("setUpBeforeTest", "CLOSED_CONNECTIONS: "
+ ConnectionWrapper.CLOSED_CONNECTIONS);
this.logger.info("setUpBeforeTest", "TOTAL_CONNECTIONS: "
+ ConnectionWrapper.TOTAL_CONNECTIONS);
this.logger.info("setUpBeforeTest", "COMMITTED_CONNECTIONS: "
+ ConnectionWrapper.COMMITTED_CONNECTIONS);
this.logger.info("setUpBeforeTest", "ROLLBACKED_CONNECTIONS: "
+ ConnectionWrapper.ROLLBACKED_CONNECTIONS);
this.logger.info("setUpBeforeTest",
"===================================================================");
setUp();
}
@After
public void tearDownAfterTest() throws Exception {
tearDown();
this.chronometer.pause();
final String time = new BigDecimal(this.chronometer.read()).divide(
new BigDecimal(1000)).toString();
this.logger.info("tearDownAfterTest",
"===================================================================");
this.logger.info("tearDownAfterTest", "END TEST " + this.name.getMethodName());
this.logger.info("tearDownAfterTest", "OPEN_CONNECTIONS: "
+ ConnectionWrapper.OPEN_CONNECTIONS);
this.logger.info("tearDownAfterTest", "CLOSED_CONNECTIONS: "
+ ConnectionWrapper.CLOSED_CONNECTIONS);
this.logger.info("tearDownAfterTest", "TOTAL_CONNECTIONS: "
+ ConnectionWrapper.TOTAL_CONNECTIONS);
this.logger.info("tearDownAfterTest", "COMMITTED_CONNECTIONS: "
+ ConnectionWrapper.COMMITTED_CONNECTIONS);
this.logger.info("tearDownAfterTest", "ROLLBACKED_CONNECTIONS: "
+ ConnectionWrapper.ROLLBACKED_CONNECTIONS);
this.logger.info("tearDownAfterTest", "Execution time: " + time + " seconds");
this.logger.info("tearDownAfterTest",
"===================================================================");
assertEquals(ConnectionWrapper.TOTAL_CONNECTIONS,
ConnectionWrapper.CLOSED_CONNECTIONS);
}
protected abstract void setUp() throws Exception;
protected abstract void tearDown() throws Exception;
protected String getTestInputBasePath() throws Exception {
return IJpOrmTestsConstants.TEST_FILE_INPUT_BASE_PATH;
}
protected String getTestOutputBasePath() throws Exception {
mkDir(IJpOrmTestsConstants.TEST_FILE_OUTPUT_BASE_PATH);
return IJpOrmTestsConstants.TEST_FILE_OUTPUT_BASE_PATH;
}
protected void mkDir(final String dirPath) throws Exception {
final File path = new File(dirPath);
if (!path.exists()) {
path.mkdirs();
}
}
protected JPOrm getJPOrm() {
return new JPOrm(this.jpOrmTestData.getSessionProvider());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy