PackageLocator { static File getClasspathFromUrl(final URL resourceUrl, final String canonicalName) { if (resourceUrl == null) { throw new IllegalArgumentException("resourceUrl must not be null"); } if (canonicalName == null) { throw new IllegalArgumentException("canonicalName must not be null"); } String resourcePath; try { final URI resourceUri = resourceUrl.toURI(); final URI schemeStrippedUri = stripSchemes(resourceUri); resourcePath = schemeStrippedUri.getPath(); } catch (final URISyntaxException ignored) { resourcePath = resourceUrl.getPath(); } final String slashedName = PATTERN_DOT.matcher(canonicalName).replaceAll("/"); final String pathToClassFile = "/" + slashedName + ".class"; final int lastIndexOfClassName = resourcePath.lastIndexOf(pathToClassFile); final File result; if (lastIndexOfClassName > 0) { final int mark = resourcePath.charAt(lastIndexOfClassName - 1) == '!' ? lastIndexOfClassName - 1 : lastIndexOfClassName; final String resourcePathMinusSuffix = resourcePath.substring(0, mark); result = new File(resourcePathMinusSuffix); } else { result = new File(resourcePath); } return result; } PackageLocator();   PackageLocator(final ClassPropertyAccessor classPropertyAccessor); File locatePackage(final Class clazz);  }
@Test public void getClasspathFromUrl_doesNotContainClass() throws Exception { final URL url = new URL("file:/E:/a%20b/c%26d/"); final String canonicalName = "com.microsoft.alm.oauth2.useragent.utils.PackageLocatorTest"; final File actual = PackageLocator.getClasspathFromUrl(url, canonicalName); assertEquals("/E:/a b/c&d", actual); }
@Test public void getClasspathFromUrl_encodedCharactersInURL() throws Exception { final URL url = new URL("file:/E:/a%20b/c%26d/com/microsoft/alm/oauth2/useragent/utils/PackageLocatorTest.class"); final String canonicalName = "com.microsoft.alm.oauth2.useragent.utils.PackageLocatorTest"; final File actual = PackageLocator.getClasspathFromUrl(url, canonicalName); assertEquals("/E:/a b/c&d", actual); }
@Test public void getClasspathFromUrl_spaceAndPlusInURL() throws Exception { final URL url = new URL("file: final String canonicalName = "com.microsoft.alm.oauth2.useragent.utils.PackageLocatorTest"; final File actual = PackageLocator.getClasspathFromUrl(url, canonicalName); assertEquals("/c:/Documents and+Settings/User", actual); }