KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > res > ResUtilsTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.res;
22
23 import java.util.*;
24 import java.io.*;
25 import junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.persistable.*;
28 import com.methodhead.test.*;
29 import org.apache.cactus.*;
30 import com.methodhead.tree.*;
31 import org.apache.struts.*;
32
33 public class ResUtilsTest extends ServletTestCase {
34
35   static {
36     TestUtils.initLogger();
37     TestUtils.initDb();
38   }
39
40   private File f1 = null;
41   private File f2 = null;
42   private File f3 = null;
43   private File f4 = null;
44
45   private FileManager fileManager = null;
46   private File file = null;
47   private FileTree fileTree = null;
48   private FoldingTreeNode node = null;
49
50   public ResUtilsTest( String JavaDoc name ) {
51     super( name );
52   }
53
54   protected void setUp() {
55     //setLogLevel( Level.DEBUG );
56
try {
57       f1 = new File( "build/testdir" );
58       f1.mkdir();
59
60       f2 = new File( "build/testdir/subdir" );
61       f2.mkdir();
62
63       f3 = new File( "build/testdir/f.txt" );
64       f3.createNewFile();
65
66       f4 = new File( "build/testdir/subdir/g.txt" );
67       f4.createNewFile();
68
69       request.setAttribute( Globals.MESSAGES_KEY, session.getServletContext().getAttribute( Globals.MESSAGES_KEY ) );
70     }
71     catch ( Exception JavaDoc e ) {
72       fail( e.getMessage() );
73     }
74   }
75
76   protected void tearDown() {
77     f4.delete();
78     f3.delete();
79     f2.delete();
80     f1.delete();
81   }
82
83   public void testIsSafeFileName() {
84     try {
85       assertEquals( true, ResUtils.isSafeFileName( "foo.html" ) );
86       assertEquals( false, ResUtils.isSafeFileName( "foo.jsp" ) );
87       assertEquals( false, ResUtils.isSafeFileName( "foo.do" ) );
88     }
89     catch ( Exception JavaDoc e ) {
90       e.printStackTrace();
91       fail();
92     }
93   }
94
95   public void testIsValidFileName() {
96     try {
97       assertEquals( true, ResUtils.isValidFileName( "foo.txt" ) );
98       assertEquals( false, ResUtils.isValidFileName( "foo\\bar.txt" ) );
99       assertEquals( false, ResUtils.isValidFileName( "foo/bar.txt" ) );
100     }
101     catch ( Exception JavaDoc 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     }
115     catch ( Exception JavaDoc e ) {
116       e.printStackTrace();
117       fail();
118     }
119   }
120
121   public void testCleanPath() {
122     try {
123       assertEquals( "", ResUtils.cleanPath( "" ) );
124       assertEquals( "foo" + File.separator + "bar", ResUtils.cleanPath( "foo" + File.separator + "bar" ) );
125       assertEquals( "foo" + File.separator + "bar", ResUtils.cleanPath( "foo" + File.separator + File.separator + "bar" ) );
126       assertEquals( "foo" + File.separator + "bar", ResUtils.cleanPath( "foo" + File.separator + File.separator + File.separator + "bar" ) );
127       assertEquals( File.separator + "foo", ResUtils.cleanPath( File.separator + File.separator + "foo" ) );
128       assertEquals( "foo" + File.separator, ResUtils.cleanPath( "foo" + File.separator + File.separator ) );
129     }
130     catch ( Exception JavaDoc e ) {
131       e.printStackTrace();
132       fail();
133     }
134   }
135
136   public void testTrimPath() {
137     try {
138       assertEquals( "foo", ResUtils.trimPath( "foo" ) );
139       assertEquals( "foo", ResUtils.trimPath( "/foo" ) );
140       assertEquals( "foo", ResUtils.trimPath( " /foo" ) );
141       assertEquals( "foo", ResUtils.trimPath( " /foo/" ) );
142       assertEquals( "foo", ResUtils.trimPath( " /foo/ " ) );
143     }
144     catch ( Exception JavaDoc e ) {
145       e.printStackTrace();
146       fail();
147     }
148   }
149
150   public void testIsPathDescendent() {
151     try {
152       assertTrue( !ResUtils.isPathDescendent( null, null ) );
153       assertTrue( ResUtils.isPathDescendent( "", "" ) );
154       assertTrue( ResUtils.isPathDescendent( "", "foo" ) );
155       assertTrue( ResUtils.isPathDescendent( "foo", "foo" ) );
156       assertTrue( ResUtils.isPathDescendent( "foo", "foo" ) );
157       assertTrue( ResUtils.isPathDescendent( "foo", "foo/bar" ) );
158       assertTrue( ResUtils.isPathDescendent( "foo", "foo/wam/bar" ) );
159       assertTrue( ResUtils.isPathDescendent( "foo/wam", "foo/wam/bar" ) );
160       assertTrue( !ResUtils.isPathDescendent( "foo", "" ) );
161       assertTrue( !ResUtils.isPathDescendent( "foo", "bam" ) );
162       assertTrue( !ResUtils.isPathDescendent( "foo", "bam/foo" ) );
163     }
164     catch ( Exception JavaDoc e ) {
165       e.printStackTrace();
166       fail();
167     }
168   }
169
170   public void testDeleteFile() {
171     assertTrue( f3.exists() );
172     ResUtils.deleteFile( f3 );
173     assertTrue( !f3.exists() );
174
175     assertTrue( f2.exists() );
176     ResUtils.deleteFile( f2 );
177     assertTrue( !f2.exists() );
178   }
179
180   public void testGetFileTree() {
181     try {
182       assertNull( session.getAttribute( ResGlobals.FILETREE_KEY ) );
183
184       fileTree = ResUtils.getFileTree( new DefaultResPolicy(), request );
185       assertNotNull( fileTree );
186
187       node = ( FoldingTreeNode )fileTree.getRoot();
188       assertEquals( 1, node.getChildCount() );
189
190       assertEquals( fileTree, session.getAttribute( ResGlobals.FILETREE_KEY ) );
191     }
192     catch ( Exception JavaDoc e ) {
193       e.printStackTrace();
194       fail();
195     }
196   }
197 }
198
Popular Tags