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.util;
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.assertTrue;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.io.File;
26  import java.lang.reflect.InvocationTargetException;
27  
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileSystemOptions;
30  import org.apache.commons.vfs2.impl.StandardFileSystemManager;
31  import org.apache.commons.vfs2.provider.http5.Http5FileSystemConfigBuilder;
32  import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
33  import org.apache.commons.vfs2.provider.sftp.TrustEveryoneUserInfo;
34  import org.junit.jupiter.api.AfterEach;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Tests {@link DelegatingFileSystemOptionsBuilder}.
40   */
41  public class DelegatingFileSystemOptionsBuilderTest {
42  
43      private static final String[] schemes = { "http", "ftp", "file", "zip", "tar", "tgz", "bz2", "gz", "jar", "tmp", "ram" };
44  
45      private StandardFileSystemManager fsm;
46  
47      @BeforeEach
48      public void setUp() throws Exception {
49  
50          // get a full-blown, fully functional manager
51          fsm = new StandardFileSystemManager();
52          fsm.init();
53      }
54  
55      @AfterEach
56      public void tearDown() throws Exception {
57          if (fsm != null) {
58              fsm.close();
59          }
60      }
61  
62      @Test
63      public void testConfiguration() throws Exception {
64          for (final String scheme : schemes) {
65              assertTrue(fsm.hasProvider(scheme), () -> "Missing " + scheme + " provider");
66          }
67      }
68  
69      @Test
70      public void testDelegatingBad() throws Throwable {
71          final FileSystemOptions opts = new FileSystemOptions();
72          final DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsm);
73  
74          try {
75              delegate.setConfigString(opts, "http", "proxyPort", "wrong_port");
76              fail();
77          } catch (final FileSystemException e) {
78              assertSame(e.getCause().getClass(), InvocationTargetException.class);
79              assertSame(((InvocationTargetException) e.getCause()).getTargetException().getClass(), NumberFormatException.class);
80          }
81  
82          try {
83              delegate.setConfigClass(opts, "sftp", "userinfo", String.class);
84              fail();
85          } catch (final FileSystemException e) {
86              assertEquals(e.getCode(), "vfs.provider/config-value-invalid.error");
87          }
88      }
89  
90      @Test
91      public void testDelegatingGood() throws Throwable {
92          final String[] identityPaths = { "/file1", "/file2", };
93  
94          final FileSystemOptions opts = new FileSystemOptions();
95          final DelegatingFileSystemOptionsBuilder delegate = new DelegatingFileSystemOptionsBuilder(fsm);
96  
97          delegate.setConfigString(opts, "http", "proxyHost", "proxy");
98          delegate.setConfigString(opts, "http", "proxyPort", "8080");
99          delegate.setConfigClass(opts, "sftp", "userinfo", TrustEveryoneUserInfo.class);
100         delegate.setConfigStrings(opts, "sftp", "identities", identityPaths);
101 
102         assertEquals(Http5FileSystemConfigBuilder.getInstance().getProxyHost(opts), "proxy", "http.proxyHost");
103         assertEquals(Http5FileSystemConfigBuilder.getInstance().getProxyPort(opts), 8080, "http.proxyPort");
104         assertSame(SftpFileSystemConfigBuilder.getInstance().getUserInfo(opts).getClass(), TrustEveryoneUserInfo.class, "sftp.userInfo");
105 
106         final File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(opts);
107         assertNotNull(identities, "sftp.identities");
108         assertEquals(identities.length, identityPaths.length, "sftp.identities size");
109         for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
110             assertEquals(identities[iterIdentities].getAbsolutePath(), new File(identityPaths[iterIdentities]).getAbsolutePath(),
111                     "sftp.identities #" + iterIdentities);
112         }
113     }
114 
115 }