2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
24
package org.hibernate.test.util;
26
import java.util.Enumeration;
27
import java.util.HashSet;
28
import java.util.Properties;
31
import junit.framework.Test;
32
import junit.framework.TestCase;
33
import junit.framework.TestSuite;
35
import org.hibernate.cache.jbc2.JBossCacheRegionFactory;
36
import org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory;
37
import org.hibernate.cache.jbc2.builder.SharedCacheInstanceManager;
38
import org.hibernate.cfg.Configuration;
39
import org.hibernate.cfg.Environment;
40
import org.hibernate.cfg.Settings;
41
import org.hibernate.test.tm.jbc2.BatchModeTransactionManagerLookup;
44
* Utilities for cache testing.
46
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
47
* @version $Revision: 1 $
49
public class CacheTestUtil {
51
public static String LOCAL_OPTIMISIC_CACHE;
52
public static String LOCAL_PESSIMISTIC_CACHE;
55
String pkg = CacheTestUtil.class.getPackage().getName().replace('.', '/');
56
LOCAL_OPTIMISIC_CACHE = pkg + "/optimistic-local-cache.xml";
57
LOCAL_PESSIMISTIC_CACHE = pkg + "/pessimistic-local-cache.xml";
60
public static Configuration buildConfiguration(String regionPrefix, Class regionFactory, boolean use2ndLevel, boolean useQueries) {
62
Configuration cfg = new Configuration();
63
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
64
cfg.setProperty(Environment.USE_STRUCTURED_CACHE, "true");
65
// cfg.setProperty(Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName());
66
cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, BatchModeTransactionManagerLookup.class.getName());
68
cfg.setProperty(Environment.CACHE_REGION_FACTORY, regionFactory.getName());
69
cfg.setProperty(Environment.CACHE_REGION_PREFIX, regionPrefix);
70
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, String.valueOf(use2ndLevel));
71
cfg.setProperty(Environment.USE_QUERY_CACHE, String.valueOf(useQueries));
76
public static Configuration buildLocalOnlyConfiguration(String regionPrefix, boolean optimistic, boolean use2ndLevel, boolean useQueries) {
77
Configuration cfg = buildConfiguration(regionPrefix, SharedJBossCacheRegionFactory.class, use2ndLevel, useQueries);
79
String resource = CacheTestUtil.class.getPackage().getName().replace('.', '/') + "/";
80
resource += optimistic ? "optimistic" : "pessimistic";
81
resource += "-local-cache.xml";
83
cfg.setProperty(SharedCacheInstanceManager.CACHE_RESOURCE_PROP, resource);
88
public static JBossCacheRegionFactory startRegionFactory(Configuration cfg)
89
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
91
Settings settings = cfg.buildSettings();
92
Properties properties = cfg.getProperties();
94
String factoryType = cfg.getProperty(Environment.CACHE_REGION_FACTORY);
95
Class factoryClass = Thread.currentThread().getContextClassLoader().loadClass(factoryType);
96
JBossCacheRegionFactory regionFactory = (JBossCacheRegionFactory) factoryClass.newInstance();
98
regionFactory.start(settings, properties);
100
return regionFactory;
103
public static JBossCacheRegionFactory startRegionFactory(Configuration cfg, CacheTestSupport testSupport)
104
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
106
JBossCacheRegionFactory factory = startRegionFactory(cfg);
107
testSupport.registerFactory(factory);
111
public static void stopRegionFactory(JBossCacheRegionFactory factory, CacheTestSupport testSupport) {
114
testSupport.unregisterFactory(factory);
118
* Supports easy creation of a TestSuite where a subclass' "FailureExpected"
119
* version of a base test is included in the suite, while the base test
120
* is excluded. E.g. test class FooTestCase includes method testBar(), while test
121
* class SubFooTestCase extends FooTestCase includes method testBarFailureExcluded().
122
* Passing SubFooTestCase.class to this method will return a suite that
123
* does not include testBar().
125
* FIXME Move this to UnitTestCase
127
public static TestSuite createFailureExpectedSuite(Class testClass) {
129
TestSuite allTests = new TestSuite(testClass);
130
Set failureExpected = new HashSet();
131
Enumeration tests = allTests.tests();
132
while (tests.hasMoreElements()) {
133
Test t = (Test) tests.nextElement();
134
if (t instanceof TestCase) {
135
String name = ((TestCase) t).getName();
136
if (name.endsWith("FailureExpected"))
137
failureExpected.add(name);
141
TestSuite result = new TestSuite();
142
tests = allTests.tests();
143
while (tests.hasMoreElements()) {
144
Test t = (Test) tests.nextElement();
145
if (t instanceof TestCase) {
146
String name = ((TestCase) t).getName();
147
if (!failureExpected.contains(name + "FailureExpected")) {
157
* Prevent instantiation.
159
private CacheTestUtil() {