diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicDialectResolver.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicDialectResolver.java index b7a670b4f2..a4eb06e78b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicDialectResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/dialect/spi/BasicDialectResolver.java @@ -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 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 diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/TestingDialects.java b/hibernate-core/src/test/java/org/hibernate/dialect/TestingDialects.java index eab5f6ba25..3b733b4ac5 100644 --- a/hibernate-core/src/test/java/org/hibernate/dialect/TestingDialects.java +++ b/hibernate-core/src/test/java/org/hibernate/dialect/TestingDialects.java @@ -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 { } diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/resolver/DialectResolverTest.java b/hibernate-core/src/test/java/org/hibernate/dialect/resolver/DialectResolverTest.java index cb0b9a0dea..a214940d12 100644 --- a/hibernate-core/src/test/java/org/hibernate/dialect/resolver/DialectResolverTest.java +++ b/hibernate-core/src/test/java/org/hibernate/dialect/resolver/DialectResolverTest.java @@ -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 dialectClass) { + testDetermination( resolver, databaseName, majorVersion, DialectResolutionInfo.NO_VERSION, dialectClass ); + } + + private void testDetermination( + DialectResolver resolver, + String databaseName, + int majorVersion, + int minorVersion, + Class dialectClass) { + Dialect dialect = resolver.resolveDialect( + TestingDialectResolutionInfo.forDatabaseInfo( databaseName, majorVersion, minorVersion ) + ); if ( dialectClass == null ) { - assertEquals( null, dialect ); + assertNull( dialect ); } else { assertEquals( dialectClass, dialect.getClass() );