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