/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. 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.test.legacy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.DefaultNamingStrategy;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.type.Type;
import org.junit.Before;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/**
* @author Steve Ebersole
*/
public abstract class LegacyTestCase extends BaseCoreFunctionalTestCase {
public static final String USE_ANTLR_PARSER_PROP = "legacy.use_antlr_hql_parser";
private boolean useAntlrParser;
@Before
@SuppressWarnings( {"UnnecessaryUnboxing"})
public void checkAntlrParserSetting() {
useAntlrParser = Boolean.valueOf( extractFromSystem( USE_ANTLR_PARSER_PROP ) ).booleanValue();
}
protected static String extractFromSystem(String systemPropertyName) {
try {
return System.getProperty( systemPropertyName );
}
catch( Throwable t ) {
return null;
}
}
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
if ( !useAntlrParser ) {
cfg.setProperty( Environment.QUERY_TRANSLATOR, ClassicQueryTranslatorFactory.class.getName() );
try {
String dialectTrueRepresentation = Dialect.getDialect().toBooleanValueString( true );
// if this call succeeds, then the dialect is saying to represent true/false as int values...
Integer.parseInt( dialectTrueRepresentation );
String subs = cfg.getProperties().getProperty( Environment.QUERY_SUBSTITUTIONS );
if ( subs == null ) {
subs = "";
}
if ( StringHelper.isEmpty( subs ) ) {
subs = "true=1, false=0";
}
else {
subs += ", true=1, false=0";
}
cfg.getProperties().setProperty( Environment.QUERY_SUBSTITUTIONS, subs );
cfg.setNamingStrategy( DefaultNamingStrategy.INSTANCE );
}
catch( NumberFormatException nfe ) {
// the Integer#parseInt call failed...
}
}
}
protected int doDelete(Session session, String queryString) {
return doDelete( session, session.createQuery( queryString ) );
}
protected int doDelete(Session session, String queryString, Object param, Type paramType) {
Query query = session.createQuery( queryString )
.setParameter( 0, param, paramType );
return doDelete( session, query );
}
protected int doDelete(Session session, Query selectQuery) {
int count = 0;
for ( Object o : selectQuery.list() ) {
session.delete( o );
count++;
}
return count;
}
}