Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-13163 - Fix DDLWithoutCallbackTest#testRangeChecksGetApplied whic…
…h fails on MariaDB
- Loading branch information
1 parent
d016462
commit af1172c
Showing
7 changed files
with
362 additions
and
130 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
hibernate-core/src/test/java/org/hibernate/orm/test/annotations/beanvalidation/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.annotations.beanvalidation; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Transient; | ||
import javax.validation.constraints.AssertTrue; | ||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
import javax.validation.constraints.Size; | ||
|
||
@Entity | ||
public class Address { | ||
@NotNull | ||
public static String blacklistedZipCode; | ||
|
||
private String line1; | ||
private String line2; | ||
private String zip; | ||
private String state; | ||
@Size(max = 20) | ||
@NotNull | ||
private String country; | ||
private long id; | ||
private boolean internalValid = true; | ||
@Min(-2) | ||
@Max(value = 50) | ||
public int floor; | ||
|
||
public String getCountry() { | ||
return country; | ||
} | ||
|
||
public void setCountry(String country) { | ||
this.country = country; | ||
} | ||
|
||
@NotNull | ||
public String getLine1() { | ||
return line1; | ||
} | ||
|
||
public void setLine1(String line1) { | ||
this.line1 = line1; | ||
} | ||
|
||
public String getLine2() { | ||
return line2; | ||
} | ||
|
||
public void setLine2(String line2) { | ||
this.line2 = line2; | ||
} | ||
|
||
@Size(max = 3) | ||
@NotNull | ||
public String getState() { | ||
return state; | ||
} | ||
|
||
public void setState(String state) { | ||
this.state = state; | ||
} | ||
|
||
@Size(max = 5) | ||
@Pattern(regexp = "[0-9]+") | ||
@NotNull | ||
public String getZip() { | ||
return zip; | ||
} | ||
|
||
public void setZip(String zip) { | ||
this.zip = zip; | ||
} | ||
|
||
@AssertTrue | ||
@Transient | ||
public boolean isValid() { | ||
return true; | ||
} | ||
|
||
@AssertTrue | ||
@Transient | ||
private boolean isInternalValid() { | ||
return internalValid; | ||
} | ||
|
||
public void setInternalValid(boolean internalValid) { | ||
this.internalValid = internalValid; | ||
} | ||
|
||
@Id | ||
@Min(1) | ||
@Max(2000) | ||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...rnate-core/src/test/java/org/hibernate/orm/test/annotations/beanvalidation/CupHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.annotations.beanvalidation; | ||
import java.math.BigDecimal; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.NotNull; | ||
|
||
/** | ||
* @author Emmanuel Bernard | ||
*/ | ||
@Entity | ||
public class CupHolder { | ||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
private BigDecimal radius; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
@Max(value = 10, message = "Radius way out") | ||
@NotNull(groups = Strict.class) | ||
public BigDecimal getRadius() { | ||
return radius; | ||
} | ||
|
||
public void setRadius(BigDecimal radius) { | ||
this.radius = radius; | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
...c/test/java/org/hibernate/orm/test/annotations/beanvalidation/DDLWithoutCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* 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.annotations.beanvalidation; | ||
|
||
import java.math.BigDecimal; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.PersistenceException; | ||
import javax.validation.ConstraintViolationException; | ||
|
||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.PersistentClass; | ||
|
||
import org.hibernate.testing.DialectChecks; | ||
import org.hibernate.testing.RequiresDialectFeature; | ||
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author Vladimir Klyushnikov | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class DDLWithoutCallbackTest extends SessionFactoryBasedFunctionalTest { | ||
|
||
@Override | ||
protected Class[] getAnnotatedClasses() { | ||
return new Class[] { | ||
Address.class, | ||
CupHolder.class, | ||
MinMax.class, | ||
RangeEntity.class | ||
}; | ||
} | ||
|
||
@Override | ||
protected void applySettings(StandardServiceRegistryBuilder builder) { | ||
builder.applySetting( "javax.persistence.validation.mode", "ddl" ); | ||
} | ||
|
||
protected boolean exportSchema() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected boolean isCleanupTestDataRequired() { | ||
return true; | ||
} | ||
|
||
@Test | ||
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class) | ||
public void testListeners() { | ||
CupHolder ch = new CupHolder(); | ||
ch.setRadius( new BigDecimal( "12" ) ); | ||
assertDatabaseConstraintViolationThrown( ch ); | ||
} | ||
|
||
@Test | ||
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class) | ||
public void testMinAndMaxChecksGetApplied() { | ||
MinMax minMax = new MinMax( 1 ); | ||
assertDatabaseConstraintViolationThrown( minMax ); | ||
|
||
minMax = new MinMax( 11 ); | ||
assertDatabaseConstraintViolationThrown( minMax ); | ||
|
||
final MinMax validMinMax = new MinMax( 5 ); | ||
|
||
doInHibernate( this::sessionFactory, session -> { | ||
session.persist( validMinMax ); | ||
} ); | ||
} | ||
|
||
@Test | ||
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class) | ||
public void testRangeChecksGetApplied() { | ||
RangeEntity range = new RangeEntity( 1 ); | ||
assertDatabaseConstraintViolationThrown( range ); | ||
|
||
range = new RangeEntity( 11 ); | ||
assertDatabaseConstraintViolationThrown( range ); | ||
|
||
RangeEntity validRange = new RangeEntity( 5 ); | ||
|
||
doInHibernate( this::sessionFactory, session -> { | ||
session.persist( validRange ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testDDLEnabled() { | ||
PersistentClass classMapping = getMetadata().getEntityBinding( Address.class.getName() ); | ||
Column countryColumn = (Column) classMapping.getProperty( "country" ).getMappedColumns().get( 0 ); | ||
assertFalse( "DDL constraints are not applied", countryColumn.isNullable() ); | ||
} | ||
|
||
private void assertDatabaseConstraintViolationThrown(Object o) { | ||
doInHibernate( this::sessionFactory, session -> { | ||
try { | ||
session.persist( o ); | ||
session.flush(); | ||
fail( "expecting SQL constraint violation" ); | ||
} | ||
catch (PersistenceException pe) { | ||
final Throwable cause = pe.getCause(); | ||
if ( cause instanceof ConstraintViolationException ) { | ||
fail( "invalid object should not be validated" ); | ||
} | ||
else if ( cause instanceof org.hibernate.exception.ConstraintViolationException ) { | ||
if ( Dialect.getDialect().supportsColumnCheck() ) { | ||
// expected | ||
} | ||
else { | ||
org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause; | ||
fail( "Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException() ); | ||
} | ||
} | ||
} | ||
} ); | ||
} | ||
|
||
@Entity(name = "RangeEntity") | ||
public static class RangeEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@org.hibernate.validator.constraints.Range(min = 2, max = 10) | ||
private Integer rangeProperty; | ||
|
||
private RangeEntity() { | ||
} | ||
|
||
public RangeEntity(Integer value) { | ||
this.rangeProperty = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
hibernate-core/src/test/java/org/hibernate/orm/test/annotations/beanvalidation/Strict.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* 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.annotations.beanvalidation; | ||
|
||
|
||
/** | ||
* @author Emmanuel Bernard | ||
*/ | ||
public interface Strict { | ||
} |
Oops, something went wrong.