KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > QueryTest


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.apache.torque.TorqueException;
53 import org.tigris.scarab.test.BaseScarabTestCase;
54 import org.tigris.scarab.util.ScarabException;
55
56 /**
57  * A Testing Suite for the om.Query class.
58  *
59  * @author <a HREF="mailto:mumbly@oneofus.org">Tim McNerney</a>
60  * @version $Id: QueryTest.java 9276 2004-11-23 14:02:30Z dep4b $
61  */

62 public class QueryTest extends BaseScarabTestCase
63 {
64     private Query query = null;
65     private Query query1 = null;
66
67
68
69     public void setUp() throws Exception JavaDoc
70     {
71         super.setUp();
72         query = Query.getInstance();
73         query1 = Query.getInstance();
74
75     }
76
77     /**
78      * May be screwing up data for other tests
79      * @throws Exception
80      */

81     public void OFFtestSaveAndDelete() throws Exception JavaDoc
82     {
83         System.out.println("\ntestSave()");
84         createQuery();
85         //
86
// Make sure the query was persisted correctly.
87
//
88
Query retQuery = QueryManager.getInstance(query.getQueryId());
89         assertEquals(query.getName(), retQuery.getName());
90         assertEquals(query.getValue(), retQuery.getValue());
91
92         
93         System.out.println("\ntestSetDeleted()");
94
95         //
96
// We expect user5 to fail in deleting since not the
97
// owner.
98
//
99
try
100         {
101             query.delete(getUser5());
102             fail("Shoud have thrown an exception, user 5 is not the owner!");
103         }
104         catch (Exception JavaDoc ex)
105         {
106             assertTrue(ex instanceof ScarabException);
107         }
108         retQuery = QueryManager.getInstance(query.getQueryId(), false);
109         assertTrue(!retQuery.getDeleted());
110         
111         // user 2 should succeed in deleting, as the owner.
112
query.delete(getUser2());
113         retQuery = QueryManager.getInstance(query.getQueryId(), false);
114         assertTrue(retQuery.getDeleted());
115
116     }
117
118     /**
119      * @throws TorqueException
120      * @throws Exception
121      */

122     private void createQuery() throws TorqueException, Exception JavaDoc
123     {
124         query.setUserId(getUser2().getUserId());
125         query.setName("Test query 1");
126         query.setValue("&searchId=1&searchisp=asc");
127         query.setDescription("Description for test query 1");
128         query.setModuleId(getModule().getModuleId());
129         query.setIssueType(getDefaultIssueType());
130         query.setApproved(false);
131         query.setScopeId(new Integer JavaDoc(1));
132         query.save();
133     }
134
135     public void testSaveAndSendEmail() throws Exception JavaDoc
136     {
137         System.out.println("\ntestSaveAndSendEmail()");
138         query1.setUserId(new Integer JavaDoc(2));
139         query1.setName("Test query 2");
140         query1.setValue("&searchId=2&searchisp=asc");
141         query1.setDescription("Description for test query 2");
142         query1.setModuleId(getModule().getModuleId());
143         query1.setIssueType(getDefaultIssueType());
144         query1.setScopeId(Scope.PERSONAL__PK);
145         query1.saveAndSendEmail(getUser1(), getModule(), null);
146         //
147
// Make sure the query was persisted correctly.
148
//
149
Query retQuery = QueryManager.getInstance(query1.getQueryId(), false);
150         assertEquals(query1.getName(), retQuery.getName());
151         assertEquals(query1.getValue(), retQuery.getValue());
152         assertEquals(query1.getScope(), retQuery.getScope());
153
154     }
155     
156     public void testSaveOnlyWithModuleScope() throws Exception JavaDoc
157     {
158         
159         System.out.println("\ntestSaveAndSendEmail()");
160         query1.setUserId(new Integer JavaDoc(1));
161         query1.setName("Test query 2");
162         query1.setValue("&searchId=2&searchisp=asc");
163         query1.setDescription("Description for test query 2");
164         query1.setModuleId(getModule().getModuleId());
165         query1.setIssueType(getDefaultIssueType());
166         query1.setScopeId(Scope.MODULE__PK);
167         query1.save();
168         //
169
// Make sure the query was persisted correctly.
170
//
171
Query retQuery = QueryManager.getInstance(query1.getQueryId(), false);
172         assertEquals(query1.getName(), retQuery.getName());
173         assertEquals(query1.getValue(), retQuery.getValue());
174         assertEquals(query1.getScope(), retQuery.getScope());
175
176     }
177 /*
178     public void testGetExecuteLink() throws Exception
179     {
180         System.out.println("\ntestGetExecuteLink()");
181         String exLink = query.getExecuteLink("dummy");
182         assertEquals("dummy/template/IssueList.vm" +
183             "?action=Search&eventSubmit_doSearch=Search&resultsperpage=25" +
184             "&pagenum=1&searchId=1&searchisp=asc&remcurmitl=true", exLink);
185     }
186
187     public void testGetEditLink() throws Exception
188     {
189         System.out.println("\ntestGetEditLink()");
190         String edLink = query.getEditLink("dummy");
191         assertEquals("dummy/template/EditQuery.vm?queryId=" +
192                      query.getQueryId() +
193                      "&searchId=1&searchisp=asc&remcurmitl=true", edLink);
194     }
195 */

196     public void testGetAllQueryTypes() throws Exception JavaDoc
197     {
198         String JavaDoc[] scopeNames = {"personal", "module"};
199         System.out.println("\ntestGetAllQueryTypes()");
200         List JavaDoc scopes = ScopePeer.getAllScopes();
201         assertEquals(scopes.size(), 2);
202         Iterator JavaDoc it = scopes.iterator();
203         Scope scope;
204         for (int i = 0; it.hasNext(); i++)
205         {
206             scope = (Scope) it.next();
207             System.out.println("getAllScopes().getName(): <" + scope.getName() + "> expected: <" + scopeNames[i] + ">");
208             assertEquals(scope.getName(), scopeNames[i]);
209         }
210     }
211
212     public void testApprove() throws Exception JavaDoc
213     {
214         System.out.println("\ntestSetApproved()");
215         createQuery();
216         //
217
// We expect user5 to fail in approving and so we catch
218
// the exceptions and proceed. user2 should be successful
219
// in approving.
220
//
221
try
222         {
223             query.approve(getUser5(), true);
224             fail("user1 should fail in approving the query");
225         }
226         catch (Exception JavaDoc ex)
227         {
228             assertTrue(ex instanceof ScarabException);
229         }
230       
231         query.approve(getUser2(), true);
232         assertTrue(query.getApproved());
233     }
234
235     public void testSubscribe() throws Exception JavaDoc
236     {
237         System.out.println("\ntestSubscribe()");
238         createQuery();
239         query.subscribe(getUser2(), new Integer JavaDoc(1));
240         RQueryUser rqu = query.getRQueryUser(getUser2());
241         query.subscribe(getUser2(), new Integer JavaDoc(1));
242         assertTrue(rqu.getIsSubscribed());
243         // Now if unsubscribed, should fail to return RQueryUser
244
query.unSubscribe(getUser2());
245         
246             rqu = query.getRQueryUser(getUser2());
247      
248
249     }
250
251     public void testCopy() throws Exception JavaDoc
252     {
253         Query newQuery = query.copy();
254         assertEquals(newQuery.getName(), query.getName());
255         assertEquals(newQuery.getUserId(), query.getUserId());
256         assertEquals(newQuery.getValue(), query.getValue());
257         RQueryUser rqu = query.getRQueryUser(getUser1());
258         RQueryUser rquNew = newQuery.getRQueryUser(getUser1());
259         assertEquals(rqu.getIsSubscribed(), rquNew.getIsSubscribed());
260     }
261
262  
263 }
264
Popular Tags