Skip to content

Commit a872f04

Browse files
committed
HHH-16578 - Add test for issue
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
1 parent 90227d9 commit a872f04

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.tool.schema;
8+
9+
import java.sql.Connection;
10+
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
import java.util.Map;
13+
14+
import org.hibernate.cfg.AvailableSettings;
15+
import org.hibernate.cfg.Environment;
16+
import org.hibernate.dialect.MySQLDialect;
17+
import org.hibernate.engine.config.spi.ConfigurationService;
18+
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
19+
import org.hibernate.internal.util.PropertiesHelper;
20+
import org.hibernate.tool.schema.internal.DefaultSchemaFilter;
21+
import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl;
22+
import org.hibernate.tool.schema.internal.HibernateSchemaManagementTool;
23+
import org.hibernate.tool.schema.internal.IndividuallySchemaValidatorImpl;
24+
import org.hibernate.tool.schema.spi.ContributableMatcher;
25+
import org.hibernate.tool.schema.spi.ExceptionHandler;
26+
import org.hibernate.tool.schema.spi.ExecutionOptions;
27+
import org.hibernate.tool.schema.spi.SchemaFilter;
28+
import org.hibernate.tool.schema.spi.SchemaManagementException;
29+
import org.hibernate.tool.schema.spi.SchemaManagementTool;
30+
import org.hibernate.tool.schema.spi.SchemaValidator;
31+
32+
import org.hibernate.testing.orm.junit.DomainModel;
33+
import org.hibernate.testing.orm.junit.JiraKey;
34+
import org.hibernate.testing.orm.junit.RequiresDialect;
35+
import org.hibernate.testing.orm.junit.ServiceRegistry;
36+
import org.hibernate.testing.orm.junit.SessionFactory;
37+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
38+
import org.hibernate.testing.orm.junit.Setting;
39+
import org.junit.jupiter.api.AfterAll;
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.Test;
42+
43+
import jakarta.persistence.Column;
44+
import jakarta.persistence.Entity;
45+
import jakarta.persistence.GeneratedValue;
46+
import jakarta.persistence.GenerationType;
47+
import jakarta.persistence.Id;
48+
import jakarta.persistence.Table;
49+
50+
import static org.junit.jupiter.api.Assertions.fail;
51+
52+
/**
53+
* @author Jan Schatteman
54+
*/
55+
@ServiceRegistry(
56+
settings = {
57+
@Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "none" )
58+
}
59+
)
60+
@DomainModel(
61+
annotatedClasses = { MySQLColumnValidationTest.TestEntity.class }
62+
)
63+
@SessionFactory
64+
@JiraKey( value = "HHH-16578" )
65+
@RequiresDialect( value = MySQLDialect.class )
66+
public class MySQLColumnValidationTest {
67+
68+
private DriverManagerConnectionProviderImpl connectionProvider;
69+
70+
@BeforeAll
71+
public void init() {
72+
connectionProvider = new DriverManagerConnectionProviderImpl();
73+
connectionProvider.configure( PropertiesHelper.map( Environment.getProperties() ) );
74+
75+
try( Connection connection = connectionProvider.getConnection();
76+
Statement statement = connection.createStatement() ) {
77+
78+
try {
79+
statement.execute( "DROP TABLE TEST_DATA" );
80+
}
81+
catch (SQLException e) {
82+
}
83+
84+
statement.execute( "CREATE TABLE `TEST_DATA` ( " +
85+
" `ID` int unsigned NOT NULL AUTO_INCREMENT, " +
86+
" `LEVEL` tinyint unsigned DEFAULT '0', " +
87+
" PRIMARY KEY (`ID`)" +
88+
") ENGINE=InnoDB" );
89+
}
90+
catch (SQLException e) {
91+
fail(e.getMessage());
92+
}
93+
}
94+
95+
@AfterAll
96+
public void releaseResources() {
97+
try( Connection connection = connectionProvider.getConnection();
98+
Statement statement = connection.createStatement() ) {
99+
try {
100+
statement.execute( "DROP TABLE TEST_DATA" );
101+
}
102+
catch (SQLException e) {
103+
}
104+
}
105+
catch (SQLException e) {
106+
fail(e.getMessage());
107+
}
108+
109+
if ( connectionProvider != null ) {
110+
connectionProvider.stop();
111+
}
112+
}
113+
114+
@Test
115+
public void testValidateColumn(SessionFactoryScope scope) {
116+
scope.inTransaction(
117+
session -> {
118+
TestEntity te = new TestEntity();
119+
te.setLevel( 2 );
120+
session.persist( te );
121+
}
122+
);
123+
124+
ConfigurationService configurationService = scope.getSessionFactory().getServiceRegistry().getService( ConfigurationService.class );
125+
ExecutionOptions executionOptions = new ExecutionOptions() {
126+
@Override
127+
public boolean shouldManageNamespaces() {
128+
return true;
129+
}
130+
131+
@Override
132+
public Map getConfigurationValues() {
133+
return configurationService.getSettings();
134+
}
135+
136+
@Override
137+
public ExceptionHandler getExceptionHandler() {
138+
return ExceptionHandlerLoggedImpl.INSTANCE;
139+
}
140+
141+
@Override
142+
public SchemaFilter getSchemaFilter() {
143+
return SchemaFilter.ALL;
144+
}
145+
};
146+
147+
HibernateSchemaManagementTool hsmt = (HibernateSchemaManagementTool) scope.getSessionFactory().getServiceRegistry().getService( SchemaManagementTool.class );
148+
SchemaValidator schemaValidator = new IndividuallySchemaValidatorImpl( hsmt, DefaultSchemaFilter.INSTANCE );
149+
150+
try {
151+
schemaValidator.doValidation( scope.getMetadataImplementor(), executionOptions, ContributableMatcher.ALL );
152+
}
153+
catch (SchemaManagementException e) {
154+
fail (e.getMessage());
155+
}
156+
157+
}
158+
159+
@Entity(name = "test_entity")
160+
@Table(name = "TEST_DATA")
161+
public class TestEntity {
162+
@Id
163+
@GeneratedValue(strategy = GenerationType.AUTO)
164+
@Column(name = "id", columnDefinition = "INT(10) UNSIGNED NOT NULL")
165+
private Integer id;
166+
167+
@Column(name = "level", columnDefinition = "tinyint UNSIGNED DEFAULT '0'")
168+
private int level;
169+
170+
public Integer getId() {
171+
return id;
172+
}
173+
174+
public void setId(Integer id) {
175+
this.id = id;
176+
}
177+
178+
public int getLevel() {
179+
return level;
180+
}
181+
182+
public void setLevel(int level) {
183+
this.level = level;
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)