001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.repository.vfs;
019:
020: import java.net.URI;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.lang.StringUtils;
028:
029: import junit.framework.TestCase;
030:
031: public class VfsResourceTest extends TestCase {
032: private VfsTestHelper helper = null;
033:
034: public VfsResourceTest() {
035: super ();
036: }
037:
038: public VfsResourceTest(String arg0) {
039: super (arg0);
040: }
041:
042: public void setUp() throws Exception {
043: helper = new VfsTestHelper();
044: }
045:
046: public void tearDown() {
047: helper = null;
048: }
049:
050: /**
051: * Validate VFSResource creation for a valid VFS URI pointing to an physically existing file
052: */
053: public void testCreateResourceThatExists() throws Exception {
054: Iterator vfsURIs = helper.createVFSUriSet(
055: VfsTestHelper.TEST_IVY_XML).iterator();
056: while (vfsURIs.hasNext()) {
057: VfsURI vfsURI = (VfsURI) vfsURIs.next();
058: String resId = vfsURI.toString();
059: VfsResource res = new VfsResource(resId, helper.fsManager);
060: if (res == null) {
061: fail("Unexpected null value on VFS URI: " + resId);
062: }
063:
064: if (!res.exists()) {
065: fail("Resource does not exist and it should: " + resId);
066: }
067:
068: // VFS apparently does some weird normalization so that resource id used to create
069: // the VFS resource is not necessarily identical to the id returned from the getName
070: // method <sigh>. We try to work around this by transforming things into java URIs.
071: if (!new URI(escapeUrl(resId)).equals(new URI(escapeUrl(res
072: .getName())).normalize())) {
073: fail("Failed on getName. Expected: " + resId
074: + ". Actual: " + res.getName());
075: }
076:
077: if (res.getLastModified() == 0) {
078: fail("Expected non-null file modification date for URI: "
079: + resId);
080: }
081:
082: if (res.getContentLength() == 0) {
083: fail("Expected non-zero file length for URI: " + resId);
084: }
085:
086: if (!res.physicallyExists()) {
087: fail("Physical existence check returned false for existing resource: "
088: + resId);
089: }
090: }
091: }
092:
093: /**
094: * Escape invalid URL characters (Copied from Wicket, just use StringUtils instead of Strings)
095: *
096: * @param queryString The orginal querystring
097: * @return url The querystring with invalid characters escaped
098: */
099: private String escapeUrl(String queryString) {
100: queryString = StringUtils.replace(queryString, " ", "%20");
101: queryString = StringUtils.replace(queryString, "\"", "%22");
102: queryString = StringUtils.replace(queryString, "%", "%26");
103: queryString = StringUtils.replace(queryString, "=", "%3D");
104: queryString = StringUtils.replace(queryString, "/", "%2F");
105: queryString = StringUtils.replace(queryString, "+", "%2B");
106: queryString = StringUtils.replace(queryString, "&", "%26");
107: queryString = StringUtils.replace(queryString, "~", "%7E");
108: queryString = StringUtils.replace(queryString, "?", "%3F");
109: return queryString;
110: }
111:
112: /**
113: * Validating that resource can be created for files which don't physically exists - e.g.
114: * resources that are going to created.
115: */
116: public void testCreateResourceThatDoesntExist() throws Exception {
117: Iterator vfsURIs = helper.createVFSUriSet("zzyyxx.zzyyxx")
118: .iterator();
119: while (vfsURIs.hasNext()) {
120: VfsURI vfsURI = (VfsURI) vfsURIs.next();
121: String resId = vfsURI.toString();
122: VfsResource res = new VfsResource(resId, helper.fsManager);
123: if (res == null) {
124: fail("Unexpected null value on VFS URI: " + resId);
125: }
126:
127: if (res.exists()) {
128: fail("Resource does not exist and it shouldn't: "
129: + resId);
130: }
131:
132: // VFS apparently does some weird normalization so that resource id used to create
133: // the VFS resource is not necessarily identical to the id returned from the getName
134: // method <sigh>. We try to work around this by transforming things into java URIs.
135: if (!new URI(escapeUrl(resId)).equals(new URI(escapeUrl(res
136: .getName())).normalize())) {
137: fail("Failed on getName. Expected: " + resId
138: + ". Actual: " + res.getName());
139: }
140:
141: if (res.getLastModified() != 0) {
142: fail("Expected null file modification date for URI: "
143: + resId + ": " + res.getLastModified());
144: }
145:
146: if (res.getContentLength() != 0) {
147: fail("Expected non-zero file length for URI: " + resId);
148: }
149:
150: if (res.physicallyExists()) {
151: fail("Physical existence check returned true for non-existent resource: "
152: + resId);
153: }
154: }
155: }
156:
157: /**
158: * Validate VFSResource creation when given a poorly formed VFS identifier
159: */
160: public void testBadURI() throws Exception {
161: String vfsURI = "smb1:/goobeldygook";
162: VfsResource res = new VfsResource(vfsURI, helper.fsManager);
163:
164: if (res == null) {
165: fail("Unexpected null value on VFS URI: " + vfsURI);
166: }
167:
168: if (res.exists()) {
169: fail("Resource is marked as existing and it should not: "
170: + vfsURI);
171: }
172:
173: if (!res.getName().equals("smb1:/goobeldygook")) {
174: fail("Failed on getName. Expected: " + vfsURI
175: + ". Actual: " + res.getName());
176: }
177:
178: if (res.getLastModified() != 0) {
179: fail("Expected null file modification date for URI: "
180: + vfsURI + ": " + res.getLastModified());
181: }
182:
183: if (res.getContentLength() != 0) {
184: fail("Expected zero file length for URI: " + vfsURI);
185: }
186:
187: if (res.physicallyExists()) {
188: fail("Physical existence check returned false for existing resource: "
189: + vfsURI);
190: }
191: }
192:
193: /**
194: * Validate getChildren when given a VFS URI for a directory
195: */
196: public void testListFolderChildren() throws Exception {
197: final String testFolder = "2/mod10.1";
198: final List expectedFiles = Arrays.asList(new String[] {
199: "ivy-1.0.xml", "ivy-1.1.xml", "ivy-1.2.xml",
200: "ivy-1.3.xml" });
201:
202: Iterator baseVfsURIs = helper.createVFSUriSet(testFolder)
203: .iterator();
204: while (baseVfsURIs.hasNext()) {
205: VfsURI baseVfsURI = (VfsURI) baseVfsURIs.next();
206:
207: List expected = new ArrayList();
208: for (int i = 0; i < expectedFiles.size(); i++) {
209: String resId = baseVfsURI.toString() + "/"
210: + expectedFiles.get(i);
211: expected.add(resId);
212: }
213:
214: List actual = new ArrayList();
215: VfsResource res = new VfsResource(baseVfsURI.toString(),
216: helper.fsManager);
217: Iterator children = res.getChildren().iterator();
218: while (children.hasNext()) {
219: String resId = (String) children.next();
220: // remove entries ending in .svn
221: if (!resId.endsWith(".svn")) {
222: actual.add(resId);
223: }
224: }
225:
226: Collections.sort(actual);
227: Collections.sort(expected);
228: if (!actual.equals(expected)) {
229: fail("\nExpected: " + expected.toString()
230: + "\n.Actual: " + actual.toString());
231: }
232: }
233: }
234:
235: /**
236: * Validate that we don't get any results when we query a VFSResource file object for its
237: * children
238: */
239: public void testListFileChildren() throws Exception {
240: Iterator testSet = helper.createVFSUriSet(
241: VfsTestHelper.TEST_IVY_XML).iterator();
242: while (testSet.hasNext()) {
243: VfsURI vfsURI = (VfsURI) testSet.next();
244: VfsResource res = new VfsResource(vfsURI.toString(),
245: helper.fsManager);
246: List results = res.getChildren();
247: if (results.size() > 0) {
248: fail("getChildren query on file provided results when it shouldn't have");
249: }
250: }
251: }
252:
253: /**
254: * Validate that we don't get any results if we ask an IMAGINARY VFSResource - a nonexistent
255: * file - for a list of its children
256: */
257: public void testListImaginary() throws Exception {
258: Iterator testSet = helper.createVFSUriSet("idontexistzzxx")
259: .iterator();
260: while (testSet.hasNext()) {
261: VfsURI vfsURI = (VfsURI) testSet.next();
262: VfsResource res = new VfsResource(vfsURI.toString(),
263: helper.fsManager);
264: List results = res.getChildren();
265: if (results.size() > 0) {
266: fail("getChildren query on file provided results when it shouldn't have");
267: }
268: }
269: }
270: }
|