001: package org.apache.ojb.broker;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020:
021: import org.apache.ojb.broker.query.Criteria;
022: import org.apache.ojb.broker.query.QueryByCriteria;
023: import org.apache.ojb.broker.query.QueryFactory;
024: import org.apache.ojb.broker.query.Query;
025: import org.apache.ojb.junit.PBTestCase;
026:
027: /**
028: * This class tests how OJB handles querying against an empty
029: * table in DB.
030: *
031: * @version $Id: EmptyTableTest.java,v 1.1.2.2 2005/12/21 22:31:23 tomdz Exp $
032: */
033: public class EmptyTableTest extends PBTestCase {
034: public EmptyTableTest(String name) {
035: super (name);
036: }
037:
038: public static void main(String[] args) {
039: final String[] arr = { EmptyTableTest.class.getName() };
040: junit.textui.TestRunner.main(arr);
041: }
042:
043: public void setUp() throws Exception {
044: super .setUp();
045: QueryByCriteria query = QueryFactory.newQuery(TestObject.class,
046: new Criteria());
047: broker.deleteByQuery(query);
048: broker.clearCache();
049: }
050:
051: public void testObjectByQuery_1() throws Exception {
052: String dummy = "nothing_to_find";
053: Criteria criteria = new Criteria();
054: criteria.addEqualTo("name", dummy);
055: QueryByCriteria query = QueryFactory.newQuery(TestObject.class,
056: criteria);
057: Object result = broker.getObjectByQuery(query);
058: assertNull("We don't expect a result object", result);
059: }
060:
061: public void testObjectByExample_1() throws Exception {
062: String dummy = "nothing_to_find";
063: TestObject t = new TestObject();
064: t.setId(new Integer(1234));
065: t.setName(dummy);
066: Query query = QueryFactory.newQuery(t);
067: Object result = broker.getObjectByQuery(query);
068: assertNull("We don't expect a result object", result);
069: }
070:
071: public void testCollectionByQuery_1() throws Exception {
072: String dummy = "nothing_to_find";
073: Criteria criteria = new Criteria();
074: criteria.addEqualTo("name", dummy);
075: Query query = QueryFactory.newQuery(TestObject.class, criteria);
076: Collection result = broker.getCollectionByQuery(query);
077: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
078: iterator.next();
079: fail("We don't expect any result objects");
080: }
081: }
082:
083: public void testCollectionByQuery_2() throws Exception {
084: String dummy = "nothing_to_find";
085: Criteria criteria = new Criteria();
086: criteria.addEqualTo("name", dummy);
087: Query query = QueryFactory.newQuery(TestObject.class, criteria);
088: // doesn't make sense to use this option here - only for test
089: query.setStartAtIndex(1);
090: query.setEndAtIndex(20);
091: Collection result = broker.getCollectionByQuery(query);
092: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
093: iterator.next();
094: fail("We don't expect any result objects");
095: }
096: }
097:
098: public void testIteratorByQuery_1() throws Exception {
099: String dummy = "nothing_to_find";
100: Criteria criteria = new Criteria();
101: criteria.addEqualTo("name", dummy);
102: Query query = QueryFactory.newQuery(TestObject.class, criteria);
103: Iterator result = broker.getIteratorByQuery(query);
104: while (result.hasNext()) {
105: result.next();
106: fail("We don't expect any result objects");
107: }
108: }
109:
110: public void testIteratorByQuery_2() throws Exception {
111: String dummy = "nothing_to_find";
112: Criteria criteria = new Criteria();
113: criteria.addEqualTo("name", dummy);
114: Query query = QueryFactory.newQuery(TestObject.class, criteria);
115: query.setStartAtIndex(1);
116: query.setEndAtIndex(20);
117: Iterator result = broker.getIteratorByQuery(query);
118: while (result.hasNext()) {
119: result.next();
120: fail("We don't expect any result objects");
121: }
122: }
123:
124: //===================================================================
125: // inner class
126: //===================================================================
127:
128: public static class TestObject {
129: private Integer id;
130: private String name;
131:
132: public Integer getId() {
133: return id;
134: }
135:
136: public void setId(Integer id) {
137: this .id = id;
138: }
139:
140: public String getName() {
141: return name;
142: }
143:
144: public void setName(String name) {
145: this.name = name;
146: }
147: }
148: }
|