Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-11-15 11:08:10 +01:00 committed by Andrea Boriero
parent 2e74d026f2
commit c1e16b4e4a
16 changed files with 418 additions and 339 deletions

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -14,28 +14,28 @@ import java.sql.Time;
import java.time.Instant; import java.time.Instant;
import java.time.OffsetTime; import java.time.OffsetTime;
import java.util.Calendar; import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -44,12 +44,15 @@ import static org.mockito.Mockito.verify;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@SkipForDialect(MySQL5Dialect.class) @SkipForDialect(dialectClass = MySQLDialect.class, matchSubTypes = true)
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class JdbcTimeCustomTimeZoneTest public class JdbcTimeCustomTimeZoneTest
extends BaseNonConfigCoreFunctionalTestCase { extends BaseSessionFactoryFunctionalTest {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(
true,
false
);
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( private static final TimeZone TIME_ZONE = TimeZone.getTimeZone(
"America/Los_Angeles" ); "America/Los_Angeles" );
@ -62,21 +65,21 @@ public class JdbcTimeCustomTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
settings.put( .get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
settings.put( builder.applySetting(
AvailableSettings.JDBC_TIME_ZONE, AvailableSettings.JDBC_TIME_ZONE,
TIME_ZONE TIME_ZONE
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }
@ -84,7 +87,7 @@ public class JdbcTimeCustomTimeZoneTest
public void testTimeZone() { public void testTimeZone() {
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = new Person(); Person person = new Person();
person.id = 1L; person.id = 1L;
s.persist( person ); s.persist( person );
@ -107,16 +110,16 @@ public class JdbcTimeCustomTimeZoneTest
calendarArgumentCaptor.getValue().getTimeZone() calendarArgumentCaptor.getValue().getTimeZone()
); );
} }
catch ( SQLException e ) { catch (SQLException e) {
fail( e.getMessage() ); fail( e.getMessage() );
} }
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
s.doWork( connection -> { s.doWork( connection -> {
try ( Statement st = connection.createStatement() ) { try (Statement st = connection.createStatement()) {
try ( ResultSet rs = st.executeQuery( try (ResultSet rs = st.executeQuery(
"select createdOn from Person" ) ) { "select createdOn from Person" )) {
while ( rs.next() ) { while ( rs.next() ) {
Time time = rs.getTime( 1 ); Time time = rs.getTime( 1 );
Time offsetTime = Time.valueOf( OffsetTime.ofInstant( Time offsetTime = Time.valueOf( OffsetTime.ofInstant(

View File

@ -4,28 +4,29 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import jakarta.persistence.Entity;
import static org.junit.Assert.assertEquals; import jakarta.persistence.Id;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -34,12 +35,14 @@ import static org.mockito.Mockito.verify;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class JdbcTimeDefaultTimeZoneTest public class JdbcTimeDefaultTimeZoneTest
extends BaseNonConfigCoreFunctionalTestCase { extends BaseSessionFactoryFunctionalTest {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(
true,
false
);
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
@ -49,17 +52,17 @@ public class JdbcTimeDefaultTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
settings.put( .get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }
@ -67,7 +70,7 @@ public class JdbcTimeDefaultTimeZoneTest
public void testTimeZone() { public void testTimeZone() {
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = new Person(); Person person = new Person();
person.id = 1L; person.id = 1L;
s.persist( person ); s.persist( person );
@ -80,11 +83,11 @@ public class JdbcTimeDefaultTimeZoneTest
try { try {
verify( ps, times( 1 ) ).setTime( anyInt(), any( Time.class ) ); verify( ps, times( 1 ) ).setTime( anyInt(), any( Time.class ) );
} }
catch ( SQLException e ) { catch (SQLException e) {
fail( e.getMessage() ); fail( e.getMessage() );
} }
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = s.find( Person.class, 1L ); Person person = s.find( Person.class, 1L );
assertEquals( assertEquals(
0, 0,

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -12,27 +12,27 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernateSessionBuilder; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -41,10 +41,10 @@ import static org.mockito.Mockito.verify;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@SkipForDialect(MySQL5Dialect.class) @SkipForDialect(dialectClass = MySQLDialect.class, matchSubTypes = true)
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class JdbcTimestampCustomSessionLevelTimeZoneTest public class JdbcTimestampCustomSessionLevelTimeZoneTest
extends BaseNonConfigCoreFunctionalTestCase { extends BaseSessionFactoryFunctionalTest {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false );
@ -59,17 +59,16 @@ public class JdbcTimestampCustomSessionLevelTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings().get( AvailableSettings.CONNECTION_PROVIDER ) );
settings.put( builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -12,27 +12,27 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -41,12 +41,15 @@ import static org.mockito.Mockito.verify;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@SkipForDialect(MySQL5Dialect.class) @SkipForDialect(dialectClass = MySQLDialect.class, matchSubTypes = true)
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class JdbcTimestampCustomTimeZoneTest public class JdbcTimestampCustomTimeZoneTest
extends BaseNonConfigCoreFunctionalTestCase { extends BaseSessionFactoryFunctionalTest {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(
true,
false
);
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( private static final TimeZone TIME_ZONE = TimeZone.getTimeZone(
"America/Los_Angeles" ); "America/Los_Angeles" );
@ -59,21 +62,21 @@ public class JdbcTimestampCustomTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
settings.put( .get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
settings.put( builder.applySetting(
AvailableSettings.JDBC_TIME_ZONE, AvailableSettings.JDBC_TIME_ZONE,
TIME_ZONE TIME_ZONE
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }
@ -81,7 +84,7 @@ public class JdbcTimestampCustomTimeZoneTest
public void testTimeZone() { public void testTimeZone() {
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = new Person(); Person person = new Person();
person.id = 1L; person.id = 1L;
s.persist( person ); s.persist( person );
@ -104,16 +107,16 @@ public class JdbcTimestampCustomTimeZoneTest
calendarArgumentCaptor.getValue().getTimeZone() calendarArgumentCaptor.getValue().getTimeZone()
); );
} }
catch ( SQLException e ) { catch (SQLException e) {
fail( e.getMessage() ); fail( e.getMessage() );
} }
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
s.doWork( connection -> { s.doWork( connection -> {
try ( Statement st = connection.createStatement() ) { try (Statement st = connection.createStatement()) {
try ( ResultSet rs = st.executeQuery( try (ResultSet rs = st.executeQuery(
"select createdOn from Person" ) ) { "select createdOn from Person" )) {
while ( rs.next() ) { while ( rs.next() ) {
Timestamp timestamp = rs.getTimestamp( 1 ); Timestamp timestamp = rs.getTimestamp( 1 );
int offsetDiff = TimeZone.getDefault() int offsetDiff = TimeZone.getDefault()

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -12,60 +12,59 @@ import java.sql.Statement;
import java.sql.Time; import java.sql.Time;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.junit.Test; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import jakarta.persistence.Entity;
import static org.junit.Assert.assertEquals; import jakarta.persistence.Id;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
@TestForIssue(jiraKey = "HHH-12988") @TestForIssue(jiraKey = "HHH-12988")
@SkipForDialect(MySQL5Dialect.class) @SkipForDialect(dialectClass = MySQLDialect.class, matchSubTypes = true)
public class JdbcTimestampDSTWithUTCTest @DomainModel(
extends BaseNonConfigCoreFunctionalTestCase { annotatedClasses = JdbcTimestampDSTWithUTCTest.Person.class
)
@SessionFactory
@ServiceRegistry(
settings = @Setting(name = AvailableSettings.JDBC_TIME_ZONE, value = "UTC")
)
public class JdbcTimestampDSTWithUTCTest {
protected final Logger log = Logger.getLogger( getClass() ); protected final Logger log = Logger.getLogger( getClass() );
@Override @AfterEach
protected Class<?>[] getAnnotatedClasses() { public void tearDown(SessionFactoryScope scope) {
return new Class<?>[] { scope.inTransaction(
Person.class session ->
}; session.createQuery( "delete from Person" ).executeUpdate()
}
@Override
protected void addSettings(Map settings) {
settings.put(
AvailableSettings.JDBC_TIME_ZONE,
"UTC"
); );
} }
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Test @Test
public void testHibernate() { public void testHibernate(SessionFactoryScope scope) {
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
Person person = new Person(); Person person = new Person();
person.setId( 1L ); person.setId( 1L );
@ -74,7 +73,7 @@ public class JdbcTimestampDSTWithUTCTest
session.persist( person ); session.persist( person );
} ); } );
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
Person person = session.find( Person.class, 1L ); Person person = session.find( Person.class, 1L );
assertEquals( LocalTime.of( 12, 0, 0 ), person.getShiftStartTime() ); assertEquals( LocalTime.of( 12, 0, 0 ), person.getShiftStartTime() );
@ -82,8 +81,8 @@ public class JdbcTimestampDSTWithUTCTest
} }
@Test @Test
public void testJDBC() { public void testJDBC(SessionFactoryScope scope) {
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
session.doWork( connection -> { session.doWork( connection -> {
Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) ); Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) );
@ -110,8 +109,8 @@ public class JdbcTimestampDSTWithUTCTest
} }
@Test @Test
public void testDBTimeValueAsEpochDST() { public void testDBTimeValueAsEpochDST(SessionFactoryScope scope) {
doInHibernate( this::sessionFactory, session -> { scope.inTransaction( session -> {
session.doWork( connection -> { session.doWork( connection -> {
Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) ); Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) );

View File

@ -4,27 +4,28 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Map;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import jakarta.persistence.Entity;
import static org.junit.Assert.assertEquals; import jakarta.persistence.Id;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -33,11 +34,14 @@ import static org.mockito.Mockito.verify;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class JdbcTimestampDefaultTimeZoneTest public class JdbcTimestampDefaultTimeZoneTest
extends BaseNonConfigCoreFunctionalTestCase { extends BaseSessionFactoryFunctionalTest {
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(
true,
false
);
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
@ -47,17 +51,17 @@ public class JdbcTimestampDefaultTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
settings.put( .get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }
@ -65,7 +69,7 @@ public class JdbcTimestampDefaultTimeZoneTest
public void testTimeZone() { public void testTimeZone() {
connectionProvider.clear(); connectionProvider.clear();
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = new Person(); Person person = new Person();
person.id = 1L; person.id = 1L;
s.persist( person ); s.persist( person );
@ -81,11 +85,11 @@ public class JdbcTimestampDefaultTimeZoneTest
any( Timestamp.class ) any( Timestamp.class )
); );
} }
catch ( SQLException e ) { catch (SQLException e) {
fail( e.getMessage() ); fail( e.getMessage() );
} }
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
Person person = s.find( Person.class, 1L ); Person person = s.find( Person.class, 1L );
assertEquals( 0, person.createdOn.getTime() ); assertEquals( 0, person.createdOn.getTime() );
} ); } );

View File

@ -4,47 +4,50 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.PostgreSQL82Dialect; import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider; import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterAll;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(value = PostgreSQL82Dialect.class) @RequiresDialect(value = PostgreSQLDialect.class, version = 820)
public class JdbcTimestampUTCTimeZoneTest public class JdbcTimestampUTCTimeZoneTest extends JdbcTimestampWithoutUTCTimeZoneTest {
extends JdbcTimestampWithoutUTCTimeZoneTest {
private TimeZoneConnectionProvider connectionProvider = new TimeZoneConnectionProvider( private TimeZoneConnectionProvider connectionProvider;
"America/Los_Angeles" );
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( "UTC" ); private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( "UTC" );
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider = new TimeZoneConnectionProvider(
settings.put( "America/Los_Angeles" );
connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
.get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
settings.put( builder.applySetting(
AvailableSettings.JDBC_TIME_ZONE, AvailableSettings.JDBC_TIME_ZONE,
TIME_ZONE TIME_ZONE
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources(); if ( connectionProvider != null ) {
connectionProvider.stop(); connectionProvider.stop();
}
} }
protected String expectedTimestampValue() { protected String expectedTimestampValue() {

View File

@ -4,40 +4,43 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.PostgreSQL82Dialect; import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider; import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterAll;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(value = PostgreSQL82Dialect.class) @RequiresDialect(value = PostgreSQLDialect.class, version = 820)
public class JdbcTimestampWithDefaultUTCTimeZoneTest public class JdbcTimestampWithDefaultUTCTimeZoneTest
extends JdbcTimestampWithoutUTCTimeZoneTest { extends JdbcTimestampWithoutUTCTimeZoneTest {
private TimeZoneConnectionProvider connectionProvider = new TimeZoneConnectionProvider( private TimeZoneConnectionProvider connectionProvider;
"UTC" );
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider = new TimeZoneConnectionProvider(
settings.put( "UTC" );
connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
.get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources(); if ( connectionProvider != null ) {
connectionProvider.stop(); connectionProvider.stop();
}
} }
protected String expectedTimestampValue() { protected String expectedTimestampValue() {

View File

@ -4,40 +4,39 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.hibernate.cfg.AvailableSettings; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.orm.jdbc.TimeZoneConnectionProvider;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(value = PostgreSQL82Dialect.class) @RequiresDialect(value = PostgreSQLDialect.class, version = 820)
public class JdbcTimestampWithoutUTCTimeZoneTest public class JdbcTimestampWithoutUTCTimeZoneTest extends BaseSessionFactoryFunctionalTest {
extends BaseNonConfigCoreFunctionalTestCase {
private TimeZoneConnectionProvider connectionProvider = new TimeZoneConnectionProvider( private TimeZoneConnectionProvider connectionProvider;
"America/Los_Angeles" );
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
@ -47,24 +46,27 @@ public class JdbcTimestampWithoutUTCTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
connectionProvider.setConnectionProvider( (ConnectionProvider) settings.get( AvailableSettings.CONNECTION_PROVIDER ) ); connectionProvider = new TimeZoneConnectionProvider( "America/Los_Angeles" );
settings.put( connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
.get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources(); if ( connectionProvider != null ) {
connectionProvider.stop(); connectionProvider.stop();
}
} }
@Test @Test
@SkipForDialect(value = CockroachDialect.class, comment = "https://github.com/cockroachdb/cockroach/issues/3781") @SkipForDialect(dialectClass = CockroachDialect.class, matchSubTypes = true, reason = "https://github.com/cockroachdb/cockroach/issues/3781")
public void testTimeZone() { public void testTimeZone() {
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
Person person = new Person(); Person person = new Person();
person.id = 1L; person.id = 1L;
long y2kMillis = LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ) long y2kMillis = LocalDateTime.of( 2000, 1, 1, 0, 0, 0 )
@ -77,12 +79,12 @@ public class JdbcTimestampWithoutUTCTimeZoneTest
session.persist( person ); session.persist( person );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( s -> {
s.doWork( connection -> { s.doWork( connection -> {
try ( Statement st = connection.createStatement() ) { try (Statement st = connection.createStatement()) {
try ( ResultSet rs = st.executeQuery( try (ResultSet rs = st.executeQuery(
"SELECT to_char(createdon, 'YYYY-MM-DD HH24:MI:SS.US') " + "SELECT to_char(createdon, 'YYYY-MM-DD HH24:MI:SS.US') " +
"FROM person" ) ) { "FROM person" )) {
while ( rs.next() ) { while ( rs.next() ) {
String timestamp = rs.getString( 1 ); String timestamp = rs.getString( 1 );
assertEquals( expectedTimestampValue(), timestamp ); assertEquals( expectedTimestampValue(), timestamp );

View File

@ -6,7 +6,7 @@
*/ */
//$Id: Person.java 8054 2005-08-31 20:12:24Z oneovthafew $ //$Id: Person.java 8054 2005-08-31 20:12:24Z oneovthafew $
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.util.Date; import java.util.Date;
/** /**

View File

@ -0,0 +1,92 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.timestamp;
import java.util.Date;
import org.hibernate.cfg.Environment;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Gavin King
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/timestamp/User.hbm.xml"
)
@SessionFactory
@ServiceRegistry(
settings = @Setting(name = Environment.GENERATE_STATISTICS,value = "true")
)
public class TimestampTest {
@Test
public void testUpdateFalse(SessionFactoryScope scope) {
final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
User u = new User( "gavin", "secret", new Person( "Gavin King", new Date(), "Karbarook Ave" ) );
session.persist( u );
session.flush();
u.getPerson().setName( "XXXXYYYYY" );
}
);
assertEquals( 1, statistics.getEntityInsertCount() );
assertEquals( 0, statistics.getEntityUpdateCount() );
scope.inTransaction(
session -> {
User u = session.get( User.class, "gavin" );
assertEquals( u.getPerson().getName(), "Gavin King" );
session.delete( u );
}
);
assertEquals( 1, statistics.getEntityDeleteCount() );
}
@Test
public void testComponent(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
User u = new User( "gavin", "secret", new Person( "Gavin King", new Date(), "Karbarook Ave" ) );
session.persist( u );
session.flush();
u.getPerson().setCurrentAddress( "Peachtree Rd" );
}
);
scope.inTransaction(
session -> {
User u = session.get( User.class, "gavin" );
u.setPassword( "$ecret" );
}
);
scope.inTransaction(
session -> {
User u = session.get( User.class, "gavin" );
assertEquals( u.getPassword(), "$ecret" );
session.delete( u );
}
);
}
}

View File

@ -13,7 +13,7 @@
--> -->
<hibernate-mapping package="org.hibernate.test.timestamp"> <hibernate-mapping package="org.hibernate.orm.test.timestamp">
<class name="User" table="T_USER" dynamic-update="true"> <class name="User" table="T_USER" dynamic-update="true">
<id name="userName"/> <id name="userName"/>

View File

@ -6,7 +6,7 @@
*/ */
//$Id: User.java 8054 2005-08-31 20:12:24Z oneovthafew $ //$Id: User.java 8054 2005-08-31 20:12:24Z oneovthafew $
package org.hibernate.test.timestamp; package org.hibernate.orm.test.timestamp;
import java.util.Date; import java.util.Date;
/** /**

View File

@ -9,27 +9,29 @@ package org.hibernate.test.timestamp;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.ConnectionProviderDelegate;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.hibernate.cfg.AvailableSettings; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.ConnectionProviderDelegate;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernateSessionBuilder;
import static org.junit.Assert.assertEquals;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(MySQL5Dialect.class) @RequiresDialect(value = MySQLDialect.class)
public class LocalDateCustomSessionLevelTimeZoneTest public class LocalDateCustomSessionLevelTimeZoneTest extends BaseSessionFactoryFunctionalTest {
extends BaseNonConfigCoreFunctionalTestCase {
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( private static final TimeZone TIME_ZONE = TimeZone.getTimeZone(
"Europe/Berlin" ); "Europe/Berlin" );
@ -38,16 +40,16 @@ public class LocalDateCustomSessionLevelTimeZoneTest
@Override @Override
public void configure(Map configurationValues) { public void configure(Map configurationValues) {
String url = (String) configurationValues.get( AvailableSettings.URL ); String url = (String) configurationValues.get( AvailableSettings.URL );
if(!url.contains( "?" )) { if ( !url.contains( "?" ) ) {
url += "?"; url += "?";
} }
else if(!url.endsWith( "&" )) { else if ( !url.endsWith( "&" ) ) {
url += "&"; url += "&";
} }
url += "useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin"; url += "useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin";
configurationValues.put( AvailableSettings.URL, url); configurationValues.put( AvailableSettings.URL, url );
super.configure( configurationValues ); super.configure( configurationValues );
} }
}; };
@ -60,21 +62,22 @@ public class LocalDateCustomSessionLevelTimeZoneTest
} }
@Override @Override
protected void addSettings(Map settings) { protected void applySettings(StandardServiceRegistryBuilder builder) {
settings.put( connectionProvider.setConnectionProvider( (ConnectionProvider) builder.getSettings()
.get( AvailableSettings.CONNECTION_PROVIDER ) );
builder.applySetting(
AvailableSettings.CONNECTION_PROVIDER, AvailableSettings.CONNECTION_PROVIDER,
connectionProvider connectionProvider
); );
} }
@Override @AfterAll
protected void releaseResources() { protected void releaseResources() {
super.releaseResources();
connectionProvider.stop(); connectionProvider.stop();
} }
@Test @Test
@TestForIssue( jiraKey = "HHH-11396" ) @TestForIssue(jiraKey = "HHH-11396")
public void testTimeZone() { public void testTimeZone() {
TimeZone old = TimeZone.getDefault(); TimeZone old = TimeZone.getDefault();
try { try {

View File

@ -1,88 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.timestamp;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Gavin King
*/
public class TimestampTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "timestamp/User.hbm.xml" };
}
public void configure(Configuration cfg) {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
}
@Test
public void testUpdateFalse() {
sessionFactory().getStatistics().clear();
Session s = openSession();
Transaction t = s.beginTransaction();
User u = new User( "gavin", "secret", new Person("Gavin King", new Date(), "Karbarook Ave") );
s.persist(u);
s.flush();
u.getPerson().setName("XXXXYYYYY");
t.commit();
s.close();
assertEquals( 1, sessionFactory().getStatistics().getEntityInsertCount() );
assertEquals( 0, sessionFactory().getStatistics().getEntityUpdateCount() );
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
assertEquals( u.getPerson().getName(), "Gavin King" );
s.delete(u);
t.commit();
s.close();
assertEquals( 1, sessionFactory().getStatistics().getEntityDeleteCount() );
}
@Test
public void testComponent() {
Session s = openSession();
Transaction t = s.beginTransaction();
User u = new User( "gavin", "secret", new Person("Gavin King", new Date(), "Karbarook Ave") );
s.persist(u);
s.flush();
u.getPerson().setCurrentAddress("Peachtree Rd");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
u.setPassword("$ecret");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
assertEquals( u.getPassword(), "$ecret" );
s.delete(u);
t.commit();
s.close();
}
}

View File

@ -16,7 +16,11 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
import org.hibernate.Session;
import org.hibernate.SessionBuilder;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataBuilder; import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
@ -37,6 +41,7 @@ import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.SimpleValue;
import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl; import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -320,4 +325,52 @@ public abstract class BaseSessionFactoryFunctionalTest
} }
} }
/**
* Execute function in a Hibernate transaction without return value
*
* @param sessionBuilderSupplier SessionFactory supplier
* @param function function
*/
public static void doInHibernateSessionBuilder(
Supplier<SessionBuilder> sessionBuilderSupplier,
TransactionUtil.HibernateTransactionConsumer function) {
Session session = null;
Transaction txn = null;
try {
session = sessionBuilderSupplier.get().openSession();
function.beforeTransactionCompletion();
txn = session.beginTransaction();
function.accept( session );
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();
if ( session != null ) {
session.close();
}
}
}
} }