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