Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b042e033b4161ef9ce1abcfd85ecba5f400f524 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Copyright (c) 2008, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *		compeople AG (Stefan Liebig) - Test fix for bug 121201 - Poor performance behind proxy/firewall
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.core;

import java.io.File;
import java.net.*;
import org.eclipse.equinox.internal.p2.core.helpers.URLUtil;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

/**
 * Tests for the {@link URLUtil} class.
 */
public class URLUtilTest extends AbstractProvisioningTest {
	private static final String[] testPaths = new String[] {"abc", "with spaces", "with%percent"};

	/**
	 * Tests for {@link URLUtil#toFile(URL)}.
	 */
	public void testToFile() {
		File base = new File(System.getProperty("java.io.tmpdir"));
		for (int i = 0; i < testPaths.length; i++) {
			File original = new File(base, testPaths[i]);
			URI uri = original.toURI();
			try {
				URL encoded = uri.toURL();
				File result = URLUtil.toFile(encoded);
				assertEquals("1." + i, original, result);
			} catch (MalformedURLException e) {
				fail("1.99", e);
			}
		}
	}

	/**
	 * Tests for {@link URLUtil#toFile(URL)}.
	 */
	public void testToFileRelative() throws URISyntaxException {
		for (int i = 0; i < testPaths.length; i++) {
			File original = new File(testPaths[i]);
			URI uri = new URI(null, testPaths[i], null);
			try {
				URL encoded = new URL("file:" + uri.getRawPath());
				File result = URLUtil.toFile(encoded);
				assertEquals("1." + i, original, result);
			} catch (MalformedURLException e) {
				fail("1.99", e);
			}
		}
	}

	/**
	 * Tests for {@link URLUtil#toURI(URL)}.
	 */
	public void testToFileFromLocalURL() throws Exception {
		File original = new File(System.getProperty("java.io.tmpdir"), "repo");
		//this URL is technically not correct because it is not hierarchical, but ensure URLUtil is lenient.
		URL url = new URL("file:" + original.toString());
		File result = URLUtil.toFile(url);
		assertEquals("1.0", original, result);
	}

	public void testToFileFromUNC() throws Exception {
		File original = new File("//a/b c");
		// this tests the two slash UNC path that URL creates
		URL url = new URL("file:" + original.toString());
		File result = URLUtil.toFile(url);
		assertEquals("1.0", original, result);

		// this tests the four slash UNC path that URI creates
		url = original.toURI().toURL();
		result = URLUtil.toFile(url);
		assertEquals("1.1", original, result);
	}
}

Back to the top