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.io.File;
021: import java.io.IOException;
022: import java.util.Iterator;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.ivy.util.FileUtil;
027:
028: /**
029: * Testing Testing was the single biggest hurdle I faced. I have tried to provide a complete test
030: * suite that covers all protocols and which can be easily extended. It does differ - somewhat - in
031: * structure from the resolver/repository test suites. Setting up smb, ftp, sftp will undoubtedly be
032: * your biggest headache (it was mine). Here are a few notes about the setup: - the VFS test suite
033: * uses the build/test/repositories area - when setting samba, sftp, etc. the corresponding user
034: * needs both read and write privileges. - the tests assume that the user and password is the same
035: * for all services. - a limited amount of configuration is available by setting the following
036: * properties in the ivy.properties file: * vfs.host * vfs.username * vfs.password * vfs.samba_share
037: * Running the test requires that commons-io and ant.jar are on the classpath. Also, I would
038: * recommend that at some time the tests be converted from straight junit to something which betters
039: * supports functional testing. Although somewhat crude I am using jsystem
040: * (http://jsystemtest.sourceforge.net/) in other projects and am finding it a much better solution
041: * than straight junit. Stephen Nesbitt
042: */
043: public class VfsRepositoryTest extends TestCase {
044: private VfsRepository repo = null;
045:
046: private VfsTestHelper helper = null;
047:
048: private File scratchDir = null;
049:
050: public VfsRepositoryTest(String arg0) throws Exception {
051: super (arg0);
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: helper = new VfsTestHelper();
057: repo = new VfsRepository();
058: scratchDir = new File(FileUtil.concat(
059: VfsTestHelper.TEST_REPO_DIR, VfsTestHelper.SCRATCH_DIR));
060: scratchDir.mkdir();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: repo = null;
066: if (scratchDir.exists()) {
067: FileUtil.forceDelete(scratchDir);
068: }
069: }
070:
071: /**
072: * Basic validation of happy path put - valid VFS URI and no conflict with existing file
073: *
074: * @throws Exception
075: */
076: public void testPutValid() throws Exception {
077: String testResource = VfsTestHelper.TEST_IVY_XML;
078: String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR,
079: testResource);
080: String destResource = VfsTestHelper.SCRATCH_DIR + "/"
081: + testResource;
082: String destFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR,
083: destResource);
084:
085: Iterator vfsURIs = helper.createVFSUriSet(destResource)
086: .iterator();
087: while (vfsURIs.hasNext()) {
088: VfsURI vfsURI = (VfsURI) vfsURIs.next();
089: if (scratchDir.exists()) {
090: FileUtil.forceDelete(scratchDir);
091: }
092:
093: try {
094: repo.put(new File(srcFile), vfsURI.toString(), false);
095: if (!new File(srcFile).exists()) {
096: fail("Put didn't happen. Src VfsURI: "
097: + vfsURI.toString() + ".\nExpected file: "
098: + destFile);
099: }
100: } catch (IOException e) {
101: fail("Caught unexpected IOException on Vfs URI: "
102: + vfsURI.toString() + "\n"
103: + e.getLocalizedMessage());
104: }
105: }
106: }
107:
108: /**
109: * Validate that we can overwrite an existing file
110: *
111: * @throws Exception
112: */
113: public void testPutOverwriteTrue() throws Exception {
114: String testResource = VfsTestHelper.TEST_IVY_XML;
115: String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR,
116: testResource);
117: String destResource = VfsTestHelper.SCRATCH_DIR + "/"
118: + testResource;
119: File destFile = new File(FileUtil.concat(
120: VfsTestHelper.TEST_REPO_DIR, destResource));
121:
122: Iterator vfsURIs = helper.createVFSUriSet(destResource)
123: .iterator();
124: while (vfsURIs.hasNext()) {
125: VfsURI vfsURI = (VfsURI) vfsURIs.next();
126:
127: // remove existing scratch dir and populate it with an empty file
128: // that we can overwrite. We do this so that we can test file sizes.
129: // seeded file has length 0, while put file will have a length > 0
130: if (scratchDir.exists()) {
131: FileUtil.forceDelete(scratchDir);
132: }
133: destFile.getParentFile().mkdirs();
134: destFile.createNewFile();
135:
136: try {
137: repo.put(new File(srcFile), vfsURI.toString(), true);
138: if (!new File(srcFile).exists()) {
139: fail("Put didn't happen. Src VfsURI: "
140: + vfsURI.toString() + ".\nExpected file: "
141: + destFile);
142: }
143: if (destFile.length() == 0) {
144: fail("Zero file size indicates file not overwritten");
145: }
146: } catch (IOException e) {
147: fail("Caught unexpected IOException on Vfs URI: "
148: + vfsURI.toString() + "\n"
149: + e.getLocalizedMessage());
150: }
151: }
152: }
153:
154: /**
155: * Validate that we put will respect a request not to overwrite an existing file
156: *
157: * @throws Exception
158: */
159: public void testPutOverwriteFalse() throws Exception {
160: String testResource = VfsTestHelper.TEST_IVY_XML;
161: String srcFile = FileUtil.concat(VfsTestHelper.TEST_REPO_DIR,
162: testResource);
163: String destResource = VfsTestHelper.SCRATCH_DIR + "/"
164: + testResource;
165: File destFile = new File(FileUtil.concat(
166: VfsTestHelper.TEST_REPO_DIR, destResource));
167: destFile.getParentFile().mkdirs();
168: destFile.createNewFile();
169:
170: Iterator vfsURIs = helper.createVFSUriSet(destResource)
171: .iterator();
172: while (vfsURIs.hasNext()) {
173: VfsURI vfsURI = (VfsURI) vfsURIs.next();
174:
175: try {
176: repo.put(new File(srcFile), vfsURI.toString(), false);
177: fail("Did not throw expected IOException from attempted overwrite of existing file");
178: } catch (IOException e) {
179: }
180: }
181: }
182:
183: /**
184: * Test the retrieval of an artifact from the repository creating a new artifact
185: *
186: * @throws Exception
187: */
188: public void testGetNoExisting() throws Exception {
189: String testResource = VfsTestHelper.TEST_IVY_XML;
190: String testFile = FileUtil.concat(scratchDir.getAbsolutePath(),
191: testResource);
192:
193: Iterator vfsURIs = helper.createVFSUriSet(testResource)
194: .iterator();
195: while (vfsURIs.hasNext()) {
196: VfsURI vfsURI = (VfsURI) vfsURIs.next();
197: if (scratchDir.exists()) {
198: FileUtil.forceDelete(scratchDir);
199: }
200:
201: try {
202: repo.get(vfsURI.toString(), new File(testFile));
203: if (!new File(testFile).exists()) {
204: fail("Expected file: " + testFile
205: + "not found. Failed vfsURI: "
206: + vfsURI.toString());
207: }
208: } catch (IOException e) {
209: fail("Caught unexpected IOException on Vfs URI: "
210: + vfsURI.toString() + "\n"
211: + e.getLocalizedMessage());
212: }
213: }
214: }
215:
216: /**
217: * Test the retrieval of an artifact from the repository overwriting an existing artifact
218: *
219: * @throws Exception
220: */
221: public void testGetOverwriteExisting() throws Exception {
222: String testResource = VfsTestHelper.TEST_IVY_XML;
223: File testFile = new File(FileUtil.concat(scratchDir
224: .getAbsolutePath(), testResource));
225:
226: Iterator vfsURIs = helper.createVFSUriSet(testResource)
227: .iterator();
228: while (vfsURIs.hasNext()) {
229: VfsURI vfsURI = (VfsURI) vfsURIs.next();
230:
231: // setup - remove existing scratch area and populate with a file to override
232: if (scratchDir.exists()) {
233: FileUtil.forceDelete(scratchDir);
234: }
235: testFile.getParentFile().mkdirs();
236: testFile.createNewFile();
237:
238: try {
239: repo.get(vfsURI.toString(), testFile);
240: if (!testFile.exists()) {
241: fail("Expected file: " + testFile
242: + "not found. Failed vfsURI: "
243: + vfsURI.toString());
244: }
245: if (testFile.length() == 0) {
246: fail("Zero file size indicates file not overwritten");
247: }
248: } catch (IOException e) {
249: fail("Caught unexpected IOException on Vfs URI: "
250: + vfsURI.toString() + "\n"
251: + e.getLocalizedMessage());
252: }
253: }
254: }
255:
256: /**
257: * Validate that we get a non null Resource instance when passed a well-formed VfsURI pointing
258: * to an existing file
259: */
260: public void testGetResourceValidExist() throws Exception {
261: String testResource = VfsTestHelper.TEST_IVY_XML;
262:
263: Iterator vfsURIs = helper.createVFSUriSet(testResource)
264: .iterator();
265:
266: while (vfsURIs.hasNext()) {
267: VfsURI vfsURI = (VfsURI) vfsURIs.next();
268: try {
269: assertNotNull(repo.getResource(vfsURI.toString()));
270: } catch (IOException e) {
271: fail("Unexpected IOError on fetch of valid resource");
272: e.printStackTrace();
273: }
274: }
275: }
276:
277: /**
278: * Validate that we get a non null Resource instance when passed a well-formed VfsURI pointing
279: * to a non-existent file.
280: */
281: public void testGetResourceValidNoExist() throws Exception {
282: String testResource = VfsTestHelper.SCRATCH_DIR
283: + "/nosuchfile.jar";
284:
285: Iterator vfsURIs = helper.createVFSUriSet(testResource)
286: .iterator();
287: while (vfsURIs.hasNext()) {
288: VfsURI vfsURI = (VfsURI) vfsURIs.next();
289:
290: // make sure the declared resource does not exist
291: if (scratchDir.exists()) {
292: FileUtil.forceDelete(scratchDir);
293: }
294: try {
295: assertNotNull(repo.getResource(vfsURI.toString()));
296: } catch (IOException e) {
297: // this should not happen
298: fail("Unexpected IOException");
299: }
300: }
301: }
302:
303: }
|