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.cache;
18  
19  import org.apache.commons.vfs2.AbstractProviderTestCase;
20  import org.apache.commons.vfs2.CacheTestSuite;
21  import org.apache.commons.vfs2.FileName;
22  import org.apache.commons.vfs2.FileObject;
23  import org.apache.commons.vfs2.FileSystem;
24  import org.apache.commons.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.FilesCache;
26  import org.junit.Test;
27  
28  /**
29   * Base class for different FilesCache tests.
30   * <p>
31   * Make sure {@link CacheTestSuite} is configured with correct FilesCache.
32   */
33  public abstract class AbstractFilesCacheTestsBase extends AbstractProviderTestCase {
34  
35      /** Helper method, may be used in cache specific tests. */
36      protected int getFileHashCode() throws FileSystemException {
37          final FileObject fo = getWriteFolder().resolveFile("file2");
38          if (!fo.exists()) {
39              fo.createFile();
40          }
41  
42          return fo.hashCode();
43      }
44  
45      /**
46       * Basic Cache operations, work for all caches (besides {@link NullFilesCacheTests#testBasicCacheOps() NullFilesCache}).
47       */
48      @Test
49      public void testBasicCacheOps() throws Exception {
50          final FilesCache cache = getManager().getFilesCache();
51          final FileObject fo = getWriteFolder().resolveFile("dir1");
52          final FileName fn = fo.getName();
53          final FileSystem fs = fo.getFileSystem();
54  
55          cache.clear(fs);
56          assertNull(cache.getFile(fs, fn));
57  
58          cache.putFile(fo);
59          assertSame(fo, cache.getFile(fs, fn));
60  
61          assertFalse(cache.putFileIfAbsent(fo));
62          cache.clear(fs);
63          assertNull(cache.getFile(fs, fn));
64          assertTrue(cache.putFileIfAbsent(fo));
65  
66          cache.removeFile(fs, fn);
67          assertNull(cache.getFile(fs, fn));
68          assertTrue(cache.putFileIfAbsent(fo));
69      }
70  
71      /**
72       * Will test if the cache is cleared and if it is still useable afterwards. It will actually ensure the test is
73       * hitting the cache.
74       */
75      @Test
76      public void testClearFiles() throws Exception {
77          final FilesCache cache = getManager().getFilesCache();
78  
79          final FileObject fo1 = getWriteFolder().resolveFile("dir1");
80  
81          // clean the cache for this file system
82          cache.clear(fo1.getFileSystem());
83          // make sure a empty cache clean does not fail
84          cache.clear(fo1.getFileSystem());
85  
86          final FileObject fo2 = getWriteFolder().resolveFile("dir1");
87  
88          assertNotSame("Objects after cache clear should be different", fo1, fo2);
89      }
90  }