HHH-13225 Fix minorVersionToMatch comparison in BasicDialectResolver

This commit is contained in:
Marek Šabo 2019-01-23 11:51:07 +01:00 committed by Guillaume Smet
parent 103629ee87
commit 5fde418a0d
3 changed files with 57 additions and 18 deletions

View File

@ -51,7 +51,11 @@ public class BasicDialectResolver implements DialectResolver {
* @param majorVersionToMatch The version of the driver to match on
* @param dialectClass The Dialect class to use on match
*/
public BasicDialectResolver(String nameToMatch, int majorVersionToMatch, int minorVersionToMatch, Class dialectClass) {
public BasicDialectResolver(
String nameToMatch,
int majorVersionToMatch,
int minorVersionToMatch,
Class<? extends Dialect> dialectClass) {
this.nameToMatch = nameToMatch;
this.majorVersionToMatch = majorVersionToMatch;
this.minorVersionToMatch = minorVersionToMatch;
@ -66,15 +70,15 @@ public class BasicDialectResolver implements DialectResolver {
if ( nameToMatch.equalsIgnoreCase( databaseName )
&& ( majorVersionToMatch == NO_VERSION || majorVersionToMatch == databaseMajorVersion )
&& ( minorVersionToMatch == NO_VERSION || majorVersionToMatch == databaseMinorVersion ) ) {
&& ( minorVersionToMatch == NO_VERSION || minorVersionToMatch == databaseMinorVersion ) ) {
try {
return (Dialect) dialectClass.newInstance();
}
catch ( HibernateException e ) {
catch (HibernateException e) {
// conceivable that the dialect ctor could throw HibernateExceptions, so don't re-wrap
throw e;
}
catch ( Throwable t ) {
catch (Throwable t) {
throw new HibernateException(
"Could not instantiate specified Dialect class [" + dialectClass.getName() + "]",
t

View File

@ -24,6 +24,12 @@ public class TestingDialects {
public static class MyDialect22 extends Dialect {
}
public static class MyDialect311 extends Dialect {
}
public static class MyDialect312 extends Dialect {
}
public static class MySpecialDB2Dialect extends Dialect {
}

View File

@ -6,21 +6,18 @@
*/
package org.hibernate.dialect.resolver;
import java.sql.SQLException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.TestingDialects;
import org.hibernate.engine.jdbc.dialect.internal.DialectResolverSet;
import org.hibernate.engine.jdbc.dialect.spi.BasicDialectResolver;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
import org.hibernate.exception.JDBCConnectionException;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
/**
* @author Steve Ebersole
@ -46,6 +43,7 @@ public class DialectResolverTest extends BaseUnitTestCase {
@Test
public void testErrorAndOrder() throws Exception {
DialectResolverSet resolvers = new DialectResolverSet();
resolvers.addResolverAtFirst( new TestingDialects.MyDialectResolver1() );
resolvers.addResolver( new TestingDialects.MyDialectResolver2() );
@ -62,25 +60,56 @@ public class DialectResolverTest extends BaseUnitTestCase {
resolvers.addResolver( new BasicDialectResolver( "MyDatabase1", TestingDialects.MyDialect1.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase2", 1, TestingDialects.MyDialect21.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase2", 2, TestingDialects.MyDialect22.class ) );
resolvers.addResolver( new BasicDialectResolver( "ErrorDatabase1", Object.class ) );
testDetermination( resolvers, "MyDatabase1", 1, TestingDialects.MyDialect1.class );
resolvers.addResolver( new BasicDialectResolver( "ErrorDatabase1", Dialect.class ) );
testDetermination( resolvers, "MyDatabase1", 1, TestingDialects.MyDialect1.class );
testDetermination( resolvers, "MyDatabase1", 2, TestingDialects.MyDialect1.class );
testDetermination( resolvers, "MyDatabase2", 0, null );
testDetermination( resolvers, "MyDatabase2", 1, TestingDialects.MyDialect21.class );
testDetermination( resolvers, "MyDatabase2", 2, TestingDialects.MyDialect22.class );
testDetermination( resolvers, "ErrorDatabase1", 0, null );
}
@Test
@TestForIssue(jiraKey = "HHH-13225")
public void testMinorVersion() {
DialectResolverSet resolvers = new DialectResolverSet();
resolvers.addResolver( new BasicDialectResolver( "MyDatabase1", TestingDialects.MyDialect1.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase2", 1, TestingDialects.MyDialect21.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase2", 2, TestingDialects.MyDialect22.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase3", 1, 1, TestingDialects.MyDialect311.class ) );
resolvers.addResolver( new BasicDialectResolver( "MyDatabase3", 1, 2, TestingDialects.MyDialect312.class ) );
resolvers.addResolver( new BasicDialectResolver( "ErrorDatabase1", Dialect.class ) );
testDetermination( resolvers, "MyDatabase1", 1, 1, TestingDialects.MyDialect1.class );
testDetermination( resolvers, "MyDatabase3", 1, null );
testDetermination( resolvers, "MyDatabase3", 1, 1, TestingDialects.MyDialect311.class );
testDetermination( resolvers, "MyDatabase3", 1, 2, TestingDialects.MyDialect312.class );
testDetermination( resolvers, "MyDatabase3", 1, 3, null );
}
private void testDetermination(
DialectResolver resolver,
String databaseName,
int version,
Class dialectClass) throws SQLException {
Dialect dialect = resolver.resolveDialect( TestingDialectResolutionInfo.forDatabaseInfo( databaseName, version ) );
int majorVersion,
Class<? extends Dialect> dialectClass) {
testDetermination( resolver, databaseName, majorVersion, DialectResolutionInfo.NO_VERSION, dialectClass );
}
private void testDetermination(
DialectResolver resolver,
String databaseName,
int majorVersion,
int minorVersion,
Class<? extends Dialect> dialectClass) {
Dialect dialect = resolver.resolveDialect(
TestingDialectResolutionInfo.forDatabaseInfo( databaseName, majorVersion, minorVersion )
);
if ( dialectClass == null ) {
assertEquals( null, dialect );
assertNull( dialect );
}
else {
assertEquals( dialectClass, dialect.getClass() );