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.sql.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.persistable.*;
028: import com.methodhead.test.*;
029: import servletunit.struts.*;
030: import org.apache.struts.action.*;
031: import org.apache.cactus.*;
032: import com.methodhead.sitecontext.*;
033: import java.io.*;
034: import com.methodhead.auth.*;
035: import com.methodhead.util.*;
036: import com.methodhead.*;
037:
038: public class SiteContextActionTest extends CactusStrutsTestCase {
039:
040: SiteContext siteContext1_ = null;
041: SiteContext siteContext2_ = null;
042: HtmlFragment fragment1_ = null;
043:
044: Panel panel = null;
045: Page page1_ = null;
046: Page page = null;
047: List list = null;
048:
049: SiteContext siteContext = null;
050: File file = null;
051:
052: private void createData() {
053:
054: //
055: // create two site contexts (resource directories already exist for ids 0,
056: // 1, and 2).
057: //
058: siteContext1_ = new SiteContext();
059: siteContext1_.getDomains().add("test.com");
060: siteContext1_.saveNew();
061:
062: siteContext2_ = new SiteContext();
063: siteContext2_.getDomains().add("test2.com");
064: siteContext2_.saveNew();
065:
066: panel = new Panel();
067: panel.setName("body");
068: panel.setModuleClass("com.methodhead.shim.MockModule");
069:
070: page1_ = new Page();
071: page1_.set("title", "Page1");
072: page1_.set("template", "template.jsp");
073: page1_.addPanel(panel);
074: page1_.setSiteContext(siteContext1_);
075: page1_.saveNew();
076:
077: fragment1_ = new HtmlFragment();
078: fragment1_.setSiteContext(siteContext2_);
079: fragment1_.saveNew();
080: }
081:
082: static {
083: TestUtils.initLogger();
084: TestUtils.initDb();
085: }
086:
087: public SiteContextActionTest(String name) {
088: super (name);
089: }
090:
091: public void setUp() {
092: try {
093: super .setUp();
094:
095: ConnectionSingleton.runBatchUpdate(new FileReader(
096: "webapp/WEB-INF/db/transfer-reset.sql"));
097:
098: TestData.createUsers();
099: AuthUtil.setUser(request, TestData.user1);
100: } catch (Exception e) {
101: fail(e.getMessage());
102: }
103: }
104:
105: public void tearDown() throws Exception {
106: super .tearDown();
107: }
108:
109: public void testDoSaveNew() {
110: try {
111: createData();
112:
113: setRequestPathInfo("/siteContext");
114: addRequestParameter("action", "saveNew");
115: addRequestParameter("domainsText", "danhensgen.com");
116: addRequestParameter("path", "");
117: actionPerform();
118:
119: verifyForwardPath("/siteContext.do?action=list");
120:
121: siteContext = new SiteContext();
122: assertTrue(siteContext.loadForDomainAndPath(
123: "danhensgen.com", ""));
124:
125: file = ServletUtils.getRealFile(request, "/"
126: + siteContext.get("id"));
127: assertTrue(file.exists());
128: assertTrue(file.isDirectory());
129:
130: file = ServletUtils.getRealFile(request,
131: "/WEB-INF/resources/" + siteContext.get("id"));
132: assertTrue(file.exists());
133: assertTrue(file.isDirectory());
134:
135: file = ServletUtils.getRealFile(request,
136: "/WEB-INF/resources/" + siteContext.get("id")
137: + "/templates");
138: assertTrue(file.exists());
139: assertTrue(file.isDirectory());
140: } catch (Exception e) {
141: e.printStackTrace();
142: fail();
143: }
144: }
145:
146: public void testDoDelete() {
147: try {
148: createData();
149:
150: setRequestPathInfo("/siteContext");
151: addRequestParameter("action", "delete");
152: addRequestParameter("id", "" + siteContext2_.get("id"));
153: actionPerform();
154:
155: verifyForwardPath("/siteContext.do?action=list");
156:
157: siteContext = new SiteContext();
158: assertTrue(!siteContext.loadForDomainAndPath("test2.com",
159: ""));
160:
161: page = new Page();
162: page.setSiteContext(siteContext2_);
163: list = page.loadAll();
164: assertEquals(0, list.size());
165:
166: file = ServletUtils.getRealFile(request, "/"
167: + siteContext2_.get("id"));
168: assertTrue(!file.exists());
169:
170: file = ServletUtils.getRealFile(request,
171: "/WEB-INF/resources/" + siteContext2_.get("id"));
172: assertTrue(!file.exists());
173: } catch (Exception e) {
174: e.printStackTrace();
175: fail();
176: }
177: }
178: }
|