From 271bf2d4cd1bffe2b267ff26b0197ebc191e486c Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 5 Mar 2019 22:12:11 +0000 Subject: [PATCH] HHH-13304 MySQLDialect shouldn't access System Properties --- .../org/hibernate/dialect/MySQLDialect.java | 9 +++----- .../unit/lockhint/MySQLStorageEngineTest.java | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java index b7ee0fdf56..3edaa85230 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java @@ -46,7 +46,7 @@ import org.hibernate.type.StandardBasicTypes; public class MySQLDialect extends Dialect { private final UniqueDelegate uniqueDelegate; - private MySQLStorageEngine storageEngine; + private final MySQLStorageEngine storageEngine; private static final LimitHandler LIMIT_HANDLER = new AbstractLimitHandler() { @Override @@ -68,10 +68,7 @@ public class MySQLDialect extends Dialect { super(); String storageEngine = Environment.getProperties().getProperty( Environment.STORAGE_ENGINE ); - if(storageEngine == null) { - storageEngine = System.getProperty( Environment.STORAGE_ENGINE ); - } - if(storageEngine == null) { + if ( storageEngine == null ) { this.storageEngine = getDefaultMySQLStorageEngine(); } else if( "innodb".equals( storageEngine.toLowerCase() ) ) { @@ -81,7 +78,7 @@ public class MySQLDialect extends Dialect { this.storageEngine = MyISAMStorageEngine.INSTANCE; } else { - throw new UnsupportedOperationException( "The " + storageEngine + " storage engine is not supported!" ); + throw new UnsupportedOperationException( "The storage engine '" + storageEngine + "' is not supported!" ); } registerColumnType( Types.BIT, "bit" ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/dialect/unit/lockhint/MySQLStorageEngineTest.java b/hibernate-core/src/test/java/org/hibernate/test/dialect/unit/lockhint/MySQLStorageEngineTest.java index d1249b86c1..36346e313b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/dialect/unit/lockhint/MySQLStorageEngineTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/dialect/unit/lockhint/MySQLStorageEngineTest.java @@ -7,6 +7,7 @@ package org.hibernate.test.dialect.unit.lockhint; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; import org.hibernate.dialect.MySQL57Dialect; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -15,11 +16,12 @@ import org.junit.Test; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -/** - * @author Vlad Mihalcea - */ +import java.lang.reflect.Field; +import java.util.Properties; + public class MySQLStorageEngineTest extends BaseUnitTestCase { @Test @@ -28,19 +30,23 @@ public class MySQLStorageEngineTest extends BaseUnitTestCase { } @Test - public void testOverrideStorage() { - String previousValue = System.setProperty( AvailableSettings.STORAGE_ENGINE, "myisam" ); + public void testOverrideStorage() throws NoSuchFieldException, IllegalAccessException { + final Field globalPropertiesField = Environment.class.getDeclaredField( "GLOBAL_PROPERTIES" ); + globalPropertiesField.setAccessible( true ); + final Properties systemProperties = (Properties) globalPropertiesField.get( null ); + assertNotNull( systemProperties ); + final Object previousValue = systemProperties.setProperty( AvailableSettings.STORAGE_ENGINE, "myisam" ); try { assertEquals( " engine=MyISAM", new MySQL57Dialect().getTableTypeString() ); } finally { if ( previousValue != null ) { - System.setProperty( AvailableSettings.STORAGE_ENGINE, previousValue ); + systemProperties.setProperty( AvailableSettings.STORAGE_ENGINE, previousValue.toString() ); } else { - System.clearProperty( AvailableSettings.STORAGE_ENGINE ); - assertThat( System.getProperty( AvailableSettings.STORAGE_ENGINE ), is( nullValue() ) ); + systemProperties.remove( AvailableSettings.STORAGE_ENGINE ); } } } + }