KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > btree > ExpectingVisitor


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.btree;
22
23 import com.db4o.foundation.*;
24
25 import db4ounit.Assert;
26
27
28 public class ExpectingVisitor implements Visitor4{
29     
30     private static final boolean DEBUG = false;
31     
32     private final Object JavaDoc[] _expected;
33     
34     private final boolean _obeyOrder;
35     
36     private final Collection4 _unexpected = new Collection4();
37     
38     private boolean _ignoreUnexpected;
39     
40     private int _cursor;
41     
42     private static final Object JavaDoc FOUND = new Object JavaDoc() {
43         public String JavaDoc toString() {
44             return "[FOUND]";
45         }
46     };
47     
48     public ExpectingVisitor(Object JavaDoc[] results, boolean obeyOrder, boolean ignoreUnexpected){
49         _expected = results;
50         _obeyOrder = obeyOrder;
51         _ignoreUnexpected = ignoreUnexpected;
52     }
53     
54     public ExpectingVisitor(Object JavaDoc[] results){
55         this(results, false, false);
56     }
57     
58     public ExpectingVisitor(Object JavaDoc singleObject){
59         this(new Object JavaDoc[] { singleObject });
60     }
61
62     /**
63      * Expect empty
64      */

65     public ExpectingVisitor(){
66         this(new Object JavaDoc[0]);
67     }
68
69     public void visit(Object JavaDoc obj) {
70         if(_obeyOrder){
71             visitOrdered(obj);
72         }else{
73             visitUnOrdered(obj);
74         }
75     }
76     
77     private void visitOrdered(Object JavaDoc obj){
78         if(_cursor < _expected.length){
79             if(areEqual(_expected[_cursor], obj)){
80                 ods("Expected OK: " + obj.toString());
81                 _expected[_cursor] = FOUND;
82                 _cursor ++;
83                 return;
84             }
85         }
86         unexpected(obj);
87     }
88
89     private void unexpected(Object JavaDoc obj) {
90         if(_ignoreUnexpected){
91             return;
92         }
93         _unexpected.add(obj);
94         ods("Unexpected: " + obj);
95     }
96     
97     private void visitUnOrdered(Object JavaDoc obj){
98         for (int i = 0; i < _expected.length; i++) {
99             final Object JavaDoc expectedItem = _expected[i];
100             if(areEqual(obj, expectedItem)){
101                 ods("Expected OK: " + obj);
102                 _expected[i] = FOUND;
103                 return;
104             }
105         }
106         unexpected(obj);
107     }
108
109     private boolean areEqual(Object JavaDoc obj, final Object JavaDoc expectedItem) {
110         return expectedItem == obj
111         || (expectedItem != null
112             && obj != null
113             && expectedItem.equals(obj));
114     }
115     
116     private static void ods(String JavaDoc 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 }
132
Popular Tags