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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
/*
* Copyright (C) 2012 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.reflect;
import static com.google.common.base.Charsets.US_ASCII;
import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closer;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.common.reflect.ClassPath.ResourceInfo;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import junit.framework.TestCase;
import org.junit.Test;
/** Functional tests of {@link ClassPath}. */
public class ClassPathTest extends TestCase {
private static final Logger log = Logger.getLogger(ClassPathTest.class.getName());
private static final File FILE = new File(".");
public void testEquals() {
new EqualsTester()
.addEqualityGroup(classInfo(ClassPathTest.class), classInfo(ClassPathTest.class))
.addEqualityGroup(classInfo(Test.class), classInfo(Test.class, getClass().getClassLoader()))
.addEqualityGroup(
new ResourceInfo(FILE, "a/b/c.txt", getClass().getClassLoader()),
new ResourceInfo(FILE, "a/b/c.txt", getClass().getClassLoader()))
.addEqualityGroup(new ResourceInfo(FILE, "x.txt", getClass().getClassLoader()))
.testEquals();
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_emptyURLClassLoader_noParent() {
assertThat(ClassPath.getClassPathEntries(new URLClassLoader(new URL[0], null)).keySet())
.isEmpty();
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_URLClassLoader_noParent() throws Exception {
URL url1 = new URL("file:/a");
URL url2 = new URL("file:/b");
URLClassLoader classloader = new URLClassLoader(new URL[] {url1, url2}, null);
assertThat(ClassPath.getClassPathEntries(classloader))
.containsExactly(new File("/a"), classloader, new File("/b"), classloader);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_URLClassLoader_withParent() throws Exception {
URL url1 = new URL("file:/a");
URL url2 = new URL("file:/b");
URLClassLoader parent = new URLClassLoader(new URL[] {url1}, null);
URLClassLoader child = new URLClassLoader(new URL[] {url2}, parent) {};
assertThat(ClassPath.getClassPathEntries(child))
.containsExactly(new File("/a"), parent, new File("/b"), child)
.inOrder();
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_duplicateUri_parentWins() throws Exception {
URL url = new URL("file:/a");
URLClassLoader parent = new URLClassLoader(new URL[] {url}, null);
URLClassLoader child = new URLClassLoader(new URL[] {url}, parent) {};
assertThat(ClassPath.getClassPathEntries(child)).containsExactly(new File("/a"), parent);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_notURLClassLoader_noParent() {
assertThat(ClassPath.getClassPathEntries(new ClassLoader(null) {})).isEmpty();
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_notURLClassLoader_withParent() throws Exception {
URL url = new URL("file:/a");
URLClassLoader parent = new URLClassLoader(new URL[] {url}, null);
assertThat(ClassPath.getClassPathEntries(new ClassLoader(parent) {}))
.containsExactly(new File("/a"), parent);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_notURLClassLoader_withParentAndGrandParent() throws Exception {
URL url1 = new URL("file:/a");
URL url2 = new URL("file:/b");
URLClassLoader grandParent = new URLClassLoader(new URL[] {url1}, null);
URLClassLoader parent = new URLClassLoader(new URL[] {url2}, grandParent);
assertThat(ClassPath.getClassPathEntries(new ClassLoader(parent) {}))
.containsExactly(new File("/a"), grandParent, new File("/b"), parent);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
public void testClassPathEntries_notURLClassLoader_withGrandParent() throws Exception {
URL url = new URL("file:/a");
URLClassLoader grandParent = new URLClassLoader(new URL[] {url}, null);
ClassLoader parent = new ClassLoader(grandParent) {};
assertThat(ClassPath.getClassPathEntries(new ClassLoader(parent) {}))
.containsExactly(new File("/a"), grandParent);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
// https://github.com/google/guava/issues/2152
public void testClassPathEntries_URLClassLoader_pathWithSpace() throws Exception {
URL url = new URL("file:///c:/Documents and Settings/");
URLClassLoader classloader = new URLClassLoader(new URL[] {url}, null);
assertThat(ClassPath.getClassPathEntries(classloader))
.containsExactly(new File("/c:/Documents and Settings/"), classloader);
}
@AndroidIncompatible // Android forbids null parent ClassLoader
// https://github.com/google/guava/issues/2152
public void testClassPathEntries_URLClassLoader_pathWithEscapedSpace() throws Exception {
URL url = new URL("file:///c:/Documents%20and%20Settings/");
URLClassLoader classloader = new URLClassLoader(new URL[] {url}, null);
assertThat(ClassPath.getClassPathEntries(classloader))
.containsExactly(new File("/c:/Documents and Settings/"), classloader);
}
// https://github.com/google/guava/issues/2152
public void testToFile() throws Exception {
assertThat(ClassPath.toFile(new URL("file:///c:/Documents%20and%20Settings/")))
.isEqualTo(new File("/c:/Documents and Settings/"));
assertThat(ClassPath.toFile(new URL("file:///c:/Documents ~ Settings, or not/11-12 12:05")))
.isEqualTo(new File("/c:/Documents ~ Settings, or not/11-12 12:05"));
}
// https://github.com/google/guava/issues/2152
@AndroidIncompatible // works in newer Android versions but fails at the version we test with
public void testToFile_AndroidIncompatible() throws Exception {
assertThat(ClassPath.toFile(new URL("file:///c:\\Documents ~ Settings, or not\\11-12 12:05")))
.isEqualTo(new File("/c:\\Documents ~ Settings, or not\\11-12 12:05"));
assertThat(ClassPath.toFile(new URL("file:///C:\\Program Files\\Apache Software Foundation")))
.isEqualTo(new File("/C:\\Program Files\\Apache Software Foundation/"));
assertThat(ClassPath.toFile(new URL("file:///C:\\\u20320 \u22909"))) // Chinese Ni Hao
.isEqualTo(new File("/C:\\\u20320 \u22909"));
}
@AndroidIncompatible // Android forbids null parent ClassLoader
// https://github.com/google/guava/issues/2152
public void testJarFileWithSpaces() throws Exception {
URL url = makeJarUrlWithName("To test unescaped spaces in jar file name.jar");
URLClassLoader classloader = new URLClassLoader(new URL[] {url}, null);
assertThat(ClassPath.from(classloader).getTopLevelClasses()).isNotEmpty();
}
public void testScan_classPathCycle() throws IOException {
File jarFile = File.createTempFile("with_circular_class_path", ".jar");
try {
writeSelfReferencingJarFile(jarFile, "test.txt");
assertThat(
new ClassPath.LocationInfo(jarFile, ClassPathTest.class.getClassLoader())
.scanResources())
.hasSize(1);
} finally {
jarFile.delete();
}
}
public void testScanFromFile_fileNotExists() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
assertThat(
new ClassPath.LocationInfo(new File("no/such/file/anywhere"), classLoader)
.scanResources())
.isEmpty();
}
public void testScanFromFile_notJarFile() throws IOException {
ClassLoader classLoader = ClassPathTest.class.getClassLoader();
File notJar = File.createTempFile("not_a_jar", "txt");
try {
assertThat(new ClassPath.LocationInfo(notJar, classLoader).scanResources()).isEmpty();
} finally {
notJar.delete();
}
}
public void testGetClassPathEntry() throws MalformedURLException, URISyntaxException {
assertEquals(
new File("/usr/test/dep.jar").toURI(),
ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "file:/usr/test/dep.jar")
.toURI());
assertEquals(
new File("/home/build/a.jar").toURI(),
ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "a.jar").toURI());
assertEquals(
new File("/home/build/x/y/z").toURI(),
ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z").toURI());
assertEquals(
new File("/home/build/x/y/z.jar").toURI(),
ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "x/y/z.jar").toURI());
assertEquals(
"/home/build/x y.jar",
ClassPath.getClassPathEntry(new File("/home/build/outer.jar"), "x y.jar").getFile());
}
public void testGetClassPathFromManifest_nullManifest() {
assertThat(ClassPath.getClassPathFromManifest(new File("some.jar"), null)).isEmpty();
}
public void testGetClassPathFromManifest_noClassPath() throws IOException {
File jarFile = new File("base.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest(""))).isEmpty();
}
public void testGetClassPathFromManifest_emptyClassPath() throws IOException {
File jarFile = new File("base.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifestClasspath(""))).isEmpty();
}
public void testGetClassPathFromManifest_badClassPath() throws IOException {
File jarFile = new File("base.jar");
Manifest manifest = manifestClasspath("nosuchscheme:an_invalid^path");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest)).isEmpty();
}
public void testGetClassPathFromManifest_pathWithStrangeCharacter() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:the^file.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/the^file.jar"));
}
public void testGetClassPathFromManifest_relativeDirectory() throws IOException {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("with/relative/dir");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/with/relative/dir"));
}
public void testGetClassPathFromManifest_relativeJar() throws IOException {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("with/relative.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/with/relative.jar"));
}
public void testGetClassPathFromManifest_jarInCurrentDirectory() throws IOException {
File jarFile = new File("base/some.jar");
// with/relative/directory is the Class-Path value in the mf file.
Manifest manifest = manifestClasspath("current.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/current.jar"));
}
public void testGetClassPathFromManifest_absoluteDirectory() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute/dir");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("/with/absolute/dir"));
}
public void testGetClassPathFromManifest_absoluteJar() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("/with/absolute.jar"));
}
public void testGetClassPathFromManifest_multiplePaths() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("file:/with/absolute.jar relative.jar relative/dir");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(
fullpath("/with/absolute.jar"),
fullpath("base/relative.jar"),
fullpath("base/relative/dir"))
.inOrder();
}
public void testGetClassPathFromManifest_leadingBlanks() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath(" relative.jar");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/relative.jar"));
}
public void testGetClassPathFromManifest_trailingBlanks() throws IOException {
File jarFile = new File("base/some.jar");
Manifest manifest = manifestClasspath("relative.jar ");
assertThat(ClassPath.getClassPathFromManifest(jarFile, manifest))
.containsExactly(fullpath("base/relative.jar"));
}
public void testGetClassName() {
assertEquals("abc.d.Abc", ClassPath.getClassName("abc/d/Abc.class"));
}
public void testResourceInfo_of() {
assertEquals(ClassInfo.class, resourceInfo(ClassPathTest.class).getClass());
assertEquals(ClassInfo.class, resourceInfo(ClassPath.class).getClass());
assertEquals(ClassInfo.class, resourceInfo(Nested.class).getClass());
}
public void testGetSimpleName() {
ClassLoader classLoader = getClass().getClassLoader();
assertEquals("Foo", new ClassInfo(FILE, "Foo.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo(FILE, "a/b/Foo.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo(FILE, "a/b/Bar$Foo.class", classLoader).getSimpleName());
assertEquals("", new ClassInfo(FILE, "a/b/Bar$1.class", classLoader).getSimpleName());
assertEquals("Foo", new ClassInfo(FILE, "a/b/Bar$Foo.class", classLoader).getSimpleName());
assertEquals("", new ClassInfo(FILE, "a/b/Bar$1.class", classLoader).getSimpleName());
assertEquals("Local", new ClassInfo(FILE, "a/b/Bar$1Local.class", classLoader).getSimpleName());
}
public void testGetPackageName() {
assertEquals(
"", new ClassInfo(FILE, "Foo.class", getClass().getClassLoader()).getPackageName());
assertEquals(
"a.b", new ClassInfo(FILE, "a/b/Foo.class", getClass().getClassLoader()).getPackageName());
}
// Test that ResourceInfo.urls() returns identical content to ClassLoader.getResources()
public void testGetClassPathUrls() throws Exception {
String oldPathSeparator = PATH_SEPARATOR.value();
String oldClassPath = JAVA_CLASS_PATH.value();
System.setProperty(PATH_SEPARATOR.key(), ":");
System.setProperty(
JAVA_CLASS_PATH.key(),
Joiner.on(":")
.join(
"relative/path/to/some.jar",
"/absolute/path/to/some.jar",
"relative/path/to/class/root",
"/absolute/path/to/class/root"));
try {
ImmutableList<URL> urls = ClassPath.parseJavaClassPath();
assertThat(urls.get(0).getProtocol()).isEqualTo("file");
assertThat(urls.get(0).getAuthority()).isNull();
assertThat(urls.get(0).getPath()).endsWith("/relative/path/to/some.jar");
assertThat(urls.get(1)).isEqualTo(new URL("file:///absolute/path/to/some.jar"));
assertThat(urls.get(2).getProtocol()).isEqualTo("file");
assertThat(urls.get(2).getAuthority()).isNull();
assertThat(urls.get(2).getPath()).endsWith("/relative/path/to/class/root");
assertThat(urls.get(3)).isEqualTo(new URL("file:///absolute/path/to/class/root"));
assertThat(urls).hasSize(4);
} finally {
System.setProperty(PATH_SEPARATOR.key(), oldPathSeparator);
System.setProperty(JAVA_CLASS_PATH.key(), oldClassPath);
}
}
private static boolean contentEquals(URL left, URL right) throws IOException {
return Resources.asByteSource(left).contentEquals(Resources.asByteSource(right));
}
private static class Nested {}
public void testNulls() throws IOException {
new NullPointerTester().testAllPublicStaticMethods(ClassPath.class);
new NullPointerTester()
.testAllPublicInstanceMethods(ClassPath.from(getClass().getClassLoader()));
}
public void testLocationsFrom_idempotentScan() throws IOException {
ImmutableSet<ClassPath.LocationInfo> locations =
ClassPath.locationsFrom(getClass().getClassLoader());
assertThat(locations).isNotEmpty();
for (ClassPath.LocationInfo location : locations) {
ImmutableSet<ResourceInfo> resources = location.scanResources();
assertThat(location.scanResources()).containsExactlyElementsIn(resources);
}
}
public void testLocationsFrom_idempotentLocations() {
ImmutableSet<ClassPath.LocationInfo> locations =
ClassPath.locationsFrom(getClass().getClassLoader());
assertThat(ClassPath.locationsFrom(getClass().getClassLoader()))
.containsExactlyElementsIn(locations);
}
public void testLocationEquals() {
ClassLoader child = getClass().getClassLoader();
ClassLoader parent = child.getParent();
new EqualsTester()
.addEqualityGroup(
new ClassPath.LocationInfo(new File("foo.jar"), child),
new ClassPath.LocationInfo(new File("foo.jar"), child))
.addEqualityGroup(new ClassPath.LocationInfo(new File("foo.jar"), parent))
.addEqualityGroup(new ClassPath.LocationInfo(new File("foo"), child))
.testEquals();
}
public void testScanAllResources() throws IOException {
assertThat(scanResourceNames(ClassLoader.getSystemClassLoader()))
.contains("com/google/common/reflect/ClassPathTest.class");
}
public void testExistsThrowsSecurityException() throws IOException, URISyntaxException {
SecurityManager oldSecurityManager = System.getSecurityManager();
try {
doTestExistsThrowsSecurityException();
} finally {
System.setSecurityManager(oldSecurityManager);
}
}
private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
File file = null;
// In Java 9, Logger may read the TZ database. Only disallow reading the class path URLs.
final PermissionCollection readClassPathFiles =
new FilePermission("", "read").newPermissionCollection();
for (URL url : ClassPath.parseJavaClassPath()) {
if (url.getProtocol().equalsIgnoreCase("file")) {
file = new File(url.toURI());
readClassPathFiles.add(new FilePermission(file.getAbsolutePath(), "read"));
}
}
assertThat(file).isNotNull();
SecurityManager disallowFilesSecurityManager =
new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (readClassPathFiles.implies(p)) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
file.exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(getClass().getClassLoader());
// ClassPath may contain resources from the boot class loader; just not from the class path.
for (ResourceInfo resource : classPath.getResources()) {
assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/");
}
}
private static ClassPath.ClassInfo findClass(
Iterable<ClassPath.ClassInfo> classes, Class<?> cls) {
for (ClassPath.ClassInfo classInfo : classes) {
if (classInfo.getName().equals(cls.getName())) {
return classInfo;
}
}
throw new AssertionError("failed to find " + cls);
}
private static ResourceInfo resourceInfo(Class<?> cls) {
String resource = cls.getName().replace('.', '/') + ".class";
ClassLoader loader = cls.getClassLoader();
return ResourceInfo.of(FILE, resource, loader);
}
private static ClassInfo classInfo(Class<?> cls) {
return classInfo(cls, cls.getClassLoader());
}
private static ClassInfo classInfo(Class<?> cls, ClassLoader classLoader) {
String resource = cls.getName().replace('.', '/') + ".class";
return new ClassInfo(FILE, resource, classLoader);
}
private static Manifest manifestClasspath(String classpath) throws IOException {
return manifest("Class-Path: " + classpath + "\n");
}
private static void writeSelfReferencingJarFile(File jarFile, String... entries)
throws IOException {
Manifest manifest = new Manifest();
// Without version, the manifest is silently ignored. Ugh!
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName());
Closer closer = Closer.create();
try {
FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut));
for (String entry : entries) {
jarOut.putNextEntry(new ZipEntry(entry));
Resources.copy(ClassPathTest.class.getResource(entry), jarOut);
jarOut.closeEntry();
}
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
private static Manifest manifest(String content) throws IOException {
InputStream in = new ByteArrayInputStream(content.getBytes(US_ASCII));
Manifest manifest = new Manifest();
manifest.read(in);
return manifest;
}
private static File fullpath(String path) {
return new File(new File(path).toURI());
}
private static URL makeJarUrlWithName(String name) throws IOException {
File fullPath = new File(Files.createTempDir(), name);
File jarFile = pickAnyJarFile();
Files.copy(jarFile, fullPath);
return fullPath.toURI().toURL();
}
private static File pickAnyJarFile() throws IOException {
for (ClassPath.LocationInfo location :
ClassPath.locationsFrom(ClassPathTest.class.getClassLoader())) {
if (!location.file().isDirectory() && location.file().exists()) {
return location.file();
}
}
throw new AssertionError("Failed to find a jar file");
}
private static ImmutableSet<String> scanResourceNames(ClassLoader loader) throws IOException {
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
for (ClassPath.LocationInfo location : ClassPath.locationsFrom(loader)) {
for (ResourceInfo resource : location.scanResources()) {
builder.add(resource.getResourceName());
}
}
return builder.build();
}
}
|