001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.sitecontext;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.aikp.*;
029: import com.methodhead.persistable.*;
030: import com.methodhead.test.*;
031: import com.methodhead.event.*;
032: import com.mockobjects.servlet.*;
033:
034: public class SiteContextTest extends DbTestCase {
035:
036: ResultSet rs = null;
037: SiteContext siteContext = null;
038: Event event = null;
039:
040: public SiteContextTest(String name) {
041: super (name);
042: }
043:
044: protected List list = null;
045:
046: protected SiteContext siteContext2 = null;
047: protected SiteContext siteContext3 = null;
048: protected SiteContext siteContext4 = null;
049:
050: protected SiteContext siteContext1_ = null;
051: protected SiteContext siteContext2_ = null;
052:
053: protected void createData() {
054: siteContext1_ = new SiteContext();
055: siteContext1_.getDomains().add("www.methodhead.com");
056: siteContext1_.getDomains().add("methodhead.com");
057: siteContext1_.saveNew();
058:
059: siteContext2_ = new SiteContext();
060: siteContext2_.setString("path", "sub");
061: siteContext2_.getDomains().add("dh.com");
062: siteContext2_.getDomains().add("dh.net");
063: siteContext2_.saveNew();
064:
065: Event.log(siteContext1_, "test event");
066: }
067:
068: protected void setUp() {
069: //setLogLevel( Level.DEBUG );
070: try {
071: ConnectionSingleton.runBatchUpdate(new FileReader(
072: "webapp/WEB-INF/db/transfer-reset.sql"));
073: } catch (Exception e) {
074: fail(e.getMessage());
075: }
076: }
077:
078: protected void tearDown() {
079: }
080:
081: public void testHashCode() {
082: try {
083: SiteContext siteContext = null;
084:
085: siteContext = new SiteContext();
086:
087: assertEquals(0, siteContext.hashCode());
088:
089: siteContext.setInt("id", 666);
090:
091: assertEquals(666, siteContext.hashCode());
092: } catch (Exception e) {
093: e.printStackTrace();
094: fail();
095: }
096: }
097:
098: public void testEquals() {
099: try {
100: SiteContext siteContext1 = null;
101: SiteContext siteContext2 = null;
102:
103: siteContext1 = new SiteContext();
104: siteContext2 = new SiteContext();
105:
106: assertTrue(!siteContext1.equals(null));
107: assertTrue(!siteContext1.equals(new String()));
108: assertTrue(siteContext1.equals(siteContext2));
109:
110: siteContext2.setInt("id", 666);
111:
112: assertTrue(!siteContext1.equals(siteContext2));
113: } catch (Exception e) {
114: e.printStackTrace();
115: fail();
116: }
117: }
118:
119: public void testToString() {
120: try {
121: siteContext = new SiteContext();
122: assertEquals("SiteContext (no domains)", siteContext
123: .toString());
124:
125: siteContext.getDomains().add("methodhead.com");
126: siteContext.getDomains().add("danhensgen.com");
127: assertEquals("methodhead.com", siteContext.toString());
128:
129: siteContext.setString("path", "test");
130: assertEquals("methodhead.com/test", siteContext.toString());
131: } catch (Exception e) {
132: e.printStackTrace();
133: fail();
134: }
135: }
136:
137: public void testSaveNew() {
138: try {
139: SiteContext siteContext = null;
140: ResultSet rs = null;
141:
142: siteContext = new SiteContext();
143: siteContext.getDomains().add("methodhead.com");
144: siteContext.getDomains().add("www.methodhead.com");
145: siteContext.saveNew();
146: rs = ConnectionSingleton
147: .runQuery("SELECT path FROM mh_sitecontext WHERE id="
148: + siteContext.getInt("id"));
149:
150: assertNotNull(rs);
151: assertTrue(rs.next());
152: assertEquals("", rs.getString("path"));
153: assertTrue(!rs.next());
154:
155: ConnectionSingleton.close(rs);
156: rs = ConnectionSingleton
157: .runQuery("SELECT name,rank FROM mh_domain WHERE sitecontext_id="
158: + siteContext.getInt("id"));
159:
160: assertNotNull(rs);
161: assertTrue(rs.next());
162: assertEquals("methodhead.com", rs.getString("name"));
163: assertEquals(0, rs.getInt("rank"));
164: assertTrue(rs.next());
165: assertEquals("www.methodhead.com", rs.getString("name"));
166: assertEquals(1, rs.getInt("rank"));
167: assertTrue(!rs.next());
168:
169: ConnectionSingleton.close(rs);
170: } catch (Exception e) {
171: e.printStackTrace();
172: fail();
173: }
174: }
175:
176: public void testLoad() {
177: try {
178: SiteContext siteContext = null;
179: createData();
180:
181: siteContext = new SiteContext();
182: siteContext.load(new IntKey(siteContext1_.getInt("id")));
183:
184: assertEquals("", siteContext.getString("path"));
185: assertNotNull(siteContext.getDomains());
186: assertEquals(2, siteContext.getDomains().size());
187: assertEquals("www.methodhead.com", siteContext.getDomains()
188: .get(0));
189: assertEquals("methodhead.com", siteContext.getDomains()
190: .get(1));
191: } catch (Exception e) {
192: e.printStackTrace();
193: fail();
194: }
195: }
196:
197: public void testSave() {
198: try {
199: ResultSet rs = null;
200: SiteContext siteContext = null;
201: createData();
202:
203: siteContext = new SiteContext();
204: siteContext.load(new IntKey(siteContext1_.getInt("id")));
205: siteContext.getDomains().clear();
206: siteContext.save();
207: rs = ConnectionSingleton
208: .runQuery("SELECT name FROM mh_domain WHERE sitecontext_id="
209: + siteContext.getInt("id"));
210:
211: assertNotNull(rs);
212: assertTrue(!rs.next());
213:
214: ConnectionSingleton.close(rs);
215: siteContext.getDomains().add("www.methodhead.com");
216: siteContext.getDomains().add("methodhead.com");
217: siteContext.save();
218: rs = ConnectionSingleton
219: .runQuery("SELECT name FROM mh_domain WHERE sitecontext_id="
220: + siteContext.getInt("id")
221: + " ORDER BY rank");
222:
223: assertNotNull(rs);
224: assertTrue(rs.next());
225: assertEquals("www.methodhead.com", rs.getString("name"));
226: assertTrue(rs.next());
227: assertEquals("methodhead.com", rs.getString("name"));
228: assertTrue(!rs.next());
229:
230: ConnectionSingleton.close(rs);
231: } catch (Exception e) {
232: e.printStackTrace();
233: fail();
234: }
235: }
236:
237: public void testDelete() {
238: try {
239: ResultSet rs = null;
240: SiteContext siteContext = null;
241: createData();
242:
243: siteContext = new SiteContext();
244: siteContext.load(new IntKey(siteContext1_.getInt("id")));
245: siteContext.delete();
246: rs = ConnectionSingleton
247: .runQuery("SELECT path FROM mh_sitecontext WHERE id="
248: + siteContext.getInt("id"));
249:
250: assertNotNull(rs);
251: assertTrue(!rs.next());
252:
253: ConnectionSingleton.close(rs);
254: rs = ConnectionSingleton
255: .runQuery("SELECT name FROM mh_domain WHERE sitecontext_id="
256: + siteContext.getInt("id"));
257:
258: assertNotNull(rs);
259: assertTrue(!rs.next());
260:
261: ConnectionSingleton.close(rs);
262:
263: event = new Event();
264: event.setSiteContext(siteContext1_);
265: list = event.getEvents(null, null);
266: assertEquals(0, list.size());
267: } catch (Exception e) {
268: e.printStackTrace();
269: fail();
270: }
271: }
272:
273: public void testGetDefaultContext() {
274: try {
275: siteContext = SiteContext.getDefaultContext();
276:
277: assertNotNull(siteContext);
278: assertEquals(0, siteContext.getInt("id"));
279: assertEquals("", siteContext.getString("path"));
280: assertEquals(1, siteContext.getDomains().size());
281: assertEquals(SiteContext.DEFAULTDOMAIN, siteContext
282: .getDomains().get(0));
283:
284: rs = ConnectionSingleton
285: .runQuery("SELECT path FROM mh_sitecontext WHERE id=0");
286:
287: assertTrue(rs.next());
288: assertEquals("", rs.getString("path"));
289:
290: ConnectionSingleton.close(rs);
291:
292: rs = ConnectionSingleton
293: .runQuery("SELECT name FROM mh_domain WHERE sitecontext_id=0");
294:
295: assertTrue(rs.next());
296: assertEquals(SiteContext.DEFAULTDOMAIN, rs
297: .getString("name"));
298:
299: ConnectionSingleton.close(rs);
300:
301: siteContext.setString("path", "foo");
302: siteContext.getDomains().add("www.methodhead.com");
303: siteContext.save();
304: siteContext = SiteContext.getDefaultContext();
305:
306: assertNotNull(siteContext);
307: assertEquals(0, siteContext.getInt("id"));
308: assertEquals("foo", siteContext.getString("path"));
309: assertEquals(2, siteContext.getDomains().size());
310: } catch (Exception e) {
311: e.printStackTrace();
312: fail();
313: }
314: }
315:
316: public void testSetContext() {
317: try {
318: SiteContext siteContext = null;
319: MockHttpServletRequest request = null;
320: MockHttpSession session = null;
321:
322: session = new MockHttpSession();
323: request = new MockHttpServletRequest();
324: request.setSession(session);
325: session.setExpectedAttribute(SiteContext.SITECONTEXT_KEY,
326: siteContext);
327: SiteContext.setContext(request, siteContext);
328:
329: session.verify();
330: } catch (Exception e) {
331: e.printStackTrace();
332: fail();
333: }
334: }
335:
336: public void testGetContext() {
337: try {
338: SiteContext siteContext = null;
339: SiteContext siteContext2 = null;
340: MockHttpServletRequest request = null;
341: MockHttpSession session = null;
342:
343: /* these won't work because MockHttpServletRequest isn't done yet...
344:
345: session = new MockHttpSession();
346: session.setupGetAttribute( SiteContext.SITECONTEXT_KEY, null );
347: request = new MockHttpServletRequest();
348: request.setupGetAttribute( SiteContext.SITECONTEXT_KEY );
349: request.setSession( session );
350: siteContext = SiteContext.getContext( request );
351:
352: session.verify();
353: assertNull( siteContext );
354: siteContext = new SiteContext();
355: siteContext.setInt( "id", 666 );
356: session = new MockHttpSession();
357: session.setupGetAttribute( SiteContext.SITECONTEXT_KEY, siteContext );
358: request = new MockHttpServletRequest();
359: request.setSession( session );
360: siteContext2 = SiteContext.getContext( request );
361:
362: session.verify();
363: assertNotNull( siteContext );
364: assertEquals( siteContext, siteContext2 );
365:
366: siteContext = new SiteContext();
367: siteContext.setInt( "id", 666 );
368: session = new MockHttpSession();
369: session.setupGetAttribute( SiteContext.SITECONTEXT_KEY, siteContext );
370: request = new MockHttpServletRequest();
371: request.setSession( session );
372: siteContext2 = SiteContext.getContext( request );
373:
374: session.verify();
375: assertEquals( siteContext, siteContext2 );
376: */
377: } catch (Exception e) {
378: e.printStackTrace();
379: fail();
380: }
381: }
382:
383: public void testLoadForDomainAndPath() {
384: try {
385: createData();
386: SiteContext context = null;
387:
388: context = new SiteContext();
389:
390: assertTrue(!context.loadForDomainAndPath("foo.com", ""));
391: assertTrue(context.loadForDomainAndPath("methodhead.com",
392: ""));
393: assertEquals(siteContext1_, context);
394: assertEquals("www.methodhead.com", siteContext1_
395: .getDomains().get(0));
396: assertTrue(!context.loadForDomainAndPath("dh.com", ""));
397: assertTrue(context.loadForDomainAndPath("dh.com", "sub"));
398: assertEquals(siteContext2_, context);
399: assertTrue(context.loadForDomainAndPath("dh.net", "sub"));
400: assertEquals(siteContext2_, context);
401: } catch (Exception e) {
402: e.printStackTrace();
403: fail();
404: }
405: }
406:
407: public void testCompareTo() {
408: try {
409: siteContext = new SiteContext(); // aaa
410: siteContext2 = new SiteContext(); // bbb
411: siteContext3 = new SiteContext(); // aaa/aaa
412: siteContext4 = new SiteContext(); // aaa/bbb
413:
414: siteContext.getDomains().add("aaa");
415: siteContext2.getDomains().add("bbb");
416: siteContext3.getDomains().add("aaa");
417: siteContext3.setString("path", "aaa");
418: siteContext4.getDomains().add("aaa");
419: siteContext4.setString("path", "bbb");
420:
421: assertTrue(siteContext.compareTo(null) < 0);
422: assertTrue(siteContext.compareTo(new Integer(666)) < 0);
423: assertTrue(siteContext.compareTo(siteContext2) < 0);
424: assertTrue(siteContext.compareTo(siteContext) == 0);
425: assertTrue(siteContext2.compareTo(siteContext) > 0);
426: assertTrue(siteContext.compareTo(siteContext3) < 0);
427: assertTrue(siteContext3.compareTo(siteContext) > 0);
428: assertTrue(siteContext3.compareTo(siteContext4) < 0);
429: assertTrue(siteContext4.compareTo(siteContext3) > 0);
430: } catch (Exception e) {
431: e.printStackTrace();
432: fail();
433: }
434: }
435:
436: public void testLoadAll() {
437: try {
438: createData();
439:
440: list = SiteContext.loadAll();
441: assertNotNull(list);
442: assertEquals(2, list.size());
443:
444: siteContext = (SiteContext) list.get(0);
445: assertTrue(siteContext.compareTo(siteContext2_) == 0);
446:
447: siteContext = (SiteContext) list.get(1);
448: assertTrue(siteContext.compareTo(siteContext1_) == 0);
449: } catch (Exception e) {
450: e.printStackTrace();
451: fail();
452: }
453: }
454: }
|