~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/cache-jbosscache2/src/test/java/org/hibernate/test/util/CacheTestUtil.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
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.
8
 
 *
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.
12
 
 *
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
16
 
 * for more details.
17
 
 *
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
23
 
 */
24
 
package org.hibernate.test.util;
25
 
 
26
 
import java.util.Enumeration;
27
 
import java.util.HashSet;
28
 
import java.util.Properties;
29
 
import java.util.Set;
30
 
 
31
 
import junit.framework.Test;
32
 
import junit.framework.TestCase;
33
 
import junit.framework.TestSuite;
34
 
 
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;
42
 
 
43
 
/**
44
 
 * Utilities for cache testing.
45
 
 * 
46
 
 * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
47
 
 * @version $Revision: 1 $
48
 
 */
49
 
public class CacheTestUtil {
50
 
 
51
 
    public static String LOCAL_OPTIMISIC_CACHE;
52
 
    public static String LOCAL_PESSIMISTIC_CACHE;
53
 
    
54
 
    static {
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";
58
 
    }
59
 
    
60
 
    public static Configuration buildConfiguration(String regionPrefix, Class regionFactory, boolean use2ndLevel, boolean useQueries) {
61
 
        
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());
67
 
 
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));
72
 
        
73
 
        return cfg;
74
 
    }
75
 
    
76
 
    public static Configuration buildLocalOnlyConfiguration(String regionPrefix, boolean optimistic, boolean use2ndLevel, boolean useQueries) {
77
 
        Configuration cfg = buildConfiguration(regionPrefix, SharedJBossCacheRegionFactory.class, use2ndLevel, useQueries);
78
 
        
79
 
        String resource = CacheTestUtil.class.getPackage().getName().replace('.', '/') + "/";
80
 
        resource += optimistic ? "optimistic" : "pessimistic";
81
 
        resource += "-local-cache.xml";
82
 
        
83
 
        cfg.setProperty(SharedCacheInstanceManager.CACHE_RESOURCE_PROP, resource);
84
 
        
85
 
        return cfg;
86
 
    }
87
 
    
88
 
    public static JBossCacheRegionFactory startRegionFactory(Configuration cfg) 
89
 
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
90
 
        
91
 
        Settings settings = cfg.buildSettings();
92
 
        Properties properties = cfg.getProperties();
93
 
        
94
 
        String factoryType = cfg.getProperty(Environment.CACHE_REGION_FACTORY);
95
 
        Class factoryClass = Thread.currentThread().getContextClassLoader().loadClass(factoryType);
96
 
        JBossCacheRegionFactory regionFactory = (JBossCacheRegionFactory) factoryClass.newInstance();
97
 
        
98
 
        regionFactory.start(settings, properties);
99
 
        
100
 
        return regionFactory;        
101
 
    }
102
 
    
103
 
    public static JBossCacheRegionFactory startRegionFactory(Configuration cfg, CacheTestSupport testSupport) 
104
 
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
105
 
    
106
 
        JBossCacheRegionFactory factory = startRegionFactory(cfg);
107
 
        testSupport.registerFactory(factory);
108
 
        return factory;
109
 
    }
110
 
    
111
 
    public static void stopRegionFactory(JBossCacheRegionFactory factory, CacheTestSupport testSupport) {
112
 
    
113
 
        factory.stop();
114
 
        testSupport.unregisterFactory(factory);
115
 
    }
116
 
 
117
 
   /**
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().
124
 
     * 
125
 
     * FIXME Move this to UnitTestCase
126
 
     */
127
 
    public static TestSuite createFailureExpectedSuite(Class testClass) {
128
 
       
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);
138
 
          }       
139
 
       }
140
 
       
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")) {
148
 
                result.addTest(t);
149
 
             }
150
 
          }       
151
 
       }
152
 
       
153
 
       return result;
154
 
    }
155
 
    
156
 
    /**
157
 
     * Prevent instantiation. 
158
 
     */
159
 
    private CacheTestUtil() {        
160
 
    }
161
 
 
162
 
}