HHH-13304 MySQLDialect shouldn't access System Properties

This commit is contained in:
Sanne Grinovero 2019-03-05 22:12:11 +00:00
parent d353a10382
commit 271bf2d4cd
2 changed files with 17 additions and 14 deletions

View File

@ -46,7 +46,7 @@
public class MySQLDialect extends Dialect { public class MySQLDialect extends Dialect {
private final UniqueDelegate uniqueDelegate; private final UniqueDelegate uniqueDelegate;
private MySQLStorageEngine storageEngine; private final MySQLStorageEngine storageEngine;
private static final LimitHandler LIMIT_HANDLER = new AbstractLimitHandler() { private static final LimitHandler LIMIT_HANDLER = new AbstractLimitHandler() {
@Override @Override
@ -68,9 +68,6 @@ public MySQLDialect() {
super(); super();
String storageEngine = Environment.getProperties().getProperty( Environment.STORAGE_ENGINE ); 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(); this.storageEngine = getDefaultMySQLStorageEngine();
} }
@ -81,7 +78,7 @@ else if( "myisam".equals( storageEngine.toLowerCase() ) ) {
this.storageEngine = MyISAMStorageEngine.INSTANCE; this.storageEngine = MyISAMStorageEngine.INSTANCE;
} }
else { 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" ); registerColumnType( Types.BIT, "bit" );

View File

@ -7,6 +7,7 @@
package org.hibernate.test.dialect.unit.lockhint; package org.hibernate.test.dialect.unit.lockhint;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.MySQL57Dialect; import org.hibernate.dialect.MySQL57Dialect;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -15,11 +16,12 @@
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** import java.lang.reflect.Field;
* @author Vlad Mihalcea import java.util.Properties;
*/
public class MySQLStorageEngineTest extends BaseUnitTestCase { public class MySQLStorageEngineTest extends BaseUnitTestCase {
@Test @Test
@ -28,19 +30,23 @@ public void testDefaultStorage() {
} }
@Test @Test
public void testOverrideStorage() { public void testOverrideStorage() throws NoSuchFieldException, IllegalAccessException {
String previousValue = System.setProperty( AvailableSettings.STORAGE_ENGINE, "myisam" ); 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 { try {
assertEquals( " engine=MyISAM", new MySQL57Dialect().getTableTypeString() ); assertEquals( " engine=MyISAM", new MySQL57Dialect().getTableTypeString() );
} }
finally { finally {
if ( previousValue != null ) { if ( previousValue != null ) {
System.setProperty( AvailableSettings.STORAGE_ENGINE, previousValue ); systemProperties.setProperty( AvailableSettings.STORAGE_ENGINE, previousValue.toString() );
} }
else { else {
System.clearProperty( AvailableSettings.STORAGE_ENGINE ); systemProperties.remove( AvailableSettings.STORAGE_ENGINE );
assertThat( System.getProperty( AvailableSettings.STORAGE_ENGINE ), is( nullValue() ) );
} }
} }
} }
} }