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.test;
18  
19  import org.apache.commons.vfs.CacheStrategy;
20  import org.apache.commons.vfs.Capability;
21  import org.apache.commons.vfs.FileObject;
22  import org.apache.commons.vfs.Selectors;
23  import org.apache.commons.vfs.provider.ram.RamFileObject;
24  import org.apache.commons.vfs.util.FileObjectUtils;
25  import org.apache.commons.vfs.impl.DefaultFileSystemManager;
26  import org.apache.commons.vfs.impl.VirtualFileSystem;
27  
28  /***
29   * Test the cache stragey
30   *
31   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
32   */
33  public class ProviderCacheStrategyTests
34      extends AbstractProviderTestCase
35  {
36      /***
37       * Returns the capabilities required by the tests of this test case.
38       */
39      protected Capability[] getRequiredCaps()
40      {
41          return new Capability[]
42          {
43              Capability.CREATE,
44              Capability.GET_TYPE,
45              Capability.LIST_CHILDREN,
46          };
47      }
48  
49      /***
50       * Test the manual cache strategy
51       */
52      public void testManualCache() throws Exception
53      {
54          FileObject scratchFolder = getWriteFolder();
55          if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class) ||
56              scratchFolder.getFileSystem() instanceof VirtualFileSystem)
57          {
58              // cant check ram filesystem as every manager holds its own ram filesystem data
59              return;
60          }
61  
62          scratchFolder.delete(Selectors.EXCLUDE_SELF);
63          
64          DefaultFileSystemManager fs = createManager();
65  	    fs.setCacheStrategy(CacheStrategy.MANUAL);
66          fs.init();
67          FileObject foBase2 = getBaseTestFolder(fs);
68  
69          FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
70          
71          FileObject[] fos = cachedFolder.getChildren();
72          assertContainsNot(fos, "file1.txt");
73          
74          scratchFolder.resolveFile("file1.txt").createFile();
75          
76          fos = cachedFolder.getChildren();
77          assertContainsNot(fos, "file1.txt");
78          
79          cachedFolder.refresh();
80          fos = cachedFolder.getChildren();
81          assertContains(fos, "file1.txt");
82      }
83  
84      /***
85       * Test the on_resolve strategy
86       */
87      public void testOnResolveCache() throws Exception
88      {
89          FileObject scratchFolder = getWriteFolder();
90          if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class) ||
91              scratchFolder.getFileSystem() instanceof VirtualFileSystem)
92          {
93              // cant check ram filesystem as every manager holds its own ram filesystem data
94              return;
95          }
96  
97          scratchFolder.delete(Selectors.EXCLUDE_SELF);
98          
99          DefaultFileSystemManager fs = createManager();
100 	    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
101         fs.init();
102         FileObject foBase2 = getBaseTestFolder(fs);
103 
104         FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
105         
106         FileObject[] fos = cachedFolder.getChildren();
107         assertContainsNot(fos, "file1.txt");
108         
109         scratchFolder.resolveFile("file1.txt").createFile();
110         
111         fos = cachedFolder.getChildren();
112         assertContainsNot(fos, "file1.txt");
113         
114         cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
115         fos = cachedFolder.getChildren();
116         assertContains(fos, "file1.txt");
117     }
118     
119     /***
120      * Test the on_call strategy
121      */
122     public void testOnCallCache() throws Exception
123     {
124         FileObject scratchFolder = getWriteFolder();
125         if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class) ||
126             scratchFolder.getFileSystem() instanceof VirtualFileSystem)
127         {
128             // cant check ram filesystem as every manager holds its own ram filesystem data
129             return;
130         }
131 
132         scratchFolder.delete(Selectors.EXCLUDE_SELF);
133         
134         DefaultFileSystemManager fs = createManager();
135 	    fs.setCacheStrategy(CacheStrategy.ON_CALL);
136         fs.init();
137         FileObject foBase2 = getBaseTestFolder(fs);
138 
139         FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
140         
141         FileObject[] fos = cachedFolder.getChildren();
142         assertContainsNot(fos, "file1.txt");
143         
144         scratchFolder.resolveFile("file1.txt").createFile();
145         
146         fos = cachedFolder.getChildren();
147         assertContains(fos, "file1.txt");
148     }
149     
150 	public void assertContainsNot(FileObject[] fos, String string)
151 	{
152 		for (int i = 0; i<fos.length; i++)
153 		{
154 			if (string.equals(fos[i].getName().getBaseName()))
155 			{
156 				fail(string + " should not be seen");
157 			}
158 		}
159 	}
160 	
161 	public void assertContains(FileObject[] fos, String string)
162 	{
163 		for (int i = 0; i<fos.length; i++)
164 		{
165 			if (string.equals(fos[i].getName().getBaseName()))
166 			{
167 				return;
168 			}
169 		}
170 		
171 		fail(string + " should be seen");
172 	}
173 }