Skip to content

Commit c2f13c3

Browse files
committed
DATAES-194 - Tests should clean up "data" directory.
1 parent 49708d3 commit c2f13c3

File tree

11 files changed

+76
-50
lines changed

11 files changed

+76
-50
lines changed

src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2015 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,6 +40,7 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
4040
private boolean enableHttp;
4141
private String clusterName;
4242
private NodeClient nodeClient;
43+
private String pathData;
4344

4445
NodeClientFactoryBean() {
4546
}
@@ -65,8 +66,10 @@ public boolean isSingleton() {
6566

6667
@Override
6768
public void afterPropertiesSet() throws Exception {
68-
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled",
69-
String.valueOf(this.enableHttp));
69+
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
70+
.put("http.enabled", String.valueOf(this.enableHttp))
71+
.put("path.data", this.pathData);
72+
7073

7174
nodeClient = (NodeClient) nodeBuilder().settings(settings).clusterName(this.clusterName).local(this.local).node()
7275
.client();
@@ -84,6 +87,10 @@ public void setClusterName(String clusterName) {
8487
this.clusterName = clusterName;
8588
}
8689

90+
public void setPathData(String pathData) {
91+
this.pathData = pathData;
92+
}
93+
8794
@Override
8895
public void destroy() throws Exception {
8996
try {

src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2015 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.
@@ -43,6 +43,7 @@ private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
4343
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
4444
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
4545
builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled")));
46+
builder.addPropertyValue("pathData", element.getAttribute("path-data"));
4647
}
4748

4849
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,

src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
</xsd:documentation>
6868
</xsd:annotation>
6969
</xsd:attribute>
70+
<xsd:attribute name="path-data" type="xsd:string" default="">
71+
<xsd:annotation>
72+
<xsd:documentation>
73+
<![CDATA[ path to the data folder for node client ]]>
74+
</xsd:documentation>
75+
</xsd:annotation>
76+
</xsd:attribute>
7077
</xsd:extension>
7178
</xsd:complexContent>
7279
</xsd:complexType>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
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+
package org.springframework.data.elasticsearch;
17+
18+
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
19+
20+
import java.util.UUID;
21+
22+
import org.elasticsearch.client.node.NodeClient;
23+
import org.elasticsearch.common.settings.ImmutableSettings;
24+
25+
/**
26+
* @author Mohsin Husen
27+
*/
28+
public class Utils {
29+
30+
public static NodeClient getNodeClient() {
31+
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
32+
.put("http.enabled", "false")
33+
.put("path.data", "target/elasticsearchTestData");
34+
return (NodeClient) nodeBuilder().settings(settings).clusterName(UUID.randomUUID().toString()).local(true).node()
35+
.client();
36+
}
37+
38+
39+
40+
}

src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.config;
1717

18-
import static org.elasticsearch.node.NodeBuilder.*;
1918
import static org.hamcrest.CoreMatchers.*;
2019
import static org.junit.Assert.*;
2120

@@ -27,6 +26,7 @@
2726
import org.springframework.context.ApplicationContextAware;
2827
import org.springframework.context.annotation.Bean;
2928
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.elasticsearch.Utils;
3030
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3131
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
3232
import org.springframework.data.elasticsearch.entities.SampleEntity;
@@ -60,7 +60,7 @@ static class Config {
6060

6161
@Bean
6262
public ElasticsearchOperations elasticsearchTemplate() {
63-
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client());
63+
return new ElasticsearchTemplate(Utils.getNodeClient());
6464
}
6565
}
6666

src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.data.elasticsearch.Utils;
2324
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2425
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
2526
import org.springframework.data.elasticsearch.entities.SampleEntity;
@@ -28,7 +29,6 @@
2829
import org.springframework.test.context.ContextConfiguration;
2930
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3031

31-
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
3232
import static org.junit.Assert.assertNotNull;
3333

3434
/**
@@ -45,7 +45,7 @@ static class Config {
4545

4646
@Bean
4747
public ElasticsearchOperations elasticsearchTemplate() {
48-
return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client());
48+
return new ElasticsearchTemplate(Utils.getNodeClient());
4949
}
5050
}
5151

src/test/java/org/springframework/data/elasticsearch/core/AliasTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -179,7 +179,6 @@ public void shouldAddAliasForVariousRoutingValues() {
179179
.withObject(sampleEntity).build();
180180

181181
elasticsearchTemplate.index(indexQuery);
182-
elasticsearchTemplate.refresh(SampleEntity.class, true);
183182

184183
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
185184
.withIndices(alias2).withTypes(TYPE_NAME).build();
@@ -194,9 +193,5 @@ public void shouldAddAliasForVariousRoutingValues() {
194193
//cleanup
195194
elasticsearchTemplate.removeAlias(aliasQuery1);
196195
elasticsearchTemplate.removeAlias(aliasQuery2);
197-
elasticsearchTemplate.deleteIndex(SampleEntity.class);
198-
elasticsearchTemplate.createIndex(SampleEntity.class);
199-
elasticsearchTemplate.putMapping(SampleEntity.class);
200-
elasticsearchTemplate.refresh(SampleEntity.class, true);
201196
}
202197
}

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -770,14 +770,15 @@ public void shouldReturnResultsWithScanAndScrollForSpecifiedFieldsForSearchCrite
770770
.withIndices(INDEX_NAME)
771771
.withTypes(TYPE_NAME)
772772
.withFields("message")
773+
.withQuery(matchAllQuery())
773774
.withPageable(new PageRequest(0, 10))
774775
.build();
775776

776-
String scrollId = elasticsearchTemplate.scan(searchQuery, 5000, false);
777+
String scrollId = elasticsearchTemplate.scan(searchQuery, 10000, false);
777778
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
778779
boolean hasRecords = true;
779780
while (hasRecords) {
780-
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() {
781+
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 10000L, new SearchResultMapper() {
781782
@Override
782783
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
783784
List<SampleEntity> result = new ArrayList<SampleEntity>();
@@ -821,7 +822,7 @@ public void shouldReturnResultsForScanAndScrollWithCustomResultMapperForGivenCri
821822
criteriaQuery.addTypes(TYPE_NAME);
822823
criteriaQuery.setPageable(new PageRequest(0, 10));
823824

824-
String scrollId = elasticsearchTemplate.scan(criteriaQuery, 1000, false);
825+
String scrollId = elasticsearchTemplate.scan(criteriaQuery, 5000, false);
825826
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
826827
boolean hasRecords = true;
827828
while (hasRecords) {
@@ -925,7 +926,7 @@ public void shouldReturnResultsWithStreamForGivenCriteriaQuery() {
925926
private static List<IndexQuery> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
926927
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
927928
for (int i = 0; i < numberOfEntities; i++) {
928-
String documentId = randomNumeric(5);
929+
String documentId = UUID.randomUUID().toString();
929930
SampleEntity sampleEntity = new SampleEntity();
930931
sampleEntity.setId(documentId);
931932
sampleEntity.setMessage(message);
@@ -1152,6 +1153,7 @@ public void shouldDeleteSpecifiedTypeFromAnIndex() {
11521153
elasticsearchTemplate.refresh(SampleEntity.class, true);
11531154
// when
11541155
elasticsearchTemplate.deleteType(INDEX_NAME, TYPE_NAME);
1156+
elasticsearchTemplate.refresh(SampleEntity.class, true);
11551157

11561158
//then
11571159
boolean typeExists = elasticsearchTemplate.typeExists(INDEX_NAME, TYPE_NAME);
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 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.
@@ -15,20 +15,15 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repositories.cdi;
1717

18-
import static org.elasticsearch.node.NodeBuilder.*;
19-
2018
import javax.annotation.PreDestroy;
2119
import javax.enterprise.context.ApplicationScoped;
2220
import javax.enterprise.inject.Produces;
2321
import javax.xml.parsers.ParserConfigurationException;
2422
import java.io.IOException;
2523

26-
import org.elasticsearch.client.node.NodeClient;
27-
import org.elasticsearch.common.settings.ImmutableSettings;
28-
import org.elasticsearch.index.query.QueryBuilders;
24+
import org.springframework.data.elasticsearch.Utils;
2925
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3026
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
31-
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
3227
import org.xml.sax.SAXException;
3328

3429
/**
@@ -39,33 +34,12 @@ class ElasticsearchTemplateProducer {
3934

4035
@Produces
4136
public ElasticsearchOperations createElasticsearchTemplate() throws IOException, ParserConfigurationException, SAXException {
42-
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled", "false");
43-
NodeClient client = (NodeClient) nodeBuilder().settings(settings).clusterName("testClusterForCDI").local(true).node()
44-
.client();
45-
return new ElasticsearchTemplate(client);
37+
return new ElasticsearchTemplate(Utils.getNodeClient());
4638
}
4739

4840
@PreDestroy
4941
public void shutdown() {
5042
// remove everything to avoid conflicts with other tests in case server not shut down properly
51-
deleteAll();
5243
}
5344

54-
private void deleteAll() {
55-
ElasticsearchOperations template;
56-
try {
57-
template = createElasticsearchTemplate();
58-
DeleteQuery deleteQuery = new DeleteQuery();
59-
deleteQuery.setQuery(QueryBuilders.matchAllQuery());
60-
deleteQuery.setIndex("test-product-index");
61-
deleteQuery.setType("test-product-type");
62-
template.delete(deleteQuery);
63-
} catch (IOException e) {
64-
throw new RuntimeException(e);
65-
} catch (ParserConfigurationException e) {
66-
throw new RuntimeException(e);
67-
} catch (SAXException e) {
68-
throw new RuntimeException(e);
69-
}
70-
}
7145
}

src/test/resources/infrastructure.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

8-
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false"/>
8+
<elasticsearch:node-client id="client" local="true" cluster-name="#{T(java.util.UUID).randomUUID().toString()}" http-enabled="false" path-data="target/elasticsearchTestData"/>
99

1010
<!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300" />-->
1111

src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

88
<elasticsearch:node-client id="client" local="true"
9-
cluster-name="testCluster" http-enabled="false"/>
9+
cluster-name="#{T(java.util.UUID).randomUUID().toString()}" http-enabled="false" path-data="target/elasticsearchTestData"/>
1010

1111
<bean name="elasticsearchTemplate"
1212
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">

0 commit comments

Comments
 (0)