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.jre12.querying;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class ObjectSetCollectionAPITestCase extends
032: AbstractDb4oTestCase {
033:
034: private static final int ID = 42;
035:
036: private static class Data {
037: private int _id;
038:
039: public Data(int id) {
040: _id = id;
041: }
042: }
043:
044: protected void store() throws Exception {
045: store(new Data(ID));
046: }
047:
048: public void testIteratorForClassQuery() {
049: assertIteratorRepeat(classQuery(), new IteratorAssertion());
050: }
051:
052: public void testIteratorForCustomQuery() {
053: assertIteratorRepeat(customQuery(), new IteratorAssertion());
054: }
055:
056: public void testToArrayForClassQuery() {
057: assertIteratorRepeat(classQuery(), new ToArrayAssertion());
058: }
059:
060: public void testToArrayForCustomQuery() {
061: assertIteratorRepeat(customQuery(), new ToArrayAssertion());
062: }
063:
064: private Query classQuery() {
065: return newQuery(Data.class);
066: }
067:
068: private Query customQuery() {
069: Query query = classQuery();
070: query.descend("_id").constrain(new Integer(ID));
071: return query;
072: }
073:
074: private void assertIteratorRepeat(Query query,
075: ObjectSetAssertion assertion) {
076: ObjectSet result = query.execute();
077: for (int round = 0; round < 2; round++) {
078: assertion.check(result);
079: }
080: }
081:
082: private static interface ObjectSetAssertion {
083: void check(ObjectSet result);
084: }
085:
086: private static class IteratorAssertion implements
087: ObjectSetAssertion {
088: public void check(ObjectSet result) {
089: Iterator iter = result.iterator();
090: int count = 0;
091: while (iter.hasNext()) {
092: Assert.isNotNull(iter.next());
093: count++;
094: }
095: Assert.areEqual(1, count);
096: }
097: }
098:
099: private static class ToArrayAssertion implements ObjectSetAssertion {
100: public void check(ObjectSet result) {
101: Object[] arr = result.toArray();
102: Assert.areEqual(1, arr.length);
103: Assert.isNotNull(arr[0]);
104: }
105: }
106:
107: }
|