KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > util > DelegatingFileSystemOptionsBuilderTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28
29 /**
30  * Some tests for the DelegatingFileSystemOptionsBuilder
31  *
32  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
33  */

34 public class DelegatingFileSystemOptionsBuilderTest extends TestCase
35 {
36     private StandardFileSystemManager fsm = null;
37
38     protected void setUp() throws Exception JavaDoc
39     {
40         super.tearDown();
41
42         // get a full blown, fully functional manager
43
fsm = new StandardFileSystemManager();
44         fsm.init();
45     }
46
47
48     protected void tearDown() throws Exception JavaDoc
49     {
50         if (fsm != null)
51         {
52             fsm.close();
53         }
54
55         super.tearDown();
56     }
57
58     public void testDelegatingGood() throws Throwable JavaDoc
59     {
60         final String JavaDoc[] identityPaths = new String JavaDoc[]
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 JavaDoc 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 JavaDoc(identityPaths[iterIdentities]).getAbsolutePath());
86         }
87     }
88
89     public void testDelegatingBad() throws Throwable JavaDoc
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 JavaDoc.class);
102             assertEquals(((InvocationTargetException JavaDoc) e.getCause()).getTargetException().getClass(), NumberFormatException JavaDoc.class);
103         }
104
105         try
106         {
107             delgate.setConfigClass(opts, "sftp", "userinfo", String JavaDoc.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