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.io.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.persistable.*;
028: import com.methodhead.test.*;
029: import org.apache.cactus.*;
030: import com.methodhead.tree.*;
031: import org.apache.struts.*;
032:
033: public class ResUtilsTest extends ServletTestCase {
034:
035: static {
036: TestUtils.initLogger();
037: TestUtils.initDb();
038: }
039:
040: private File f1 = null;
041: private File f2 = null;
042: private File f3 = null;
043: private File f4 = null;
044:
045: private FileManager fileManager = null;
046: private File file = null;
047: private FileTree fileTree = null;
048: private FoldingTreeNode node = null;
049:
050: public ResUtilsTest(String name) {
051: super (name);
052: }
053:
054: protected void setUp() {
055: //setLogLevel( Level.DEBUG );
056: try {
057: f1 = new File("build/testdir");
058: f1.mkdir();
059:
060: f2 = new File("build/testdir/subdir");
061: f2.mkdir();
062:
063: f3 = new File("build/testdir/f.txt");
064: f3.createNewFile();
065:
066: f4 = new File("build/testdir/subdir/g.txt");
067: f4.createNewFile();
068:
069: request.setAttribute(Globals.MESSAGES_KEY, session
070: .getServletContext().getAttribute(
071: Globals.MESSAGES_KEY));
072: } catch (Exception e) {
073: fail(e.getMessage());
074: }
075: }
076:
077: protected void tearDown() {
078: f4.delete();
079: f3.delete();
080: f2.delete();
081: f1.delete();
082: }
083:
084: public void testIsSafeFileName() {
085: try {
086: assertEquals(true, ResUtils.isSafeFileName("foo.html"));
087: assertEquals(false, ResUtils.isSafeFileName("foo.jsp"));
088: assertEquals(false, ResUtils.isSafeFileName("foo.do"));
089: } catch (Exception e) {
090: e.printStackTrace();
091: fail();
092: }
093: }
094:
095: public void testIsValidFileName() {
096: try {
097: assertEquals(true, ResUtils.isValidFileName("foo.txt"));
098: assertEquals(false, ResUtils
099: .isValidFileName("foo\\bar.txt"));
100: assertEquals(false, ResUtils.isValidFileName("foo/bar.txt"));
101: } catch (Exception e) {
102: e.printStackTrace();
103: fail();
104: }
105: }
106:
107: public void testIsValidPath() {
108: try {
109: assertEquals(true, ResUtils.isValidPath("foo/bar"));
110: assertEquals(true, ResUtils.isValidPath("foo\\bar"));
111: assertEquals(false, ResUtils.isValidPath("foo/../bar"));
112: assertEquals(false, ResUtils.isValidPath("foo\\..\\bar"));
113: assertEquals(false, ResUtils.isValidPath(null));
114: } catch (Exception e) {
115: e.printStackTrace();
116: fail();
117: }
118: }
119:
120: public void testCleanPath() {
121: try {
122: assertEquals("", ResUtils.cleanPath(""));
123: assertEquals("foo" + File.separator + "bar", ResUtils
124: .cleanPath("foo" + File.separator + "bar"));
125: assertEquals("foo" + File.separator + "bar", ResUtils
126: .cleanPath("foo" + File.separator + File.separator
127: + "bar"));
128: assertEquals("foo" + File.separator + "bar", ResUtils
129: .cleanPath("foo" + File.separator + File.separator
130: + File.separator + "bar"));
131: assertEquals(File.separator + "foo", ResUtils
132: .cleanPath(File.separator + File.separator + "foo"));
133: assertEquals("foo" + File.separator, ResUtils
134: .cleanPath("foo" + File.separator + File.separator));
135: } catch (Exception e) {
136: e.printStackTrace();
137: fail();
138: }
139: }
140:
141: public void testTrimPath() {
142: try {
143: assertEquals("foo", ResUtils.trimPath("foo"));
144: assertEquals("foo", ResUtils.trimPath("/foo"));
145: assertEquals("foo", ResUtils.trimPath(" /foo"));
146: assertEquals("foo", ResUtils.trimPath(" /foo/"));
147: assertEquals("foo", ResUtils.trimPath(" /foo/ "));
148: } catch (Exception e) {
149: e.printStackTrace();
150: fail();
151: }
152: }
153:
154: public void testIsPathDescendent() {
155: try {
156: assertTrue(!ResUtils.isPathDescendent(null, null));
157: assertTrue(ResUtils.isPathDescendent("", ""));
158: assertTrue(ResUtils.isPathDescendent("", "foo"));
159: assertTrue(ResUtils.isPathDescendent("foo", "foo"));
160: assertTrue(ResUtils.isPathDescendent("foo", "foo"));
161: assertTrue(ResUtils.isPathDescendent("foo", "foo/bar"));
162: assertTrue(ResUtils.isPathDescendent("foo", "foo/wam/bar"));
163: assertTrue(ResUtils.isPathDescendent("foo/wam",
164: "foo/wam/bar"));
165: assertTrue(!ResUtils.isPathDescendent("foo", ""));
166: assertTrue(!ResUtils.isPathDescendent("foo", "bam"));
167: assertTrue(!ResUtils.isPathDescendent("foo", "bam/foo"));
168: } catch (Exception e) {
169: e.printStackTrace();
170: fail();
171: }
172: }
173:
174: public void testDeleteFile() {
175: assertTrue(f3.exists());
176: ResUtils.deleteFile(f3);
177: assertTrue(!f3.exists());
178:
179: assertTrue(f2.exists());
180: ResUtils.deleteFile(f2);
181: assertTrue(!f2.exists());
182: }
183:
184: public void testGetFileTree() {
185: try {
186: assertNull(session.getAttribute(ResGlobals.FILETREE_KEY));
187:
188: fileTree = ResUtils.getFileTree(new DefaultResPolicy(),
189: request);
190: assertNotNull(fileTree);
191:
192: node = (FoldingTreeNode) fileTree.getRoot();
193: assertEquals(1, node.getChildCount());
194:
195: assertEquals(fileTree, session
196: .getAttribute(ResGlobals.FILETREE_KEY));
197: } catch (Exception e) {
198: e.printStackTrace();
199: fail();
200: }
201: }
202: }
|