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.provider.test;
18  
19  import static org.apache.commons.vfs2.VfsTestUtils.assertSameMessage;
20  import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectoryFile;
21  
22  import java.io.File;
23  
24  import org.apache.commons.vfs2.AbstractProviderTestCase;
25  import org.apache.commons.vfs2.FileChangeEvent;
26  import org.apache.commons.vfs2.FileListener;
27  import org.apache.commons.vfs2.FileObject;
28  import org.apache.commons.vfs2.FileSystem;
29  import org.apache.commons.vfs2.FileSystemException;
30  import org.apache.commons.vfs2.provider.DelegateFileObject;
31  import org.apache.commons.vfs2.util.WeakRefFileListener;
32  import org.junit.Test;
33  
34  class DebugFileListener implements FileListener {
35  
36      private boolean changed;
37      private boolean created;
38      private boolean deleted;
39  
40      @Override
41      public void fileChanged(final FileChangeEvent event) throws Exception {
42          changed = true;
43      }
44  
45      @Override
46      public void fileCreated(final FileChangeEvent event) throws Exception {
47          created = true;
48      }
49  
50      @Override
51      public void fileDeleted(final FileChangeEvent event) throws Exception {
52          deleted = true;
53      }
54  
55      @Override
56      public String toString() {
57          return "Listener " + changed + " " + created + " " + deleted;
58      }
59  }
60  
61  /**
62   * Additional junction test cases.
63   */
64  public class JunctionTests extends AbstractProviderTestCase {
65  
66      private FileObject getBaseDir() throws FileSystemException {
67          final File file = getTestDirectoryFile();
68          assertTrue(file.exists());
69          return getManager().toFileObject(file);
70      }
71  
72      /**
73       * Checks ancestors are created when a junction is created.
74       */
75      @Test
76      public void testAncestors() throws Exception {
77          final FileSystem fs = getManager().createVirtualFileSystem("vfs://").getFileSystem();
78          final FileObject baseDir = getBaseDir();
79  
80          // Make sure the file at the junction point and its ancestors do not exist
81          FileObject file = fs.resolveFile("/a/b");
82          assertFalse(file.exists());
83          file = file.getParent();
84          assertFalse(file.exists());
85          file = file.getParent();
86          assertFalse(file.exists());
87  
88          // Add the junction
89          fs.addJunction("/a/b", baseDir);
90  
91          // Make sure the file at the junction point and its ancestors exist
92          file = fs.resolveFile("/a/b");
93          assertTrue("Does not exist", file.exists());
94          file = file.getParent();
95          assertTrue("Does not exist", file.exists());
96          file = file.getParent();
97          assertTrue("Does not exist", file.exists());
98      }
99  
100     /**
101      * Checks that change events from delegatedd files are fired.
102      */
103     public void testEvent() throws Exception {
104         // we use the VirtualFileSystem to check change event propagation of DecoratedFileObject
105         final FileSystem fs = getManager().createVirtualFileSystem("vfs://").getFileSystem();
106         final FileObject baseDir = getBaseDir().resolveFile("junctiontest");
107 
108         // Add the junction
109         fs.addJunction("/a", baseDir);
110 
111         // Make sure the file at the junction point and its ancestors exist
112         final FileObject file = fs.resolveFile("/a/hardref.txt");
113         assertSame("VirtualFileSystem does not use DelegateFO anymore?", file.getClass(), DelegateFileObject.class);
114 
115         // Do with a hard reference listener
116         final FileListener listener1 = new DebugFileListener();
117         file.getFileSystem().addListener(file, listener1);
118         final FileObject real1 = baseDir.resolveFile("hardref.txt");
119         real1.createFile();
120         assertEquals("Strong Listener was not notified (create)", "Listener false true false", listener1.toString());
121         real1.delete();
122         assertEquals("Strong Listener was not notified (delete)", "Listener false true true", listener1.toString());
123 
124         final FileObject file2 = fs.resolveFile("/a/weakref.txt");
125         assertSame("VirtualFileSystem does not use DelegateFO anymore?", file2.getClass(), DelegateFileObject.class);
126         // repeat with Weak reference listener
127         final FileListener listener2 = new DebugFileListener();
128         // since we hold the listener2 reference it should not get GC
129         WeakRefFileListener.installListener(file2, listener2);
130 
131         // force the WekRefFileListener reference to (not) clear
132         System.gc();
133         Thread.sleep(1000);
134         System.gc();
135         Thread.sleep(1000);
136         System.gc();
137         Thread.sleep(1000);
138         System.gc();
139         Thread.sleep(1000);
140 
141         final FileObject real2 = baseDir.resolveFile("weakref.txt");
142         real2.createFile();
143         try {
144             assertEquals("Weak Listener was abandoned", "Listener false true false", listener2.toString());
145         } finally {
146             assertTrue("Don't contaminate the fs for the next time the test runs", file2.delete());
147         }
148     }
149 
150     /**
151      * Checks nested junctions are not supported.
152      */
153     @Test
154     public void testNestedJunction() throws Exception {
155         final FileSystem fs = getManager().createVirtualFileSystem("vfs:").getFileSystem();
156         final FileObject baseDir = getBaseDir();
157         fs.addJunction("/a", baseDir);
158 
159         // Nested
160         try {
161             fs.addJunction("/a/b", baseDir);
162             fail();
163         } catch (final Exception e) {
164             assertSameMessage("vfs.impl/nested-junction.error", "vfs:/a/b", e);
165         }
166 
167         // At same point
168         try {
169             fs.addJunction("/a", baseDir);
170             fail();
171         } catch (final Exception e) {
172             assertSameMessage("vfs.impl/nested-junction.error", "vfs:/a", e);
173         }
174     }
175 
176     // Check that file @ junction point exists only when backing file exists
177     // Add 2 junctions with common parent
178     // Compare real and virtual files
179     // Events
180     // Remove junctions
181 
182 }
183