1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.provider.ram.test;
18  
19  import java.io.OutputStream;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.vfs.FileObject;
24  import org.apache.commons.vfs.FileSystemException;
25  import org.apache.commons.vfs.FileSystemOptions;
26  import org.apache.commons.vfs.FileType;
27  import org.apache.commons.vfs.impl.DefaultFileSystemManager;
28  import org.apache.commons.vfs.provider.ram.RamFileProvider;
29  import org.apache.commons.vfs.provider.ram.RamFileSystemConfigBuilder;
30  
31  /***
32   * Custom tests
33   * 
34   * @author edgar poce
35   * @version
36   * 
37   */
38  public class CustomRamProviderTest extends TestCase
39  {
40  	DefaultFileSystemManager manager;
41  
42  	FileSystemOptions zeroSized = new FileSystemOptions();
43  
44  	FileSystemOptions smallSized = new FileSystemOptions();
45  
46      FileSystemOptions defaultRamFs = new FileSystemOptions();
47  
48      protected void setUp() throws Exception
49  	{
50  		super.setUp();
51  
52  		manager = new DefaultFileSystemManager();
53  		manager.addProvider("ram", new RamFileProvider());
54  		manager.init();
55  
56  		// File Systems Options
57  		RamFileSystemConfigBuilder.getInstance().setMaxSize(zeroSized, 0);
58  		RamFileSystemConfigBuilder.getInstance().setMaxSize(smallSized, 10);
59  	}
60  
61  	protected void tearDown() throws Exception
62  	{
63  		super.tearDown();
64  		manager.close();
65  	}
66  
67  	public void testSmallFS() throws Exception
68  	{
69  
70  		// Default FS
71  		FileObject fo1 = manager.resolveFile("ram:/");
72  		FileObject fo2 = manager.resolveFile("ram:/");
73  		assertTrue("Both files should exist in the same fs instance.", fo1
74  				.getFileSystem() == fo2.getFileSystem());
75  
76  		// Small FS
77  		FileObject fo3 = manager.resolveFile("ram:/fo3", smallSized);
78  		FileObject fo4 = manager.resolveFile("ram:/", smallSized);
79  		assertTrue("Both files should exist in different fs instances.", fo3
80  				.getFileSystem() == fo4.getFileSystem());
81  		assertTrue("These file shouldn't be in the same file system.", fo1
82  				.getFileSystem() != fo3.getFileSystem());
83  
84  		fo3.createFile();
85  		try
86  		{
87  			OutputStream os = fo3.getContent().getOutputStream();
88  			os.write(new byte[10]);
89  			os.close();
90  		}
91  		catch (FileSystemException e)
92  		{
93  			fail("It shouldn't save such a small file");
94  		}
95  
96  		try
97  		{
98  			OutputStream os = fo3.getContent().getOutputStream();
99  			os.write(new byte[11]);
100 			os.close();
101 			fail("It shouldn't save such a big file");
102 		}
103 		catch (FileSystemException e)
104 		{
105 			// exception awaited
106 			;
107 		}
108 
109 	}
110 
111     /***
112      * 
113      * Checks root folder exists
114      * 
115      * @throws FileSystemException
116      */
117     public void testRootFolderExists() throws FileSystemException {
118         FileObject root = manager.resolveFile("ram:///", defaultRamFs);
119         assertTrue(root.getType().hasChildren());
120 
121         try {
122             root.delete();
123             fail();
124         } catch (FileSystemException e) {
125             
126         }
127 
128     }
129 
130     
131 }