001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.util.Iterator;
050: import java.util.List;
051:
052: import org.apache.torque.TorqueException;
053: import org.tigris.scarab.test.BaseScarabTestCase;
054: import org.tigris.scarab.util.ScarabException;
055:
056: /**
057: * A Testing Suite for the om.Query class.
058: *
059: * @author <a href="mailto:mumbly@oneofus.org">Tim McNerney</a>
060: * @version $Id: QueryTest.java 9276 2004-11-23 14:02:30Z dep4b $
061: */
062: public class QueryTest extends BaseScarabTestCase {
063: private Query query = null;
064: private Query query1 = null;
065:
066: public void setUp() throws Exception {
067: super .setUp();
068: query = Query.getInstance();
069: query1 = Query.getInstance();
070:
071: }
072:
073: /**
074: * May be screwing up data for other tests
075: * @throws Exception
076: */
077: public void OFFtestSaveAndDelete() throws Exception {
078: System.out.println("\ntestSave()");
079: createQuery();
080: //
081: // Make sure the query was persisted correctly.
082: //
083: Query retQuery = QueryManager.getInstance(query.getQueryId());
084: assertEquals(query.getName(), retQuery.getName());
085: assertEquals(query.getValue(), retQuery.getValue());
086:
087: System.out.println("\ntestSetDeleted()");
088:
089: //
090: // We expect user5 to fail in deleting since not the
091: // owner.
092: //
093: try {
094: query.delete(getUser5());
095: fail("Shoud have thrown an exception, user 5 is not the owner!");
096: } catch (Exception ex) {
097: assertTrue(ex instanceof ScarabException);
098: }
099: retQuery = QueryManager.getInstance(query.getQueryId(), false);
100: assertTrue(!retQuery.getDeleted());
101:
102: // user 2 should succeed in deleting, as the owner.
103: query.delete(getUser2());
104: retQuery = QueryManager.getInstance(query.getQueryId(), false);
105: assertTrue(retQuery.getDeleted());
106:
107: }
108:
109: /**
110: * @throws TorqueException
111: * @throws Exception
112: */
113: private void createQuery() throws TorqueException, Exception {
114: query.setUserId(getUser2().getUserId());
115: query.setName("Test query 1");
116: query.setValue("&searchId=1&searchisp=asc");
117: query.setDescription("Description for test query 1");
118: query.setModuleId(getModule().getModuleId());
119: query.setIssueType(getDefaultIssueType());
120: query.setApproved(false);
121: query.setScopeId(new Integer(1));
122: query.save();
123: }
124:
125: public void testSaveAndSendEmail() throws Exception {
126: System.out.println("\ntestSaveAndSendEmail()");
127: query1.setUserId(new Integer(2));
128: query1.setName("Test query 2");
129: query1.setValue("&searchId=2&searchisp=asc");
130: query1.setDescription("Description for test query 2");
131: query1.setModuleId(getModule().getModuleId());
132: query1.setIssueType(getDefaultIssueType());
133: query1.setScopeId(Scope.PERSONAL__PK);
134: query1.saveAndSendEmail(getUser1(), getModule(), null);
135: //
136: // Make sure the query was persisted correctly.
137: //
138: Query retQuery = QueryManager.getInstance(query1.getQueryId(),
139: false);
140: assertEquals(query1.getName(), retQuery.getName());
141: assertEquals(query1.getValue(), retQuery.getValue());
142: assertEquals(query1.getScope(), retQuery.getScope());
143:
144: }
145:
146: public void testSaveOnlyWithModuleScope() throws Exception {
147:
148: System.out.println("\ntestSaveAndSendEmail()");
149: query1.setUserId(new Integer(1));
150: query1.setName("Test query 2");
151: query1.setValue("&searchId=2&searchisp=asc");
152: query1.setDescription("Description for test query 2");
153: query1.setModuleId(getModule().getModuleId());
154: query1.setIssueType(getDefaultIssueType());
155: query1.setScopeId(Scope.MODULE__PK);
156: query1.save();
157: //
158: // Make sure the query was persisted correctly.
159: //
160: Query retQuery = QueryManager.getInstance(query1.getQueryId(),
161: false);
162: assertEquals(query1.getName(), retQuery.getName());
163: assertEquals(query1.getValue(), retQuery.getValue());
164: assertEquals(query1.getScope(), retQuery.getScope());
165:
166: }
167:
168: /*
169: public void testGetExecuteLink() throws Exception
170: {
171: System.out.println("\ntestGetExecuteLink()");
172: String exLink = query.getExecuteLink("dummy");
173: assertEquals("dummy/template/IssueList.vm" +
174: "?action=Search&eventSubmit_doSearch=Search&resultsperpage=25" +
175: "&pagenum=1&searchId=1&searchisp=asc&remcurmitl=true", exLink);
176: }
177:
178: public void testGetEditLink() throws Exception
179: {
180: System.out.println("\ntestGetEditLink()");
181: String edLink = query.getEditLink("dummy");
182: assertEquals("dummy/template/EditQuery.vm?queryId=" +
183: query.getQueryId() +
184: "&searchId=1&searchisp=asc&remcurmitl=true", edLink);
185: }
186: */
187: public void testGetAllQueryTypes() throws Exception {
188: String[] scopeNames = { "personal", "module" };
189: System.out.println("\ntestGetAllQueryTypes()");
190: List scopes = ScopePeer.getAllScopes();
191: assertEquals(scopes.size(), 2);
192: Iterator it = scopes.iterator();
193: Scope scope;
194: for (int i = 0; it.hasNext(); i++) {
195: scope = (Scope) it.next();
196: System.out.println("getAllScopes().getName(): <"
197: + scope.getName() + "> expected: <" + scopeNames[i]
198: + ">");
199: assertEquals(scope.getName(), scopeNames[i]);
200: }
201: }
202:
203: public void testApprove() throws Exception {
204: System.out.println("\ntestSetApproved()");
205: createQuery();
206: //
207: // We expect user5 to fail in approving and so we catch
208: // the exceptions and proceed. user2 should be successful
209: // in approving.
210: //
211: try {
212: query.approve(getUser5(), true);
213: fail("user1 should fail in approving the query");
214: } catch (Exception ex) {
215: assertTrue(ex instanceof ScarabException);
216: }
217:
218: query.approve(getUser2(), true);
219: assertTrue(query.getApproved());
220: }
221:
222: public void testSubscribe() throws Exception {
223: System.out.println("\ntestSubscribe()");
224: createQuery();
225: query.subscribe(getUser2(), new Integer(1));
226: RQueryUser rqu = query.getRQueryUser(getUser2());
227: query.subscribe(getUser2(), new Integer(1));
228: assertTrue(rqu.getIsSubscribed());
229: // Now if unsubscribed, should fail to return RQueryUser
230: query.unSubscribe(getUser2());
231:
232: rqu = query.getRQueryUser(getUser2());
233:
234: }
235:
236: public void testCopy() throws Exception {
237: Query newQuery = query.copy();
238: assertEquals(newQuery.getName(), query.getName());
239: assertEquals(newQuery.getUserId(), query.getUserId());
240: assertEquals(newQuery.getValue(), query.getValue());
241: RQueryUser rqu = query.getRQueryUser(getUser1());
242: RQueryUser rquNew = newQuery.getRQueryUser(getUser1());
243: assertEquals(rqu.getIsSubscribed(), rquNew.getIsSubscribed());
244: }
245:
246: }
|