Minimise dependencies on Log4j2

Closes gh-15441
This commit is contained in:
Andy Wilkinson
2018-12-11 15:51:46 +00:00
parent e16511279a
commit 2b453bbb16
15 changed files with 189 additions and 283 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.data.elasticsearch;
import java.util.List;
@@ -22,10 +21,12 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.junit.After;
import org.junit.jupiter.api.Test;
import org.junit.ClassRule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -41,6 +42,9 @@ import static org.mockito.Mockito.mock;
*/
public class ElasticsearchAutoConfigurationTests {
@ClassRule
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private AnnotationConfigApplicationContext context;
@After
@@ -65,19 +69,17 @@ public class ElasticsearchAutoConfigurationTests {
@Test
public void createTransportClient() {
this.context = new AnnotationConfigApplicationContext();
new ElasticsearchNodeTemplate().doWithNode((node) -> {
TestPropertyValues.of(
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ node.getTcpPort(),
"spring.data.elasticsearch.properties.path.home:target/es/client")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class);
this.context.refresh();
List<DiscoveryNode> connectedNodes = this.context
.getBean(TransportClient.class).connectedNodes();
assertThat(connectedNodes).hasSize(1);
});
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class);
this.context.refresh();
List<DiscoveryNode> connectedNodes = this.context.getBean(TransportClient.class)
.connectedNodes();
assertThat(connectedNodes).hasSize(1);
}
@Configuration

View File

