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.res;
022:
023: import java.util.*;
024: import java.sql.*;
025: import java.io.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.auth.*;
031: import com.methodhead.*;
032: import com.methodhead.sitecontext.*;
033: import com.methodhead.util.*;
034: import servletunit.struts.*;
035: import org.apache.cactus.*;
036: import org.apache.struts.action.*;
037: import org.apache.struts.util.*;
038: import com.methodhead.tree.*;
039: import org.apache.commons.io.*;
040:
041: public class ResFormTest extends CactusStrutsTestCase {
042:
043: private ResForm form = null;
044: File base = null;
045: File file = null;
046: LabelValueBean labelValue = null;
047: List list = null;
048: FileManager fileManager = null;
049: FoldingTreeNode node = null;
050: ActionMapping mapping = null;
051:
052: private File testDir_ = null;
053: private File subDir1_ = null;
054: private File subDir2_ = null;
055: private File subDir3_ = null;
056: private File testFile1_ = null;
057: private File testFile2_ = null;
058: private File testFile3_ = null;
059: private File testFile4_ = null;
060: private File testFile5_ = null;
061:
062: private void setUpFiles() {
063: try {
064:
065: TestData.createWebappFiles(ServletUtils.getRealFile(
066: request, ""));
067:
068: //
069: // create a resources dir (we won't use it in these tests though)
070: //
071: File privateTestDir = new File(session.getServletContext()
072: .getRealPath("/WEB-INF/resources/1"));
073: if (privateTestDir.exists())
074: FileUtils.deleteDirectory(privateTestDir);
075: privateTestDir.mkdirs();
076:
077: //
078: // delete testdir if it exists
079: //
080: testDir_ = new File(session.getServletContext()
081: .getRealPath("/1/testdir"));
082: if (testDir_.exists())
083: FileUtils.deleteDirectory(testDir_);
084:
085: //
086: // create testdir
087: //
088: testDir_.mkdir();
089:
090: //
091: // testdir
092: // subdir1
093: // subdir3
094: // testfile4.txt
095: // testfile3.txt
096: // subdir2
097: // testfile5.txt
098: // testfile1.txt
099: // testfile2.txt
100: //
101: subDir1_ = new File(testDir_, "subdir1");
102: subDir1_.mkdir();
103:
104: subDir3_ = new File(testDir_, "subdir1/subdir3");
105: subDir3_.mkdir();
106:
107: testFile4_ = new File(testDir_,
108: "subdir1/subdir3/testfile4.txt");
109: testFile4_.createNewFile();
110:
111: testFile3_ = new File(testDir_, "subdir1/testfile3.txt");
112: testFile3_.createNewFile();
113:
114: subDir2_ = new File(testDir_, "subdir2");
115: subDir2_.mkdir();
116:
117: testFile5_ = new File(testDir_, "subdir2/testfile5.txt");
118: testFile5_.createNewFile();
119:
120: testFile1_ = new File(testDir_, "testfile1.txt");
121: testFile1_.createNewFile();
122:
123: testFile2_ = new File(testDir_, "testfile2.txt");
124: testFile2_.createNewFile();
125: } catch (Exception e) {
126: fail();
127: }
128: }
129:
130: static {
131: TestUtils.initLogger();
132: }
133:
134: public ResFormTest(String name) {
135: super (name);
136: }
137:
138: public void setUp() {
139: //setLogLevel( Level.DEBUG );
140: try {
141: super .setUp();
142:
143: ConnectionSingleton.runBatchUpdate(new FileReader(
144: "webapp/WEB-INF/db/transfer-reset.sql"));
145:
146: TestData.createUsers();
147: SiteContext.setContext(request, TestData.siteContext1);
148:
149: setUpFiles();
150:
151: //
152: // log in an admin
153: //
154: AuthUtil.setUser(request, TestData.user1);
155: } catch (Exception e) {
156: fail(e.getMessage());
157: }
158: }
159:
160: public void tearDown() throws Exception {
161: super .tearDown();
162: }
163:
164: public void testGetRelativePath() {
165: try {
166: form = new ResForm();
167:
168: base = new File("/foo");
169: file = new File("/foo/bar");
170: assertEquals("bar", form.getRelativePath(base, file));
171:
172: base = new File("");
173: file = new File("bar");
174: assertEquals("bar", form.getRelativePath(base, file));
175: } catch (Exception e) {
176: e.printStackTrace();
177: fail();
178: }
179: }
180:
181: public void testAddSubdirOptions() {
182: try {
183: form = new ResForm();
184:
185: //
186: // get options for testdir
187: //
188: list = new ArrayList();
189: form
190: .addSubdirOptions(list, "Test", new File(session
191: .getServletContext().getRealPath(
192: "/1/testdir")), "");
193:
194: //
195: // correct number of options
196: //
197: assertEquals(4, list.size());
198:
199: //
200: // correct options in the correct order
201: //
202: labelValue = (LabelValueBean) list.get(0);
203: assertEquals("Test", labelValue.getLabel());
204: assertEquals("Test", labelValue.getValue());
205:
206: labelValue = (LabelValueBean) list.get(1);
207: assertEquals("Test/subdir1", labelValue.getLabel());
208: assertEquals("Test/subdir1", labelValue.getValue());
209:
210: labelValue = (LabelValueBean) list.get(2);
211: assertEquals("Test/subdir1/subdir3", labelValue.getLabel());
212: assertEquals("Test/subdir1/subdir3", labelValue.getValue());
213:
214: labelValue = (LabelValueBean) list.get(3);
215: assertEquals("Test/subdir2", labelValue.getLabel());
216: assertEquals("Test/subdir2", labelValue.getValue());
217: } catch (Exception e) {
218: e.printStackTrace();
219: fail();
220: }
221: }
222:
223: public void testGetDestinationOptions() {
224: try {
225: form = new ResForm();
226: fileManager = new FileManager();
227: fileManager.addDirectory("Test", new File(session
228: .getServletContext().getRealPath("/1/testdir")));
229:
230: //
231: // get options for testdir
232: //
233: list = form.getDestinationOptions(fileManager);
234:
235: //
236: // correct number of options
237: //
238: assertEquals(4, list.size());
239:
240: //
241: // correct options in the correct order
242: //
243: labelValue = (LabelValueBean) list.get(0);
244: assertEquals("Test", labelValue.getLabel());
245: assertEquals("Test", labelValue.getValue());
246:
247: labelValue = (LabelValueBean) list.get(1);
248: assertEquals("Test/subdir1", labelValue.getLabel());
249: assertEquals("Test/subdir1", labelValue.getValue());
250:
251: labelValue = (LabelValueBean) list.get(2);
252: assertEquals("Test/subdir1/subdir3", labelValue.getLabel());
253: assertEquals("Test/subdir1/subdir3", labelValue.getValue());
254:
255: labelValue = (LabelValueBean) list.get(3);
256: assertEquals("Test/subdir2", labelValue.getLabel());
257: assertEquals("Test/subdir2", labelValue.getValue());
258: } catch (Exception e) {
259: e.printStackTrace();
260: fail();
261: }
262: }
263:
264: public void beginReset(WebRequest request) {
265: request.addParameter("path", "Public/testdir");
266: request.addParameter("file1", "testfile1.txt");
267: request.addParameter("file2", "subdir1");
268: }
269:
270: public void testReset() {
271: try {
272: setRequestPathInfo("/manageFilesForm.do");
273: actionPerform();
274: form = (ResForm) getActionForm();
275:
276: //
277: // make sure we got some options
278: //
279: list = (List) form.get("destinations");
280: assertNotNull(list);
281: assertTrue(list.size() > 0);
282:
283: //
284: // make sure we got our files
285: //
286: list = (List) form.get("files");
287: assertNotNull(list);
288: assertEquals(2, list.size());
289:
290: node = (FoldingTreeNode) list.get(0);
291: assertEquals("subdir1", node.getLabel());
292: file = (File) node.getUserObject();
293: assertEquals("subdir1", file.getName());
294: assertTrue(file.isDirectory());
295:
296: node = (FoldingTreeNode) list.get(1);
297: assertEquals("testfile1.txt", node.getLabel());
298: file = (File) node.getUserObject();
299: assertEquals("testfile1.txt", file.getName());
300: } catch (Exception e) {
301: e.printStackTrace();
302: fail();
303: }
304: }
305: }
|