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.operations;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.File;
25  import java.util.Collection;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.vfs2.FileObject;
29  import org.apache.commons.vfs2.FileSystemException;
30  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
31  import org.apache.commons.vfs2.operations.vcs.VcsLog;
32  import org.apache.commons.vfs2.provider.FileProvider;
33  import org.apache.commons.vfs2.provider.VfsComponent;
34  import org.apache.commons.vfs2.provider.VfsComponentContext;
35  import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
36  import org.junit.jupiter.api.AfterEach;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Basic Tests for the FileOperations and FileOperationsProvider API.
42   */
43  public class BasicOperationsTest {
44  
45      /**
46       * Base class for different Test Providers. This is also a compile test to ensure interface stability.
47       */
48      static class MyFileOperationProviderBase implements FileOperationProvider {
49          int ops; // bit array to record invocations (poor man's mock)
50  
51          @Override
52          public void collectOperations(final Collection<Class<? extends FileOperation>> operationsList,
53                  final FileObject file) throws FileSystemException {
54              assertNotNull(operationsList, "collect operationsList");
55              assertNotNull(file, "collect file");
56              ops |= 16;
57          }
58  
59          @Override
60          public FileOperation getOperation(final FileObject file, final Class<? extends FileOperation> operationClass)
61                  throws FileSystemException {
62              assertNotNull(file, "file object");
63              assertNotNull(operationClass, "operationClass");
64              ops |= 32;
65              return null;
66          }
67      }
68  
69      /** This FileOperationsProvider is a VfsComponent and records invocations. */
70      static class MyFileOperationProviderComp extends MyFileOperationProviderBase implements VfsComponent {
71          @Override
72          public void close() {
73              ops |= 8;
74          }
75  
76          @Override
77          public void init() throws FileSystemException {
78              ops |= 4;
79          }
80  
81          @Override
82          public void setContext(final VfsComponentContext context) {
83              assertNotNull(context, "setContext");
84              ops |= 2;
85          }
86  
87          @Override
88          public void setLogger(final Log logger) {
89              assertNotNull(logger, "setLogger");
90              ops |= 1;
91          }
92      }
93  
94      /** This FileOperationsProvider is no VfsComponent. */
95      static class MyFileOperationProviderNoncomp extends MyFileOperationProviderBase {
96          // empty
97      }
98  
99      /** FSM to work with, maintained by JUnit Fixture. */
100     private DefaultFileSystemManager manager;
101 
102     /**
103      * JUnit Fixture: Prepare a simple FSM.
104      *
105      * @throws FileSystemException for runtime problems
106      */
107     @BeforeEach
108     public void setUp() throws FileSystemException {
109         manager = new DefaultFileSystemManager();
110         @SuppressWarnings("resource") // manager is closed on @AfterEach
111         final FileProvider fp = new DefaultLocalFileProvider();
112         manager.addProvider("file", fp);
113         manager.init();
114     }
115 
116     /**
117      * JUnit Fixture: Tear Down the FSM.
118      */
119     @AfterEach
120     public void tearDown() {
121         if (manager != null) {
122             manager.close();
123             manager = null;
124         }
125     }
126 
127     /**
128      * Ensure FileOperationProviders which are VfsComponents are set up and teared down.
129      *
130      * @throws FileSystemException for runtime problems
131      */
132     @Test
133     public void testLifecycleComp() throws FileSystemException {
134         try (final MyFileOperationProviderComp myop = new MyFileOperationProviderComp()) {
135             assertEquals(0, myop.ops);
136             manager.addOperationProvider("file", myop);
137             assertEquals(7, myop.ops);
138             manager.close();
139             assertEquals(15, myop.ops, "close() not called"); // VFS-577
140         }
141         // fixture will close again
142     }
143 
144     /**
145      * Ensure you can use FileOperationProvider which is not a VfsComponent.
146      *
147      * @throws FileSystemException for runtime problems
148      */
149     @Test
150     public void testLifecycleNoncomp() throws FileSystemException {
151         final MyFileOperationProviderBase myop = new MyFileOperationProviderNoncomp();
152         manager.addOperationProvider("file", myop);
153         final FileOperationProvider[] ops = manager.getOperationProviders("file");
154         assertSame(1, ops.length, "exactly one provider registered");
155         assertSame(myop, ops[0]);
156         assertEquals(0, myop.ops); // collect not invoked
157     }
158 
159     /**
160      * Ensures getOperations calls collect and allows empty response.
161      *
162      * @throws FileSystemException for runtime problems
163      */
164     @Test
165     public void testNotFoundAny() throws FileSystemException {
166         final MyFileOperationProviderBase myop = new MyFileOperationProviderNoncomp();
167         manager.addOperationProvider("file", myop);
168         try (final FileObject fo = manager.toFileObject(new File("."))) {
169 
170             final FileOperations ops = fo.getFileOperations();
171             assertNotNull(ops);
172 
173             final Class<? extends FileOperation>[] oparray = ops.getOperations();
174             assertSame(0, oparray.length, "no ops should be found");
175             assertSame(16, myop.ops); // collect
176         }
177     }
178 
179     /**
180      * Ensure proper response for not found FileOperation.
181      *
182      * @throws FileSystemException for runtime problems
183      */
184     @Test
185     public void testNotFoundOperation() throws FileSystemException {
186         final MyFileOperationProviderBase myop = new MyFileOperationProviderNoncomp();
187         manager.addOperationProvider("file", myop);
188         try (final FileObject fo = manager.toFileObject(new File("."))) {
189 
190             final FileOperations ops = fo.getFileOperations();
191             assertNotNull(ops);
192 
193             final FileSystemException thrown = assertThrows(FileSystemException.class, () -> ops.getOperation(VcsLog.class));
194             assertEquals("vfs.operation/operation-not-supported.error", thrown.getCode());
195             assertSame(32, myop.ops); // getOperation was called
196         }
197     }
198 
199 }