001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.rpc;
017:
018: import com.google.gwt.user.client.rpc.TestSetFactory.SerializableClass;
019: import com.google.gwt.user.client.rpc.TestSetFactory.SerializableDoublyLinkedNode;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027: import java.util.Vector;
028: import java.util.Map.Entry;
029:
030: /**
031: * TODO: document me.
032: */
033: public class TestSetValidator {
034:
035: public static boolean equals(boolean[] expected, boolean[] actual) {
036: if (actual == null) {
037: return false;
038: }
039:
040: if (expected.length != actual.length) {
041: return false;
042: }
043:
044: for (int i = 0; i < expected.length; ++i) {
045: if (expected[i] != actual[i]) {
046: return false;
047: }
048: }
049:
050: return true;
051: }
052:
053: public static boolean equals(byte[] expected, byte[] actual) {
054: if (actual == null) {
055: return false;
056: }
057:
058: if (actual.length != expected.length) {
059: return false;
060: }
061:
062: for (int i = 0; i < expected.length; ++i) {
063: if (expected[i] != actual[i]) {
064: return false;
065: }
066: }
067:
068: return true;
069: }
070:
071: public static boolean equals(char[] expected, char[] actual) {
072: if (actual == null) {
073: return false;
074: }
075:
076: if (actual.length != expected.length) {
077: return false;
078: }
079:
080: for (int i = 0; i < expected.length; ++i) {
081: if (expected[i] != actual[i]) {
082: return false;
083: }
084: }
085:
086: return true;
087: }
088:
089: public static boolean equals(double[] expected, double[] actual) {
090: if (actual == null) {
091: return false;
092: }
093:
094: if (actual.length != expected.length) {
095: return false;
096: }
097:
098: for (int i = 0; i < expected.length; ++i) {
099: if (expected[i] != actual[i]) {
100: return false;
101: }
102: }
103:
104: return true;
105: }
106:
107: public static boolean equals(float[] expected, float[] actual) {
108: if (actual == null) {
109: return false;
110: }
111:
112: if (actual.length != expected.length) {
113: return false;
114: }
115:
116: for (int i = 0; i < expected.length; ++i) {
117: if (expected[i] != actual[i]) {
118: return false;
119: }
120: }
121:
122: return true;
123: }
124:
125: public static boolean equals(int[] expected, int[] actual) {
126: if (actual == null) {
127: return false;
128: }
129:
130: if (actual.length != expected.length) {
131: return false;
132: }
133:
134: for (int i = 0; i < expected.length; ++i) {
135: if (expected[i] != actual[i]) {
136: return false;
137: }
138: }
139:
140: return true;
141: }
142:
143: public static boolean equals(long[] expected, long[] actual) {
144: if (actual == null) {
145: return false;
146: }
147:
148: if (actual.length != expected.length) {
149: return false;
150: }
151:
152: for (int i = 0; i < expected.length; ++i) {
153: if (expected[i] != actual[i]) {
154: return false;
155: }
156: }
157:
158: return true;
159: }
160:
161: public static boolean equals(Object[] o1, Object[] o2) {
162: if (o1 == o2) {
163: return true;
164: }
165:
166: if (o1 == null || o2 == null) {
167: return false;
168: }
169:
170: if (o1.length != o2.length) {
171: return false;
172: }
173:
174: for (int i = 0; i < o1.length; ++i) {
175: Object e1 = o1[i];
176: Object e2 = o2[i];
177:
178: if (e1 == e2) {
179: continue;
180: }
181:
182: if (e1 == null || e2 == null) {
183: return false;
184: }
185:
186: if (!e1.equals(e2)) {
187: return false;
188: }
189: }
190:
191: return true;
192: }
193:
194: public static boolean equals(short[] expected, short[] actual) {
195: if (actual == null) {
196: return false;
197: }
198:
199: if (actual.length != expected.length) {
200: return false;
201: }
202:
203: for (int i = 0; i < expected.length; ++i) {
204: if (expected[i] != actual[i]) {
205: return false;
206: }
207: }
208:
209: return true;
210: }
211:
212: public static boolean isValid(ArrayList list) {
213: if (list == null) {
214: return false;
215: }
216:
217: ArrayList reference = TestSetFactory.createArrayList();
218: if (reference.size() != list.size()) {
219: return false;
220: }
221:
222: return reference.equals(list);
223: }
224:
225: public static boolean isValid(HashSet expected, HashSet actual) {
226: if (actual == null) {
227: return false;
228: }
229:
230: if (expected.size() != actual.size()) {
231: return false;
232: }
233:
234: Iterator entryIter = expected.iterator();
235: while (entryIter.hasNext()) {
236: Object entry = entryIter.next();
237:
238: if (!actual.contains(entry)) {
239: return false;
240: }
241: }
242:
243: return true;
244: }
245:
246: public static boolean isValid(Map expected, HashMap map) {
247: if (map == null) {
248: return false;
249: }
250:
251: if (expected.size() != map.size()) {
252: return false;
253: }
254:
255: Set entries = expected.entrySet();
256: Iterator entryIter = entries.iterator();
257: while (entryIter.hasNext()) {
258: Entry entry = (Entry) entryIter.next();
259:
260: Object value = map.get(entry.getKey());
261:
262: if (value != entry.getValue()) {
263: if (value == null || entry.getValue() == null) {
264: return false;
265: }
266:
267: if (!map.get(entry.getKey()).equals(entry.getValue())) {
268: return false;
269: }
270: }
271: }
272:
273: return true;
274: }
275:
276: public static boolean isValid(SerializableClass actual) {
277: if (actual == null) {
278: return false;
279: }
280:
281: IsSerializable[] elements = actual.getElements();
282: IsSerializable elementRef = actual.getElementRef();
283:
284: if (elements == null || elementRef == null) {
285: return false;
286: }
287:
288: if (elements.length != 4) {
289: return false;
290: }
291:
292: if (elements[3] != elementRef) {
293: return false;
294: }
295:
296: return true;
297: }
298:
299: public static boolean isValid(Vector expected, Vector actual) {
300: if (actual == null) {
301: return false;
302: }
303:
304: return expected.equals(actual);
305: }
306:
307: public static boolean isValidAcyclicGraph(
308: SerializableDoublyLinkedNode actual) {
309: if (actual == null) {
310: return false;
311: }
312:
313: if (!actual.getData().equals("head")) {
314: return false;
315: }
316:
317: SerializableDoublyLinkedNode leftChild = actual.getLeftChild();
318: if (leftChild == null) {
319: return false;
320: }
321:
322: if (!leftChild.getData().equals("lchild")) {
323: return false;
324: }
325:
326: if (leftChild.getLeftChild() != null
327: || leftChild.getRightChild() != null) {
328: return false;
329: }
330:
331: SerializableDoublyLinkedNode rightChild = actual
332: .getRightChild();
333: if (rightChild == null) {
334: return false;
335: }
336:
337: if (!rightChild.getData().equals("rchild")) {
338: return false;
339: }
340:
341: if (rightChild.getLeftChild() != null
342: || rightChild.getRightChild() != null) {
343: return false;
344: }
345:
346: return true;
347: }
348:
349: public static boolean isValidComplexCyclicGraph(
350: SerializableDoublyLinkedNode actual) {
351:
352: if (actual == null) {
353: return false;
354: }
355:
356: int i = 0;
357: SerializableDoublyLinkedNode currNode = actual;
358: for (; i < 5; ++i) {
359: if (!currNode.getData().equals("n" + Integer.toString(i))) {
360: return false;
361: }
362:
363: SerializableDoublyLinkedNode nextNode = currNode
364: .getRightChild();
365: SerializableDoublyLinkedNode prevNode = currNode
366: .getLeftChild();
367:
368: if (nextNode == null || prevNode == null) {
369: return false;
370: }
371:
372: if (nextNode.getLeftChild() != currNode) {
373: return false;
374: }
375:
376: if (prevNode.getRightChild() != currNode) {
377: return false;
378: }
379:
380: currNode = currNode.getRightChild();
381: if (currNode == actual) {
382: break;
383: }
384: }
385:
386: if (i >= 4) {
387: return false;
388: }
389:
390: return true;
391: }
392:
393: public static boolean isValidTrivialCyclicGraph(
394: SerializableDoublyLinkedNode actual) {
395: if (actual == null) {
396: return false;
397: }
398:
399: if (!actual.getData().equals("head")) {
400: return false;
401: }
402:
403: SerializableDoublyLinkedNode lchild = actual.getLeftChild();
404: if (lchild == null) {
405: return false;
406: }
407:
408: SerializableDoublyLinkedNode rchild = actual.getRightChild();
409: if (rchild == null) {
410: return false;
411: }
412:
413: if (actual != lchild && actual != rchild) {
414: return false;
415: }
416:
417: return true;
418: }
419:
420: public static void rethrowException(Throwable caught) {
421: if (caught instanceof RuntimeException) {
422: throw (RuntimeException) caught;
423: } else {
424: throw new RuntimeException(caught);
425: }
426: }
427:
428: }
|