/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.jpa;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.util.ArrayList;import java.util.Arrays;
import java.io.InputStream;
import java.io.IOException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.cfg.Environment;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.HibernatePersistence;
import org.hibernate.search.test.TestConstants;
import org.junit.After;
import org.junit.Before;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.util.Version;
/**
* @author Emmanuel Bernard
*/
public abstract class JPATestCase extends junit.framework.TestCase {
protected EntityManagerFactory factory;
public JPATestCase() {
super();
}
public JPATestCase(String name) {
super( name );
}
@Before
public void setUp() {
factory = new HibernatePersistence().createEntityManagerFactory( getConfig() );
}
@After
public void tearDown() {
factory.close();
}
public abstract Class[] getAnnotatedClasses();
public String[] getEjb3DD() {
return new String[]{};
}
public Map<Class, String> getCachedClasses() {
return new HashMap<Class, String>();
}
public Map<String, String> getCachedCollections() {
return new HashMap<String, String>();
}
public static Properties loadProperties() {
Properties props = new Properties();
InputStream stream = Persistence.class.getResourceAsStream( "/hibernate.properties" );
if ( stream != null ) {
try {
props.load( stream );
}
catch (Exception e) {
throw new RuntimeException( "could not load hibernate.properties" );
}
finally {
try {
stream.close();
}
catch (IOException ioe) {
}
}
}
props.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
return props;
}
public Map getConfig() {
Map config = loadProperties();
ArrayList<Class> classes = new ArrayList<Class>();
classes.addAll( Arrays.asList( getAnnotatedClasses() ) );
config.put( AvailableSettings.LOADED_CLASSES, classes );
for ( Map.Entry<Class, String> entry : getCachedClasses().entrySet() ) {
config.put(
AvailableSettings.CLASS_CACHE_PREFIX + "." + entry.getKey().getName(),
entry.getValue()
);
}
for ( Map.Entry<String, String> entry : getCachedCollections().entrySet() ) {
config.put(
AvailableSettings.COLLECTION_CACHE_PREFIX + "." + entry.getKey(),
entry.getValue()
);
}
if ( getEjb3DD().length > 0 ) {
ArrayList<String> dds = new ArrayList<String>();
dds.addAll( Arrays.asList( getEjb3DD() ) );
config.put( AvailableSettings.XML_FILE_NAMES, dds );
}
//Search config
config.put( "hibernate.search.default.directory_provider", "ram" );
config.put( org.hibernate.search.Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
config.put( AvailableSettings.ENTITY_MANAGER_FACTORY_NAME, "Test" + getClass() );
configure( config );
return config;
}
public static Version getTargetLuceneVersion() {
return TestConstants.getTargetLuceneVersion();
}
protected void configure(Map cfg) {
// for extensions
}
}