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