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;
022:
023: import com.db4o.nativequery.expr.*;
024: import com.db4o.nativequery.expr.cmp.*;
025: import com.db4o.nativequery.expr.cmp.operand.*;
026:
027: import db4ounit.*;
028:
029: public class ExpressionTestCase implements TestCase {
030: public void testEqualsHashCodeFieldValue() {
031: FieldValue fieldValue = new FieldValue(
032: PredicateFieldRoot.INSTANCE, "a");
033: assertEqualsHashCode(fieldValue, new FieldValue(
034: PredicateFieldRoot.INSTANCE, "a"));
035: assertNotEquals(fieldValue, new FieldValue(
036: PredicateFieldRoot.INSTANCE, "b"));
037: }
038:
039: public void testEqualsHashCodeConst() {
040: BoolConstExpression expr = BoolConstExpression.TRUE;
041: assertEqualsHashCode(expr, BoolConstExpression.TRUE);
042: assertNotEquals(expr, new FieldValue(
043: PredicateFieldRoot.INSTANCE, "b"));
044: }
045:
046: public void testEqualsHashCodeNot() {
047: NotExpression expr = new NotExpression(BoolConstExpression.TRUE);
048: assertEqualsHashCode(expr, new NotExpression(
049: BoolConstExpression.TRUE));
050: assertNotEquals(expr, new NotExpression(
051: BoolConstExpression.FALSE));
052: }
053:
054: public void testEqualsHashCodeAnd() {
055: AndExpression expr = new AndExpression(
056: BoolConstExpression.TRUE, BoolConstExpression.FALSE);
057: assertEqualsHashCode(expr, new AndExpression(
058: BoolConstExpression.TRUE, BoolConstExpression.FALSE));
059: assertNotEquals(expr, new AndExpression(
060: BoolConstExpression.FALSE, BoolConstExpression.FALSE));
061: assertNotEquals(expr, new AndExpression(
062: BoolConstExpression.TRUE, BoolConstExpression.TRUE));
063: assertEqualsHashCode(expr, new AndExpression(
064: BoolConstExpression.FALSE, BoolConstExpression.TRUE));
065: }
066:
067: public void testEqualsHashCodeOr() {
068: OrExpression expr = new OrExpression(BoolConstExpression.TRUE,
069: BoolConstExpression.FALSE);
070: assertEqualsHashCode(expr, new OrExpression(
071: BoolConstExpression.TRUE, BoolConstExpression.FALSE));
072: assertNotEquals(expr, new OrExpression(
073: BoolConstExpression.FALSE, BoolConstExpression.FALSE));
074: assertNotEquals(expr, new OrExpression(
075: BoolConstExpression.TRUE, BoolConstExpression.TRUE));
076: assertEqualsHashCode(expr, new OrExpression(
077: BoolConstExpression.FALSE, BoolConstExpression.TRUE));
078: }
079:
080: public void testEqualsHashCodeComparison() {
081: FieldValue[] fieldVals = {
082: new FieldValue(PredicateFieldRoot.INSTANCE, "A"),
083: new FieldValue(CandidateFieldRoot.INSTANCE, "B") };
084: ConstValue[] constVals = { new ConstValue("X"),
085: new ConstValue("Y") };
086: ComparisonExpression expr = new ComparisonExpression(
087: fieldVals[0], constVals[0], ComparisonOperator.EQUALS);
088: assertEqualsHashCode(expr, new ComparisonExpression(
089: fieldVals[0], constVals[0], ComparisonOperator.EQUALS));
090: assertNotEquals(expr, new ComparisonExpression(fieldVals[1],
091: constVals[0], ComparisonOperator.EQUALS));
092: assertNotEquals(expr, new ComparisonExpression(fieldVals[0],
093: constVals[1], ComparisonOperator.EQUALS));
094: assertNotEquals(expr, new ComparisonExpression(fieldVals[0],
095: constVals[0], ComparisonOperator.SMALLER));
096: }
097:
098: private void assertEqualsHashCode(Object obj, Object same) {
099: Assert.isTrue(obj.equals(obj));
100: Assert.isTrue(obj.equals(same));
101: Assert.isTrue(same.equals(obj));
102: Assert.isFalse(obj.equals(null));
103: Assert.isFalse(obj.equals(new Object()));
104: Assert.areEqual(obj.hashCode(), same.hashCode());
105: }
106:
107: private void assertNotEquals(Object obj, Object other) {
108: Assert.isFalse(obj.equals(other));
109: Assert.isFalse(other.equals(obj));
110: }
111: }
|