1 /******************************************************************************* 2 * Copyright (c) 2000, 2017 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 * Alexander Kurtakov <akurtako@redhat.com> - Bug 459343 14 *******************************************************************************/ 15 package org.eclipse.core.tests.resources; 16 17 import java.io.IOException; 18 import java.io.InputStream; 19 import java.net.MalformedURLException; 20 import java.net.URL; 21 import org.eclipse.core.internal.resources.PlatformURLResourceConnection; 22 import org.eclipse.core.resources.*; 23 import org.eclipse.core.runtime.*; 24 25 /** 26 * Test suites for {@link org.eclipse.core.internal.resources.PlatformURLResourceConnection} 27 */ 28 public class ResourceURLTest extends ResourceTest { 29 private static final String CONTENT = "content"; 30 protected static IPath[] interestingPaths; 31 protected static IResource[] interestingResources; 32 static boolean noSideEffects = false; 33 34 /** 35 * Need a zero argument constructor to satisfy the test harness. 36 * This constructor should not do any real work nor should it be 37 * called by user code. 38 */ ResourceURLTest()39 public ResourceURLTest() { 40 super(); 41 } 42 ResourceURLTest(String name)43 public ResourceURLTest(String name) { 44 super(name); 45 } 46 checkURL(IResource resource)47 private void checkURL(IResource resource) throws Throwable { 48 URL url = getURL(resource); 49 IPath file = new Path(FileLocator.resolve(url).getFile()); 50 IPath metric = resource.getLocation(); 51 assertEquals(metric, file); 52 } 53 54 /** 55 * Returns a collection of string paths describing the standard 56 * resource hierarchy for this test. In the string forms, folders are 57 * represented as having trailing separators ('/'). All other resources 58 * are files. It is generally assumed that this hierarchy will be 59 * inserted under some solution and project structure. 60 */ 61 @Override defineHierarchy()62 public String[] defineHierarchy() { 63 return new String[] {"/", "/1/", "/1/1", "/1/2", "/1/3", "/2/", "/2/1", "/2/2", "/2/3", "/3/", "/3/1", "/3/2", "/3/3", "/4/", "/5"}; 64 } 65 doCleanup()66 public void doCleanup() throws CoreException { 67 getWorkspace().getRoot().delete(true, true, null); 68 } 69 getTestProject()70 protected IProject getTestProject() { 71 return getWorkspace().getRoot().getProject("testProject"); 72 } 73 getTestProject2()74 protected IProject getTestProject2() { 75 return getWorkspace().getRoot().getProject("testProject2"); 76 } 77 getURL(IPath path)78 private URL getURL(IPath path) throws Throwable { 79 return new URL("platform:/resource" + path.makeAbsolute().toString()); 80 } 81 getURL(IResource resource)82 private URL getURL(IResource resource) throws Throwable { 83 return getURL(resource.getFullPath()); 84 } 85 86 @Override tearDown()87 protected void tearDown() throws Exception { 88 // overwrite the superclass and do nothing since our test methods build on each other 89 } 90 testBasicURLs()91 public void testBasicURLs() throws Throwable { 92 IResource[] resources = buildResources(); 93 ensureExistsInWorkspace(resources, true); 94 for (IResource resource : resources) { 95 checkURL(resource); 96 } 97 } 98 testExternalURLs()99 public void testExternalURLs() throws Throwable { 100 IProject project = getWorkspace().getRoot().getProject("test"); 101 IProjectDescription desc = getWorkspace().newProjectDescription("test"); 102 desc.setLocation(Platform.getLocation().append("../testproject")); 103 project.create(desc, null); 104 project.open(null); 105 IResource[] resources = buildResources(project, defineHierarchy()); 106 ensureExistsInWorkspace(resources, true); 107 for (IResource resource : resources) { 108 checkURL(resource); 109 } 110 } 111 testNonExistantURLs()112 public void testNonExistantURLs() throws Throwable { 113 IResource[] resources = buildResources(); 114 for (int i = 1; i < resources.length; i++) { 115 try { 116 checkURL(resources[i]); 117 fail("1.0"); 118 } catch (IOException e) { 119 // expected 120 } 121 } 122 } 123 124 /** 125 * Tests decoding of normalized URLs containing spaces 126 */ testSpaces()127 public void testSpaces() { 128 IProject project = getWorkspace().getRoot().getProject("My Project"); 129 IFile file = project.getFile("a.txt"); 130 ensureExistsInWorkspace(file, CONTENT); 131 try { 132 URL url = new URL(PlatformURLResourceConnection.RESOURCE_URL_STRING + "My%20Project/a.txt"); 133 InputStream stream = url.openStream(); 134 assertTrue("1.0", compareContent(stream, getContents(CONTENT))); 135 } catch (MalformedURLException e) { 136 fail("0.99", e); 137 } catch (IOException e) { 138 fail("1.99", e); 139 } 140 141 } 142 } 143