Merge pull request #6759 from hengyunab

* gh-6759:
  Polish “Avoid null handler package in JarFile protocol handler registration”
  Avoid null handler package in JarFile protocol handler registration
pull/6855/head
Andy Wilkinson 8 years ago
commit 20df899b7a

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -449,7 +449,7 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
* {@link URLStreamHandler} will be located to deal with jar URLs.
*/
public static void registerUrlProtocolHandler() {
String handlers = System.getProperty(PROTOCOL_HANDLER);
String handlers = System.getProperty(PROTOCOL_HANDLER, "");
System.setProperty(PROTOCOL_HANDLER, ("".equals(handlers) ? HANDLERS_PACKAGE
: handlers + "|" + HANDLERS_PACKAGE));
resetCachedUrlHandlers();

@ -63,6 +63,9 @@ import static org.mockito.Mockito.verify;
* @author Andy Wilkinson
*/
public class JarFileTests {
private static final String PROTOCOL_HANDLER = "java.protocol.handler.pkgs";
private static final String HANDLERS_PACKAGE = "org.springframework.boot.loader";
@Rule
public ExpectedException thrown = ExpectedException.none();
@ -453,4 +456,42 @@ public class JarFileTests {
url.openConnection().getInputStream();
}
@Test
public void registerUrlProtocolHandlerWithNoExistingRegistration() {
String original = System.getProperty(PROTOCOL_HANDLER);
try {
System.clearProperty(PROTOCOL_HANDLER);
JarFile.registerUrlProtocolHandler();
String protocolHandler = System.getProperty(PROTOCOL_HANDLER);
assertThat(protocolHandler, equalTo(HANDLERS_PACKAGE));
}
finally {
if (original == null) {
System.clearProperty(PROTOCOL_HANDLER);
}
else {
System.setProperty(PROTOCOL_HANDLER, original);
}
}
}
@Test
public void registerUrlProtocolHandlerAddsToExistingRegistration() {
String original = System.getProperty(PROTOCOL_HANDLER);
try {
System.setProperty(PROTOCOL_HANDLER, "com.example");
JarFile.registerUrlProtocolHandler();
String protocolHandler = System.getProperty(PROTOCOL_HANDLER);
assertThat(protocolHandler, equalTo("com.example|" + HANDLERS_PACKAGE));
}
finally {
if (original == null) {
System.clearProperty(PROTOCOL_HANDLER);
}
else {
System.setProperty(PROTOCOL_HANDLER, original);
}
}
}
}

Loading…
Cancel
Save