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.shim;
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.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.sitecontext.*;
031:
032: public class SiteMapTest extends TestCase {
033:
034: static {
035: TestUtils.initLogger();
036: TestUtils.initDb();
037: }
038:
039: public SiteMapTest(String name) {
040: super (name);
041: }
042:
043: private SiteContext siteContext1_ = null;
044:
045: private Page page1_ = null;
046: private Page page2_ = null;
047: private Page page3_ = null;
048: private Page page4_ = null;
049: private Page page5_ = null;
050:
051: private Link link1_ = null;
052: private Link link2_ = null;
053: private Link link3_ = null;
054: private Link link4_ = null;
055: private Link link5_ = null;
056:
057: protected void createData() {
058:
059: // Link1
060: // Link2 (hidden)
061: // Link3
062: // Link4
063: // Link5
064: link1_ = new Link();
065: link1_.setTitle("Link1");
066: link1_.setPageId(page1_.getInt("id"));
067:
068: link2_ = new Link();
069: link2_.setTitle("Link2");
070: link2_.setPageId(page2_.getInt("id"));
071:
072: link3_ = new Link();
073: link3_.setTitle("Link3");
074: link3_.setPageId(page3_.getInt("id"));
075:
076: link4_ = new Link();
077: link4_.setTitle("Link4");
078: link4_.setPageId(page4_.getInt("id"));
079:
080: link5_ = new Link();
081: link5_.setTitle("Link5");
082: link5_.setPageId(page5_.getInt("id"));
083:
084: link1_.add(link2_);
085: link2_.add(link3_);
086: link1_.add(link4_);
087: link1_.add(link5_);
088: }
089:
090: protected void setUp() {
091: try {
092: ConnectionSingleton.runBatchUpdate(new FileReader(
093: "webapp/WEB-INF/db/transfer-reset.sql"));
094:
095: siteContext1_ = new SiteContext();
096: siteContext1_.saveNew();
097:
098: page1_ = new Page();
099: page1_.setString("title", "Page1");
100: page1_.setString("aliasname", "page1");
101: page1_.setSiteContext(SiteContext.getDefaultContext());
102: page1_.saveNew();
103:
104: page2_ = new Page();
105: page2_.setString("title", "Page2");
106: page2_.setString("aliasname", "page2");
107: page2_.setBoolean("hidden", true);
108: page2_.setSiteContext(SiteContext.getDefaultContext());
109: page2_.saveNew();
110:
111: page3_ = new Page();
112: page3_.setString("title", "Page3");
113: page3_.setString("aliasname", "page3");
114: page3_.setSiteContext(SiteContext.getDefaultContext());
115: page3_.saveNew();
116:
117: page4_ = new Page();
118: page4_.setString("title", "Page4");
119: page4_.setString("aliasname", "page4");
120: page4_.setSiteContext(SiteContext.getDefaultContext());
121: page4_.saveNew();
122:
123: page5_ = new Page();
124: page5_.setString("title", "Page5");
125: page5_.setString("aliasname", "page5");
126: page5_.setSiteContext(SiteContext.getDefaultContext());
127: page5_.saveNew();
128: } catch (Exception e) {
129: fail(e.getMessage());
130: }
131: }
132:
133: protected void tearDown() {
134: }
135:
136: public void testFind() {
137: try {
138: createData();
139:
140: SiteMap siteMap = null;
141: Link link = null;
142:
143: siteMap = new SiteMap();
144: siteMap.setSiteContext(SiteContext.getDefaultContext());
145: siteMap.setRoot(link1_);
146:
147: assertEquals(link1_, siteMap.find(1));
148: assertEquals(link2_, siteMap.find(2));
149: assertEquals(link3_, siteMap.find(3));
150: assertEquals(link4_, siteMap.find(4));
151: assertEquals(link5_, siteMap.find(5));
152: } catch (Exception e) {
153: e.printStackTrace();
154: fail();
155: }
156: }
157:
158: public void testSave() {
159: try {
160: SiteMap siteMap = null;
161: Link link = null;
162:
163: createData();
164: siteMap = new SiteMap();
165: siteMap.setSiteContext(SiteContext.getDefaultContext());
166: siteMap.setRoot(link1_);
167: siteMap.save();
168:
169: ResultSet rs = ConnectionSingleton
170: .runQuery("SELECT page_id,parent_id,rank FROM shim_link WHERE sitecontext_id=0 ORDER BY page_id");
171:
172: assertTrue(rs.next());
173: assertEquals(1, rs.getInt("page_id"));
174: assertEquals(0, rs.getInt("parent_id"));
175: assertEquals(0, rs.getInt("rank"));
176:
177: assertTrue(rs.next());
178: assertEquals(2, rs.getInt("page_id"));
179: assertEquals(1, rs.getInt("parent_id"));
180: assertEquals(0, rs.getInt("rank"));
181:
182: assertTrue(rs.next());
183: assertEquals(3, rs.getInt("page_id"));
184: assertEquals(2, rs.getInt("parent_id"));
185: assertEquals(0, rs.getInt("rank"));
186:
187: assertTrue(rs.next());
188: assertEquals(4, rs.getInt("page_id"));
189: assertEquals(1, rs.getInt("parent_id"));
190: assertEquals(1, rs.getInt("rank"));
191:
192: assertTrue(rs.next());
193: assertEquals(5, rs.getInt("page_id"));
194: assertEquals(1, rs.getInt("parent_id"));
195: assertEquals(2, rs.getInt("rank"));
196:
197: assertTrue(!rs.next());
198:
199: ConnectionSingleton.close(rs);
200:
201: //
202: // save for a different site context
203: //
204: siteMap = new SiteMap();
205: siteMap.setSiteContext(siteContext1_);
206: siteMap.setRoot(link1_);
207: siteMap.save();
208:
209: rs = ConnectionSingleton
210: .runQuery("SELECT page_id,parent_id,rank FROM shim_link WHERE sitecontext_id=1 ORDER BY page_id");
211:
212: assertTrue(rs.next());
213: assertEquals(1, rs.getInt("page_id"));
214: assertEquals(0, rs.getInt("parent_id"));
215: assertEquals(0, rs.getInt("rank"));
216:
217: assertTrue(rs.next());
218: assertEquals(2, rs.getInt("page_id"));
219: assertEquals(1, rs.getInt("parent_id"));
220: assertEquals(0, rs.getInt("rank"));
221:
222: assertTrue(rs.next());
223: assertEquals(3, rs.getInt("page_id"));
224: assertEquals(2, rs.getInt("parent_id"));
225: assertEquals(0, rs.getInt("rank"));
226:
227: assertTrue(rs.next());
228: assertEquals(4, rs.getInt("page_id"));
229: assertEquals(1, rs.getInt("parent_id"));
230: assertEquals(1, rs.getInt("rank"));
231:
232: assertTrue(rs.next());
233: assertEquals(5, rs.getInt("page_id"));
234: assertEquals(1, rs.getInt("parent_id"));
235: assertEquals(2, rs.getInt("rank"));
236:
237: assertTrue(!rs.next());
238:
239: ConnectionSingleton.close(rs);
240:
241: //
242: // save an empty site map
243: //
244: siteMap.setRoot(null);
245: siteMap.save();
246:
247: rs = ConnectionSingleton
248: .runQuery("SELECT page_id,parent_id,rank FROM shim_link WHERE sitecontext_id=1 ORDER BY page_id");
249:
250: assertTrue(!rs.next());
251:
252: ConnectionSingleton.close(rs);
253: } catch (Exception e) {
254: e.printStackTrace();
255: fail();
256: }
257: }
258:
259: public void testLoad() {
260: try {
261: SiteMap siteMap = null;
262: Link link = null;
263:
264: createData();
265: siteMap = new SiteMap();
266: siteMap.setSiteContext(SiteContext.getDefaultContext());
267: siteMap.setRoot(link1_);
268: siteMap.save();
269:
270: siteMap = new SiteMap();
271: siteMap.setSiteContext(SiteContext.getDefaultContext());
272: siteMap.load();
273:
274: assertNotNull(siteMap.getRoot());
275:
276: link = (Link) siteMap.getRoot();
277:
278: assertEquals(link.getPageId(), page1_.getInt("id"));
279: assertEquals("Page1", link.getTitle());
280: assertEquals("page1", link.getAlias());
281: assertEquals(false, link.getHidden());
282: assertEquals(3, link.getChildCount());
283:
284: link = (Link) siteMap.getRoot().getChildAt(0);
285:
286: assertEquals(link.getPageId(), page2_.getInt("id"));
287: assertEquals("Page2", link.getTitle());
288: assertEquals("page2", link.getAlias());
289: assertEquals(true, link.getHidden());
290: assertEquals(1, link.getChildCount());
291:
292: link = (Link) link.getChildAt(0);
293:
294: assertEquals(link.getPageId(), page3_.getInt("id"));
295: assertEquals("Page3", link.getTitle());
296: assertEquals("page3", link.getAlias());
297: assertEquals(false, link.getHidden());
298: assertEquals(0, link.getChildCount());
299:
300: link = (Link) siteMap.getRoot().getChildAt(1);
301:
302: assertEquals(link.getPageId(), page4_.getInt("id"));
303: assertEquals("Page4", link.getTitle());
304: assertEquals("page4", link.getAlias());
305: assertEquals(false, link.getHidden());
306: assertEquals(0, link.getChildCount());
307:
308: link = (Link) siteMap.getRoot().getChildAt(2);
309:
310: assertEquals(link.getPageId(), page5_.getInt("id"));
311: assertEquals("Page5", link.getTitle());
312: assertEquals("page5", link.getAlias());
313: assertEquals(false, link.getHidden());
314: assertEquals(0, link.getChildCount());
315: } catch (Exception e) {
316: e.printStackTrace();
317: fail();
318: }
319: }
320:
321: }
|