Skip to content

Commit 76057be

Browse files
committed
Merge branch '1.5.x'
2 parents f7ddacf + 08ec3d2 commit 76057be

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListener.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,8 @@ public class LiquibaseServiceLocatorApplicationListener
4040

4141
@Override
4242
public void onApplicationEvent(ApplicationStartingEvent event) {
43-
if (ClassUtils.isPresent("liquibase.servicelocator.ServiceLocator", null)) {
43+
if (ClassUtils.isPresent("liquibase.servicelocator.CustomResolverServiceLocator",
44+
event.getSpringApplication().getClassLoader())) {
4445
new LiquibasePresent().replaceServiceLocator();
4546
}
4647
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/liquibase/LiquibaseServiceLocatorApplicationListenerTests.java

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,13 @@
1717
package org.springframework.boot.liquibase;
1818

1919
import java.lang.reflect.Field;
20+
import java.net.URL;
21+
import java.net.URLClassLoader;
22+
import java.util.Arrays;
23+
import java.util.List;
2024

25+
import liquibase.servicelocator.CustomResolverServiceLocator;
26+
import liquibase.servicelocator.DefaultPackageScanClassResolver;
2127
import liquibase.servicelocator.ServiceLocator;
2228
import org.junit.After;
2329
import org.junit.Test;
@@ -26,6 +32,7 @@
2632
import org.springframework.boot.WebApplicationType;
2733
import org.springframework.context.ConfigurableApplicationContext;
2834
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.core.io.DefaultResourceLoader;
2936
import org.springframework.util.ReflectionUtils;
3037

3138
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,6 +41,7 @@
3441
* Tests for {@link LiquibaseServiceLocatorApplicationListener}.
3542
*
3643
* @author Phillip Webb
44+
* @author Stephane Nicoll
3745
*/
3846
public class LiquibaseServiceLocatorApplicationListenerTests {
3947

@@ -47,20 +55,66 @@ public void cleanUp() {
4755
}
4856

4957
@Test
50-
public void replacesServiceLocator() throws Exception {
58+
public void replacesServiceLocator() throws IllegalAccessException {
5159
SpringApplication application = new SpringApplication(Conf.class);
5260
application.setWebApplicationType(WebApplicationType.NONE);
5361
this.context = application.run();
62+
Object resolver = getServiceLocator();
63+
assertThat(resolver).isInstanceOf(SpringPackageScanClassResolver.class);
64+
}
65+
66+
@Test
67+
public void replaceServiceLocatorBacksOffIfNotPresent()
68+
throws IllegalAccessException {
69+
SpringApplication application = new SpringApplication(Conf.class);
70+
application.setWebApplicationType(WebApplicationType.NONE);
71+
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
72+
resourceLoader.setClassLoader(new ClassHidingClassLoader(
73+
CustomResolverServiceLocator.class));
74+
application.setResourceLoader(resourceLoader);
75+
this.context = application.run();
76+
Object resolver = getServiceLocator();
77+
assertThat(resolver).isInstanceOf(DefaultPackageScanClassResolver.class);
78+
}
79+
80+
private Object getServiceLocator() throws IllegalAccessException {
5481
ServiceLocator instance = ServiceLocator.getInstance();
5582
Field field = ReflectionUtils.findField(ServiceLocator.class, "classResolver");
5683
field.setAccessible(true);
57-
Object resolver = field.get(instance);
58-
assertThat(resolver).isInstanceOf(SpringPackageScanClassResolver.class);
84+
return field.get(instance);
5985
}
6086

6187
@Configuration
6288
public static class Conf {
6389

6490
}
6591

92+
private final class ClassHidingClassLoader extends URLClassLoader {
93+
94+
private final List<Class<?>> hiddenClasses;
95+
96+
private ClassHidingClassLoader(Class<?>... hiddenClasses) {
97+
super(new URL[0], LiquibaseServiceLocatorApplicationListenerTests.class.getClassLoader());
98+
this.hiddenClasses = Arrays.asList(hiddenClasses);
99+
}
100+
101+
@Override
102+
public Class<?> loadClass(String name) throws ClassNotFoundException {
103+
if (isHidden(name)) {
104+
throw new ClassNotFoundException();
105+
}
106+
return super.loadClass(name);
107+
}
108+
109+
private boolean isHidden(String name) {
110+
for (Class<?> hiddenClass : this.hiddenClasses) {
111+
if (hiddenClass.getName().equals(name)) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
118+
}
119+
66120
}

0 commit comments

Comments
 (0)