HibernatePersistenceTests.java ( File view )

  • By 2016-05-17
  • View(s):0
  • Download(s):0
  • Point(s): 1
			/*
 * Copyright (c) 2005 Chris Richardson
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 


package net.chrisrichardson.ormunit.hibernate;

import java.io.Serializable;

import net.chrisrichardson.util.TxnCallback;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.hibernate.SessionFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;

/**
 * Base class for writing unit tests that create, find, update and delete
 * persistent objects using Hibernate
 * 
 * @author cer
 * 
 */

public abstract  class HibernatePersistenceTests<Q> extends HibernateORMTestCase {

	
	protected HibernateTemplate hibernateTemplate;
	protected Q txnThis;

	private HibernatePersistenceTestsStrategy transactionStrategy;

	public HibernatePersistenceTests() {

		setDependencyCheck(false);
	
}

	private final class LocalTransactionInterceptor implements MethodInterceptor {

		private final class MyTxnCallback implements TxnCallback {

			private final MethodInvocation mi;
			private Object returnValue;

			private MyTxnCallback(MethodInvocation mi) {

				this.mi = mi;
			
}

			public void execute() throws Throwable {

				returnValue = mi.proceed();
			
}

			public Object getReturnValue() {

				return returnValue;
			
}
		
}

		public Object invoke(final MethodInvocation mi) throws Throwable {

			MyTxnCallback callback = new MyTxnCallback(mi);
			doWithTransaction(callback);
			return callback.getReturnValue();
		
}
	
}


	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

		this.hibernateTemplate = hibernateTemplate;
	
}

	public void setSessionFactory(SessionFactory sessionFactory) {

		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	
}
	
	public void setTransactionStrategy(HibernatePersistenceTestsStrategy strategy) {

		this.transactionStrategy = strategy;
	
}

	protected void onSetUp() throws Exception {

		super.onSetUp();
		if (transactionStrategy == null) {

			transactionStrategy = makeDefaultTransactionStrategy();
		
}
		hibernateTemplate.setFlushMode(transactionStrategy.getFlushMode());
		transactionStrategy.onSetUp();
		txnThis = makeTxnThis();
	
}

	protected RollbackTransactionHibernatePersistenceTestsStrategy makeDefaultTransactionStrategy() {

		return new RollbackTransactionHibernatePersistenceTestsStrategy(hibernateTemplate, makeDefaultTransactionManager());
	
}

	protected HibernateTransactionManager makeDefaultTransactionManager() {

		return new HibernateTransactionManager(hibernateTemplate.getSessionFactory());
	
}

    protected Q makeTxnThis() {

		ProxyFactory pf = new ProxyFactory(this);
		pf.setProxyTargetClass(true);
		pf.addAdvice(new LocalTransactionInterceptor());
		Object proxy = pf.getProxy();
		return (Q) proxy;
	
}

	@Override
	protected void onTearDown() throws Exception {

		super.onTearDown();
		transactionStrategy.onTearDown();
	
}

	protected Serializable save(Object r) {

		return hibernateTemplate.save(r);
	
}

	protected <T> T load(Class<T> type, Serializable id) {

		return (T) hibernateTemplate.load(type, id);
	
}

	protected <T> T get(Class<T> type, Serializable id) {

		return (T)hibernateTemplate.get(type, id);
	
}

	protected void doWithTransaction(TxnCallback cb) {

		transactionStrategy.doWithTransaction(cb);
	
}

	protected HibernateTemplate getHibernateTemplate() {

		return hibernateTemplate;
	
}

	protected void delete(Class type) {

		HibernateTemplate t = getHibernateTemplate();
		t.deleteAll(t.find("from " + type.getName()));
	
}

	protected void delete(Object object) {

		getHibernateTemplate().delete(object);
	
}


}			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
pom.xml5.04 kB2007-04-27 13:43
dep.xml726.00 B2007-04-29 11:25
MANIFEST.MF39.00 B2007-03-25 15:17
ClassMappingException.java1.43 kB2007-03-25 15:17
AbstractORMappingTests.java1.54 kB2007-04-27 17:54
AccessStrategy.java789.00 B2007-04-27 17:54
ClassPropertyUtil.java2.01 kB2007-04-26 16:05
ComponentCollectionFieldMapping.java1.96 kB2007-04-27 13:43
ComponentPropertyMapping.java3.57 kB2007-04-27 13:43
CompositeListPropertyMapping.java1.67 kB2007-04-27 13:43
CompositeSetPropertyMapping.java1.05 kB2007-04-27 13:43
DatabaseInitializer.java134.00 B2007-03-25 15:17
DatabaseResetStrategy.java829.00 B2007-03-25 15:17
FieldMapping.java726.00 B2007-03-25 15:17
HibernateAssertUtil.java1.40 kB2007-04-27 13:43
HibernateMappingChecker.java1.34 kB2007-04-27 17:54
HibernateMappingTests.java5.27 kB2007-04-27 17:54
HibernateORMappingTests.java4.93 kB2007-04-27 17:54
HibernateORMTestCase.java1.80 kB2007-04-27 13:43
HibernatePersistenceTests.java4.28 kB2007-04-27 13:43
HibernatePersistenceTestsStrategy.java1.21 kB2007-04-27 13:43
HibernateSchemaChecker.java2.03 kB2007-04-27 13:43
HibernateSchemaTests.java1.36 kB2007-04-27 13:43
ListPropertyMapping.java2.37 kB2007-04-27 13:43
LocalSessionFactoryBeanUtil.java1.53 kB2007-03-25 15:17
MappedClassChecker.java13.11 kB2007-04-27 17:54
NoopDatabaseResetStrategy.java856.00 B2007-03-25 15:17
PathUtil.java1.49 kB2007-04-27 17:54
ResetDatabaseByRecreatingSchemaStrategy.java3.68 kB2007-03-25 15:17
RollbackTransactionHibernatePersistenceTestsStrategy.java2.42 kB2007-04-27 13:43
SimpleHibernatePersistenceTestsStrategy.java3.04 kB2007-04-27 13:43
ToOnePropertyMapping.java502.00 B2007-04-27 13:43
NonexistentPropertiesException.java1.25 kB2007-04-27 13:43
NonPersistentPropertyException.java1.20 kB2007-04-27 13:43
ORMUnitException.java927.00 B2007-03-25 15:17
ORMUnitMappingException.java962.00 B2007-03-25 15:17
PropertyNameSet.java1.33 kB2007-04-27 17:54
UnmappedPropertiesException.java1.31 kB2007-04-27 13:43
TxnCallback.java743.00 B2007-03-25 15:17
TxnCallbackWithResult.java755.00 B2007-03-25 15:17
Address.java1.47 kB2007-03-25 15:17
AddressWithProperties.java1.60 kB2007-04-26 13:33
BaseClass.java122.00 B2007-04-26 16:05
ClassWithUnmappedField.java785.00 B2007-03-25 15:17
ClassWithUnmappedProperty.java1,017.00 B2007-04-26 13:33
Customer.java1.21 kB2007-03-25 15:17
CustomerPersistenceTests.java1.80 kB2007-04-27 13:43
CustomerWithComponentAddress.java804.00 B2007-03-25 15:17
CustomerWithProperties.java574.00 B2007-04-26 13:33
CustomerWithPropertiesAndComponentAddress.java1.19 kB2007-04-26 13:33
ExampleMappingTests.java3.79 kB2007-04-27 13:43
ExampleWithPropertiesMappingTests.java4.25 kB2007-04-27 13:43
InheritanceTests.java1.94 kB2007-04-27 13:43
PointlessSchemaTests.java398.00 B2007-04-27 13:43
Subclass.java140.00 B2007-04-26 16:05
context.xml2.00 kB2007-03-25 15:17
contextWithProperties.xml2.02 kB2007-04-26 13:33
log4j.properties1.04 kB2007-03-25 15:17
example.hbm.xml1.41 kB2007-04-26 13:33
exampleWithProperties.hbm.xml1.52 kB2007-04-26 13:33
inheritanceOutsideOfHibernate.hbm.xml422.00 B2007-04-26 16:05
inheritanceUsingSubclass.hbm.xml559.00 B2007-04-26 16:05
01.96 kB
ormunit-hibernate-1.0-SNAPSHOT.jar49.43 kB2007-04-29 11:26
allclasses-frame.html7.33 kB2007-04-29 11:26
allclasses-noframe.html6.61 kB2007-04-29 11:26
constant-values.html5.24 kB2007-04-29 11:26
deprecated-list.html5.18 kB2007-04-29 11:26
help-doc.html9.77 kB2007-04-29 11:26
index-all.html105.10 kB2007-04-29 11:26
index.html1.42 kB2007-04-29 11:26
ClassMappingException.html6.04 kB2007-04-29 11:26
NonexistentPropertiesException.html6.13 kB2007-04-29 11:26
NonPersistentPropertyException.html6.13 kB2007-04-29 11:26
ORMUnitException.html9.69 kB2007-04-29 11:26
ORMUnitMappingException.html9.34 kB2007-04-29 11:26
PropertyNameSet.html19.14 kB2007-04-29 11:26
UnmappedPropertiesException.html6.10 kB2007-04-29 11:26
ClassMappingException.html13.09 kB2007-04-29 11:26
AbstractORMappingTests.html24.59 kB2007-04-29 11:26
AccessStrategy.html15.67 kB2007-04-29 11:26
AbstractORMappingTests.html8.78 kB2007-04-29 11:26
AccessStrategy.html14.86 kB2007-04-29 11:26
ClassPropertyUtil.html6.15 kB2007-04-29 11:26
ComponentCollectionFieldMapping.html8.77 kB2007-04-29 11:26
ComponentPropertyMapping.html9.76 kB2007-04-29 11:26
CompositeListPropertyMapping.html9.86 kB2007-04-29 11:26
CompositeSetPropertyMapping.html10.49 kB2007-04-29 11:26
DatabaseInitializer.html6.19 kB2007-04-29 11:26
DatabaseResetStrategy.html10.00 kB2007-04-29 11:26
FieldMapping.html8.46 kB2007-04-29 11:26
HibernateAssertUtil.html6.17 kB2007-04-29 11:26
HibernateMappingChecker.html8.38 kB2007-04-29 11:26
HibernateMappingTests.html6.19 kB2007-04-29 11:26
HibernateORMappingTests.html6.21 kB2007-04-29 11:26
HibernateORMTestCase.html10.33 kB2007-04-29 11:26
HibernatePersistenceTests.html6.23 kB2007-04-29 11:26
HibernatePersistenceTestsStrategy.html10.32 kB2007-04-29 11:26
HibernateSchemaChecker.html6.20 kB2007-04-29 11:26
HibernateSchemaTests.html6.18 kB2007-04-29 11:26
ListPropertyMapping.html14.53 kB2007-04-29 11:26
LocalSessionFactoryBeanUtil.html6.25 kB2007-04-29 11:26
MappedClassChecker.html9.40 kB2007-04-29 11:26
NoopDatabaseResetStrategy.html6.23 kB2007-04-29 11:26
PathUtil.html6.06 kB2007-04-29 11:26
ResetDatabaseByRecreatingSchemaStrategy.html6.37 kB2007-04-29 11:26
RollbackTransactionHibernatePersistenceTestsStrategy.html8.90 kB2007-04-29 11:26
SimpleHibernatePersistenceTestsStrategy.html6.37 kB2007-04-29 11:26
ToOnePropertyMapping.html10.53 kB2007-04-29 11:26
ClassPropertyUtil.html12.22 kB2007-04-29 11:26
ComponentCollectionFieldMapping.html15.50 kB2007-04-29 11:26
ComponentPropertyMapping.html16.16 kB2007-04-29 11:26
CompositeListPropertyMapping.html15.71 kB2007-04-29 11:26
CompositeSetPropertyMapping.html13.57 kB2007-04-29 11:26
DatabaseInitializer.html8.49 kB2007-04-29 11:26
DatabaseResetStrategy.html9.00 kB2007-04-29 11:26
FieldMapping.html12.08 kB2007-04-29 11:26
HibernateAssertUtil.html11.43 kB2007-04-29 11:26
HibernateMappingChecker.html13.26 kB2007-04-29 11:26
HibernateMappingTests.html45.05 kB2007-04-29 11:26
HibernateORMappingTests.html44.47 kB2007-04-29 11:26
HibernateORMTestCase.html19.56 kB2007-04-29 11:26
HibernatePersistenceTests.html31.88 kB2007-04-29 11:26
HibernatePersistenceTestsStrategy.html11.42 kB2007-04-29 11:26
HibernateSchemaChecker.html11.84 kB2007-04-29 11:26
HibernateSchemaTests.html19.65 kB2007-04-29 11:26
ListPropertyMapping.html17.05 kB2007-04-29 11:26
LocalSessionFactoryBeanUtil.html10.82 kB2007-04-29 11:26
MappedClassChecker.html31.44 kB2007-04-29 11:26
NoopDatabaseResetStrategy.html11.22 kB2007-04-29 11:26
package-frame.html5.50 kB2007-04-29 11:26
package-summary.html15.04 kB2007-04-29 11:26
package-tree.html14.34 kB2007-04-29 11:26
package-use.html11.65 kB2007-04-29 11:26
PathUtil.html12.76 kB2007-04-29 11:26
ResetDatabaseByRecreatingSchemaStrategy.html14.10 kB2007-04-29 11:26
RollbackTransactionHibernatePersistenceTestsStrategy.html16.54 kB2007-04-29 11:26
SimpleHibernatePersistenceTestsStrategy.html17.97 kB2007-04-29 11:26
ToOnePropertyMapping.html13.06 kB2007-04-29 11:26
NonexistentPropertiesException.html13.41 kB2007-04-29 11:26
NonPersistentPropertyException.html12.76 kB2007-04-29 11:26
ORMUnitException.html11.04 kB2007-04-29 11:26
ORMUnitMappingException.html12.00 kB2007-04-29 11:26
package-frame.html2.07 kB2007-04-29 11:26
package-summary.html8.28 kB2007-04-29 11:26
package-tree.html7.42 kB2007-04-29 11:26
package-use.html8.50 kB2007-04-29 11:26
PropertyNameSet.html17.07 kB2007-04-29 11:26
UnmappedPropertiesException.html13.12 kB2007-04-29 11:26
TxnCallback.html9.88 kB2007-04-29 11:26
TxnCallbackWithResult.html6.03 kB2007-04-29 11:26
package-frame.html1.18 kB2007-04-29 11:26
package-summary.html6.44 kB2007-04-29 11:26
package-tree.html6.13 kB2007-04-29 11:26
package-use.html6.80 kB2007-04-29 11:26
TxnCallback.html7.96 kB2007-04-29 11:26
TxnCallbackWithResult.html8.09 kB2007-04-29 11:26
options1.21 kB2007-04-29 11:26
overview-frame.html1.45 kB2007-04-29 11:26
overview-summary.html6.01 kB2007-04-29 11:26
overview-tree.html15.30 kB2007-04-29 11:26
package-list94.00 B2007-04-29 11:26
packages92.00 B2007-04-29 11:26
inherit.gif57.00 B2007-04-29 11:26
serialized-form.html11.13 kB2007-04-29 11:26
stylesheet.css1.20 kB2007-04-29 11:26
...
Sponsored links

HibernatePersistenceTests.java (316.73 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D