1   //
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3   // All rights reserved.
4   //
5   // Redistribution and use in source and binary forms, with or without modification, 
6   // are permitted provided that the following conditions are met:
7   //
8   // * Redistributions of source code must retain the above copyright notice, 
9   //	 this list of conditions and the following disclaimer.
10  // * Redistributions in binary form must reproduce the above copyright notice, 
11  //	 this list of conditions and the following disclaimer in the documentation 
12  //	 and/or other materials provided with the distribution.
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
14  //	 nor the names of its contributors may be used to endorse or promote products 
15  //	 derived from this software without specific prior written permission.
16  //
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  // POSSIBILITY OF SUCH DAMAGE.
27  //
28  
29  package org.objectledge.filesystem;
30  
31  import java.io.IOException;
32  
33  import junit.framework.TestCase;
34  
35  /***
36   *
37   *
38   * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
39   * @version $Id: LocalFileSystemProviderTest.java,v 1.7 2004/10/13 10:59:41 rafal Exp $
40   */
41  public class LocalFileSystemProviderTest extends TestCase
42  {
43      protected LocalFileSystemProvider provider;
44  
45      /***
46       * Constructor for LocalFileProviderTest.
47       * @param arg0
48       */
49      public LocalFileSystemProviderTest(String arg0)
50      {
51          super(arg0);
52      }
53  
54      /*
55       * @see TestCase#setUp()
56       */
57      protected void setUp() throws Exception
58      {
59          super.setUp();
60          provider = new LocalFileSystemProvider("local", "src/test/resources");
61      }
62  
63  	protected void tearDown() throws Exception
64  	{
65  		super.tearDown();
66  		String[] tab = new String[] { "var/new.file", "var/new.directory1", "var/new.directory2" }; 
67  		for (int i = 0; i < tab.length; i++)
68          {
69              String element = tab[i];
70              if(provider.exists(element))
71              {
72  				provider.delete(element);
73              }
74          }
75  	}
76  
77      public void testGetName()
78      {
79          assertEquals(provider.getName(), "local");
80      }
81  
82      public void testIsReadOnly()
83      {
84          assertFalse("Provider is not read only", provider.isReadOnly());
85      }
86  
87      public void testCheckPathChars()
88      {
89          assertTrue(provider.checkPathChars("asciiChars"));
90          assertFalse(provider.checkPathChars(""));
91          assertFalse(provider.checkPathChars(null));
92      }
93      
94      public void testExists()
95      {
96          assertTrue("File does not exist - check test resources!", provider.exists("file"));
97          assertFalse("File exists - check test resources!", provider.exists("nofile"));
98      }
99  
100     public void testIsFile()
101     {
102         assertTrue("The resource is not a file - check test resources!", 
103         			provider.isFile("file"));
104         assertFalse("The resource is a file - check test resources!", 
105         			provider.isFile("directory"));
106     }
107 
108     public void testIsDirectory()
109     {
110         assertTrue("The resource is not a directory - check test resources!", 
111         			provider.isDirectory("directory"));
112         assertFalse("The resource is a directory - check test resources!", 
113         			provider.isDirectory("file"));
114     }
115 
116     public void testCanRead()
117     {
118 		assertTrue("The resource is not readable - check test resources!", 
119 					provider.canRead("file"));
120 		assertFalse("The resource is readable - check test resources!", 
121 					provider.canRead("nofile"));
122     }
123     
124     private static final String UNREADABLE_PATH = "src/test/resources/unreadablefile";
125 
126     public void testCanReadUnreadable()
127     	throws Exception
128     {
129         if(System.getProperty("os.name").equals("Linux"))
130         {
131             Runtime.getRuntime().exec("chmod u-r "+UNREADABLE_PATH).waitFor();
132             try
133             {
134         		assertTrue("The resource does not exist - check test resources!", 
135     				provider.exists("unreadablefile"));
136         		assertFalse("The resource is readable - check test resources!", 
137     				provider.canRead("unreadablefile"));
138             }
139             finally
140             {
141                 Runtime.getRuntime().exec("chmod u+r "+UNREADABLE_PATH);
142             }
143         }
144     }
145 
146     public void testCanWrite()
147     {
148 		assertTrue("The resource is not writable - check test resources!", 
149 					provider.canWrite("file"));
150 		assertFalse("The resource is writable - check test resources!", 
151 					provider.canWrite("nofile"));
152     }
153     
154     private static final String UNWRITABLE_PATH = "src/test/resources/unwritablefile";
155     
156     public void testCanWriteUnwritable()
157     	throws Exception
158     {
159         if(System.getProperty("os.name").equals("Linux"))
160         {
161             Runtime.getRuntime().exec("chmod u-w "+UNWRITABLE_PATH).waitFor();
162             try
163             {
164 	    		assertTrue("The resource does not exist - check test resources!", 
165 					provider.exists("unwritablefile"));
166 	    		assertFalse("The resource is writable - check test resources!", 
167 					provider.canWrite("unwritablefile"));
168             }
169             finally
170             {
171                 Runtime.getRuntime().exec("chmod u+w "+UNWRITABLE_PATH);
172             }
173         }
174     }
175 
176     public void testList()
177     {
178 		//TODO Implement list().
179     }
180 
181     public void testCreateNewFile()
182     	throws Exception 
183     {
184 		assertTrue("This resource shouldn't exist - check test resources!", 
185 					provider.createNewFile("var/new.file"));
186 		assertFalse("This resource should exist - check test resources!", 
187 					provider.createNewFile("file"));
188 		try
189 		{
190 			provider.createNewFile("var/new.directory1/new.file");
191 			fail("The directory shouldn't exist - check test resources!");
192 		}
193 		catch(IOException e)
194 		{
195 			// expected
196 		}
197 	}
198 		
199     public void testMkdirs()
200     throws IOException
201     {
202 		try
203         {
204             provider.mkdirs("var/new.directory2");
205             assertTrue("The directory should just have been created!", 
206                 provider.exists("var/new.directory2"));
207         }
208         catch(UnsupportedCharactersInFilePathException e)
209         {
210             fail("simple ascii characters should be supported: "+e.getMessage());
211         }
212     }
213 
214     public void testDelete()
215     {
216         //TODO Implement delete().
217     }
218 
219     public void testRename()
220     {
221         //TODO Implement rename().
222     }
223 
224     public void testGetInputStream()
225     {
226         //TODO Implement getInputStream().
227     }
228 
229     public void testGetOutputStream()
230     {
231         //TODO Implement getOutputStream().
232     }
233 
234     public void testGetRandomAccess()
235     {
236         //TODO Implement getRandomAccess().
237     }
238 
239     public void testLastModified()
240     {
241         //TODO Implement lastModified().
242     }
243 
244     public void testLength()
245     {
246         //TODO Implement length().
247     }
248 
249     public void testGetFile()
250     {
251         //TODO Implement getFile().
252     }
253 
254 }