001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.tasklist.db;
018:
019: import java.util.Properties;
020:
021: import org.apache.commons.dbcp.BasicDataSource;
022: import org.apache.log4j.PropertyConfigurator;
023: import org.bpmscript.jbi.tasklist.ITaskListStore;
024: import org.bpmscript.jbi.tasklist.TaskListConfiguration;
025: import org.hibernate.SessionFactory;
026: import org.springframework.aop.framework.ProxyFactoryBean;
027: import org.springframework.jdbc.support.lob.DefaultLobHandler;
028: import org.springframework.orm.hibernate3.HibernateInterceptor;
029: import org.springframework.orm.hibernate3.HibernateTransactionManager;
030: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
031: import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
032: import org.springframework.transaction.interceptor.TransactionInterceptor;
033:
034: import junit.framework.TestCase;
035:
036: public class HibernateTaskListStoreTest extends TestCase {
037:
038: private ITaskListStore taskListStore;
039: private SessionFactory sessionFactory;
040: private BasicDataSource dataSource;
041:
042: public HibernateTaskListStoreTest() {
043: Properties properties = new Properties();
044: properties.put("log4j.rootCategory", "INFO, stdout");
045: properties.put("log4j.appender.stdout",
046: "org.apache.log4j.ConsoleAppender");
047: properties.put("log4j.appender.stdout.layout",
048: "org.apache.log4j.PatternLayout");
049: properties.put(
050: "log4j.appender.stdout.layout.ConversionPattern",
051: "%p [%t] %c{1}.%M(%L) | %m%n");
052: PropertyConfigurator.configure(properties);
053: }
054:
055: protected void setUp() throws Exception {
056: TaskListConfiguration configuration = new TaskListConfiguration();
057:
058: dataSource = new BasicDataSource();
059: dataSource.setDriverClassName(configuration
060: .getDataSourceDriverClassName());
061: dataSource.setUrl(configuration.getDataSourceUrl());
062: dataSource.setUsername(configuration.getDataSourceUsername());
063: dataSource.setPassword(configuration.getDataSourcePassword());
064:
065: Properties properties = new Properties();
066: properties.setProperty("hibernate.hbm2ddl.auto", "update");
067: properties.setProperty("hibernate.dialect", configuration
068: .getHibernateDialect());
069: properties.setProperty("hibernate.query.substitutions",
070: "true 1, false 0");
071: properties.setProperty("hibernate.show_sql", "false");
072: properties.setProperty("hibernate.c3p0.minPoolSize", "5");
073: properties.setProperty("hibernate.c3p0.maxPoolSize", "20");
074: properties.setProperty("hibernate.c3p0.timeout", "600");
075: properties.setProperty("hibernate.c3p0.max_statement", "50");
076: properties.setProperty(
077: "hibernate.c3p0.testConnectionOnCheckout", "false");
078:
079: LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
080: sessionFactoryBean.setLobHandler(new DefaultLobHandler());
081: sessionFactoryBean.setHibernateProperties(properties);
082: sessionFactoryBean
083: .setMappingResources(new String[] { "/org/bpmscript/jbi/tasklist/db/DbTask.hbm.xml" });
084: sessionFactoryBean.setDataSource(dataSource);
085: sessionFactoryBean.afterPropertiesSet();
086:
087: sessionFactory = (SessionFactory) sessionFactoryBean
088: .getObject();
089: HibernateTransactionManager transactionManager = new HibernateTransactionManager();
090: transactionManager.setSessionFactory(sessionFactory);
091:
092: HibernateInterceptor hibernateInterceptor = new HibernateInterceptor();
093: hibernateInterceptor.setSessionFactory(sessionFactory);
094: hibernateInterceptor.afterPropertiesSet();
095:
096: TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
097: transactionInterceptor
098: .setTransactionManager(transactionManager);
099: transactionInterceptor
100: .setTransactionAttributeSource(new MatchAlwaysTransactionAttributeSource());
101:
102: HibernateTaskListStore hibernateTaskListStore = new HibernateTaskListStore();
103: hibernateTaskListStore.setSessionFactory(sessionFactory);
104: hibernateTaskListStore.afterPropertiesSet();
105:
106: ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
107: proxyFactoryBean
108: .setProxyInterfaces(new Class[] { ITaskListStore.class });
109: proxyFactoryBean.addAdvice(hibernateInterceptor);
110: proxyFactoryBean.addAdvice(transactionInterceptor);
111: proxyFactoryBean.setTarget(hibernateTaskListStore);
112: taskListStore = (ITaskListStore) proxyFactoryBean.getObject();
113: }
114:
115: public void testHibernateTaskListStore() throws Exception {
116:
117: taskListStore.getTasksForInstance("2134");
118: }
119:
120: protected void tearDown() throws Exception {
121: sessionFactory.close();
122: dataSource.close();
123: }
124:
125: }
|