|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.conformance; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URISyntaxException; |
| 22 | +import java.net.URL; |
| 23 | +import java.nio.file.DirectoryStream; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Enumeration; |
| 29 | +import java.util.List; |
| 30 | +import java.util.jar.JarEntry; |
| 31 | +import java.util.jar.JarFile; |
| 32 | + |
| 33 | +public final class ConformanceTestLocator { |
| 34 | + |
| 35 | + private ConformanceTestLocator() {} |
| 36 | + |
| 37 | + /** |
| 38 | + * Given the provided {@link MatchPattern matchPattern} list resources on the classpath starting |
| 39 | + * from {@link MatchPattern#getBaseResourcePath() MatchPattern.getBaseResourcePath} and for each |
| 40 | + * resources resource test that it is a file and that matches according to {@link |
| 41 | + * MatchPattern#matches(String) MatchPattern.matches(String)}. |
| 42 | + * |
| 43 | + * <p>Resolution of resources paths will be against the current threads context class loader |
| 44 | + * ({@link Thread#currentThread()}.{@link Thread#getContextClassLoader() getContextClassLoader()} |
| 45 | + * |
| 46 | + * @param matchPattern The {@link MatchPattern} to match against |
| 47 | + * @return The list of all resources on the classpath that match the specified {@code |
| 48 | + * matchPattern} |
| 49 | + * @throws IOException If there is an error attempting to read a classpath resource (listing the |
| 50 | + * contents of a directory or jar). |
| 51 | + * @throws URISyntaxException If there is an error translating a classpath URL into a filesystem |
| 52 | + * URI. |
| 53 | + */ |
| 54 | + public static List<String> findAllResourcePaths(final MatchPattern matchPattern) |
| 55 | + throws IOException, URISyntaxException { |
| 56 | + return findAllResourcePaths(matchPattern, Thread.currentThread().getContextClassLoader()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Given the provided {@link MatchPattern matchPattern} list resources on the classpath starting |
| 61 | + * from {@link MatchPattern#getBaseResourcePath() MatchPattern.getBaseResourcePath} and for each |
| 62 | + * resources resource test that it is a file and that matches according to {@link |
| 63 | + * MatchPattern#matches(String) MatchPattern.matches(String)}. |
| 64 | + * |
| 65 | + * <p>Resolution of resources paths will be against the parameter {@code classLoader} |
| 66 | + * |
| 67 | + * @param matchPattern The {@link MatchPattern} to match against |
| 68 | + * @param classLoader The classLoader to scan for resources |
| 69 | + * @return The list of all resources on the classpath that match the specified {@code |
| 70 | + * matchPattern} |
| 71 | + * @throws IOException If there is an error attempting to read a classpath resource (listing the |
| 72 | + * contents of a directory or jar). |
| 73 | + * @throws URISyntaxException If there is an error translating a classpath URL into a filesystem |
| 74 | + * URI. |
| 75 | + */ |
| 76 | + public static List<String> findAllResourcePaths( |
| 77 | + final MatchPattern matchPattern, ClassLoader classLoader) |
| 78 | + throws IOException, URISyntaxException { |
| 79 | + final List<String> resourcePaths = new ArrayList<>(); |
| 80 | + final Enumeration<URL> pkgDir = classLoader.getResources(matchPattern.getBaseResourcePath()); |
| 81 | + while (pkgDir.hasMoreElements()) { |
| 82 | + URL url = pkgDir.nextElement(); |
| 83 | + if (url != null) { |
| 84 | + final String scheme = url.getProtocol(); |
| 85 | + switch (scheme) { |
| 86 | + case "file": |
| 87 | + final List<String> cf = handleFileScheme(url, matchPattern); |
| 88 | + resourcePaths.addAll(cf); |
| 89 | + break; |
| 90 | + case "jar": |
| 91 | + final List<String> cj = handleJarScheme(url, matchPattern); |
| 92 | + resourcePaths.addAll(cj); |
| 93 | + break; |
| 94 | + default: |
| 95 | + throw new IllegalStateException("Unable to scan scheme '" + scheme + "'"); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return resourcePaths; |
| 101 | + } |
| 102 | + |
| 103 | + private static List<String> handleFileScheme(final URL url, MatchPattern mp) |
| 104 | + throws IOException, URISyntaxException { |
| 105 | + final Path path = Paths.get(url.toURI()); |
| 106 | + return handleFileScheme(mp, path); |
| 107 | + } |
| 108 | + |
| 109 | + private static List<String> handleFileScheme(final MatchPattern mp, final Path path) |
| 110 | + throws IOException { |
| 111 | + final List<String> resourcePaths = new ArrayList<>(); |
| 112 | + try (final DirectoryStream<Path> paths = Files.newDirectoryStream(path)) { |
| 113 | + for (Path p : paths) { |
| 114 | + if (Files.isDirectory(p)) { |
| 115 | + resourcePaths.addAll(handleFileScheme(mp, p)); |
| 116 | + } else { |
| 117 | + final String filePath = normalizePath(p); |
| 118 | + final String resourcePath = |
| 119 | + filePath.substring(filePath.indexOf(mp.getBaseResourcePath())); |
| 120 | + if (mp.matches(resourcePath)) { |
| 121 | + resourcePaths.add(resourcePath); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return resourcePaths; |
| 127 | + } |
| 128 | + |
| 129 | + private static List<String> handleJarScheme(final URL url, MatchPattern mp) throws IOException { |
| 130 | + final String urlPath = url.getPath(); |
| 131 | + final String jarPath = urlPath.substring(5, urlPath.indexOf("!")); |
| 132 | + |
| 133 | + final List<String> resourcePaths = new ArrayList<>(); |
| 134 | + final JarFile jarFile = new JarFile(jarPath); |
| 135 | + final Enumeration<JarEntry> jarEntries = jarFile.entries(); |
| 136 | + while (jarEntries.hasMoreElements()) { |
| 137 | + JarEntry je = jarEntries.nextElement(); |
| 138 | + if (!je.isDirectory() && mp.matches(je.getName())) { |
| 139 | + resourcePaths.add(je.getName()); |
| 140 | + } |
| 141 | + } |
| 142 | + return resourcePaths; |
| 143 | + } |
| 144 | + |
| 145 | + private static String normalizePath(Path p) { |
| 146 | + final String s = p.normalize().toString(); |
| 147 | + if (File.separatorChar == '\\') { |
| 148 | + return s.replace('\\', '/'); |
| 149 | + } else { |
| 150 | + return s; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Factory method to create a relatively simple {@link MatchPattern}. |
| 156 | + * |
| 157 | + * @param baseResourcePath The non-null base path to start scanning for resources on the classpath |
| 158 | + * @param suffix The non-null suffix to match found elements against for inclusion |
| 159 | + * @return A new {@link MatchPattern} where classpath scanning will start from {@code |
| 160 | + * baseResourcePath} walking the classpath and matching to elements that end in {@code |
| 161 | + * suffix}. Suffix matching is a simple match (non-regex). |
| 162 | + */ |
| 163 | + public static MatchPattern newMatchPattern(final String baseResourcePath, final String suffix) { |
| 164 | + if (baseResourcePath == null) { |
| 165 | + throw new IllegalArgumentException("baseResourcePath must be non-null"); |
| 166 | + } |
| 167 | + if (suffix == null || suffix.isEmpty()) { |
| 168 | + throw new IllegalArgumentException("suffix must be non-null and non-empty"); |
| 169 | + } |
| 170 | + // when listing the resources from the classpath, a leading slash will result in resources |
| 171 | + // not being found. If the baseResourcePath passed in here has a leading slash, detect and |
| 172 | + // remove it. |
| 173 | + int begin = 0; |
| 174 | + if (baseResourcePath.startsWith("/")) { |
| 175 | + begin = 1; |
| 176 | + } |
| 177 | + return new SimpleMatchPattern(baseResourcePath.substring(begin), suffix); |
| 178 | + } |
| 179 | + |
| 180 | + public interface MatchPattern { |
| 181 | + String getBaseResourcePath(); |
| 182 | + |
| 183 | + boolean matches(String resourcePath); |
| 184 | + } |
| 185 | + |
| 186 | + private static final class SimpleMatchPattern implements MatchPattern { |
| 187 | + private final String baseResourcePath; |
| 188 | + private final String suffix; |
| 189 | + |
| 190 | + SimpleMatchPattern(final String baseResourcePath, final String suffix) { |
| 191 | + // parameter construction validation is performed in the factory method #newMatchPattern |
| 192 | + this.baseResourcePath = baseResourcePath; |
| 193 | + this.suffix = suffix; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public String getBaseResourcePath() { |
| 198 | + return baseResourcePath; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public boolean matches(final String resourcePath) { |
| 203 | + return resourcePath.startsWith(baseResourcePath) && resourcePath.endsWith(suffix); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments