`
Sev7en_jun
  • 浏览: 1212962 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
84184fc0-d0b6-3f7f-a3f0-4202acb3caf5
Apache CXF使用s...
浏览量:109897
社区版块
存档分类
最新评论

SSH单元测试代码整理

 
阅读更多
package test;

import java.util.List;

import org.junit.Test;

import com.etone.service.ICacheService;
import com.etone.service.ITaskService;
import com.etone.service.OtSystemServicePortType;
import org.apache.log4j.Logger;

public class UnitTestTools extends JdbcHibernateSupport {
	private static final Logger log = Logger.getLogger(UnitTestTools.class);

	@Test
	public void testJdbc() {
		String sql = "select tal.fdiActionNo,tal.fddStartTime"
				+ " from TestActionLog tal where 1=1"
				+ " and tal.fdiTestItemId=" + 20005099
				+ " and tal.fddStartTime>'"
				+ "2011-11-3 10:10:38' order by tal.fdiActionNo asc";
		log.info("########"+this.getJdbcTemplate());
		List list = this.getJdbcTemplate().queryForList(sql);
		log.info("################ list.size =" + list.size());
	}

	public void testCache() {
		ICacheService cs = (ICacheService) context.getBean("cacheService");
		cs.createBussinessCacheData();
	}

	public void testTask() {
		ITaskService ts = (ITaskService) context.getBean("taskService");
		String str = ts.getTestActionLogContent("20005099",
				"2011-11-3 10:10:38");
		log.info("##########" + str);
	}

	public void testWs() {
		OtSystemServicePortType cs = (OtSystemServicePortType) context
				.getBean("wsOtSystemTarget");
		cs.getAreaBusinessVoList("1");

	}

}

 

 

package test;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.dao.CleanupFailureDataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class JdbcHibernateSupport {
	private static final Logger log = Logger
			.getLogger(JdbcHibernateSupport.class);

	private HibernateTemplate hibernateTemplate;

	private DataSource dataSource;

	private SessionFactory sessionFactory;

	private static final String path = "WebRoot/WEB-INF/";

	private static final String files[] = { "applicationContext-hibernate.xml",
			"log4j.properties" };

	protected static ApplicationContext context;

	public JdbcHibernateSupport() {
		context = new FileSystemXmlApplicationContext(path + files[0]);// 加载spring配置文件
		PropertyConfigurator.configure(path + files[1]);// 加载log4j配置文件
		dataSource = (DataSource) context.getBean("dataSource");// 用于创建JdbcTemplate
		sessionFactory = (SessionFactory) context.getBean("sessionFactory");// 用于创建HibernateTemplate

	}

	public HibernateTemplate getHibernateTemplate() {
		return new HibernateTemplate(sessionFactory);
	}

	public JdbcTemplate getJdbcTemplate() {
		return new JdbcTemplate(dataSource);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public DataSource getDataSource() {

		return dataSource;
	}

	public void flushHibernate() {
		HibernateCallback hibernateCallback = new HibernateCallback() {
			public Object doInHibernate(Session session) {
				try {
					session.flush();
				} catch (HibernateException ex) {
					log.error(ex.getMessage(), ex);
				}
				return null;
			}
		};
		hibernateTemplate.execute(hibernateCallback);
	}

	protected Session getSession(SessionFactory sessionFactory,
			boolean isAllowCreate) throws DataAccessResourceFailureException {
		Session session = SessionFactoryUtils.getSession(sessionFactory,
				isAllowCreate);
		session.setFlushMode(FlushMode.NEVER);
		return session;

	}

	protected void bindResource(Session session, SessionFactory sessionFactory) {
		TransactionSynchronizationManager.bindResource(sessionFactory,
				new SessionHolder(session));
	}

	protected void unBindResource(Session session, SessionFactory sessionFactory) {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		closeSessionIfNecessary(session, sessionFactory);
	}

	public void closeSessionIfNecessary(Session session,
			SessionFactory sessionFactory)
			throws CleanupFailureDataAccessException {

		if (session == null
				|| TransactionSynchronizationManager
						.hasResource(sessionFactory)) {
			return;
		}
		System.out.println("Closing Hibernate session");
		try {
			session.close();

		} catch (JDBCException ex) {
			throw new CleanupFailureDataAccessException(
					"Could not close Hibernate session", ex.getSQLException());
		} catch (HibernateException ex) {
			throw new CleanupFailureDataAccessException(
					"Could not close Hibernate session", ex);
		}
	}

}

 

 

分享到:
评论

相关推荐

    SSH架构测试代码

    SSH架构测试代码,很好的测试代码,最新整理的,用于学习

    Extjs4学习指南源代码(JAVA后台)

    个人《Extjs4学习指南》电子文档过程中整理的Myeclipse工程,有以下修改:1、数据库改为Mysql;2、前四章后台改为servlet;3、最后一章后台改为SSH架构。 使用说明:1、BeginExtjs4目录为前四章的例子代码;2、SSH_...

    基于Servlet、SSH、SSM、SpringBoot等流行技术实现的JavaWeb项目

    Hello,在这里,我将为您分享流行技术实现的JavaWeb源码(包括但不局限于Servlet、SSH、SSM、SpringBoot)及相关技术的讲解。 项目说明 目前这个项目只是初创阶段,很多细节还没有确定。但可以确定,这个仓库的目...

    javassh框架源码-nutz-sourcecode-read:阅读nutz的阅读博客整理,nutz是一个精巧的java开发框架,你可以用它

    先通过官方文档和单元测试代码,了解整理每个模块的细致功能列表。 通过阅读源码了解具体设计细节,要能能明白其中设计思想就更好。 一些阅读源码的工具 在idea中,选中一个模块包路径或者在一个类中,摁住ctrl+...

    分享基于Servlet、SSH、SSM、SpringBoot、SpringCloud等流行技术实现的JavaWeb项目

    将使用的jdk版本、tomcat版本、数据库版本测试和编辑器版本进行说明,保证版本一致的情况下正常使用。 分析项目中采用的前置技术,对读数进行分级,大家可以根据自己的水平选择相应的读数项目进行研究学习。 保证本...

    lando-pantheon-ci-workflow-example:在将代码集成到生产中之前,配置Lando和Travis CI以进行不错的本地开发人员+健全性检查

    单元测试和behat测试您需要什么在开始之前,您需要确保您拥有一些东西: GitHub帐户,最好添加您的SSH密钥具有Pantheon帐户Travis CI帐户 * (可选) 用于GitHub中的看板样式问题管理也绝对值得阅读有关上游。...

    iOS应用逆向工程:分析与实战

    7.6 代码结果整理 178 7.7 小结 179 第8 章 实战2:WhatsApp消息拦截 180 8.1 WhatsApp 简介 180 8.2 分析源代码结构,并定位监听点 180 8.2.1 class-dump 获取.h 文件 180 8.2.2 导入Xcode 182 8.2.3...

    python入门到高级全栈工程师培训 第3期 附课件代码

    06 Python 今日内容整理 第13章 第13章共1课 第14章 01 数据类型和变量总结 02 集合定义和基本操作方法 03 集合关系运算交,差,并集 04 集合的其他内置方法 05 集合补充 06 百分号字符串拼接 07 format字符串...

    Java学习笔记-个人整理的

    {2.10.3}静态代码块}{62}{subsection.2.10.3} {2.11}\ttfamily final}{63}{section.2.11} {2.12}\ttfamily abstract}{63}{section.2.12} {2.13}\ttfamily interface}{64}{section.2.13} {2.14}JavaBean规范}{66...

    spring-boot示例项目

    helloworld|[spring mvc,Spring Boot项目创建,单元测试](https://github.com/smltq/spring-boot-demo/blob/master/helloworld/HELP.md) web|[ssh项目,spring mvc,过滤器,拦截器,监视器,thymeleaf,lombok,jquery,...

    Git权威指南PDF完整版

    32.10.4 评审任务没有通过测试/ 480 32.10.5 重新提交新的补丁集/ 482 32.10.6 新修订集通过评审/ 483 32.10.7 从远程版本库更新/ 485 32.11 更多 Gerrit 参考/ 486 第33章 Git 版本库托管/ 487 33.1 Github/ 487 ...

    x-SCAN -V3.3-CN.

    需要“Nessus攻击脚本引擎”源代码、X-Scan插件SDK、示例插件源代码或愿意参与脚本翻译工作的朋友,可通过本站“X-Scan”项目链接获取详细资料:“http://www.xfocus.net/projects/X-Scan/index.html”。 三. 所需...

    强大的扫描工具x-scan

    需要“Nessus攻击脚本引擎”源代码、X-Scan插件SDK、示例插件源代码或愿意参与脚本翻译工作的朋友 ,可通过本站“X-Scan”项目链接获取详细资料:“http://www.xfocus.net/projects/X- Scan/index.html”。 三. ...

Global site tag (gtag.js) - Google Analytics