mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
extended test cases
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
|
||||
package com.avaje.tests.family;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import com.avaje.tests.lib.EbeanTestCase;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import com.avaje.tests.model.family.ChildPerson;
|
||||
@@ -15,19 +20,29 @@ import com.avaje.tests.model.family.ParentPerson;
|
||||
|
||||
|
||||
|
||||
public class TestInheritance extends EbeanTestCase {
|
||||
public class TestInheritance extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void testDescriptor() {
|
||||
BeanDescriptor<GrandParentPerson> desc = spiEbeanServer().getBeanDescriptor(GrandParentPerson.class);
|
||||
BeanProperty prop1 = desc.getBeanProperty("someBean");
|
||||
BeanProperty prop2 = desc.getBeanProperty("effectiveBean");
|
||||
|
||||
assertEquals(BeanPropertyAssocOne.class, prop1.getClass());
|
||||
assertEquals(BeanPropertyAssocOne.class, prop2.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInheritance() {
|
||||
// create some other beans
|
||||
EBasic someBean1 = new EBasic();
|
||||
someBean1.setName("A Bean");
|
||||
getServer().save(someBean1);
|
||||
server().save(someBean1);
|
||||
|
||||
EBasic someBean2 = new EBasic();
|
||||
someBean2.setName("An other Bean");
|
||||
getServer().save(someBean2);
|
||||
server().save(someBean2);
|
||||
|
||||
ChildPerson child1 = new ChildPerson();
|
||||
child1.setAge(13);
|
||||
@@ -71,12 +86,21 @@ public class TestInheritance extends EbeanTestCase {
|
||||
grandparent1.getChildren().add(parent1);
|
||||
grandparent1.getChildren().add(parent2);
|
||||
grandparent1.getChildren().add(parent3);
|
||||
|
||||
getServer().save(grandparent1);
|
||||
// Test setup complete, so retrieve bean from db
|
||||
grandparent1 = getServer().find(GrandParentPerson.class).setId(grandparent1.getIdentifier()).findUnique();
|
||||
|
||||
|
||||
server().save(grandparent1);
|
||||
// Test setup complete, so retrieve bean from db
|
||||
//grandparent1 = server().find(GrandParentPerson.class).setId(grandparent1.getIdentifier()).where()
|
||||
// .in("effectiveBean.id",2,null) // geht nicht!
|
||||
// .findUnique();
|
||||
grandparent1 = server().find(GrandParentPerson.class).setId(grandparent1.getIdentifier()).where()
|
||||
.or()
|
||||
.in("effectiveBean.id",2)
|
||||
.isNull("effectiveBean.id")
|
||||
.endOr()
|
||||
.findUnique();
|
||||
assertNotNull(grandparent1);
|
||||
|
||||
// check if aggregation works
|
||||
assertEquals(3, grandparent1.getChildCount().intValue());
|
||||
assertEquals(150, grandparent1.getTotalAge().intValue());
|
||||
@@ -85,7 +109,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Josef", grandparent1.getName());
|
||||
assertEquals("Foo", grandparent1.getFamilyName());
|
||||
assertEquals("Munich", grandparent1.getAddress());
|
||||
assertNull(grandparent1.getEffectiveBeanId());
|
||||
// assertEquals(2, grandparent1.getEffectiveBean().getId().intValue());
|
||||
|
||||
// now check children of grandparent
|
||||
parent1 = grandparent1.getChildren().get(0);
|
||||
@@ -103,7 +127,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Munich", parent1.getEffectiveAddress()); // -> inherit munich
|
||||
assertEquals(2, parent1.getChildCount().intValue());
|
||||
assertEquals(21, parent1.getTotalAge().intValue());
|
||||
assertEquals(1, parent1.getEffectiveBeanId().intValue());
|
||||
assertEquals(1, parent1.getEffectiveBean().getId().intValue());
|
||||
|
||||
// parent2
|
||||
assertEquals("Sandra", parent2.getName());
|
||||
@@ -113,7 +137,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Berlin", parent2.getEffectiveAddress());
|
||||
assertEquals(1, parent2.getChildCount().intValue());
|
||||
assertEquals(36, parent2.getTotalAge().intValue());
|
||||
assertNull(parent2.getEffectiveBeanId());
|
||||
assertNull(parent2.getEffectiveBean());
|
||||
|
||||
// parent3
|
||||
assertEquals("Michael", parent3.getName());
|
||||
@@ -123,7 +147,7 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Munich", parent3.getEffectiveAddress());
|
||||
assertEquals(0, parent3.getChildCount().intValue());
|
||||
assertEquals(0, parent3.getTotalAge().intValue());
|
||||
assertNull(parent3.getEffectiveBeanId());
|
||||
assertNull(parent3.getEffectiveBean());
|
||||
|
||||
child1 = parent1.getChildren().get(0);
|
||||
child2 = parent1.getChildren().get(1);
|
||||
@@ -137,16 +161,16 @@ public class TestInheritance extends EbeanTestCase {
|
||||
assertEquals("Bar", child1.getEffectiveFamilyName());
|
||||
assertEquals("Munich", child1.getEffectiveAddress());
|
||||
assertEquals(2, child1.getSomeBean().getId().intValue());
|
||||
assertEquals(2, child1.getEffectiveBeanId().intValue());
|
||||
assertEquals(2, child1.getEffectiveBean().getId().intValue());
|
||||
|
||||
assertEquals("Baz", child2.getEffectiveFamilyName());
|
||||
assertEquals("Munich", child2.getEffectiveAddress());
|
||||
assertNull(child2.getSomeBean());
|
||||
assertEquals(1, child2.getEffectiveBeanId().intValue());
|
||||
assertEquals(1, child2.getEffectiveBean().getId().intValue());
|
||||
|
||||
assertEquals("Foo", child3.getEffectiveFamilyName());
|
||||
assertEquals("Berlin", child3.getEffectiveAddress());
|
||||
assertNull(child3.getEffectiveBeanId());
|
||||
assertNull(child3.getEffectiveBean());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import com.avaje.ebean.annotation.Formula;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
|
||||
@Entity
|
||||
public class ChildPerson extends InheritablePerson {
|
||||
@@ -29,7 +30,8 @@ public class ChildPerson extends InheritablePerson {
|
||||
private String effectiveAddress;
|
||||
|
||||
@Formula(select = "coalesce(${ta}.some_bean_id, j1.some_bean_id, j2.some_bean_id)")
|
||||
private Integer effectiveBeanId;
|
||||
@ManyToOne
|
||||
private EBasic effectiveBean;
|
||||
|
||||
public ParentPerson getParent() {
|
||||
return parent;
|
||||
@@ -63,9 +65,8 @@ public class ChildPerson extends InheritablePerson {
|
||||
return effectiveAddress;
|
||||
}
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
public EBasic getEffectiveBean() {
|
||||
return effectiveBean;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,11 @@ import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import com.avaje.ebean.annotation.Formula;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
|
||||
@Entity
|
||||
public class GrandParentPerson extends InheritablePerson {
|
||||
@@ -34,8 +36,9 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
private String address;
|
||||
|
||||
|
||||
@Formula(select = "${ta}.some_bean_id")
|
||||
private Integer effectiveBeanId;
|
||||
@Formula(select = "${ta}.some_bean_id+1")
|
||||
@ManyToOne
|
||||
private EBasic effectiveBean;
|
||||
|
||||
public List<ParentPerson> getChildren() {
|
||||
return children;
|
||||
@@ -70,7 +73,7 @@ public class GrandParentPerson extends InheritablePerson {
|
||||
return childCount;
|
||||
}
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
public EBasic getEffectiveBean() {
|
||||
return effectiveBean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import com.avaje.ebean.annotation.Formula;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
|
||||
|
||||
@Entity
|
||||
@@ -49,7 +50,8 @@ public class ParentPerson extends InheritablePerson {
|
||||
private String effectiveAddress;
|
||||
|
||||
@Formula(select = "coalesce(${ta}.some_bean_id, j1.some_bean_id)")
|
||||
private Integer effectiveBeanId;
|
||||
@ManyToOne
|
||||
private EBasic effectiveBean;
|
||||
|
||||
public GrandParentPerson getParent() {
|
||||
return parent;
|
||||
@@ -95,8 +97,8 @@ public class ParentPerson extends InheritablePerson {
|
||||
return effectiveAddress;
|
||||
}
|
||||
|
||||
public Integer getEffectiveBeanId() {
|
||||
return effectiveBeanId;
|
||||
public EBasic getEffectiveBean() {
|
||||
return effectiveBean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user