KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > EmptyTableTest


1 package org.apache.ojb.broker;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.ojb.broker.query.Criteria;
22 import org.apache.ojb.broker.query.QueryByCriteria;
23 import org.apache.ojb.broker.query.QueryFactory;
24 import org.apache.ojb.broker.query.Query;
25 import org.apache.ojb.junit.PBTestCase;
26
27 /**
28  * This class tests how OJB handles querying against an empty
29  * table in DB.
30  *
31  * @version $Id: EmptyTableTest.java,v 1.1.2.2 2005/12/21 22:31:23 tomdz Exp $
32  */

33 public class EmptyTableTest extends PBTestCase
34 {
35     public EmptyTableTest(String JavaDoc name)
36     {
37         super(name);
38     }
39
40     public static void main(String JavaDoc[] args)
41     {
42         final String JavaDoc[] arr = {EmptyTableTest.class.getName()};
43         junit.textui.TestRunner.main(arr);
44     }
45
46     public void setUp() throws Exception JavaDoc
47     {
48         super.setUp();
49         QueryByCriteria query = QueryFactory.newQuery(TestObject.class, new Criteria());
50         broker.deleteByQuery(query);
51         broker.clearCache();
52     }
53
54     public void testObjectByQuery_1() throws Exception JavaDoc
55     {
56         String JavaDoc dummy = "nothing_to_find";
57         Criteria criteria = new Criteria();
58         criteria.addEqualTo("name", dummy);
59         QueryByCriteria query = QueryFactory.newQuery(TestObject.class, criteria);
60         Object JavaDoc result = broker.getObjectByQuery(query);
61         assertNull("We don't expect a result object", result);
62     }
63
64     public void testObjectByExample_1() throws Exception JavaDoc
65     {
66         String JavaDoc dummy = "nothing_to_find";
67         TestObject t = new TestObject();
68         t.setId(new Integer JavaDoc(1234));
69         t.setName(dummy);
70         Query query = QueryFactory.newQuery(t);
71         Object JavaDoc result = broker.getObjectByQuery(query);
72         assertNull("We don't expect a result object", result);
73     }
74
75     public void testCollectionByQuery_1() throws Exception JavaDoc
76     {
77         String JavaDoc dummy = "nothing_to_find";
78         Criteria criteria = new Criteria();
79         criteria.addEqualTo("name", dummy);
80         Query query = QueryFactory.newQuery(TestObject.class, criteria);
81         Collection JavaDoc result = broker.getCollectionByQuery(query);
82         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
83         {
84             iterator.next();
85             fail("We don't expect any result objects");
86         }
87     }
88
89     public void testCollectionByQuery_2() throws Exception JavaDoc
90     {
91         String JavaDoc dummy = "nothing_to_find";
92         Criteria criteria = new Criteria();
93         criteria.addEqualTo("name", dummy);
94         Query query = QueryFactory.newQuery(TestObject.class, criteria);
95         // doesn't make sense to use this option here - only for test
96
query.setStartAtIndex(1);
97         query.setEndAtIndex(20);
98         Collection JavaDoc result = broker.getCollectionByQuery(query);
99         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
100         {
101             iterator.next();
102             fail("We don't expect any result objects");
103         }
104     }
105
106     public void testIteratorByQuery_1() throws Exception JavaDoc
107     {
108         String JavaDoc dummy = "nothing_to_find";
109         Criteria criteria = new Criteria();
110         criteria.addEqualTo("name", dummy);
111         Query query = QueryFactory.newQuery(TestObject.class, criteria);
112         Iterator JavaDoc result = broker.getIteratorByQuery(query);
113         while(result.hasNext())
114         {
115             result.next();
116             fail("We don't expect any result objects");
117         }
118     }
119
120     public void testIteratorByQuery_2() throws Exception JavaDoc
121     {
122         String JavaDoc dummy = "nothing_to_find";
123         Criteria criteria = new Criteria();
124         criteria.addEqualTo("name", dummy);
125         Query query = QueryFactory.newQuery(TestObject.class, criteria);
126         query.setStartAtIndex(1);
127         query.setEndAtIndex(20);
128         Iterator JavaDoc result = broker.getIteratorByQuery(query);
129         while(result.hasNext())
130         {
131             result.next();
132             fail("We don't expect any result objects");
133         }
134     }
135
136
137
138
139     //===================================================================
140
// inner class
141
//===================================================================
142

143     public static class TestObject
144     {
145         private Integer JavaDoc id;
146         private String JavaDoc name;
147
148         public Integer JavaDoc getId()
149         {
150             return id;
151         }
152
153         public void setId(Integer JavaDoc id)
154         {
155             this.id = id;
156         }
157
158         public String JavaDoc getName()
159         {
160             return name;
161         }
162
163         public void setName(String JavaDoc name)
164         {
165             this.name = name;
166         }
167     }
168 }
169
Popular Tags