    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.vfs.util;
    18
    19  import junit.framework.TestCase;
    20  import org.apache.commons.vfs.FileSystemException;
    21  import org.apache.commons.vfs.FileSystemOptions;
    22  import org.apache.commons.vfs.impl.StandardFileSystemManager;
    23  import org.apache.commons.vfs.provider.http.HttpFileSystemConfigBuilder;
    24  import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
    25  import org.apache.commons.vfs.provider.sftp.TrustEveryoneUserInfo;
    26
    27  import java.io.File;
    28  import java.lang.reflect.InvocationTargetException;
    29
    30  /***
    31   * Some tests for the DelegatingFileSystemOptionsBuilder
    32   *
    33   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
    34   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    35   */
    36  public class DelegatingFileSystemOptionsBuilderTest extends TestCase
    37  {
    38      private StandardFileSystemManager fsm = null;
    39
    40      protected void setUp() throws Exception
    41      {
    42          super.tearDown();
    43
    44          // get a full blown, fully functional manager
    45          fsm = new StandardFileSystemManager();
    46          fsm.init();
    47      }
    48
    49
    50      protected void tearDown() throws Exception
    51      {
    52          if (fsm != null)
    53          {
    54              fsm.close();
    55          }
    56
    57          super.tearDown();
    58      }
    59
    60      public void testDelegatingGood() throws Throwable
    61      {
    62          final String[] identityPaths = new String[]
    63          {
    64              "/file1",
    65              "/file2",
    66          };
    67
    68          FileSystemOptions opts = new FileSystemOptions();
    69          DelegatingFileSystemOptionsBuilder delgate = new DelegatingFileSystemOptionsBuilder(fsm);
    70
    71          delgate.setConfigString(opts, "http", "proxyHost", "proxy");
    72          delgate.setConfigString(opts, "http", "proxyPort", "8080");
    73          delgate.setConfigClass(opts, "sftp", "userinfo", TrustEveryoneUserInfo.class);
    74          delgate.setConfigStrings(opts, "sftp", "identities", identityPaths);
    75
    76          assertEquals("http.proxyHost", HttpFileSystemConfigBuilder.getInstance().getProxyHost(opts), "proxy");
    77          assertEquals("http.proxyPort", HttpFileSystemConfigBuilder.getInstance().getProxyPort(opts), 8080);
    78          assertEquals("sftp.userInfo", SftpFileSystemConfigBuilder.getInstance().getUserInfo(opts).getClass(), TrustEveryoneUserInfo.class);
    79
    80          File identities[] = SftpFileSystemConfigBuilder.getInstance().getIdentities(opts);
    81          assertNotNull("sftp.identities", identities);
    82          assertEquals("sftp.identities size", identities.length, identityPaths.length);
    83          for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
    84          {
    85              assertEquals("sftp.identities #" + iterIdentities,
    86                  identities[iterIdentities].getAbsolutePath(),
    87                  new File(identityPaths[iterIdentities]).getAbsolutePath());
    88          }
    89      }
    90
    91      public void testDelegatingBad() throws Throwable
    92      {
    93          FileSystemOptions opts = new FileSystemOptions();
    94          DelegatingFileSystemOptionsBuilder delgate = new DelegatingFileSystemOptionsBuilder(fsm);
    95
    96          try
    97          {
    98              delgate.setConfigString(opts, "http", "proxyPort", "wrong_port");
    99              fail();
    100         }
    101         catch (FileSystemException e)
    102         {
    103             assertEquals(e.getCause().getClass(), InvocationTargetException.class);
    104             assertEquals(((InvocationTargetException) e.getCause()).getTargetException().getClass(), NumberFormatException.class);
    105         }
    106
    107         try
    108         {
    109             delgate.setConfigClass(opts, "sftp", "userinfo", String.class);
    110             fail();
    111         }
    112         catch (FileSystemException e)
    113         {
    114             assertEquals(e.getCode(), "vfs.provider/config-value-invalid.error");
    115         }
    116     }
    117 }