1 /******************************************************************************* 2 * Copyright (c) 2007, 2012 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.core.tests.filesystem; 15 16 import java.net.URI; 17 import org.eclipse.core.filesystem.EFS; 18 import org.eclipse.core.filesystem.URIUtil; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileSystem; 21 22 /** 23 * Tests API methods of the class {@link org.eclipse.core.filesystem.URIUtil}. 24 */ 25 public class URIUtilTest extends FileSystemTest { 26 /** 27 * Tests API method {@link org.eclipse.core.filesystem.URIUtil#equals(java.net.URI, java.net.URI)}. 28 */ testEquals()29 public void testEquals() { 30 if (EFS.getLocalFileSystem().isCaseSensitive()) { 31 //test that case variants are not equal 32 URI one = new java.io.File("c:\\temp\\test").toURI(); 33 URI two = new java.io.File("c:\\TEMP\\test").toURI(); 34 assertTrue("1.0", !URIUtil.equals(one, two)); 35 } else { 36 //test that case variants are equal 37 URI one = new java.io.File("c:\\temp\\test").toURI(); 38 URI two = new java.io.File("c:\\TEMP\\test").toURI(); 39 assertTrue("1.0", URIUtil.equals(one, two)); 40 } 41 42 } 43 44 /** 45 * Tests API method {@link org.eclipse.core.filesystem.URIUtil#toURI(org.eclipse.core.runtime.IPath)}. 46 */ testPathToURI()47 public void testPathToURI() { 48 if (Platform.getOS().equals(Platform.OS_WIN32)) { 49 //path with spaces 50 assertEquals("1.0", "/c:/temp/with spaces", URIUtil.toURI("c:\\temp\\with spaces").getSchemeSpecificPart()); 51 } else { 52 //path with spaces 53 assertEquals("2.0", "/tmp/with spaces", URIUtil.toURI("/tmp/with spaces").getSchemeSpecificPart()); 54 } 55 } 56 57 /** 58 * Tests API method {@link org.eclipse.core.filesystem.URIUtil#toURI(String)}. 59 */ testStringToURI()60 public void testStringToURI() { 61 if (Platform.getOS().equals(Platform.OS_WIN32)) { 62 assertEquals("1.0", "/c:/temp/with spaces", URIUtil.toURI(new Path("c:\\temp\\with spaces")).getSchemeSpecificPart()); 63 } else { 64 assertEquals("1.0", "/tmp/with spaces", URIUtil.toURI(new Path("/tmp/with spaces")).getSchemeSpecificPart()); 65 } 66 } 67 68 /** 69 * Tests API method {@link org.eclipse.core.filesystem.URIUtil#toPath(java.net.URI)}. 70 */ testToPath()71 public void testToPath() throws Exception { 72 // Relative path 73 String pathString = "test/path with/spaces to_file.txt"; 74 assertEquals("1.0", new Path(pathString), URIUtil.toPath(URIUtil.toURI(pathString, false))); 75 // Absolute path 76 if (Platform.getOS().equals(Platform.OS_WIN32)) { 77 pathString = "c:/test/path with/spaces to_file.txt"; 78 } else { 79 pathString = "/test/path with/spaces to_file.txt"; 80 } 81 assertEquals("2.0", new Path(pathString), URIUtil.toPath(URIUtil.toURI(pathString))); 82 // User defined file system 83 assertEquals("3.0", new Path(pathString), URIUtil.toPath(WrapperFileSystem.getWrappedURI(URIUtil.toURI(pathString)))); 84 } 85 86 /** 87 * Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(IPath)}, 88 * {@link org.eclipse.core.filesystem.URIUtil#toURI(String)} results equality 89 */ testToURIAbsolute()90 public void testToURIAbsolute() { 91 String pathString = null; 92 if (Platform.getOS().equals(Platform.OS_WIN32)) { 93 pathString = "c:/test/path with/spaces to_file.txt"; 94 } else { 95 pathString = "/test/path with/spaces to_file.txt"; 96 } 97 IPath path = new Path(pathString); 98 URI uri01 = URIUtil.toURI(path); 99 URI uri02 = URIUtil.toURI(pathString); 100 assertEquals("1.0", uri01, uri02); 101 } 102 103 /** 104 * Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(IPath)}, 105 * {@link org.eclipse.core.filesystem.URIUtil#toURI(String)} results equality 106 */ testToURIRelative()107 public void testToURIRelative() { 108 String pathString = "test/path with/spaces to_file.txt"; 109 IPath path = new Path(pathString); 110 URI uri01 = URIUtil.toURI(path); 111 URI uri02 = URIUtil.toURI(pathString, false); 112 assertEquals("1.0", uri01, uri02); 113 assertTrue("1.1", !uri01.isAbsolute()); 114 assertTrue("1.2", !uri02.isAbsolute()); 115 } 116 117 /** 118 * Test API methods {@link org.eclipse.core.filesystem.URIUtil#toURI(org.eclipse.core.runtime.IPath)}. 119 * {@link org.eclipse.core.filesystem.URIUtil#toPath(URI)} transformation with relative and absolute paths 120 */ testFromPathToURI()121 public void testFromPathToURI() { 122 //absolute path 123 IPath aPath = null; 124 if (Platform.getOS().equals(Platform.OS_WIN32)) { 125 aPath = new Path("c:/test/path with spaces/to_file.txt"); 126 } else { 127 aPath = new Path("/test/path with spaces/to_file.txt"); 128 } 129 //relative path 130 IPath rPath = new Path("relative/with spaces/path/to_file.txt"); 131 132 URI aUri = URIUtil.toURI(aPath); 133 URI rUri = URIUtil.toURI(rPath); 134 135 assertEquals("1.0", aPath.toString(), URIUtil.toPath(aUri).toString()); 136 assertEquals("2.0", rPath.toString(), URIUtil.toPath(rUri).toString()); 137 } 138 testBug291323_doubleDotLocationPath()139 public void testBug291323_doubleDotLocationPath() { 140 URI aUri = URIUtil.toURI(".."); 141 URI bUri = URIUtil.toURI(""); 142 assertEquals("1.0", URIUtil.toPath(bUri).toString(), URIUtil.toPath(aUri).toString()); 143 } 144 } 145