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 java.util.Objects;
20  
21  import org.apache.commons.vfs2.FileObject;
22  import org.apache.commons.vfs2.FilesCache;
23  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  /**
28   * Tests for {@link LRUFilesCache} used by {@link LRUFilesCacheTestCase}.
29   */
30  public class LRUFilesCacheTests extends AbstractFilesCacheTestsBase {
31  
32      @Test
33      public void testClass() {
34          @SuppressWarnings("resource")
35          final DefaultFileSystemManager manager = getManager();
36          Assert.assertNotNull("manager", manager);
37          final FilesCache filesCache = manager.getFilesCache();
38          assertTrue(Objects.toString(filesCache), filesCache instanceof LRUFilesCache);
39      }
40  
41      @Test
42      public void testFilesCache() throws Exception {
43          final FileObject scratchFolder = getWriteFolder();
44          Assert.assertNotNull("scratchFolder", scratchFolder);
45  
46          // releaseable
47          final FileObject dir1 = scratchFolder.resolveFile("dir1");
48  
49          // avoid cache removal
50          final FileObject dir2 = scratchFolder.resolveFile("dir2");
51          dir2.getContent();
52  
53          // releaseable
54          @SuppressWarnings("unused")
55          final FileObject dir3 = scratchFolder.resolveFile("dir3");
56  
57          // releaseable
58          @SuppressWarnings("unused")
59          final FileObject dir4 = scratchFolder.resolveFile("dir4");
60  
61          // releaseable
62          @SuppressWarnings("unused")
63          final FileObject dir5 = scratchFolder.resolveFile("dir5");
64  
65          // releaseable
66          @SuppressWarnings("unused")
67          final FileObject dir6 = scratchFolder.resolveFile("dir6");
68  
69          // releaseable
70          @SuppressWarnings("unused")
71          final FileObject dir7 = scratchFolder.resolveFile("dir7");
72  
73          // releaseable
74          @SuppressWarnings("unused")
75          final FileObject dir8 = scratchFolder.resolveFile("dir8");
76  
77          // check if the cache still holds the right instance
78          final FileObject dir2_2 = scratchFolder.resolveFile("dir2");
79          assertSame(dir2, dir2_2);
80  
81          // check if the cache still holds the right instance
82          final FileObject dir1_2 = scratchFolder.resolveFile("dir1");
83          assertNotSame(dir1, dir1_2);
84      }
85  }