TESTS: Start adding low-level object unit tests.
git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@981625 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class BetweenCondition extends Condition {
|
||||
private final Value left;
|
||||
private final Value r1;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class BooleanCondition extends Condition {
|
||||
private final Value left;
|
||||
private final String op;
|
||||
|
||||
@@ -21,8 +21,10 @@ package org.ofbiz.sql;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
@SourceMonitored
|
||||
public final class ConditionList extends Condition implements Iterable<Condition> {
|
||||
private final Joiner joiner;
|
||||
private final List<Condition> conditions;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class CountAllFunction extends StaticValue {
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class CountFunction extends StaticValue {
|
||||
private final boolean isDistinct;
|
||||
private final FieldValue field;
|
||||
|
||||
@@ -21,8 +21,10 @@ package org.ofbiz.sql;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
@SourceMonitored
|
||||
public final class FieldAll extends Atom implements Iterable<String> {
|
||||
private final String alias;
|
||||
private final Set<String> exclude;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class FieldDef extends Atom {
|
||||
private final String alias;
|
||||
private final StaticValue value;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class FieldValue extends StaticValue {
|
||||
private final String fieldName;
|
||||
private final String tableName;
|
||||
|
||||
@@ -21,8 +21,10 @@ package org.ofbiz.sql;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
@SourceMonitored
|
||||
public final class FunctionCall extends StaticValue implements Iterable<Value> {
|
||||
private final String name;
|
||||
private final List<Value> values;
|
||||
|
||||
@@ -20,8 +20,10 @@ package org.ofbiz.sql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
@SourceMonitored
|
||||
public final class ListCondition extends Condition {
|
||||
private final Value left;
|
||||
private final String op;
|
||||
|
||||
@@ -21,8 +21,10 @@ package org.ofbiz.sql;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
@SourceMonitored
|
||||
public final class MathValue extends StaticValue implements Iterable<ConstantValue> {
|
||||
private final String op;
|
||||
private final List<ConstantValue> values;
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class NumberValue<N extends Number> extends ConstantValue {
|
||||
private final N number;
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class OrderByItem extends Atom {
|
||||
public enum Order { DEFAULT, ASCENDING, DESCENDING };
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class ParameterValue extends Value {
|
||||
private final String name;
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
*/
|
||||
package org.ofbiz.sql;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
|
||||
@SourceMonitored
|
||||
public final class StringValue extends ConstantValue {
|
||||
private final String str;
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.ofbiz.sql.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.sql.BetweenCondition;
|
||||
import org.ofbiz.sql.BooleanCondition;
|
||||
import org.ofbiz.sql.Condition;
|
||||
import org.ofbiz.sql.ConditionList;
|
||||
import org.ofbiz.sql.Joiner;
|
||||
import org.ofbiz.sql.ListCondition;
|
||||
import org.ofbiz.sql.NumberValue;
|
||||
import org.ofbiz.sql.StringValue;
|
||||
import org.ofbiz.sql.Value;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.test.GenericTestCaseBase;
|
||||
|
||||
@SourceMonitored
|
||||
public class ConditionsTest extends GenericTestCaseBase {
|
||||
private static final StringValue s1 = new StringValue("a");
|
||||
private static final StringValue s2 = new StringValue("b");
|
||||
private static final StringValue s3 = new StringValue("c");
|
||||
private static final NumberValue<Long> l1 = NumberValue.valueOf(1);
|
||||
private static final NumberValue<Long> l2 = NumberValue.valueOf(5);
|
||||
private static final NumberValue<Long> l3 = NumberValue.valueOf(10);
|
||||
|
||||
public ConditionsTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
private static <C extends Condition> void basicTest(String label, Class<C> clz, C c, String s, C o, boolean matches) {
|
||||
assertEquals(label + ":toString", s, c.toString());
|
||||
assertNotEquals(label + ":not-equals-this", c, ConditionsTest.class);
|
||||
if (o != null) {
|
||||
if (matches) {
|
||||
assertEquals(label + ":equals", o, c);
|
||||
} else {
|
||||
assertNotEquals(label + ":not-equals", o, c);
|
||||
}
|
||||
}
|
||||
ConditionVisitorRecorder visitor = new ConditionVisitorRecorder();
|
||||
c.accept(visitor);
|
||||
c.accept(visitor);
|
||||
assertEquals(label + ":visited", 2, visitor.counts.remove(clz).intValue());
|
||||
assertTrue(label + ":nothing-else-visited", visitor.counts.isEmpty());
|
||||
}
|
||||
|
||||
private static void betweenConditionTest(String label, BetweenCondition c, Value left, Value r1, Value r2, String s, BetweenCondition o, boolean matches) {
|
||||
assertEquals(label + ":left", left, c.getLeft());
|
||||
assertEquals(label + ":r1", r1, c.getR1());
|
||||
assertEquals(label + ":r2", r2, c.getR2());
|
||||
basicTest(label, BetweenCondition.class, c, s, o, matches);
|
||||
}
|
||||
|
||||
public void testBetweenCondition() {
|
||||
BetweenCondition c1 = new BetweenCondition(l1, l2, l3);
|
||||
betweenConditionTest("c1", c1, l1, l2, l3, "1 BETWEEN 5 AND 10", null, false);
|
||||
BetweenCondition c2 = new BetweenCondition(l3, l1, l2);
|
||||
betweenConditionTest("c2", c2, l3, l1, l2, "10 BETWEEN 1 AND 5", c1, false);
|
||||
BetweenCondition c3 = new BetweenCondition(l1, l2, l3);
|
||||
betweenConditionTest("c3", c3, l1, l2, l3, "1 BETWEEN 5 AND 10", c1, true);
|
||||
BetweenCondition c4 = new BetweenCondition(l1, l3, l2);
|
||||
betweenConditionTest("c4", c4, l1, l3, l2, "1 BETWEEN 10 AND 5", c1, false);
|
||||
BetweenCondition c5 = new BetweenCondition(l1, l2, l1);
|
||||
betweenConditionTest("c5", c5, l1, l2, l1, "1 BETWEEN 5 AND 1", c1, false);
|
||||
}
|
||||
|
||||
private static void booleanConditionTest(String label, BooleanCondition c, Value left, String op, Value right, String s, BooleanCondition o, boolean matches) {
|
||||
assertEquals(label + ":left", left, c.getLeft());
|
||||
assertEquals(label + ":op", op, c.getOp());
|
||||
assertEquals(label + ":right", right, c.getRight());
|
||||
basicTest(label, BooleanCondition.class, c, s, o, matches);
|
||||
}
|
||||
|
||||
public void testBooleanCondition() {
|
||||
BooleanCondition c1 = new BooleanCondition(l1, "=", l2);
|
||||
booleanConditionTest("c1", c1, l1, "=", l2, "1 = 5", null, false);
|
||||
BooleanCondition c2 = new BooleanCondition(l1, "=", l3);
|
||||
booleanConditionTest("c2", c2, l1, "=", l3, "1 = 10", c1, false);
|
||||
BooleanCondition c3 = new BooleanCondition(l1, "<", l2);
|
||||
booleanConditionTest("c3", c3, l1, "<", l2, "1 < 5", c1, false);
|
||||
BooleanCondition c4 = new BooleanCondition(l2, ">", l3);
|
||||
booleanConditionTest("c4", c4, l2, ">", l3, "5 > 10", c1, false);
|
||||
BooleanCondition c5 = new BooleanCondition(l1, "=", l2);
|
||||
booleanConditionTest("c5", c5, l1, "=", l2, "1 = 5", c1, true);
|
||||
}
|
||||
|
||||
private static void conditionListTest(String label, ConditionList c, Joiner joiner, List<? extends Condition> conditions, String s, ConditionList o, boolean matches) {
|
||||
assertEquals(label + ":joiner", joiner, c.getJoiner());
|
||||
basicTest(label, ConditionList.class, c, s, o, matches);
|
||||
BooleanCondition b = new BooleanCondition(l1, "=", l3);
|
||||
c.add(b);
|
||||
assertEqualsIterable(label + ":iterable", conditions, Collections.<Condition>emptyList(), false, c, list(b), true);
|
||||
}
|
||||
|
||||
public void testConditionList() {
|
||||
BooleanCondition b1 = new BooleanCondition(l1, "=", l2);
|
||||
BooleanCondition b2 = new BooleanCondition(l2, "=", l3);
|
||||
BooleanCondition b3 = new BooleanCondition(l1, "=", l3);
|
||||
ConditionList c1 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1));
|
||||
conditionListTest("c1", c1, Joiner.AND, list(b1), "( 1 = 5 )", null, false);
|
||||
ConditionList c2 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1, b2));
|
||||
conditionListTest("c2", c2, Joiner.AND, list(b1, b2), "( 1 = 5 AND 5 = 10 )", c1, false);
|
||||
ConditionList c3 = new ConditionList(Joiner.OR, GenericTestCaseBase.<Condition>list(b2, b1));
|
||||
conditionListTest("c3", c3, Joiner.OR, list(b2, b1), "( 5 = 10 OR 1 = 5 )", c1, false);
|
||||
ConditionList c4 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1));
|
||||
conditionListTest("c4", c4, Joiner.AND, list(b1), "( 1 = 5 )", c1, true);
|
||||
}
|
||||
|
||||
private static void listConditionTest(String label, ListCondition c, Value left, String op, List<? extends Value> values, String s, ListCondition o, boolean matches) {
|
||||
assertEquals(label + ":left", left, c.getLeft());
|
||||
assertEquals(label + ":op", op, c.getOp());
|
||||
assertEquals(label + ":right", values, c.getValues());
|
||||
basicTest(label, ListCondition.class, c, s, o, matches);
|
||||
}
|
||||
|
||||
public void testListCondition() {
|
||||
ListCondition c1 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2));
|
||||
listConditionTest("c1", c1, l1, "in", list(l2), "1 in (5)", null, false);
|
||||
ListCondition c2 = new ListCondition(l1, "NOT IN", GenericTestCaseBase.<Value>list(l2));
|
||||
listConditionTest("c2", c2, l1, "not in", list(l2), "1 not in (5)", c1, false);
|
||||
ListCondition c3 = new ListCondition(l2, "IN", GenericTestCaseBase.<Value>list(l3));
|
||||
listConditionTest("c3", c3, l2, "in", list(l3), "5 in (10)", c1, false);
|
||||
ListCondition c4 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2, l3));
|
||||
listConditionTest("c4", c4, l1, "in", list(l2, l3), "1 in (5, 10)", c1, false);
|
||||
ListCondition c5 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2));
|
||||
listConditionTest("c5", c5, l1, "in", list(l2), "1 in (5)", c1, true);
|
||||
}
|
||||
|
||||
public static class ConditionVisitorRecorder extends Recorder<Class<? extends Condition>> implements Condition.Visitor {
|
||||
public void visit(BetweenCondition condition) {
|
||||
record(BetweenCondition.class);
|
||||
}
|
||||
|
||||
public void visit(BooleanCondition condition) {
|
||||
record(BooleanCondition.class);
|
||||
}
|
||||
|
||||
public void visit(ConditionList condition) {
|
||||
record(ConditionList.class);
|
||||
}
|
||||
|
||||
public void visit(ListCondition condition) {
|
||||
record(ListCondition.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.ofbiz.sql.test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Recorder<I> {
|
||||
public Map<I, Integer> counts = new HashMap<I, Integer>();
|
||||
|
||||
protected void record(I key) {
|
||||
Integer count = counts.get(key);
|
||||
if (count == null) {
|
||||
counts.put(key, Integer.valueOf(1));
|
||||
} else {
|
||||
counts.put(key, Integer.valueOf(count.intValue() + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.ofbiz.sql.test;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.ofbiz.sql.ConstantValue;
|
||||
import org.ofbiz.sql.CountAllFunction;
|
||||
import org.ofbiz.sql.CountFunction;
|
||||
import org.ofbiz.sql.FieldValue;
|
||||
import org.ofbiz.sql.FunctionCall;
|
||||
import org.ofbiz.sql.MathValue;
|
||||
import org.ofbiz.sql.NumberValue;
|
||||
import org.ofbiz.sql.ParameterValue;
|
||||
import org.ofbiz.sql.StaticValue;
|
||||
import org.ofbiz.sql.StringValue;
|
||||
import org.ofbiz.sql.Value;
|
||||
|
||||
import org.ofbiz.sql.FieldAll;
|
||||
import org.ofbiz.sql.FieldDef;
|
||||
import org.ofbiz.sql.Parser;
|
||||
import org.ofbiz.sql.OrderByItem;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.test.GenericTestCaseBase;
|
||||
|
||||
@SourceMonitored
|
||||
public class SelectTest extends GenericTestCaseBase {
|
||||
private static final FieldValue fv1 = new FieldValue("partyId");
|
||||
private static final FieldValue fv2 = new FieldValue("a", "partyId");
|
||||
private static final FieldValue fv3 = new FieldValue(null, "partyId");
|
||||
private static final FieldValue fv4 = new FieldValue(null, "firstName");
|
||||
private static final NumberValue<Long> nv1 = NumberValue.valueOf(1);
|
||||
private static final NumberValue<Double> nv2 = NumberValue.valueOf(2D);
|
||||
private static final NumberValue<Long> nv3 = NumberValue.valueOf(3);
|
||||
|
||||
public SelectTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
private static Parser parser(Object v) {
|
||||
return new Parser(new StringReader(v.toString()));
|
||||
}
|
||||
|
||||
private static <V> void basicTest(String label, Class<V> clz, V v, V o, boolean matches) {
|
||||
assertNotEquals(label + ":not-equals-this", v, SelectTest.class);
|
||||
if (o != null) {
|
||||
if (matches) {
|
||||
assertEquals(label + ":equals", o, v);
|
||||
} else {
|
||||
assertNotEquals(label + ":not-equals", o, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void fieldAllTest(String label, FieldAll v, String alias, Set<String> exclude, FieldAll o, boolean matches) throws Exception {
|
||||
assertEquals(label + ":alias", alias, v.getAlias());
|
||||
assertEquals(label + ":exclude", exclude, set(v));
|
||||
assertEquals(label + ":parse", v, parser(v).parse_FieldAll());
|
||||
basicTest(label, FieldAll.class, v, o, matches);
|
||||
}
|
||||
|
||||
public void testFieldAll() throws Exception {
|
||||
FieldAll v1 = new FieldAll("a", set("a", "b"));
|
||||
fieldAllTest("v1", v1, "a", set("a", "b"), null, false);
|
||||
FieldAll v2 = new FieldAll("b", set("a", "b"));
|
||||
fieldAllTest("v2", v2, "b", set("a", "b"), v1, false);
|
||||
FieldAll v3 = new FieldAll("a", set("b", "c"));
|
||||
fieldAllTest("v3", v3, "a", set("b", "c"), v1, false);
|
||||
FieldAll v4 = new FieldAll("a", Collections.<String>emptySet());
|
||||
fieldAllTest("v4", v4, "a", Collections.<String>emptySet(), v1, false);
|
||||
FieldAll v5 = new FieldAll("a", set("b", "a"));
|
||||
fieldAllTest("v5", v5, "a", set("b", "a"), v1, true);
|
||||
}
|
||||
|
||||
private static void fieldDefTest(String label, FieldDef v, StaticValue value, String alias, String defaultName, FieldDef o, boolean matches) throws Exception {
|
||||
assertEquals(label + ":value", value, v.getValue());
|
||||
assertEquals(label + ":alias", alias, v.getAlias());
|
||||
assertEquals(label + ":default-name", defaultName, v.getDefaultName());
|
||||
assertEquals(label + ":parse", v, parser(v).parse_FieldDef());
|
||||
basicTest(label, FieldDef.class, v, o, matches);
|
||||
}
|
||||
|
||||
public void testFieldDef() throws Exception {
|
||||
FieldDef v1 = new FieldDef(fv1, null);
|
||||
fieldDefTest("v1", v1, fv1, null, "partyId", null, false);
|
||||
FieldDef v2 = new FieldDef(fv1, "partyId");
|
||||
fieldDefTest("v2", v2, fv1, "partyId", "partyId", v1, false);
|
||||
FieldDef v3 = new FieldDef(fv4, null);
|
||||
fieldDefTest("v3", v3, fv4, null, "firstName", v1, false);
|
||||
FieldDef v4 = new FieldDef(fv4, "partyId");
|
||||
fieldDefTest("v4", v4, fv4, "partyId", "partyId", v1, false);
|
||||
FieldDef v5 = new FieldDef(fv1, null);
|
||||
fieldDefTest("v5", v5, fv1, null, "partyId", v1, true);
|
||||
}
|
||||
|
||||
private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, String functionName, String fieldName, OrderByItem o, boolean matches) throws Exception {
|
||||
assertEquals(label + ":order", order, v.getOrder());
|
||||
assertEquals(label + ":function-name", functionName, v.getFunctionName());
|
||||
assertEquals(label + ":field-name", fieldName, v.getFieldName());
|
||||
assertEquals(label + ":parse", v, parser(v).parse_OrderByItem());
|
||||
basicTest(label, OrderByItem.class, v, o, matches);
|
||||
}
|
||||
|
||||
public void testOrderByItem() throws Exception {
|
||||
OrderByItem v1 = new OrderByItem(OrderByItem.Order.DEFAULT, null, "partyId");
|
||||
orderByItemTest("v1", v1, OrderByItem.Order.DEFAULT, null, "partyId", null, false);
|
||||
OrderByItem v2 = new OrderByItem(OrderByItem.Order.ASCENDING, null, "partyId");
|
||||
orderByItemTest("v3", v2, OrderByItem.Order.ASCENDING, null, "partyId", v1, false);
|
||||
OrderByItem v3 = new OrderByItem(OrderByItem.Order.DESCENDING, null, "partyId");
|
||||
orderByItemTest("v2", v3, OrderByItem.Order.DESCENDING, null, "partyId", v1, false);
|
||||
OrderByItem v4 = new OrderByItem(OrderByItem.Order.DEFAULT, "LOWER", "partyId");
|
||||
orderByItemTest("v4", v4, OrderByItem.Order.DEFAULT, "LOWER", "partyId", v1, false);
|
||||
OrderByItem v5 = new OrderByItem(OrderByItem.Order.DEFAULT, null, "firstName");
|
||||
orderByItemTest("v5", v5, OrderByItem.Order.DEFAULT, null, "firstName", v1, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.ofbiz.sql.test;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.ofbiz.sql.ConstantValue;
|
||||
import org.ofbiz.sql.CountAllFunction;
|
||||
import org.ofbiz.sql.CountFunction;
|
||||
import org.ofbiz.sql.FieldValue;
|
||||
import org.ofbiz.sql.FunctionCall;
|
||||
import org.ofbiz.sql.MathValue;
|
||||
import org.ofbiz.sql.NumberValue;
|
||||
import org.ofbiz.sql.ParameterValue;
|
||||
import org.ofbiz.sql.StaticValue;
|
||||
import org.ofbiz.sql.StringValue;
|
||||
import org.ofbiz.sql.Value;
|
||||
|
||||
import org.ofbiz.base.lang.SourceMonitored;
|
||||
import org.ofbiz.base.test.GenericTestCaseBase;
|
||||
|
||||
@SourceMonitored
|
||||
public class ValuesTest extends GenericTestCaseBase {
|
||||
private static final FieldValue fv1 = new FieldValue("partyId");
|
||||
private static final FieldValue fv2 = new FieldValue("a", "partyId");
|
||||
private static final FieldValue fv3 = new FieldValue(null, "partyId");
|
||||
private static final FieldValue fv4 = new FieldValue(null, "firstName");
|
||||
private static final NumberValue<Long> nv1 = NumberValue.valueOf(1);
|
||||
private static final NumberValue<Double> nv2 = NumberValue.valueOf(2D);
|
||||
private static final NumberValue<Long> nv3 = NumberValue.valueOf(3);
|
||||
|
||||
public ValuesTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
private static <V extends Value> void basicTest(String label, Class<V> clz, V v, String defaultName, String s, V o, boolean matches) {
|
||||
if (v instanceof StaticValue) {
|
||||
assertEquals(label + ":default-name", defaultName, ((StaticValue) v).getDefaultName());
|
||||
}
|
||||
assertEquals(label + ":toString", s, v.toString());
|
||||
assertNotEquals(label + ":not-equals-this", v, ValuesTest.class);
|
||||
if (o != null) {
|
||||
if (matches) {
|
||||
assertEquals(label + ":equals", o, v);
|
||||
} else {
|
||||
assertNotEquals(label + ":not-equals", o, v);
|
||||
}
|
||||
}
|
||||
ValueVisitorRecorder visitor = new ValueVisitorRecorder();
|
||||
v.accept(visitor);
|
||||
v.accept(visitor);
|
||||
assertEquals(label + ":visited", 2, visitor.counts.remove(clz).intValue());
|
||||
assertTrue(label + ":nothing-else-visited", visitor.counts.isEmpty());
|
||||
}
|
||||
|
||||
public void testCountAllFunction() {
|
||||
CountAllFunction v1 = new CountAllFunction();
|
||||
basicTest("count-all", CountAllFunction.class, v1, "COUNT", "COUNT(*)", null, false);
|
||||
}
|
||||
|
||||
private static void countFunctionTest(String label, CountFunction v, boolean isDistinct, FieldValue fv, String s, CountFunction o, boolean matches) {
|
||||
assertEquals(label + ":left", isDistinct, v.isDistinct());
|
||||
assertEquals(label + ":field-value", fv, v.getField());
|
||||
basicTest(label, CountFunction.class, v, "COUNT", s, o, matches);
|
||||
}
|
||||
|
||||
public void testCountFunction() {
|
||||
CountFunction v1 = new CountFunction(false, fv2);
|
||||
countFunctionTest("v1", v1, false, fv2, "COUNT(a.partyId)", null, false);
|
||||
CountFunction v2 = new CountFunction(true, fv2);
|
||||
countFunctionTest("v2", v2, true, fv2, "COUNT(DISTINCT a.partyId)", v1, false);
|
||||
CountFunction v3 = new CountFunction(true, fv1);
|
||||
countFunctionTest("v3", v3, true, fv1, "COUNT(DISTINCT partyId)", v1, false);
|
||||
CountFunction v4 = new CountFunction(false, fv1);
|
||||
countFunctionTest("v4", v4, false, fv1, "COUNT(partyId)", v1, false);
|
||||
CountFunction v5 = new CountFunction(false, fv2);
|
||||
countFunctionTest("v5", v5, false, fv2, "COUNT(a.partyId)", v1, true);
|
||||
}
|
||||
|
||||
private static void fieldValueTest(String label, FieldValue v, String tableName, String fieldName, String s, FieldValue o, boolean matches) {
|
||||
assertEquals(label + ":table-name", tableName, v.getTableName());
|
||||
assertEquals(label + ":field-name", fieldName, v.getFieldName());
|
||||
basicTest(label, FieldValue.class, v, fieldName, s, o, matches);
|
||||
}
|
||||
|
||||
public void testFieldValue() {
|
||||
fieldValueTest("fv1", fv1, null, "partyId", "partyId", null, false);
|
||||
fieldValueTest("fv2", fv2, "a", "partyId", "a.partyId", fv1, false);
|
||||
fieldValueTest("fv3", fv3, null, "partyId", "partyId", fv1, true);
|
||||
fieldValueTest("fv4", fv4, null, "firstName", "firstName", fv1, false);
|
||||
}
|
||||
|
||||
private static void functionCallTest(String label, FunctionCall v, String name, List<? extends Value> values, String s, FunctionCall o, boolean matches) {
|
||||
assertEquals(label + ":name", name, v.getName());
|
||||
assertEquals(label + ":arg-count", values.size(), v.getArgCount());
|
||||
assertEqualsIterable(label, values, v);
|
||||
basicTest(label, FunctionCall.class, v, name, s, o, matches);
|
||||
}
|
||||
|
||||
public void testFunctionCall() {
|
||||
FunctionCall v1 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv2));
|
||||
functionCallTest("v1", v1, "LENGTH", list(nv1, nv2), "LENGTH(1, 2.0)", null, false);
|
||||
FunctionCall v2 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv3));
|
||||
functionCallTest("v2", v2, "LENGTH", list(nv1, nv3), "LENGTH(1, 3)", v1, false);
|
||||
FunctionCall v3 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv2));
|
||||
functionCallTest("v3", v3, "LENGTH", list(nv1, nv2), "LENGTH(1, 2.0)", v1, true);
|
||||
FunctionCall v4 = new FunctionCall("TRIM", GenericTestCaseBase.<Value>list(nv1, nv2));
|
||||
functionCallTest("v4", v4, "TRIM", list(nv1, nv2), "TRIM(1, 2.0)", v1, false);
|
||||
}
|
||||
|
||||
private static void mathValueTest(String label, MathValue v, String op, List<? extends ConstantValue> values, String s, MathValue o, boolean matches) {
|
||||
assertEquals(label + ":op", op, v.getOp());
|
||||
assertEqualsIterable(label, values, v);
|
||||
basicTest(label, MathValue.class, v, null, s, o, matches);
|
||||
}
|
||||
|
||||
public void testMathValue() {
|
||||
MathValue v1 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv2));
|
||||
mathValueTest("v1", v1, "+", list(nv1, nv2), "( 1+ 2.0)", null, false);
|
||||
MathValue v2 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv3));
|
||||
mathValueTest("v2", v2, "+", list(nv1, nv3), "( 1+ 3)", v1, false);
|
||||
MathValue v3 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv2));
|
||||
mathValueTest("v3", v3, "+", list(nv1, nv2), "( 1+ 2.0)", v1, true);
|
||||
MathValue v4 = new MathValue("-", GenericTestCaseBase.<ConstantValue>list(nv1, nv3));
|
||||
mathValueTest("v4", v4, "-", list(nv1, nv3), "( 1- 3)", v1, false);
|
||||
}
|
||||
|
||||
public void testNull() {
|
||||
basicTest("null", Value.Null.class, Value.NULL, null, "NULL", null, false);
|
||||
}
|
||||
|
||||
private static <N extends Number> void numberValueTest(String label, NumberValue<N> v, N n, String s, NumberValue o, boolean matches) {
|
||||
assertEquals(label + ":number", n, v.getNumber());
|
||||
basicTest(label, NumberValue.class, v, null, s, o, matches);
|
||||
}
|
||||
|
||||
public void testNumberValue() {
|
||||
numberValueTest("nv1", nv1, Long.valueOf(1), "1", null, false);
|
||||
numberValueTest("nv2", nv2, Double.valueOf(2), "2.0", nv1, false);
|
||||
NumberValue<Long> nv3 = NumberValue.valueOf(1);
|
||||
numberValueTest("nv3", nv3, Long.valueOf(1), "1", nv1, true);
|
||||
}
|
||||
|
||||
private static void parameterValueTest(String label, ParameterValue v, String name, String s, ParameterValue o, boolean matches) {
|
||||
assertEquals(label + ":name", name, v.getName());
|
||||
basicTest(label, ParameterValue.class, v, null, s, o, matches);
|
||||
}
|
||||
|
||||
public void testParameterValue() {
|
||||
ParameterValue v1 = new ParameterValue("a");
|
||||
parameterValueTest("v1", v1, "a", "?a", null, false);
|
||||
ParameterValue v2 = new ParameterValue("b");
|
||||
parameterValueTest("v2", v2, "b", "?b", v1, false);
|
||||
ParameterValue v3 = new ParameterValue("a");
|
||||
parameterValueTest("v3", v3, "a", "?a", v1, true);
|
||||
}
|
||||
|
||||
private static void stringValueTest(String label, StringValue v, String string, String s, StringValue o, boolean matches) {
|
||||
assertEquals(label + ":name", string, v.getString());
|
||||
basicTest(label, StringValue.class, v, null, s, o, matches);
|
||||
}
|
||||
|
||||
public void testStringValue() {
|
||||
StringValue v1 = new StringValue("foo");
|
||||
stringValueTest("v1", v1, "foo", "'foo'", null, false);
|
||||
StringValue v2 = new StringValue("b'r");
|
||||
stringValueTest("v2", v2, "b'r", "'b''r'", v1, false);
|
||||
StringValue v3 = new StringValue("foo");
|
||||
stringValueTest("v3", v3, "foo", "'foo'", v1, true);
|
||||
}
|
||||
|
||||
public static class ValueVisitorRecorder extends Recorder<Class<? extends Value>> implements Value.Visitor {
|
||||
public void visit(FieldValue value) {
|
||||
record(FieldValue.class);
|
||||
}
|
||||
|
||||
public void visit(FunctionCall value) {
|
||||
record(FunctionCall.class);
|
||||
}
|
||||
|
||||
public void visit(MathValue value) {
|
||||
record(MathValue.class);
|
||||
}
|
||||
|
||||
public void visit(Value.Null value) {
|
||||
record(Value.Null.class);
|
||||
}
|
||||
|
||||
public void visit(NumberValue value) {
|
||||
record(NumberValue.class);
|
||||
}
|
||||
|
||||
public void visit(ParameterValue value) {
|
||||
record(ParameterValue.class);
|
||||
}
|
||||
|
||||
public void visit(StringValue value) {
|
||||
record(StringValue.class);
|
||||
}
|
||||
|
||||
public void visit(CountFunction value) {
|
||||
record(CountFunction.class);
|
||||
}
|
||||
|
||||
public void visit(CountAllFunction value) {
|
||||
record(CountAllFunction.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user