HHH-10577 - Added additional test cases for VALUE()

This commit is contained in:
Chris Cranford 2017-02-01 11:00:56 -05:00
parent 18e8be0037
commit fa3171530c
1 changed files with 52 additions and 10 deletions

View File

@ -12,16 +12,15 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.query.Query;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -31,6 +30,7 @@ import static org.hamcrest.core.Is.is;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Andrea Boriero
@ -38,6 +38,7 @@ import static org.junit.Assert.assertEquals;
*/
public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestCase {
private final KeyValue keyValue = new KeyValue( "key1" );
private final EmbeddableValue embeddableValue = new EmbeddableValue( 3 );
@Override
protected Class<?>[] getAnnotatedClasses() {
@ -53,8 +54,6 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
s.save( keyValue );
TestEntity testEntity = new TestEntity();
EmbeddableValue embeddableValue = new EmbeddableValue();
embeddableValue.value = 3;
Map<KeyValue, EmbeddableValue> map = new HashMap<>();
map.put( keyValue, embeddableValue );
testEntity.values = map;
@ -63,10 +62,8 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
KeyValue keyValue2 = new KeyValue( "key2" );
s.save( keyValue2 );
TestEntity testEntity2 = new TestEntity();
EmbeddableValue embeddableValue2 = new EmbeddableValue();
embeddableValue2.value = 3;
Map<KeyValue, EmbeddableValue> map2 = new HashMap<>();
map.put( keyValue2, embeddableValue2 );
map.put( keyValue2, embeddableValue );
testEntity2.values = map2;
s.save( testEntity2 );
} );
@ -76,8 +73,7 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
public void testMapKeyExpressionInWhere() {
doInHibernate( this::sessionFactory, s -> {
// JPA form
Query query = s.createQuery(
"select te from TestEntity te join te.values v where ? in (key(v)) " );
Query query = s.createQuery( "select te from TestEntity te join te.values v where ? in (key(v)) " );
query.setParameter( 0, keyValue );
assertThat( query.list().size(), is( 1 ) );
@ -87,6 +83,44 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
query.setParameter( 0, keyValue );
assertThat( query.list().size(), is( 1 ) );
// Test key property dereference
query = s.createQuery( "select te from TestEntity te join te.values v where key(v).name in :names" );
query.setParameterList( "names", Arrays.asList( keyValue.name ) );
assertThat( query.list().size(), is( 1 ) );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-10577")
public void testMapValueExpressionInWhere() {
doInHibernate( this::sessionFactory, s -> {
// JPA form
try {
Query query = s.createQuery( "select te from TestEntity te join te.values v where ? in (value(v))" );
query.setParameter( 0, new EmbeddableValue( 3 ) );
assertThat( query.list().size(), is( 2 ) );
fail( "HibernateException expected - Could not determine type for EmbeddableValue" );
}
catch ( Exception e ) {
assertTyping( HibernateException.class, e );
}
// Hibernate additional form
try {
Query query = s.createQuery( "select te from TestEntity te where ? in (value(te.values))" );
query.setParameter( 0, new EmbeddableValue( 3 ) );
assertThat( query.list().size(), is( 2 ) );
fail( "HibernateException expected - Could not determine type for EmbeddableValue" );
}
catch ( Exception e ) {
assertTyping( HibernateException.class, e );
}
// Test value property dereference
Query query = s.createQuery( "select te from TestEntity te join te.values v where value(v).value in :values" );
query.setParameterList( "values", Arrays.asList( 3 ) );
assertThat( query.list().size(), is( 2 ) );
} );
}
@ -178,5 +212,13 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
@Embeddable
public static class EmbeddableValue {
Integer value;
EmbeddableValue() {
}
EmbeddableValue(Integer value) {
this.value = value;
}
}
}