KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > smb > test > FileNameTestCase


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.provider.smb.test;
17
18 import org.apache.commons.AbstractVfsTestCase;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.provider.smb.SmbFileName;
21 import org.apache.commons.vfs.provider.smb.SmbFileNameParser;
22
23 /**
24  * Some additional SMB file name test cases.
25  *
26  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27  */

28 public class FileNameTestCase
29     extends AbstractVfsTestCase
30 {
31     /**
32      * Tests parsing a URI into its parts.
33      */

34     public void testParseUri() throws Exception JavaDoc
35     {
36         // Simple name
37
SmbFileName name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://hostname/share/file");
38         assertEquals("smb", name.getScheme());
39         assertNull(name.getUserName());
40         assertNull(name.getPassword());
41         assertEquals("hostname", name.getHostName());
42         assertEquals(139, name.getPort());
43         assertEquals(name.getDefaultPort(), name.getPort());
44         assertEquals("share", name.getShare());
45         assertEquals("/file", name.getPath());
46         assertEquals("smb://hostname/share/", name.getRootURI());
47         assertEquals("smb://hostname/share/file", name.getURI());
48
49         // Name with port
50
name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://hostname:9090/share/file");
51         assertEquals("smb", name.getScheme());
52         assertNull(name.getUserName());
53         assertNull(name.getPassword());
54         assertEquals("hostname", name.getHostName());
55         assertEquals(9090, name.getPort());
56         assertEquals("share", name.getShare());
57         assertEquals("/file", name.getPath());
58         assertEquals("smb://hostname:9090/share/", name.getRootURI());
59         assertEquals("smb://hostname:9090/share/file", name.getURI());
60
61         // Name with no path
62
name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://hostname/share");
63         assertEquals("smb", name.getScheme());
64         assertNull(name.getUserName());
65         assertNull(name.getPassword());
66         assertEquals("hostname", name.getHostName());
67         assertEquals(139, name.getPort());
68         assertEquals("share", name.getShare());
69         assertEquals("/", name.getPath());
70         assertEquals("smb://hostname/share/", name.getRootURI());
71         assertEquals("smb://hostname/share/", name.getURI());
72
73         // Name with username
74
name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://user@hostname/share/file");
75         assertEquals("smb", name.getScheme());
76         assertEquals("user", name.getUserName());
77         assertNull(name.getPassword());
78         assertEquals("hostname", name.getHostName());
79         assertEquals(139, name.getPort());
80         assertEquals("share", name.getShare());
81         assertEquals("/file", name.getPath());
82         assertEquals("smb://user@hostname/share/", name.getRootURI());
83         assertEquals("smb://user@hostname/share/file", name.getURI());
84
85         // Name with extension
86
name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://user@hostname/share/file.txt");
87         assertEquals("smb", name.getScheme());
88         assertEquals("user", name.getUserName());
89         assertNull(name.getPassword());
90         assertEquals("hostname", name.getHostName());
91         assertEquals(139, name.getPort());
92         assertEquals("share", name.getShare());
93         assertEquals("/file.txt", name.getPath());
94         assertEquals("file.txt", name.getBaseName());
95         assertEquals("txt", name.getExtension());
96         assertEquals("smb://user@hostname/share/", name.getRootURI());
97         assertEquals("smb://user@hostname/share/file.txt", name.getURI());
98
99         // Name look likes extension, but isnt
100
name = (SmbFileName) SmbFileNameParser.getInstance().parseUri(null, null, "smb://user@hostname/share/.bashrc");
101         assertEquals("smb", name.getScheme());
102         assertEquals("user", name.getUserName());
103         assertNull(name.getPassword());
104         assertEquals("hostname", name.getHostName());
105         assertEquals(139, name.getPort());
106         assertEquals("share", name.getShare());
107         assertEquals("/.bashrc", name.getPath());
108         assertEquals(".bashrc", name.getBaseName());
109         assertEquals("", name.getExtension());
110         assertEquals("smb://user@hostname/share/", name.getRootURI());
111         assertEquals("smb://user@hostname/share/.bashrc", name.getURI());
112     }
113
114     /**
115      * Tests error handling in URI parser.
116      */

117     public void testBadlyFormedUri() throws Exception JavaDoc
118     {
119         // Does not start with smb://
120
testBadlyFormedUri("smb:", "vfs.provider/missing-double-slashes.error");
121         testBadlyFormedUri("smb:/", "vfs.provider/missing-double-slashes.error");
122         testBadlyFormedUri("smb:a", "vfs.provider/missing-double-slashes.error");
123
124         // Missing hostname
125
testBadlyFormedUri("smb://", "vfs.provider/missing-hostname.error");
126         testBadlyFormedUri("smb://:21/share", "vfs.provider/missing-hostname.error");
127         testBadlyFormedUri("smb:///share", "vfs.provider/missing-hostname.error");
128
129         // Empty port
130
testBadlyFormedUri("smb://host:", "vfs.provider/missing-port.error");
131         testBadlyFormedUri("smb://host:/share", "vfs.provider/missing-port.error");
132         testBadlyFormedUri("smb://host:port/share/file", "vfs.provider/missing-port.error");
133
134         // Missing absolute path
135
testBadlyFormedUri("smb://host:90a", "vfs.provider/missing-hostname-path-sep.error");
136         testBadlyFormedUri("smb://host?a", "vfs.provider/missing-hostname-path-sep.error");
137
138         // Missing share name
139
testBadlyFormedUri("smb://host", "vfs.provider.smb/missing-share-name.error");
140         testBadlyFormedUri("smb://host/", "vfs.provider.smb/missing-share-name.error");
141         testBadlyFormedUri("smb://host:9090/", "vfs.provider.smb/missing-share-name.error");
142     }
143
144     /**
145      * Tests that parsing a URI fails with the expected error.
146      */

147     private void testBadlyFormedUri(final String JavaDoc uri, final String JavaDoc errorMsg)
148     {
149         try
150         {
151             SmbFileNameParser.getInstance().parseUri(null, null, uri);
152             fail();
153         }
154         catch (final FileSystemException e)
155         {
156             assertSameMessage(errorMsg, uri, e);
157         }
158     }
159 }
160
Popular Tags