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