#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave
2016-12-11 23:23:22 +13:00
parent 5821dc96b9
commit 971e2dc91b
2134 changed files with 10721 additions and 8652 deletions
@@ -0,0 +1,118 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class AllEqualsExpressionTest {
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", 10).isSameByPlan(exp("a", 10))).isTrue();
}
@Test
public void isSameByBind_when_sameMultiple() {
assertThat(exp("a", 10, "b", "23").isSameByBind(exp("a", 10, "b", "23"))).isTrue();
}
@Test
public void isSameByPlan_when_diffBindValue() {
assertThat(exp("a", 10).isSameByPlan(exp("a", 20))).isTrue();
}
@Test
public void isSameByPlan_when_multiple() {
assertThat(exp("a", 10, "b", 20, "c", 30).isSameByPlan(exp("a", 10, "b", 20, "c", 30))).isTrue();
}
@Test
public void isSameByPlan_when_less() {
assertThat(exp("a", 10, "b", 20, "c", 30).isSameByPlan(exp("a", 10, "b", 20))).isFalse();
}
@Test
public void isSameByPlan_when_more() {
assertThat(exp("a", 10, "b", 20).isSameByPlan(exp("a", 10, "b", 20, "c", 30))).isFalse();
}
@Test
public void isSameByPlan_when_diffProperty_diff() {
assertThat(exp("a", 10).isSameByPlan(exp("b", 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffType_diff() {
assertThat(exp("a", 10).isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByPlan_when_diffBindByNull_last() {
assertThat(exp("a", 10).isSameByPlan(exp("a", null))).isFalse();
}
@Test
public void isSameByPlan_when_diffBindByNull_first() {
assertThat(exp("a", null).isSameByPlan(exp("a", 10))).isFalse();
}
@Test
public void isSameByPlan_when_differentExpressionType() {
assertThat(exp("a", null).isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_diffBindByNull_last() {
assertThat(exp("a", 10).isSameByBind(exp("a", null))).isFalse();
}
@Test
public void isSameByBind_when_diffBindByNull_first() {
assertThat(exp("a", null).isSameByBind(exp("a", 10))).isFalse();
}
@NotNull
private AllEqualsExpression exp(Map<String, Object> propMap) {
return new AllEqualsExpression(propMap);
}
AllEqualsExpression exp(String key0, Object val1) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put(key0, val1);
return exp(map);
}
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put(key0, val1);
map.put(key2, val2);
return exp(map);
}
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2, String key3, Object val3) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put(key0, val1);
map.put(key2, val2);
map.put(key3, val3);
return exp(map);
}
}
@@ -0,0 +1,13 @@
package io.ebeaninternal.server.expression;
import io.ebean.BaseTestCase;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import org.tests.model.basic.Order;
public abstract class BaseExpressionTest extends BaseTestCase {
protected DefaultExpressionRequest newExpressionRequest() {
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
return new DefaultExpressionRequest(desc);
}
}
@@ -0,0 +1,69 @@
package io.ebeaninternal.server.expression;
import io.ebeaninternal.api.SpiExpression;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class BetweenExpressionTest extends BaseExpressionTest {
@Test
public void addSql() throws Exception {
DefaultExpressionRequest expReq = newExpressionRequest();
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
exp.addSql(expReq);
assertThat(expReq.getSql()).isEqualTo("startDate between ? and ? ");
}
@Test
public void copyForPlanKey_isSameInstance() throws Exception {
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
SpiExpression other = exp.copyForPlanKey();
assertThat(exp).isSameAs(other);
}
@Test
public void isSameByPlan_when_properties_match() throws Exception {
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
BetweenExpression exp1 = new BetweenExpression("startDate", 3, 4);
assertThat(exp0.isSameByPlan(exp1)).isTrue();
assertThat(exp1.isSameByPlan(exp0)).isTrue();
}
@Test
public void isSameByPlan_when_properties_do_not_match() throws Exception {
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
BetweenExpression exp1 = new BetweenExpression("endDate", 1, 2);
assertThat(exp0.isSameByPlan(exp1)).isFalse();
assertThat(exp1.isSameByPlan(exp0)).isFalse();
}
@Test
public void isSameByBind_when_values_do_not_match() throws Exception {
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 3);
assertThat(exp0.isSameByBind(exp1)).isFalse();
assertThat(exp1.isSameByBind(exp0)).isFalse();
}
@Test
public void isSameByBind_when_values_match() throws Exception {
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 2);
assertThat(exp0.isSameByBind(exp1)).isTrue();
assertThat(exp1.isSameByBind(exp0)).isTrue();
}
}
@@ -0,0 +1,45 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class BetweenPropertyExpressionTest {
@NotNull
private BetweenPropertyExpression exp(String lowProperty, String highProperty, Object value) {
return new BetweenPropertyExpression(lowProperty, highProperty, value);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", "b", 10).isSameByPlan(exp("a", "b", 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffValue() {
assertThat(exp("a", "b", 10).isSameByPlan(exp("a", "b", 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffProperty() {
assertThat(exp("a", "b", 10).isSameByPlan(exp("a", "c", 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffExpressionType() {
assertThat(exp("a", "b", 10).isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_same() {
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 10))).isTrue();
}
@Test
public void isSameByBind_when_diff() {
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 20))).isFalse();
}
}
@@ -0,0 +1,50 @@
package io.ebeaninternal.server.expression;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class CaseInsensitiveEqualExpressionTest {
CaseInsensitiveEqualExpression exp(String propName, String value) {
return new CaseInsensitiveEqualExpression(propName, value);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", "10").isSameByPlan(exp("a", "10"))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp("a", "10").isSameByPlan(exp("a", "20"))).isTrue();
}
@Test
public void isSameByPlan_when_diffProperty_diff() {
assertThat(exp("a", "10").isSameByPlan(exp("b", "10"))).isFalse();
}
@Test
public void isSameByPlan_when_diffType_diff() {
assertThat(exp("a", "10").isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("a", "10").isSameByBind(exp("b", "10"))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", "10").isSameByBind(exp("b", "20"))).isFalse();
}
}
@@ -0,0 +1,124 @@
package io.ebeaninternal.server.expression;
import io.ebean.BaseTestCase;
import io.ebean.LikeType;
import io.ebean.Query;
import io.ebean.bean.EntityBean;
import io.ebean.event.BeanQueryRequest;
import io.ebeaninternal.api.HashQueryPlanBuilder;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import org.tests.model.basic.Address;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultExampleExpressionTest extends BaseTestCase {
Customer customer() {
Address address = new Address();
address.setCity("billingAddress.city");
Customer customer = new Customer();
customer.setName("name");
customer.setBillingAddress(address);
return customer;
}
DefaultExampleExpression exp() {
Customer customer = customer();
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
}
DefaultExampleExpression expExtra() {
Customer customer = customer();
customer.setSmallnote("smallNote");
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
}
DefaultExampleExpression expDiffName() {
Customer customer = customer();
customer.setName("otherName");
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
}
BeanDescriptor<Customer> customerBeanDescriptor() {
return getBeanDescriptor(Customer.class);
}
DefaultExampleExpression prepare(DefaultExampleExpression expr) {
SpiQuery<Customer> query = (SpiQuery<Customer>) spiEbeanServer().find(Customer.class);
BeanQueryRequest<?> request = create(query);
expr.containsMany(customerBeanDescriptor(), new ManyWhereJoins());
expr.prepareExpression(request);
return expr;
}
@Test
public void test() {
DefaultExampleExpression expr = exp();
prepare(expr);
HashQueryPlanBuilder builder = new HashQueryPlanBuilder();
expr.queryPlanHash(builder);
TDSpiExpressionRequest req = new TDSpiExpressionRequest(customerBeanDescriptor());
expr.addBindValues(req);
assertThat(req.bindValues).contains("name", "billingAddress.city");
Customer customer = customer();
customer.setName("Rob");
customer.getBillingAddress().setCity("Auckland");
ResetBasicData.reset();
Query<Customer> query1 = server().find(Customer.class)
.where().exampleLike(customer)
.query();
query1.findList();
assertThat(query1.getGeneratedSql()).contains("(t0.name like ? ");
assertThat(query1.getGeneratedSql()).contains(" and t1.city like ? ");
}
private <T> OrmQueryRequest<T> create(SpiQuery<T> query) {
return new OrmQueryRequest<>(null, null, query, null);
}
@Test
public void isSameByPlan_whenSame() {
assertThat(prepare(exp()).isSameByPlan(prepare(exp()))).isTrue();
}
@Test
public void isSameByPlan_when_diffBindValue_stillSame() {
assertThat(prepare(exp()).isSameByPlan(prepare(expDiffName()))).isTrue();
}
@Test
public void isSameByPlan_when_extraExpression_then_different() {
assertThat(prepare(exp()).isSameByPlan(prepare(expExtra()))).isFalse();
}
@Test
public void isSameByPlan_when_lessExpression_then_different() {
assertThat(prepare(expExtra()).isSameByPlan(prepare(exp()))).isFalse();
}
}
@@ -0,0 +1,74 @@
package io.ebeaninternal.server.expression;
import io.ebean.Expression;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class DefaultExpressionFactoryTest {
@Test
public void testLowerILike() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ilike("name", "foo");
assertThat(expression).isInstanceOf(LikeExpression.class);
}
@Test
public void testNativeILike() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, true);
Expression expression = factory.ilike("name", "foo");
assertThat(expression).isInstanceOf(NativeILikeExpression.class);
}
@Test
public void testEq() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.eq("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
}
@Test
public void testNe() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ne("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
}
@Test
public void testIeq() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
Expression expression = factory.ieq("name", null);
assertThat(expression).isInstanceOf(NullExpression.class);
}
@Test
public void testEq_with_equalsWithNullAsNoop() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
Expression expression = factory.eq("name", null);
assertThat(expression).isInstanceOf(NoopExpression.class);
}
@Test
public void testNe_with_equalsWithNullAsNoop() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
Expression expression = factory.ne("name", null);
assertThat(expression).isInstanceOf(NoopExpression.class);
}
@Test
public void testIeq_with_equalsWithNullAsNoop() throws Exception {
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
Expression expression = factory.ieq("name", null);
assertThat(expression).isInstanceOf(NoopExpression.class);
}
}
@@ -0,0 +1,98 @@
package io.ebeaninternal.server.expression;
import io.ebean.ExpressionList;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class DefaultExpressionListTest {
DefaultExpressionList exp() {
return new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, true), null);
}
DefaultExpressionList spi(ExpressionList list) {
return (DefaultExpressionList) list;
}
@Test
public void isSameByPlan_when_same() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(spi(exp().eq("a", 10).eq("b", 20)))).isTrue();
}
@Test
public void isSameByPlan_when_diffExpressionType() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByPlan_when_less() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(spi(exp().eq("a", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_lessEmptyLast() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(spi(exp()))).isFalse();
}
@Test
public void isSameByPlan_when_lessEmptyFirst() {
assertThat(spi(exp())
.isSameByPlan(spi(exp().eq("a", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_more() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(spi(exp().eq("a", 10).eq("b", 20).eq("c", 30)))).isFalse();
}
@Test
public void isSameByPlan_when_diffProperties() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByPlan(spi(exp().eq("c", 10).eq("b", 20)))).isFalse();
}
@Test
public void isSameByBind_when_same() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isTrue();
}
@Test
public void isSameByBind_when_diffValues() {
assertThat(spi(exp().eq("a", 10).eq("b", 20))
.isSameByBind(spi(exp().eq("a", 10).eq("b", 30)))).isFalse();
}
@Test
public void isSameByBind_when_less() {
assertThat(spi(exp().eq("a", 10))
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
}
@Test
public void isSameByBind_when_more() {
assertThat(spi(exp().eq("a", 10))
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
}
}
@@ -0,0 +1,85 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ExistsQueryExpressionTest {
@NotNull
private ExistsQueryExpression exp(boolean not, String sql, Object... bindValues) {
return new ExistsQueryExpression(not, sql, Arrays.asList(bindValues));
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp(true, "a", 10).isSameByPlan(exp(true, "a", 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp(true, "a", 10).isSameByPlan(exp(true, "a", 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffNot() {
assertThat(exp(true, "a", 10).isSameByPlan(exp(false, "a", 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffSql() {
assertThat(exp(true, "a", 10).isSameByPlan(exp(true, "b", 10))).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 10))).isTrue();
}
@Test
public void isSameByBind_when_sameMultipleBindValues() {
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, "ABC", 20))).isTrue();
}
@Test
public void isSameByBind_when_diffMultipleBindValues() {
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, "ABC", 21))).isFalse();
}
@Test
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, 20, "ABC"))).isFalse();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 20))).isFalse();
}
@Test
public void isSameByBind_when_lessBindValues() {
assertThat(exp(true, "a", 10, 20).isSameByBind(exp(true, "a", 20))).isFalse();
}
@Test
public void isSameByBind_when_moreBindValues() {
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 10, 20))).isFalse();
}
}
@@ -0,0 +1,41 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class IdExpressionTest {
@NotNull
private IdExpression exp(Object value) {
return new IdExpression(value);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp(10).isSameByPlan(exp(10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp(10).isSameByPlan(exp(20))).isTrue();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp(10).isSameByBind(exp(20))).isFalse();
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
}
}
@@ -0,0 +1,67 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class IdInExpressionTest {
@NotNull
private IdInExpression exp(Object... values) {
return new IdInExpression(Arrays.asList(values));
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp(10).isSameByPlan(exp(10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp(10).isSameByPlan(exp(20))).isTrue();
}
@Test
public void isSameByPlan_when_diffBindCount() {
assertThat(exp(10).isSameByPlan(exp(10, 20))).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
}
@Test
public void isSameByBind_when_mulitpleSameBindValues() {
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC", 20))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
}
@Test
public void isSameByBind_when_lessBindValues() {
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC"))).isFalse();
}
@Test
public void isSameByBind_when_moreBindValues() {
assertThat(exp(10, "ABC").isSameByBind(exp(10, "ABC", 30))).isFalse();
}
}
@@ -0,0 +1,174 @@
package io.ebeaninternal.server.expression;
import io.ebeaninternal.api.HashQueryPlanBuilder;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.StrictAssertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class InExpressionTest {
@Test
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, false);
InExpression ex2 = new InExpression("bar", values, false);
ex1.prepareExpression(null);
ex2.prepareExpression(null);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() throws Exception {
List<Integer> values1 = values(42, 92);
List<Integer> values2 = values(42, 92, 82);
InExpression ex1 = new InExpression("foo", values1, false);
InExpression ex2 = new InExpression("foo", values2, false);
ex1.prepareExpression(null);
ex2.prepareExpression(null);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, true);
InExpression ex2 = new InExpression("foo", values, false);
ex1.prepareExpression(null);
ex2.prepareExpression(null);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(b2);
assertNotEquals(b1.build(), b2.build());
}
@Test
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() throws Exception {
List<Integer> values = values(42, 92);
InExpression ex1 = new InExpression("foo", values, true);
InExpression ex2 = new InExpression("foo", values, true);
ex1.prepareExpression(null);
ex2.prepareExpression(null);
HashQueryPlanBuilder b1 = new HashQueryPlanBuilder();
ex1.queryPlanHash(b1);
HashQueryPlanBuilder b2 = new HashQueryPlanBuilder();
ex2.queryPlanHash(b2);
assertEquals(b1.build(), b2.build());
}
List<Integer> values(int... vals) {
ArrayList list = new ArrayList<Integer>();
for (int val : vals) {
list.add(val);
}
return list;
}
@NotNull
private InExpression exp(String propName, boolean not, Object... values) {
InExpression ex = new InExpression(propName, Arrays.asList(values), not);
ex.prepareExpression(null);
return ex;
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", false, 10).isSameByPlan(exp("a", false, 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffPropertyName() {
assertThat(exp("a", false, 10).isSameByPlan(exp("b", false, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffNot() {
assertThat(exp("a", false, 10).isSameByPlan(exp("a", true, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp("a", false, 10).isSameByPlan(exp("a", false, 10, 20))).isFalse();
}
@Test
public void isSameByPlan_when_diffBindCount() {
assertThat(exp("a", false, 10).isSameByPlan(exp("a", false, 10, 20))).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, 10))).isTrue();
}
@Test
public void isSameByBind_when_sameMultipleBindValues() {
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC", 20))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, "foo"))).isFalse();
}
@Test
public void isSameByBind_when_lessBindValues() {
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC"))).isFalse();
}
@Test
public void isSameByBind_when_moreBindValues() {
assertThat(exp("a", false, 10, "ABC").isSameByBind(exp("a", false, 10, "ABC", 30))).isFalse();
}
}
@@ -0,0 +1,89 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class InQueryExpressionTest {
@NotNull
private InQueryExpression exp(String propertyName, boolean not, String sql, Object... bindValues) {
return new InQueryExpression(propertyName, not, sql, Arrays.asList(bindValues));
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("name", true, "sql", 10).isSameByPlan(exp("name", true, "sql", 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp("name", true, "sql", 10).isSameByPlan(exp("name", true, "sql", 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffNPropertyName() {
assertThat(exp("name", true, "sql", 10).isSameByPlan(exp("nameDiff", true, "sql", 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffNot() {
assertThat(exp("name", true, "sql", 10).isSameByPlan(exp("name", false, "sql", 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffSql() {
assertThat(exp("name", true, "sql", 10).isSameByPlan(exp("name", true, "sqlDiff", 10))).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10))).isTrue();
}
@Test
public void isSameByBind_when_sameMultipleBindValues() {
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 20))).isTrue();
}
@Test
public void isSameByBind_when_diffMultipleBindValues() {
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 21))).isFalse();
}
@Test
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, 20, "ABC"))).isFalse();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 20))).isFalse();
}
@Test
public void isSameByBind_when_lessBindValues() {
assertThat(exp("name", true, "sql", 10, 20).isSameByBind(exp("name", true, "sql", 20))).isFalse();
}
@Test
public void isSameByBind_when_moreBindValues() {
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10, 20))).isFalse();
}
}
@@ -0,0 +1,114 @@
package io.ebeaninternal.server.expression;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import org.tests.model.basic.Contact;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class IsEmptyExpressionQueryTest extends BaseTestCase {
@Test
public void isEmpty() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().isEmpty("contacts")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where not exists (select 1 from contact where customer_id = t0.id");
}
@Test
public void isNotEmpty() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().isNotEmpty("contacts")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where exists (select 1 from contact where customer_id = t0.id");
}
@Test
public void isEmpty_contacts() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.select("id")
.where().isEmpty("notes")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where not exists (select 1 from contact_note where contact_id = t0.id");
}
@Test
public void isNotEmpty_contacts() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.select("id")
.where().isNotEmpty("notes")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where exists (select 1 from contact_note where contact_id = t0.id");
}
@Test
public void isEmpty_manyToMany() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.select("id")
.where().isEmpty("notes")
.query();
query.findList();
}
@Test
public void isEmpty_nested() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().isEmpty("contacts.notes")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note where contact_id = u1.id)");
}
@Test
public void isNotEmpty_nested() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().isNotEmpty("contacts.notes")
.query();
query.findList();
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note where contact_id = u1.id)");
}
}
@@ -0,0 +1,81 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class JsonPathExpressionTest {
@NotNull
private JsonPathExpression exp(String propertyName, String path, Op operator, Object value) {
return new JsonPathExpression(propertyName, path, operator, value);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(exp("a", "path", Op.EQ, 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(exp("a", "path", Op.EQ, 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffPath() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(exp("a", "pathDiff", Op.EQ, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffProperty_diff() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(exp("b", "path", Op.EQ, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffOperator_diff() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(exp("a", "path", Op.LT, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffType_diff() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, 10))).isTrue();
}
@Test
public void isSameByBind_when_sameNullBindValues() {
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, null))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.EQ, "junk"))).isFalse();
}
@Test
public void isSameByBind_when_diffFirstNullBindValues() {
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, 10))).isFalse();
}
@Test
public void isSameByBind_when_diffLastNullBindValues() {
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, null))).isFalse();
}
}
@@ -0,0 +1,61 @@
package io.ebeaninternal.server.expression;
import io.ebean.Expr;
import io.ebean.Expression;
import io.ebean.Junction;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class JunctionExpressionTest {
Expression eq(String propName, int value) {
return Expr.eq(propName, value);
}
DefaultExpressionList<?> exp(Expression... expressions) {
DefaultExpressionList<Object> list = new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, false), null);
for (Expression ex : expressions) {
list.add(ex);
}
return list;
}
<T> JunctionExpression and(DefaultExpressionList<T> list) {
return new JunctionExpression<>(Junction.Type.AND, list);
}
<T> JunctionExpression or(DefaultExpressionList<T> list) {
return new JunctionExpression<>(Junction.Type.OR, list);
}
@Test
public void isSameByPlan_when_same() {
assertThat(and(exp(eq("a", 10), eq("b", 10)))
.isSameByPlan(and(exp(eq("a", 10), eq("b", 10))))).isTrue();
}
@Test
public void copyForPlanKey_isSameByPlan_when_same() {
assertThat(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey())
.isSameByPlan(and(exp(eq("a", 10), eq("b", 10))))).isTrue();
}
@Test
public void copyForPlanKey_isSameByPlan_when_diff() {
assertThat(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey())
.isSameByPlan(and(exp(eq("a", 10), eq("c", 10))))).isFalse();
}
@Test
public void isSameByPlan_when_diffType() {
assertThat(and(exp(eq("a", 10), eq("b", 10)))
.isSameByPlan(or(exp(eq("a", 10), eq("b", 10))))).isFalse();
}
}
@@ -0,0 +1,66 @@
package io.ebeaninternal.server.expression;
import io.ebean.LikeType;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class LikeExpressionTest extends BaseExpressionTest {
@NotNull
private LikeExpression exp(String propertyName, String value, boolean caseInsensitive, LikeType type) {
return new LikeExpression(propertyName, value, caseInsensitive, type);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByPlan(exp("a", "rob", true, LikeType.STARTS_WITH))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_then_stillSame() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByPlan(exp("a", "bor", true, LikeType.STARTS_WITH))).isTrue();
}
@Test
public void isSameByPlan_when_diffCaseInsensitive() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByPlan(exp("a", "rob", false, LikeType.STARTS_WITH))).isFalse();
}
@Test
public void isSameByPlan_when_diffLikeType() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByPlan(exp("a", "rob", true, LikeType.ENDS_WITH))).isFalse();
}
@Test
public void isSameByPlan_when_diffProperty() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByPlan(exp("b", "rob", true, LikeType.STARTS_WITH))).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByBind(exp("a", "rob", true, LikeType.STARTS_WITH))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
.isSameByBind(exp("a", "bor", true, LikeType.STARTS_WITH))).isFalse();
}
}
@@ -0,0 +1,129 @@
package io.ebeaninternal.server.expression;
import io.ebean.Expr;
import io.ebean.Expression;
import org.tests.model.basic.Order;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class LogicExpressionTest extends BaseExpressionTest {
Expression eq(String propName, int value) {
return Expr.eq(propName, value);
}
LogicExpression and(Expression a, Expression b) {
return new LogicExpression.And(a, b);
}
LogicExpression or(Expression a, Expression b) {
return new LogicExpression.Or(a, b);
}
@Test
public void isSameByPlan_when_same() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByPlan(and(eq("a", 10), eq("b", 10)))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_then_stillSame() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByPlan(and(eq("a", 20), eq("b", 20)))).isTrue();
}
@Test
public void isSameByPlan_when_diffExp1_then_diff() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByPlan(and(eq("c", 10), eq("b", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_diffExp2_then_diff() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByPlan(and(eq("a", 10), eq("c", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_diffType_then_diff() {
assertThat(or(eq("a", 10), eq("b", 10))
.isSameByPlan(and(eq("a", 10), eq("c", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_diffExpressionType() {
assertThat(or(eq("a", 10), eq("b", 10))
.isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByBind(and(eq("a", 10), eq("c", 10)))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(and(eq("a", 10), eq("b", 10))
.isSameByBind(and(eq("a", 10), eq("c", 20)))).isFalse();
}
@Test
public void nestedPath_when_notNested() {
LogicExpression and = and(eq("orderDate", 10), eq("shipDate", 10));
and.nestedPath(getBeanDescriptor(Order.class));
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
@Test
public void nestedPath_when_nestedSame() {
LogicExpression and = and(eq("details.orderQty", 10), eq("details.unitPrice", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isEqualTo("details");
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
@Test
public void nestedPath_when_nestedDifferent() {
LogicExpression and = and(eq("details.orderQty", 10), eq("shipments.shipTime", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isNull();
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
assertThat(and.expTwo).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression) and.expTwo).nestedPath).isEqualTo("shipments");
}
@Test
public void nestedPath_when_oneNested() {
LogicExpression and = and(eq("details.orderQty", 10), eq("orderDate", 10));
String path = and.nestedPath(getBeanDescriptor(Order.class));
assertThat(path).isNull();
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
}
}
@@ -0,0 +1,68 @@
package io.ebeaninternal.server.expression;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import org.tests.model.basic.Customer;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NoopExpressionTest extends BaseTestCase {
@Test
public void test() {
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().add(NoopExpression.INSTANCE)
.query();
query.findList();
String generatedSql = sqlOf(query);
assertThat(generatedSql).contains("select t0.id from o_customer t0 where 1=1");
}
@Test
public void test_withPreAndPost() {
Query<Customer> query = Ebean.find(Customer.class)
.select("id")
.where().eq("name", null)
.add(NoopExpression.INSTANCE)
.ne("status", null)
.query();
query.findList();
String generatedSql = sqlOf(query);
assertThat(generatedSql).contains("select t0.id from o_customer t0 where t0.name is null and 1=1 and t0.status is not null");
}
@Test
public void isSameByPlan_when_same() {
assertThat(new NoopExpression().isSameByPlan(new NoopExpression())).isTrue();
}
@Test
public void isSameByPlan_when_diffExpressionType() {
assertThat(new NoopExpression().isSameByPlan(null)).isFalse();
}
@Test
public void isSameByBind_when_same() {
assertThat(new NoopExpression().isSameByBind(new NoopExpression())).isTrue();
}
@Test
public void isSameByBind_when_diffExpressionType() {
assertThat(new NoopExpression().isSameByBind(null)).isTrue();
}
}
@@ -0,0 +1,58 @@
package io.ebeaninternal.server.expression;
import io.ebean.Expression;
import org.junit.Test;
import static io.ebean.Expr.eq;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class NotExpressionTest {
NotExpression not(Expression expression) {
return new NotExpression(expression);
}
@Test
public void isSameByPlan_when_same() {
assertThat(not(eq("a", 10))
.isSameByPlan(not(eq("a", 10)))).isTrue();
}
@Test
public void isSameByPlan_when_sameByPlan() {
assertThat(not(eq("a", 10))
.isSameByPlan(not(eq("a", 20)))).isTrue();
}
@Test
public void isSameByPlan_when_different() {
assertThat(not(eq("a", 10))
.isSameByPlan(not(eq("b", 10)))).isFalse();
}
@Test
public void isSameByPlan_when_differentExpressionType() {
assertThat(not(eq("a", 10))
.isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_same() {
assertThat(not(eq("a", 10))
.isSameByBind(not(eq("b", 10)))).isTrue();
}
@Test
public void isSameByBind_when_different() {
assertThat(not(eq("a", 10))
.isSameByBind(not(eq("a", 20)))).isFalse();
}
}
@@ -0,0 +1,95 @@
package io.ebeaninternal.server.expression;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.api.SpiExpression;
import org.tests.model.basic.Order;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NullExpressionTest extends BaseExpressionTest {
NullExpression nullExp(String propertyName, boolean notNull) {
NullExpression expr = new NullExpression(propertyName, notNull);
expr.containsMany(getBeanDescriptor(Order.class), new ManyWhereJoins());
return expr;
}
@Test
public void addSql_when_notNull() throws Exception {
DefaultExpressionRequest expReq = newExpressionRequest();
nullExp("id", true).addSql(expReq);
assertThat(expReq.getSql()).isEqualTo("id is not null ");
}
@Test
public void addSql_when_null() throws Exception {
DefaultExpressionRequest expReq = newExpressionRequest();
nullExp("id", false).addSql(expReq);
assertThat(expReq.getSql()).isEqualTo("id is null ");
}
@Test
public void addSql_when_notNull_and_assocOne() throws Exception {
DefaultExpressionRequest expReq = newExpressionRequest();
nullExp("customer", true).addSql(expReq);
assertThat(expReq.getSql()).isEqualTo("customer.id is not null ");
}
@Test
public void addSql_when_null_and_assocOne() throws Exception {
DefaultExpressionRequest expReq = newExpressionRequest();
nullExp("customer", false).addSql(expReq);
assertThat(expReq.getSql()).isEqualTo("customer.id is null ");
}
@Test
public void copyForPlanKey_isSameInstance() throws Exception {
NullExpression exp = nullExp("customer.name", false);
SpiExpression other = exp.copyForPlanKey();
assertThat(exp).isSameAs(other);
}
@Test
public void isSameByBind_true() throws Exception {
assertThat(nullExp("customer.name", false)
.isSameByBind(nullExp("customer.name", false))).isTrue();
}
@Test
public void isSameByPlan_true() throws Exception {
assertThat(nullExp("customer.name", false)
.isSameByPlan(nullExp("customer.name", false))).isTrue();
}
@Test
public void isSameByPlan_false_when_notNullDiff() throws Exception {
assertThat(new NullExpression("customer.name", false)
.isSameByPlan(new NullExpression("customer.name", true))).isFalse();
}
@Test
public void isSameByPlan_false_when_propertyNameDiff() throws Exception {
assertThat(new NullExpression("customer.startDate", true)
.isSameByPlan(new NullExpression("customer.name", true))).isFalse();
}
}
@@ -0,0 +1,148 @@
package io.ebeaninternal.server.expression;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.ExpressionList;
import io.ebeaninternal.api.SpiExpression;
import org.tests.model.basic.Order;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class PrepareDocNestedTest extends BaseTestCase {
@Test
public void prepare() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("details.orderQty", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 1);
assertEquals(exp.allDocNestedPath, "details");
}
@Test
public void prepare_when_multipleOfSamePath() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertEquals(exp.allDocNestedPath, "details");
}
@Test
public void prepare_when_mixed() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("customer.id", 1)
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> second = (DefaultExpressionList<?>) underlyingList.get(1);
assertEquals(second.allDocNestedPath, "details");
}
@Test
public void prepare_when_nestedJunction() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.not()
.gt("customer.id", 1)
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 1);
assertNull(exp.allDocNestedPath);
JunctionExpression<?> junction = (JunctionExpression<?>) underlyingList.get(0);
List<SpiExpression> junctionUnderlying = junction.exprList.getUnderlyingList();
JunctionExpression<?> nestedNestedPath = (JunctionExpression) junctionUnderlying.get(1);
assertEquals(nestedNestedPath.exprList.allDocNestedPath, "details");
}
@Test
public void prepare_when_nestedMultiple() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.isNotNull("shipments.shipTime")
.gt("details.orderQty", 1)
.gt("details.unitPrice", 1)
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 2);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(0);
assertEquals(shipExpr.allDocNestedPath, "shipments");
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(1);
assertEquals(detailsExpr.allDocNestedPath, "details");
}
@Test
public void prepare_when_manyMixed() throws Exception {
ExpressionList<Order> where = Ebean.find(Order.class)
.where()
.gt("customer.id", 1) // 0
.isNotNull("shipments.shipTime") // shipments 0
.isNotNull("status") // 1
.gt("details.orderQty", 1) // details 0
.isNotNull("orderDate") // 2
.gt("details.unitPrice", 1) // details 1
.query().where();
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
List<SpiExpression> underlyingList = exp.getUnderlyingList();
assertEquals(underlyingList.size(), 5);
assertNull(exp.allDocNestedPath);
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(3);
assertEquals(shipExpr.allDocNestedPath, "shipments");
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(4);
assertEquals(detailsExpr.allDocNestedPath, "details");
}
}
@@ -0,0 +1,50 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class RawExpressionTest {
@NotNull
private RawExpression exp(String sql, Object... values) {
return new RawExpression(sql, values);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", 10).isSameByPlan(exp("a", 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBindValues() {
assertThat(exp("a", 10).isSameByPlan(exp("a", 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffSql() {
assertThat(exp("a", 10).isSameByPlan(exp("b", 10))).isFalse();
}
@Test
public void isSameByBind_when_same() {
assertThat(exp("a", 10).isSameByBind(exp("a", 10))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", 10).isSameByBind(exp("a", 20))).isFalse();
}
@Test
public void isSameByBind_when_moreBindValues() {
assertThat(exp("a", 10).isSameByBind(exp("a", 10, 20))).isFalse();
}
@Test
public void isSameByBind_when_lessBindValues() {
assertThat(exp("a", 10, 20).isSameByBind(exp("a", 10))).isFalse();
}
}
@@ -0,0 +1,29 @@
package io.ebeaninternal.server.expression;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class SameTest {
@Test
public void sameByNull() throws Exception {
assertThat(Same.sameByNull("a", "a")).isTrue();
assertThat(Same.sameByNull("a", "b")).isTrue();
assertThat(Same.sameByNull(null, null)).isTrue();
assertThat(Same.sameByNull("a", null)).isFalse();
assertThat(Same.sameByNull(null, "a")).isFalse();
}
@Test
public void sameByValue() throws Exception {
assertThat(Same.sameByValue("a", "a")).isTrue();
assertThat(Same.sameByValue("a", "b")).isFalse();
assertThat(Same.sameByValue(null, null)).isTrue();
assertThat(Same.sameByValue("a", null)).isFalse();
assertThat(Same.sameByValue(null, "a")).isFalse();
}
}
@@ -0,0 +1,58 @@
package io.ebeaninternal.server.expression;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class SimpleExpressionTest extends BaseExpressionTest {
@NotNull
private SimpleExpression exp(String propertyName, Op operator, Object value) {
return new SimpleExpression(propertyName, operator, value);
}
@Test
public void isSameByPlan_when_same() {
assertThat(exp("a", Op.EQ, 10).isSameByPlan(exp("a", Op.EQ, 10))).isTrue();
}
@Test
public void isSameByPlan_when_diffBind_same() {
assertThat(exp("a", Op.EQ, 10).isSameByPlan(exp("a", Op.EQ, 20))).isTrue();
}
@Test
public void isSameByPlan_when_diffProperty_diff() {
assertThat(exp("a", Op.EQ, 10).isSameByPlan(exp("b", Op.EQ, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffOperator_diff() {
assertThat(exp("a", Op.EQ, 10).isSameByPlan(exp("a", Op.LT, 10))).isFalse();
}
@Test
public void isSameByPlan_when_diffType_diff() {
assertThat(exp("a", Op.EQ, 10).isSameByPlan(new NoopExpression())).isFalse();
}
@Test
public void isSameByBind_when_sameBindValues() {
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.LT, 10))).isTrue();
}
@Test
public void isSameByBind_when_diffBindValues() {
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.EQ, "junk"))).isFalse();
}
}
@@ -0,0 +1,78 @@
package io.ebeaninternal.server.expression;
import io.ebeaninternal.api.SpiExpressionRequest;
import io.ebeaninternal.server.core.DbExpressionHandler;
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.ArrayList;
import java.util.List;
/**
* Test double for testing with SpiExpressionRequest.
*/
public class TDSpiExpressionRequest implements SpiExpressionRequest {
List<Object> bindValues = new ArrayList<>();
final BeanDescriptor<?> descriptor;
public TDSpiExpressionRequest(BeanDescriptor<?> descriptor) {
this.descriptor = descriptor;
}
@Override
public DbExpressionHandler getDbPlatformHandler() {
return null;
}
@Override
public String parseDeploy(String logicalProp) {
return null;
}
@Override
public BeanDescriptor<?> getBeanDescriptor() {
return descriptor;
}
@Override
public SpiOrmQueryRequest<?> getQueryRequest() {
return null;
}
@Override
public SpiExpressionRequest append(String sql) {
return null;
}
@Override
public void addBindEncryptKey(Object encryptKey) {
}
@Override
public void addBindValue(Object bindValue) {
bindValues.add(bindValue);
}
@Override
public String getSql() {
return null;
}
@Override
public ArrayList<Object> getBindValues() {
return null;
}
@Override
public int nextParameter() {
return 0;
}
@Override
public void appendLike() {
}
}