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.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.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.http.HttpFileSystemConfigBuilder;
32  import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
33  import org.apache.commons.vfs2.provider.sftp.TrustEveryoneUserInfo;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  /**
39   * Some tests for the DelegatingFileSystemOptionsBuilder
40   */
41  public class DelegatingFileSystemOptionsBuilderTest {
42      private static final String[] schemes = new String[] { "http", "ftp", "file", "zip", "tar", "tgz", "bz2", "gz",
43              "jar", "tmp", "ram" };
44  
45      private StandardFileSystemManager fsm;
46  
47      @Before
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      @After
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("Missing " + scheme + " provider", fsm.hasProvider(scheme));
66          }
67      }
68  
69      @Test
70      public void testDelegatingBad() throws Throwable {
71          final FileSystemOptions opts = new FileSystemOptions();
72          final DelegatingFileSystemOptionsBuilder delgate = new DelegatingFileSystemOptionsBuilder(fsm);
73  
74          try {
75              delgate.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(),
80                      NumberFormatException.class);
81          }
82  
83          try {
84              delgate.setConfigClass(opts, "sftp", "userinfo", String.class);
85              fail();
86          } catch (final FileSystemException e) {
87              assertEquals(e.getCode(), "vfs.provider/config-value-invalid.error");
88          }
89      }
90  
91      @Test
92      public void testDelegatingGood() throws Throwable {
93          final String[] identityPaths = new String[] { "/file1", "/file2", };
94  
95          final FileSystemOptions opts = new FileSystemOptions();
96          final DelegatingFileSystemOptionsBuilder delgate = new DelegatingFileSystemOptionsBuilder(fsm);
97  
98          delgate.setConfigString(opts, "http", "proxyHost", "proxy");
99          delgate.setConfigString(opts, "http", "proxyPort", "8080");
100         delgate.setConfigClass(opts, "sftp", "userinfo", TrustEveryoneUserInfo.class);
101         delgate.setConfigStrings(opts, "sftp", "identities", identityPaths);
102 
103         assertEquals("http.proxyHost", HttpFileSystemConfigBuilder.getInstance().getProxyHost(opts), "proxy");
104         assertEquals("http.proxyPort", HttpFileSystemConfigBuilder.getInstance().getProxyPort(opts), 8080);
105         assertSame("sftp.userInfo", SftpFileSystemConfigBuilder.getInstance().getUserInfo(opts).getClass(),
106                 TrustEveryoneUserInfo.class);
107 
108         final File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(opts);
109         assertNotNull("sftp.identities", identities);
110         assertEquals("sftp.identities size", identities.length, identityPaths.length);
111         for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
112             assertEquals("sftp.identities #" + iterIdentities, identities[iterIdentities].getAbsolutePath(),
113                     new File(identityPaths[iterIdentities]).getAbsolutePath());
114         }
115     }
116 }