001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.nativequery.expr.build;
022:
023: import com.db4o.nativequery.expr.AndExpression;
024: import com.db4o.nativequery.expr.BoolConstExpression;
025: import com.db4o.nativequery.expr.ComparisonExpression;
026: import com.db4o.nativequery.expr.Expression;
027: import com.db4o.nativequery.expr.NotExpression;
028: import com.db4o.nativequery.expr.OrExpression;
029: import com.db4o.nativequery.expr.build.ExpressionBuilder;
030: import com.db4o.nativequery.expr.cmp.ComparisonOperator;
031: import com.db4o.nativequery.expr.cmp.operand.*;
032:
033: import db4ounit.Assert;
034: import db4ounit.TestCase;
035: import db4ounit.TestLifeCycle;
036:
037: public class ExpressionBuilderTestCase implements TestCase,
038: TestLifeCycle {
039: private MockComparisonExpressionBuilder mockBuilder;
040: private ExpressionBuilder builder;
041: private Expression expr;
042: private Expression other;
043:
044: public void setUp() throws Exception {
045: mockBuilder = new MockComparisonExpressionBuilder();
046: builder = new ExpressionBuilder();
047: expr = mockBuilder.build();
048: other = mockBuilder.build();
049: }
050:
051: public void testConstant() {
052: Assert.areSame(BoolConstExpression.TRUE, builder
053: .constant(Boolean.TRUE));
054: Assert.areSame(BoolConstExpression.FALSE, builder
055: .constant(Boolean.FALSE));
056: // TODO: Move to const expr (or expr) test
057: Assert.areEqual(BoolConstExpression.FALSE, BoolConstExpression
058: .expr(false));
059: Assert.areEqual(BoolConstExpression.TRUE, BoolConstExpression
060: .expr(true));
061: }
062:
063: public void testNot() {
064: Assert.areSame(BoolConstExpression.FALSE, builder
065: .not(BoolConstExpression.TRUE));
066: Assert.areSame(BoolConstExpression.TRUE, builder
067: .not(BoolConstExpression.FALSE));
068: Assert.areSame(BoolConstExpression.TRUE, builder.not(builder
069: .not(BoolConstExpression.TRUE)));
070: Assert.areSame(BoolConstExpression.FALSE, builder.not(builder
071: .not(BoolConstExpression.FALSE)));
072: Assert.areEqual(new NotExpression(expr), builder.not(expr));
073: Assert.areEqual(new ComparisonExpression(new FieldValue(
074: CandidateFieldRoot.INSTANCE, "foo"), new ConstValue(
075: Boolean.TRUE), ComparisonOperator.EQUALS), builder
076: .not(new ComparisonExpression(new FieldValue(
077: CandidateFieldRoot.INSTANCE, "foo"),
078: new ConstValue(Boolean.FALSE),
079: ComparisonOperator.EQUALS)));
080: }
081:
082: public void testAnd() {
083: Assert.areSame(BoolConstExpression.FALSE, builder.and(
084: BoolConstExpression.FALSE, expr));
085: Assert.areSame(BoolConstExpression.FALSE, builder.and(expr,
086: BoolConstExpression.FALSE));
087: Assert.areSame(expr, builder
088: .and(BoolConstExpression.TRUE, expr));
089: Assert.areSame(expr, builder
090: .and(expr, BoolConstExpression.TRUE));
091: Assert.areEqual(expr, builder.and(expr, expr));
092: Assert.areEqual(BoolConstExpression.FALSE, builder.and(expr,
093: builder.not(expr)));
094: Assert.areEqual(new AndExpression(expr, other), builder.and(
095: expr, other));
096: }
097:
098: public void testOr() {
099: Assert.areSame(BoolConstExpression.TRUE, builder.or(
100: BoolConstExpression.TRUE, expr));
101: Assert.areSame(BoolConstExpression.TRUE, builder.or(expr,
102: BoolConstExpression.TRUE));
103: Assert.areSame(expr, builder
104: .or(BoolConstExpression.FALSE, expr));
105: Assert.areSame(expr, builder
106: .or(expr, BoolConstExpression.FALSE));
107: Assert.areSame(expr, builder.or(expr, expr));
108: Assert.areEqual(BoolConstExpression.TRUE, builder.or(expr,
109: builder.not(expr)));
110: Assert.areEqual(new OrExpression(expr, other), builder.or(expr,
111: other));
112: }
113:
114: public void testIfThenElse() {
115: Assert.areSame(expr, builder.ifThenElse(
116: BoolConstExpression.TRUE, expr, other));
117: Assert.areSame(other, builder.ifThenElse(
118: BoolConstExpression.FALSE, expr, other));
119: Assert.areSame(BoolConstExpression.TRUE, builder.ifThenElse(
120: expr, BoolConstExpression.TRUE,
121: BoolConstExpression.TRUE));
122: Assert.areSame(BoolConstExpression.FALSE, builder.ifThenElse(
123: expr, BoolConstExpression.FALSE,
124: BoolConstExpression.FALSE));
125: Assert.areSame(expr, builder.ifThenElse(expr,
126: BoolConstExpression.TRUE, BoolConstExpression.FALSE));
127: Assert.areEqual(new NotExpression(expr), builder.ifThenElse(
128: expr, BoolConstExpression.FALSE,
129: BoolConstExpression.TRUE));
130: Assert.areEqual(builder.or(expr, other), builder.ifThenElse(
131: expr, BoolConstExpression.TRUE, other));
132: // FIXME more compund boolean constraint tests
133: //Assert.areEqual(builder.or(expr,builder.and(builder.not(expr),other)),builder.ifThenElse(expr,BoolConstExpression.TRUE,other));
134: }
135:
136: public void testCombined() {
137: Expression a = mockBuilder.build();
138: Expression b = mockBuilder.build();
139: Expression exp1 = builder
140: .and(a, builder.constant(Boolean.TRUE));
141: Expression exp2 = builder.and(BoolConstExpression.FALSE,
142: builder.not(b));
143: Expression exp = builder.or(exp1, exp2);
144: Assert.areEqual(a, exp);
145: }
146:
147: public void tearDown() throws Exception {
148: }
149: }
|