HHH-4881: Added test for Path.type()

This commit is contained in:
James Gilbertson 2011-10-08 00:25:46 -06:00 committed by Strong Liu
parent 972aa060a6
commit e05b3a6c38
4 changed files with 163 additions and 1 deletions

View File

@ -28,15 +28,58 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.ejb.metamodel.Order;
import org.hibernate.ejb.metamodel.Thing;
import org.hibernate.ejb.metamodel.ThingWithQuantity;
import static org.junit.Assert.assertEquals;
/**
* @author Michael Rudolf
* @author James Gilbertson
*/
public class AbstractPathImplTest extends AbstractMetamodelSpecificTest {
@Before
public void prepareTestData() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Thing thing = new Thing();
thing.setId( "thing1" );
thing.setName( "A Thing" );
em.persist( thing );
thing = new Thing();
thing.setId( "thing2" );
thing.setName( "Another Thing" );
em.persist( thing );
ThingWithQuantity thingWithQuantity = new ThingWithQuantity();
thingWithQuantity.setId( "thingWithQuantity3" );
thingWithQuantity.setName( "3 Things" );
thingWithQuantity.setQuantity( 3 );
em.persist( thingWithQuantity );
em.getTransaction().commit();
em.close();
}
@After
public void cleanupTestData() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.remove( em.find( Thing.class, "thing1" ) );
em.remove( em.find( Thing.class, "thing2" ) );
em.remove( em.find( ThingWithQuantity.class, "thingWithQuantity3" ) );
em.getTransaction().commit();
em.close();
}
@Test(expected = IllegalArgumentException.class)
public void testGetNonExistingAttributeViaName() {
EntityManager em = getOrCreateEntityManager();
@ -50,4 +93,23 @@ public class AbstractPathImplTest extends AbstractMetamodelSpecificTest {
em.close();
}
}
@Test
public void testTypeExpression() {
EntityManager em = getOrCreateEntityManager();
try {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Thing> criteria = criteriaBuilder.createQuery( Thing.class );
Root<Thing> thingRoot = criteria.from( Thing.class );
criteria.select( thingRoot );
assertEquals( em.createQuery( criteria ).getResultList().size(), 3);
criteria.where( criteriaBuilder.equal( thingRoot.type(), criteriaBuilder.literal( Thing.class ) ) );
assertEquals( em.createQuery( criteria ).getResultList().size(), 2 );
}
finally {
em.close();
}
}
}

View File

@ -33,7 +33,8 @@ public abstract class AbstractMetamodelSpecificTest extends BaseEntityManagerFun
return new Class[] {
Address.class, Alias.class, Country.class, CreditCard.class, Customer.class,
Info.class, LineItem.class, Order.class, Phone.class, Product.class,
ShelfLife.class, Spouse.class, VersionedEntity.class
ShelfLife.class, Spouse.class, Thing.class, ThingWithQuantity.class,
VersionedEntity.class
};
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.metamodel;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* @author James Gilbertson
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Thing {
private String id;
private String name;
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,43 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.metamodel;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author James Gilbertson
*/
@Entity
public class ThingWithQuantity extends Thing {
private int quantity;
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}