#671 - ENH: Add support for @View and extra-ddl.xml to nicely support entity beans based on views

This commit is contained in:
Robin Bygrave
2016-04-28 20:19:05 +12:00
parent 69c2810fbe
commit 81531b6cda
48 changed files with 1187 additions and 255 deletions
@@ -12,6 +12,8 @@ public class EbeanServerFactory_ServerConfigStart_Test {
@Test
public void test() throws InterruptedException {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2");
config.loadFromProperties();
@@ -2,11 +2,85 @@ package com.avaje.ebean.dbmigration.model;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class MigrationVersionTest {
@Test
public void sort() {
List<MigrationVersion> list= new ArrayList<MigrationVersion>();
list.add(MigrationVersion.parse("1.1__point"));
list.add(MigrationVersion.parse("3.0__three"));
list.add(MigrationVersion.parse("1.0__init"));
list.add(MigrationVersion.parse("R__beta"));
list.add(MigrationVersion.parse("R__alpha"));
Collections.sort(list);
assertThat(list.get(0).getComment()).isEqualTo("init");
assertThat(list.get(1).getComment()).isEqualTo("point");
assertThat(list.get(2).getComment()).isEqualTo("three");
assertThat(list.get(3).getComment()).isEqualTo("alpha");
assertThat(list.get(4).getComment()).isEqualTo("beta");
}
@Test
public void test_parse_when_repeatable() throws Exception {
MigrationVersion version = MigrationVersion.parse("R__Foo");
assertThat(version.getComment()).isEqualTo("Foo");
assertThat(version.normalised()).isEqualTo("R");
}
@Test
public void test_parse_when_repeatable_case() throws Exception {
MigrationVersion version = MigrationVersion.parse("r__Foo");
assertThat(version.isRepeatable()).isTrue();
assertThat(version.getComment()).isEqualTo("Foo");
assertThat(version.normalised()).isEqualTo("R");
assertThat(version.normalised()).isEqualTo("R");
}
@Test
public void test_parse_when_v_prefix() throws Exception {
MigrationVersion version = MigrationVersion.parse("v1_0__Foo");
assertThat(version.isRepeatable()).isFalse();
assertThat(version.getComment()).isEqualTo("Foo");
assertThat(version.normalised()).isEqualTo("1.0");
assertThat(version.asString()).isEqualTo("1_0");
assertThat(version.getRaw()).isEqualTo("1_0__Foo");
}
@Test
public void repeatable_compareTo() throws Exception {
MigrationVersion foo = MigrationVersion.parse("R__Foo");
MigrationVersion bar = MigrationVersion.parse("R__Bar");
assertThat(foo.compareTo(bar)).isGreaterThan(0);
assertThat(bar.compareTo(foo)).isLessThan(0);
MigrationVersion bar2 = MigrationVersion.parse("R__Bar");
assertThat(bar.compareTo(bar2)).isEqualTo(0);
}
@Test
public void repeatable_compareTo_when_caseDifferent() throws Exception {
MigrationVersion none = MigrationVersion.parse("R__");
MigrationVersion bar = MigrationVersion.parse("R__Bar");
MigrationVersion bar2 = MigrationVersion.parse("R__bar");
assertThat(none.compareTo(bar)).isLessThan(0);
assertThat(bar.compareTo(bar2)).isLessThan(0);
assertThat(none.compareTo(bar2)).isLessThan(0);
}
@Test
public void test_parse_getComment() throws Exception {
@@ -51,10 +125,14 @@ public class MigrationVersionTest {
MigrationVersion v0 = MigrationVersion.parse("1.1.1_2__Foo");
MigrationVersion v1 = MigrationVersion.parse("1.1.1.2_junk");
MigrationVersion v2 = MigrationVersion.parse("1.1_1.2_foo");
MigrationVersion v3 = MigrationVersion.parse("1.1_1.2__foo");
assertThat(v0.compareTo(v1)).isEqualTo(0);
assertThat(v1.compareTo(v0)).isEqualTo(0);
assertThat(v0.compareTo(v1)).isGreaterThan(0);
assertThat(v1.compareTo(v0)).isLessThan(0);
assertThat(v1.compareTo(v2)).isEqualTo(0);
assertThat(v0.compareTo(v3)).isLessThan(0);
assertThat(v3.compareTo(v0)).isGreaterThan(0);
}
@Test
@@ -24,6 +24,9 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ModelBuild_compoundKeyTest extends BaseTestCase {
private SpiEbeanServer getServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2");
config.loadFromProperties();
@@ -38,7 +41,6 @@ public class ModelBuild_compoundKeyTest extends BaseTestCase {
config.addClass(CKeyAssoc.class);
config.addClass(CKeyParentId.class);
return (SpiEbeanServer) EbeanServerFactory.create(config);
}
@@ -18,6 +18,9 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ModelBuild_explicitSequencesTest extends BaseTestCase {
private SpiEbeanServer getServer(boolean postgres) {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2");
config.loadFromProperties();
@@ -20,6 +20,8 @@ public class BeanFindControllerTest extends BaseTestCase {
@Test
public void test() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2otherfind");
@@ -71,6 +71,7 @@ public class BeanPersistControllerTest {
private EbeanServer getEbeanServer(PersistAdapter persistAdapter) {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2ebasicver");
@@ -47,6 +47,8 @@ public class BeanPostLoadTest extends BaseTestCase {
private EbeanServer getEbeanServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2ebasicver");
@@ -0,0 +1,47 @@
package com.avaje.ebeaninternal.extraddl.model;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
public class ExtraDdlXmlReaderTest {
@Test
public void read() throws Exception {
ExtraDdl read = ExtraDdlXmlReader.read("/extra-ddl.xml");
assertNotNull(read);
}
@Test
public void buildExtra_when_h2() {
String ddl = ExtraDdlXmlReader.buildExtra("h2");
assertThat(ddl).contains("create or replace view order_agg_vw");
assertThat(ddl).contains("-- h2 and postgres script");
assertThat(ddl).doesNotContain(" -- oracle only script");
}
@Test
public void buildExtra_when_oracle() {
String ddl = ExtraDdlXmlReader.buildExtra("oracle");
assertThat(ddl).contains("create or replace view order_agg_vw");
assertThat(ddl).doesNotContain("-- h2 and postgres script");
assertThat(ddl).contains(" -- oracle only script");
}
@Test
public void buildExtra_when_mysql() {
String ddl = ExtraDdlXmlReader.buildExtra("mysql");
assertThat(ddl).contains("create or replace view order_agg_vw");
assertThat(ddl).doesNotContain("-- h2 and postgres script");
assertThat(ddl).doesNotContain(" -- oracle only script");
}
}
@@ -19,6 +19,8 @@ public class BeanDescriptor_registerTest {
@Test
public void testRegisterDeregister() throws Exception {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2other");
@@ -37,6 +37,8 @@ public class MainDbBoolean {
*/
private EbeanServer createOracleEbeanServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig c = new ServerConfig();
c.setName("ora");
@@ -71,6 +73,8 @@ public class MainDbBoolean {
private EbeanServer createEbeanServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig c = new ServerConfig();
c.setName("pgtest");
@@ -57,6 +57,8 @@ public class TestChangeLog extends BaseTestCase {
private SpiEbeanServer getServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2other");
config.loadFromProperties();
@@ -1,30 +0,0 @@
package com.avaje.tests.compositekeys;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.tests.model.composite.RCustomer;
import com.avaje.tests.model.composite.RCustomerKey;
import com.avaje.tests.model.composite.ROrder;
import com.avaje.tests.model.composite.ROrderPK;
public class CreateIdExpandedFormServer {
public static EbeanServer create() {
ServerConfig config = new ServerConfig();
config.setName("h2");
config.loadFromProperties();
config.setName("modifiedH2");
config.addClass(ROrder.class);
config.addClass(ROrderPK.class);
config.addClass(RCustomer.class);
config.addClass(RCustomerKey.class);
config.setDatabasePlatform(new ModifiedH2Platform());
return EbeanServerFactory.create(config);
}
}
@@ -0,0 +1,58 @@
package com.avaje.tests.model.view;
import com.avaje.ebean.annotation.View;
import com.avaje.tests.model.basic.Order;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
@View(name = "order_agg_vw", dependentTables = {"o_order", "o_order_detail"})
public class EOrderAgg {
@Id @Column(name = "order_id")
Long id;
@OneToOne
@JoinColumn(name = "order_id")
Order order;
Double orderTotal;
Double shipTotal;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Double getOrderTotal() {
return orderTotal;
}
public void setOrderTotal(Double orderTotal) {
this.orderTotal = orderTotal;
}
public Double getShipTotal() {
return shipTotal;
}
public void setShipTotal(Double shipTotal) {
this.shipTotal = shipTotal;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
@@ -0,0 +1,69 @@
package com.avaje.tests.model.view;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.Query;
import com.avaje.tests.model.basic.Order;
import com.avaje.tests.model.basic.OrderDetail;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestViewBaseEntity {
@Test
public void fetch() {
ResetBasicData.reset();
Query<EOrderAgg> query = Ebean.find(EOrderAgg.class)
.where().gt("orderTotal", 20)
.query();
List<EOrderAgg> list = query.findList();
assertThat(query.getGeneratedSql()).contains("select t0.order_id c0, t0.order_total c1, t0.ship_total c2, t0.order_id c3 from order_agg_vw t0 where t0.order_total > ? ");
assertThat(list).isNotEmpty();
}
@Test
public void lazyLoad() {
ResetBasicData.reset();
Query<EOrderAgg> query = Ebean.find(EOrderAgg.class)
//.fetch("order", "id")
.where().gt("orderTotal", 20)
.query();
List<EOrderAgg> list = query.findList();
for (EOrderAgg agg : list) {
Order order = agg.getOrder();
List<OrderDetail> details = order.getDetails();
assertThat(details).isNotEmpty();
}
}
@Test
public void fetchJoin() {
ResetBasicData.reset();
Query<EOrderAgg> query = Ebean.find(EOrderAgg.class)
.fetch("order")
.fetch("order.details")
.where().gt("orderTotal", 20)
.query();
List<EOrderAgg> list = query.findList();
for (EOrderAgg agg : list) {
Order order = agg.getOrder();
List<OrderDetail> details = order.getDetails();
assertThat(details).isNotEmpty();
}
assertThat(query.getGeneratedSql()).contains("from order_agg_vw t0 left outer join o_order t1 on t1.id = t0.order_id left outer join o_customer t3 on t3.id = t1.kcustomer_id left outer join o_order_detail t2 on t2.order_id = t1.id where t2.id > 0 and t0.order_total > ?");
}
}
@@ -32,6 +32,8 @@ public class TestPersistenceContextServerConfig extends BaseTestCase {
static EbeanServer create() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("withPCQuery");
@@ -306,6 +306,8 @@ public class TestReadAudit extends BaseTestCase {
private SpiEbeanServer getServer() {
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2other");
config.loadFromProperties();
@@ -39,6 +39,8 @@ public class TestAutoCommitDataSource extends BaseTestCase {
assertTrue(connection.getAutoCommit());
connection.close();
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2autocommit");
config.loadFromProperties();
@@ -39,6 +39,8 @@ public class TestExplicitTransactionMode extends BaseTestCase {
assertTrue(connection.getAutoCommit());
connection.close();
System.setProperty("ebean.ignoreExtraDdl", "true");
ServerConfig config = new ServerConfig();
config.setName("h2autocommit");
config.loadFromProperties();