mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#593 - Refactor SPI - Move methods from ElPropertyValue to ExpressionPath
This commit is contained in:
@@ -3,17 +3,24 @@ package com.avaje.ebean.plugin;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.FetchPath;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import com.avaje.tests.inheritance.Stockforecast;
|
||||
import com.avaje.tests.model.basic.Car;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.Person;
|
||||
import com.avaje.tests.model.basic.Product;
|
||||
import com.avaje.tests.model.basic.Vehicle;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class BeanTypeTest {
|
||||
@@ -189,4 +196,48 @@ public class BeanTypeTest {
|
||||
public void docStoreUpdateEmbedded() throws Exception {
|
||||
beanType(Order.class).docStore().updateEmbedded(1, "customer", "someJson", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasInheritance_when_not() {
|
||||
assertFalse(beanType(Order.class).hasInheritance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasInheritance_when_root() {
|
||||
assertTrue(beanType(Vehicle.class).hasInheritance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasInheritance_when_leaf() {
|
||||
assertTrue(beanType(Car.class).hasInheritance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDiscColumn_when_default() {
|
||||
assertEquals(beanType(Car.class).getDiscColumn(),"dtype");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDiscColumn_when_set() {
|
||||
assertEquals(beanType(Stockforecast.class).getDiscColumn(),"type");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createBeanUsingDisc_when_set() {
|
||||
Vehicle vehicle = beanType(Vehicle.class).createBeanUsingDisc("C");
|
||||
assertTrue(vehicle instanceof Car);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInheritanceWhere_when_leaf() {
|
||||
Query<Vehicle> query = server.find(Vehicle.class);
|
||||
beanType(Car.class).addInheritanceWhere((SpiQuery<?>)query);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addInheritanceWhere_when_root() {
|
||||
Query<Vehicle> query = server.find(Vehicle.class);
|
||||
beanType(Vehicle.class).addInheritanceWhere((SpiQuery<?>)query);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class ExpressionPathTest {
|
||||
|
||||
BeanType<Order> beanType = beanType(Order.class);
|
||||
Order order = new Order();
|
||||
beanType.getExpressionPath("id").set(order, 42);
|
||||
beanType.getExpressionPath("id").pathSet(order, 42);
|
||||
assertThat(order.getId()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class ExpressionPathTest {
|
||||
|
||||
BeanType<Order> beanType = beanType(Order.class);
|
||||
Order order = new Order();
|
||||
beanType.getExpressionPath("customer.name").set(order, "Rob");
|
||||
beanType.getExpressionPath("customer.name").pathSet(order, "Rob");
|
||||
assertThat(order.getCustomer().getName()).isEqualTo("Rob");
|
||||
}
|
||||
|
||||
|
||||
@@ -2,62 +2,67 @@ package com.avaje.tests.ddd.iud;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.plugin.BeanType;
|
||||
import com.avaje.ebean.plugin.ExpressionPath;
|
||||
import com.avaje.ebean.plugin.SpiServer;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.tests.model.ddd.DPerson;
|
||||
import com.avaje.tests.model.ivo.CMoney;
|
||||
import com.avaje.tests.model.ivo.Money;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Currency;
|
||||
|
||||
public class TestDPersonEl extends TestCase {
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public void test() throws IOException {
|
||||
public class TestDPersonEl {
|
||||
|
||||
Currency NZD = Currency.getInstance("NZD");
|
||||
|
||||
DPerson p = new DPerson();
|
||||
p.setFirstName("first");
|
||||
p.setLastName("last");
|
||||
p.setSalary(new Money("12200"));
|
||||
p.setCmoney(new CMoney(new Money("12"), NZD));
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
|
||||
BeanDescriptor<DPerson> descriptor = server.getBeanDescriptor(DPerson.class);
|
||||
|
||||
ElPropertyValue elCmoney = descriptor.getElGetValue("cmoney");
|
||||
// ElPropertyValue elCmoneyAmt = descriptor.getElGetValue("cmoney.amount");
|
||||
// ElPropertyValue elCmoneyCur = descriptor.getElGetValue("cmoney.currency");
|
||||
|
||||
JsonContext jsonContext = server.json();
|
||||
String json = jsonContext.toJson(p);
|
||||
|
||||
DPerson bean = jsonContext.toBean(DPerson.class, json);
|
||||
Assert.assertEquals("first", bean.getFirstName());
|
||||
Assert.assertEquals(new Money("12200"), bean.getSalary());
|
||||
Assert.assertEquals(new Money("12"), bean.getCmoney().getAmount());
|
||||
Assert.assertEquals(NZD, bean.getCmoney().getCurrency());
|
||||
|
||||
|
||||
EntityBean entityBean = (EntityBean)p;
|
||||
|
||||
Object cmoney = elCmoney.elGetValue(entityBean);
|
||||
// Object amt = elCmoneyAmt.elGetValue(entityBean);
|
||||
// Object cur = elCmoneyCur.elGetValue(entityBean);
|
||||
|
||||
Assert.assertNotNull(cmoney);
|
||||
// Assert.assertEquals(new Money("12"), amt);
|
||||
// Assert.assertEquals(NZD, cur);
|
||||
|
||||
p.setCmoney(null);
|
||||
Assert.assertNull(p.getCmoney());
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
Currency NZD = Currency.getInstance("NZD");
|
||||
|
||||
DPerson p = new DPerson();
|
||||
p.setFirstName("first");
|
||||
p.setLastName("last");
|
||||
p.setSalary(new Money("12200"));
|
||||
p.setCmoney(new CMoney(new Money("12"), NZD));
|
||||
|
||||
SpiServer server = Ebean.getDefaultServer().getPluginApi();
|
||||
|
||||
BeanType<DPerson> descriptor = server.getBeanType(DPerson.class);
|
||||
|
||||
|
||||
JsonContext jsonContext = server.json();
|
||||
String json = jsonContext.toJson(p);
|
||||
|
||||
DPerson bean = jsonContext.toBean(DPerson.class, json);
|
||||
assertEquals("first", bean.getFirstName());
|
||||
assertEquals(new Money("12200"), bean.getSalary());
|
||||
assertEquals(new Money("12"), bean.getCmoney().getAmount());
|
||||
assertEquals(NZD, bean.getCmoney().getCurrency());
|
||||
|
||||
|
||||
EntityBean entityBean = (EntityBean) p;
|
||||
|
||||
ExpressionPath elCmoney = descriptor.getExpressionPath("cmoney");
|
||||
ExpressionPath elCmoneyAmt = descriptor.getExpressionPath("cmoney.amount");
|
||||
ExpressionPath elCmoneyCur = descriptor.getExpressionPath("cmoney.currency");
|
||||
|
||||
Object cmoney = elCmoney.pathGet(entityBean);
|
||||
Object amt = elCmoneyAmt.pathGet(entityBean);
|
||||
Object cur = elCmoneyCur.pathGet(entityBean);
|
||||
|
||||
assertNotNull(cmoney);
|
||||
assertEquals(new Money("12"), amt);
|
||||
assertEquals(NZD, cur);
|
||||
|
||||
p.setCmoney(null);
|
||||
assertNull(p.getCmoney());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.avaje.tests.el;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
|
||||
public class TestElGetReference extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
Address a = new Address();
|
||||
a.setId((short)12);
|
||||
a.setLine1("line1");
|
||||
a.setCity("Auckland");
|
||||
|
||||
Customer c0 = new Customer();
|
||||
c0.setBillingAddress(a);
|
||||
|
||||
Customer c1 = new Customer();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
BeanDescriptor<Customer> descriptor = server.getBeanDescriptor(Customer.class);
|
||||
|
||||
ElPropertyValue elProp = descriptor.getElGetValue("billingAddress.id");
|
||||
ElPropertyValue addrLine1Prop = descriptor.getElGetValue("billingAddress.line1");
|
||||
ElPropertyValue addrCityProp = descriptor.getElGetValue("billingAddress.city");
|
||||
|
||||
elProp.elGetReference((EntityBean)c0);
|
||||
elProp.elGetReference((EntityBean)c1);
|
||||
|
||||
addrLine1Prop.elSetValue((EntityBean)c1, "12 someplace", true);
|
||||
addrCityProp.elSetValue((EntityBean)c1, "Auckland", true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.avaje.tests.el;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.plugin.BeanType;
|
||||
import com.avaje.ebean.plugin.ExpressionPath;
|
||||
import com.avaje.ebean.plugin.SpiServer;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Country;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestPathExpression {
|
||||
|
||||
final BeanType<Customer> beanType;
|
||||
final ExpressionPath billingId;
|
||||
final ExpressionPath line1;
|
||||
final ExpressionPath city;
|
||||
|
||||
public TestPathExpression() {
|
||||
SpiServer server = Ebean.getDefaultServer().getPluginApi();
|
||||
beanType = server.getBeanType(Customer.class);
|
||||
billingId = beanType.getExpressionPath("billingAddress.id");
|
||||
line1 = beanType.getExpressionPath("billingAddress.line1");
|
||||
city = beanType.getExpressionPath("billingAddress.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSet() {
|
||||
|
||||
Customer c1 = new Customer();
|
||||
line1.pathSet(c1, "12 someplace");
|
||||
city.pathSet(c1, "Auckland");
|
||||
billingId.pathSet(c1, 4);
|
||||
|
||||
beanType.getExpressionPath("id").pathSet(c1, 42L);
|
||||
beanType.getExpressionPath("name").pathSet(c1, "jimmy");
|
||||
beanType.getExpressionPath("status").pathSet(c1, "ACTIVE");
|
||||
beanType.getExpressionPath("billingAddress.country.code").pathSet(c1, "NZ");
|
||||
beanType.getExpressionPath("billingAddress.country.name").pathSet(c1, "New Zealand");
|
||||
|
||||
|
||||
assertEquals(c1.getId(), Integer.valueOf(42));
|
||||
assertEquals(c1.getName(), "jimmy");
|
||||
assertEquals(c1.getStatus(), Customer.Status.ACTIVE);
|
||||
assertEquals(c1.getBillingAddress().getLine1(), "12 someplace");
|
||||
assertEquals(c1.getBillingAddress().getCity(), "Auckland");
|
||||
assertEquals(c1.getBillingAddress().getId(), Short.valueOf("4"));
|
||||
|
||||
assertEquals(c1.getBillingAddress().getCountry().getCode(), "NZ");
|
||||
assertEquals(c1.getBillingAddress().getCountry().getName(), "New Zealand");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathGet() {
|
||||
|
||||
Address billingAddress = new Address();
|
||||
billingAddress.setId((short)12);
|
||||
billingAddress.setLine1("line1");
|
||||
billingAddress.setCity("Auckland");
|
||||
|
||||
Country nz = new Country();
|
||||
nz.setCode("NZ");
|
||||
nz.setName("New Zealand");
|
||||
billingAddress.setCountry(nz);
|
||||
|
||||
Customer c0 = new Customer();
|
||||
c0.setBillingAddress(billingAddress);
|
||||
|
||||
EntityBean e0 = (EntityBean)c0;
|
||||
assertEquals(line1.pathGet(e0), "line1");
|
||||
assertEquals(city.pathGet(e0), "Auckland");
|
||||
assertEquals(billingId.pathGet(e0), Short.valueOf("12"));
|
||||
|
||||
|
||||
assertEquals(beanType.getExpressionPath("billingAddress.country.code").pathGet(e0), "NZ");
|
||||
assertEquals(beanType.getExpressionPath("billingAddress.country.name").pathGet(e0), "New Zealand");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user