44 */
55package org .hibernate .orm .test .dialect .unit .lockhint ;
66
7+ import jakarta .persistence .Entity ;
8+ import jakarta .persistence .Id ;
79import org .hibernate .cfg .AvailableSettings ;
810import org .hibernate .cfg .Environment ;
11+ import org .hibernate .dialect .DatabaseVersion ;
12+ import org .hibernate .dialect .Dialect ;
913import org .hibernate .dialect .MySQLDialect ;
10-
11- import org .hibernate .testing .junit4 .BaseUnitTestCase ;
14+ import org .hibernate .orm .test .dialect .resolver .TestingDialectResolutionInfo ;
15+ import org .hibernate .testing .orm .junit .BaseUnitTest ;
16+ import org .hibernate .testing .orm .junit .DomainModel ;
1217import org .hibernate .testing .orm .junit .RequiresDialect ;
13- import org .junit .Test ;
14-
15- import static org .junit .Assert .assertEquals ;
16- import static org .junit .Assert .assertNotNull ;
18+ import org .hibernate .testing .orm .junit .ServiceRegistry ;
19+ import org .hibernate .testing .orm .junit .SessionFactory ;
20+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
21+ import org .hibernate .testing .orm .junit .Setting ;
22+ import org .junit .jupiter .api .Test ;
1723
1824import java .lang .reflect .Field ;
25+ import java .util .HashMap ;
26+ import java .util .Map ;
1927import java .util .Properties ;
2028
29+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
30+
2131@ RequiresDialect (MySQLDialect .class )
22- public class MySQLStorageEngineTest extends BaseUnitTestCase {
32+ @ BaseUnitTest
33+ public class MySQLStorageEngineTest {
2334
2435 @ Test
2536 public void testDefaultStorage () {
26- assertEquals ( " engine=InnoDB" , new MySQLDialect ().getTableTypeString () );
37+ assertThat ( new MySQLDialect ().getTableTypeString () ). isEqualTo ( " engine=InnoDB" );
2738 }
2839
2940 @ Test
3041 public void testOverrideStorage () throws NoSuchFieldException , IllegalAccessException {
3142 final Field globalPropertiesField = Environment .class .getDeclaredField ( "GLOBAL_PROPERTIES" );
3243 globalPropertiesField .setAccessible ( true );
3344 final Properties systemProperties = (Properties ) globalPropertiesField .get ( null );
34- assertNotNull ( systemProperties );
45+ assertThat ( systemProperties ).isNotNull ();
46+ final Object previousValue = systemProperties .setProperty ( AvailableSettings .STORAGE_ENGINE , "myisam" );
47+ try {
48+ assertThat ( new MySQLDialect ().getTableTypeString () ).isEqualTo ( " engine=MyISAM" );
49+ }
50+ finally {
51+ if ( previousValue != null ) {
52+ systemProperties .setProperty ( AvailableSettings .STORAGE_ENGINE , previousValue .toString () );
53+ }
54+ else {
55+ systemProperties .remove ( AvailableSettings .STORAGE_ENGINE );
56+ }
57+ }
58+ }
59+
60+ @ Test
61+ @ SessionFactory
62+ @ ServiceRegistry (settings = {@ Setting (name = AvailableSettings .STORAGE_ENGINE , value = "myisam" )})
63+ @ DomainModel (annotatedClasses = {TestEntity .class })
64+ public void testOverrideStorageWithConfigurationProperties (SessionFactoryScope scope ) {
65+ Dialect dialect = scope .getSessionFactory ().getJdbcServices ().getDialect ();
66+ assertThat ( dialect .getTableTypeString () ).isEqualTo ( " engine=MyISAM" );
67+ }
68+
69+ @ Test
70+ public void testOverrideStorageEngineConfigurationPropertyHasPrecedenceOverSystemProperty ()
71+ throws Exception {
72+ final Field globalPropertiesField = Environment .class .getDeclaredField ( "GLOBAL_PROPERTIES" );
73+ globalPropertiesField .setAccessible ( true );
74+ final Properties systemProperties = (Properties ) globalPropertiesField .get ( null );
75+ assertThat ( systemProperties ).isNotNull ();
3576 final Object previousValue = systemProperties .setProperty ( AvailableSettings .STORAGE_ENGINE , "myisam" );
3677 try {
37- assertEquals ( " engine=MyISAM" , new MySQLDialect ().getTableTypeString () );
78+ final Map <String , Object > configurationValues = new HashMap <>();
79+ configurationValues .put ( AvailableSettings .STORAGE_ENGINE , "innodb" );
80+ Dialect dialect = new MySQLDialect (
81+ TestingDialectResolutionInfo .forDatabaseInfo (
82+ "MySQL" ,
83+ null ,
84+ DatabaseVersion .NO_VERSION ,
85+ DatabaseVersion .NO_VERSION ,
86+ configurationValues ) );
87+ assertThat ( dialect .getTableTypeString () ).isEqualTo ( " engine=InnoDB" );
3888 }
3989 finally {
4090 if ( previousValue != null ) {
@@ -46,4 +96,12 @@ public void testOverrideStorage() throws NoSuchFieldException, IllegalAccessExce
4696 }
4797 }
4898
99+ @ Entity (name = "TestEntity" )
100+ public static class TestEntity {
101+ @ Id
102+ private Long id ;
103+
104+ private String name ;
105+ }
106+
49107}
0 commit comments