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.db4ounit.common.btree;
022:
023: import com.db4o.foundation.*;
024:
025: import db4ounit.Assert;
026:
027: public class ExpectingVisitor implements Visitor4 {
028:
029: private static final boolean DEBUG = false;
030:
031: private final Object[] _expected;
032:
033: private final boolean _obeyOrder;
034:
035: private final Collection4 _unexpected = new Collection4();
036:
037: private boolean _ignoreUnexpected;
038:
039: private int _cursor;
040:
041: private static final Object FOUND = new Object() {
042: public String toString() {
043: return "[FOUND]";
044: }
045: };
046:
047: public ExpectingVisitor(Object[] results, boolean obeyOrder,
048: boolean ignoreUnexpected) {
049: _expected = new Object[results.length];
050: System.arraycopy(results, 0, _expected, 0, results.length);
051: _obeyOrder = obeyOrder;
052: _ignoreUnexpected = ignoreUnexpected;
053: }
054:
055: public ExpectingVisitor(Object[] results) {
056: this (results, false, false);
057: }
058:
059: public ExpectingVisitor(Object singleObject) {
060: this (new Object[] { singleObject });
061: }
062:
063: /**
064: * Expect empty
065: */
066: public ExpectingVisitor() {
067: this (new Object[0]);
068: }
069:
070: public void visit(Object obj) {
071: if (_obeyOrder) {
072: visitOrdered(obj);
073: } else {
074: visitUnOrdered(obj);
075: }
076: }
077:
078: private void visitOrdered(Object obj) {
079: if (_cursor < _expected.length) {
080: if (areEqual(_expected[_cursor], obj)) {
081: ods("Expected OK: " + obj.toString());
082: _expected[_cursor] = FOUND;
083: _cursor++;
084: return;
085: }
086: }
087: unexpected(obj);
088: }
089:
090: private void unexpected(Object obj) {
091: if (_ignoreUnexpected) {
092: return;
093: }
094: _unexpected.add(obj);
095: ods("Unexpected: " + obj);
096: }
097:
098: private void visitUnOrdered(Object obj) {
099: for (int i = 0; i < _expected.length; i++) {
100: final Object expectedItem = _expected[i];
101: if (areEqual(obj, expectedItem)) {
102: ods("Expected OK: " + obj);
103: _expected[i] = FOUND;
104: return;
105: }
106: }
107: unexpected(obj);
108: }
109:
110: private boolean areEqual(Object obj, final Object expectedItem) {
111: return expectedItem == obj
112: || (expectedItem != null && obj != null && expectedItem
113: .equals(obj));
114: }
115:
116: private static void ods(String message) {
117: if (DEBUG) {
118: System.out.println(message);
119: }
120: }
121:
122: public void assertExpectations() {
123: if (_unexpected.size() > 0) {
124: Assert.fail("UNEXPECTED: " + _unexpected.toString());
125: }
126: for (int i = 0; i < _expected.length; i++) {
127: Assert.areSame(FOUND, _expected[i]);
128: }
129: }
130:
131: }
|