@@ -17,10 +17,12 @@
package org.springframework.boot.autoconfigure.data.elasticsearch;
import org.junit.After;
import org.junit.jupiter.api.Test;
import org.junit.ClassRule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
@@ -36,6 +38,9 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ElasticsearchDataAutoConfigurationTests {
@ClassRule
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private AnnotationConfigApplicationContext context;
@After
@@ -55,55 +60,46 @@ public class ElasticsearchDataAutoConfigurationTests {
@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext();
new ElasticsearchNodeTemplate().doWithNode((node) -> {
TestPropertyValues
.of("spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs",
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ node.getTcpPort())
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchTemplate.class);
});
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchTemplate.class);
}
@Test
public void mappingContextExists() {
this.context = new AnnotationConfigApplicationContext();
new ElasticsearchNodeTemplate().doWithNode((node) -> {
TestPropertyValues
.of("spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs",
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ node.getTcpPort())
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(SimpleElasticsearchMappingContext.class);
});
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(SimpleElasticsearchMappingContext.class);
}
@Test
public void converterExists() {
this.context = new AnnotationConfigApplicationContext();
new ElasticsearchNodeTemplate().doWithNode((node) -> {
TestPropertyValues
.of("spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs",
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ node.getTcpPort())
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchConverter.class);
});
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchConverter.class);
}
private void assertHasSingleBean(Class<?> type) {

View File

@@ -1,112 +0,0 @@
/*
* Copyright 2012-2018 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.
* 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 org.springframework.boot.autoconfigure.data.elasticsearch;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.function.Consumer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.InternalSettingsPreparer;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeValidationException;
import org.elasticsearch.transport.Netty4Plugin;
import org.elasticsearch.transport.Transport;
/**
* Helper class for managing an Elasticsearch {@link Node}.
*
* @author Andy Wilkinson
*/
public class ElasticsearchNodeTemplate {
public void doWithNode(Consumer<ElasticsearchNode> consumer) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
Node node = null;
try {
node = startNode();
consumer.accept(new ElasticsearchNode(node));
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
finally {
if (node != null) {
try {
node.close();
}
catch (Exception ex) {
// Continue
}
}
System.clearProperty("es.set.netty.runtime.available.processors");
}
}
private Node startNode() throws NodeValidationException {
Node node = new NettyTransportNode();
node.start();
return node;
}
private static final class NettyTransportNode extends Node {
private NettyTransportNode() {
super(InternalSettingsPreparer.prepareEnvironment(Settings.builder()
.put("path.home", "target/es/node").put("transport.type", "netty4")
.put("http.enabled", true).put("node.portsfile", true)
.put("http.port", 0).put("transport.tcp.port", 0).build(), null),
Arrays.asList(Netty4Plugin.class));
new File("target/es/node/logs").mkdirs();
}
}
public final class ElasticsearchNode {
private final Node node;
private ElasticsearchNode(Node node) {
this.node = node;
}
public int getTcpPort() {
return this.node.injector().getInstance(Transport.class).boundAddress()
.publishAddress().getPort();
}
public int getHttpPort() {
try {
for (String line : Files
.readAllLines(Paths.get("target/es/node/logs/http.ports"))) {
if (line.startsWith("127.0.0.1")) {
return Integer.parseInt(line.substring(line.indexOf(":") + 1));
}
}
throw new IllegalStateException("HTTP port not found");
}
catch (IOException ex) {
throw new IllegalStateException("Failed to read HTTP port", ex);
}
}
}
}

View File

@@ -18,19 +18,17 @@ package org.springframework.boot.autoconfigure.data.elasticsearch;
import org.elasticsearch.client.Client;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.alt.elasticsearch.CityElasticsearchDbRepository;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchNodeTemplate.ElasticsearchNode;
import org.springframework.boot.autoconfigure.data.elasticsearch.city.City;
import org.springframework.boot.autoconfigure.data.elasticsearch.city.CityRepository;
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.runner.classpath.ClassPathOverrides;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@@ -43,10 +41,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
* @author Andy Wilkinson
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathOverrides("org.apache.logging.log4j:log4j-core:2.10.0")
public class ElasticsearchRepositoriesAutoConfigurationTests {
@ClassRule
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private AnnotationConfigApplicationContext context;
@After
@@ -56,34 +55,27 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@Test
public void testDefaultRepositoryConfiguration() {
new ElasticsearchNodeTemplate().doWithNode((node) -> {
load(TestConfiguration.class, node);
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
assertThat(this.context.getBean(Client.class)).isNotNull();
});
load(TestConfiguration.class);
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
assertThat(this.context.getBean(Client.class)).isNotNull();
}
@Test
public void testNoRepositoryConfiguration() {
new ElasticsearchNodeTemplate().doWithNode((node) -> {
load(EmptyConfiguration.class, node);
assertThat(this.context.getBean(Client.class)).isNotNull();
});
load(EmptyConfiguration.class);
assertThat(this.context.getBean(Client.class)).isNotNull();
}
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
new ElasticsearchNodeTemplate().doWithNode((node) -> {
load(CustomizedConfiguration.class, node);
assertThat(this.context.getBean(CityElasticsearchDbRepository.class))
.isNotNull();
});
load(CustomizedConfiguration.class);
assertThat(this.context.getBean(CityElasticsearchDbRepository.class)).isNotNull();
}
private void load(Class<?> config, ElasticsearchNode node) {
private void load(Class<?> config) {
this.context = new AnnotationConfigApplicationContext();
addElasticsearchProperties(this.context, node);
addElasticsearchProperties(this.context);
this.context.register(config, ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class,
@@ -91,11 +83,11 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
this.context.refresh();
}
private void addElasticsearchProperties(AnnotationConfigApplicationContext context,
ElasticsearchNode node) {
TestPropertyValues.of("spring.data.elasticsearch.properties.path.home:target",
"spring.data.elasticsearch.cluster-nodes:localhost:" + node.getTcpPort())
.applyTo(context);
private void addElasticsearchProperties(AnnotationConfigApplicationContext context) {
TestPropertyValues.of(
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster").applyTo(context);
}
@Configuration

View File

@@ -27,18 +27,14 @@ import io.searchbox.client.JestResult;
import io.searchbox.client.http.JestHttpClient;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchNodeTemplate;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.runner.classpath.ClassPathOverrides;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -52,24 +48,15 @@ import static org.mockito.Mockito.mock;
* @author Stephane Nicoll
* @author Andy Wilkinson
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathOverrides("org.apache.logging.log4j:log4j-core:2.10.0")
public class JestAutoConfigurationTests {
@ClassRule
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GsonAutoConfiguration.class,
JestAutoConfiguration.class));
@Before
public void preventElasticsearchFromConfiguringNetty() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
@After
public void close() {
System.clearProperty("es.set.netty.runtime.available.processors");
}
@Test
public void jestClientOnLocalhostByDefault() {
this.contextRunner
@@ -122,9 +109,9 @@ public class JestAutoConfigurationTests {
@Test
public void jestCanCommunicateWithElasticsearchInstance() {
new ElasticsearchNodeTemplate().doWithNode((node) -> this.contextRunner
this.contextRunner
.withPropertyValues("spring.elasticsearch.jest.uris=http://localhost:"
+ node.getHttpPort())
+ elasticsearch.getMappedPort())
.run((context) -> {
JestClient client = context.getBean(JestClient.class);
Map<String, String> source = new HashMap<>();
@@ -136,7 +123,7 @@ public class JestAutoConfigurationTests {
Get getRequest = new Get.Builder("foo", "1").build();
assertThat(execute(client, getRequest).getResponseCode())
.isEqualTo(200);
}));
});
}
private JestResult execute(JestClient client, Action<? extends JestResult> action) {

View File

@@ -25,11 +25,12 @@ import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.junit.ClassRule;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchNodeTemplate;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
@@ -44,6 +45,9 @@ import static org.mockito.Mockito.mock;
*/
public class RestClientAutoConfigurationTests {
@ClassRule
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class));
@@ -77,9 +81,9 @@ public class RestClientAutoConfigurationTests {
@Test
public void restClientCanQueryElasticsearchNode() {
new ElasticsearchNodeTemplate().doWithNode((node) -> this.contextRunner
this.contextRunner
.withPropertyValues("spring.elasticsearch.rest.uris=http://localhost:"
+ node.getHttpPort())
+ RestClientAutoConfigurationTests.elasticsearch.getMappedPort())
.run((context) -> {
RestHighLevelClient client = context
.getBean(RestHighLevelClient.class);
@@ -92,7 +96,7 @@ public class RestClientAutoConfigurationTests {
GetRequest getRequest = new GetRequest("foo", "bar", "1");
assertThat(client.get(getRequest, RequestOptions.DEFAULT).isExists())
.isTrue();
}));
});
}
@Configuration