From 2569cf8de336adb8c9fea8cf7fc8f4434378e05d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 12 Mar 2015 13:56:06 +0100 Subject: [PATCH] #57 - Polishing. Replaced custom builder with Lombok generated one. Turned Lombok into a value object as a side effect. Removed obsolete toString() method. Extended copyright clauses wehere missing. Reduced visibility of repository extension. --- .../java/example/springdata/solr/Product.java | 15 ++-- .../springdata/solr/ProductRepository.java | 2 +- .../solr/ProductRepositoryCustom.java | 4 +- .../solr/ProductRepositoryImpl.java | 5 +- .../solr/AdvancedSolrRepositoryTests.java | 84 ++++--------------- .../solr/BasicSolrRepositoryTests.java | 11 +-- .../solr/SolrTestConfiguration.java | 9 +- 7 files changed, 32 insertions(+), 98 deletions(-) diff --git a/solr/example/src/main/java/example/springdata/solr/Product.java b/solr/example/src/main/java/example/springdata/solr/Product.java index fb385032..4fc89851 100644 --- a/solr/example/src/main/java/example/springdata/solr/Product.java +++ b/solr/example/src/main/java/example/springdata/solr/Product.java @@ -17,7 +17,8 @@ package example.springdata.solr; import java.util.List; -import lombok.Data; +import lombok.Builder; +import lombok.Value; import org.springframework.data.annotation.Id; import org.springframework.data.geo.Point; @@ -26,12 +27,14 @@ import org.springframework.data.solr.core.mapping.SolrDocument; import org.springframework.data.solr.repository.Score; /** - * Document representing a Product and its attributes matching the fields defined in the example solr schema. * * @author Christoph Strobl + * @author Oliver Gierke */ -@Data +@Value +@Builder @SolrDocument(solrCoreName = "collection1") public class Product { @@ -43,10 +46,4 @@ public class Product { private @Indexed boolean inStock; private @Indexed Integer popularity; private @Score Float score; - - @Override - public String toString() { - return "Product [id=" + id + ", name=" + name + ", category=" + category + ", location=" + location + ", inStock=" - + inStock + ", score=" + score + "]"; - } } diff --git a/solr/example/src/main/java/example/springdata/solr/ProductRepository.java b/solr/example/src/main/java/example/springdata/solr/ProductRepository.java index fdcbd7e9..84dcc689 100644 --- a/solr/example/src/main/java/example/springdata/solr/ProductRepository.java +++ b/solr/example/src/main/java/example/springdata/solr/ProductRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. diff --git a/solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.java b/solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.java index e968db9b..31ab8ec4 100644 --- a/solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.java +++ b/solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -23,7 +23,7 @@ import org.springframework.data.solr.core.query.result.Cursor; * * @author Christoph Strobl */ -public interface ProductRepositoryCustom { +interface ProductRepositoryCustom { /** * Use a {@link Cursor} to scroll through documents in index.
diff --git a/solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.java b/solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.java index ed2da7de..4492f0fe 100644 --- a/solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.java +++ b/solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -25,8 +25,9 @@ import org.springframework.data.solr.core.query.result.Cursor; * Implementation of {@link ProductRepositoryCustom}. * * @author Christoph Strobl + * @author Oliver Gierke */ -public class ProductRepositoryImpl implements ProductRepositoryCustom { +class ProductRepositoryImpl implements ProductRepositoryCustom { @Autowired SolrTemplate solrTemplate; diff --git a/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java b/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java index 6717f119..ed21da84 100644 --- a/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java +++ b/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java @@ -20,9 +20,7 @@ import static org.junit.Assert.*; import static org.springframework.data.solr.core.query.Criteria.*; import static org.springframework.data.solr.core.query.ExistsFunction.*; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import org.junit.ClassRule; import org.junit.Test; @@ -44,6 +42,7 @@ import example.springdata.solr.test.util.RequiresSolrServer; /** * @author Christoph Strobl + * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @@ -57,22 +56,18 @@ public class AdvancedSolrRepositoryTests { @Override protected void doInitTestData(CrudRepository repository) { - Product playstation = new ProductBuilder().withId("id-1").named("Playstation") - .withDescription("The Sony playstation was the top selling gaming system in 1994.").withPopularity(5).build(); - - Product playstation2 = new ProductBuilder().withId("id-2").named("Playstation Two") - .withDescription("Playstation two is the successor of playstation in 2000.").build(); - - Product superNES = new ProductBuilder().withId("id-3").named("Super Nintendo").withPopularity(3).build(); - - Product nintendo64 = new ProductBuilder().withId("id-4").named("N64").withDescription("Nintendo 64") - .withPopularity(2).build(); + Product playstation = Product.builder().id("id-1").name("Playstation") + .description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build(); + Product playstation2 = Product.builder().id("id-2").name("Playstation Two") + .description("Playstation two is the successor of playstation in 2000.").build(); + Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build(); + Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build(); repository.save(Arrays.asList(playstation, playstation2, superNES, nintendo64)); } } - @Autowired ProductRepository repo; + @Autowired ProductRepository repository; @Autowired SolrOperations operations; /** @@ -82,7 +77,7 @@ public class AdvancedSolrRepositoryTests { @Test public void annotationBasedHighlighting() { - HighlightPage products = repo.findByDescriptionStartingWith("play", new PageRequest(0, 10)); + HighlightPage products = repository.findByDescriptionStartingWith("play", new PageRequest(0, 10)); products.getHighlighted().forEach( entry -> entry.getHighlights().forEach( @@ -96,9 +91,7 @@ public class AdvancedSolrRepositoryTests { */ @Test public void annotationBasedBoosting() { - - repo.findTop10ByNameOrDescription("Nintendo", "Nintendo") // - .forEach(System.out::println); + repository.findTop10ByNameOrDescription("Nintendo", "Nintendo").forEach(System.out::println); } /** @@ -109,9 +102,9 @@ public class AdvancedSolrRepositoryTests { @Test public void influcenceScoreWithFunctions() { - operations.queryForPage(new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score"), - Product.class) // - .forEach(System.out::println); + Query query = new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score"); + + operations.queryForPage(query, Product.class).forEach(System.out::println); } /** @@ -121,8 +114,7 @@ public class AdvancedSolrRepositoryTests { @Test public void useRealtimeGetToReadUncommitedDocuments() throws InterruptedException { - Product xbox = new ProductBuilder().withId("id-5").named("XBox").withDescription("Microsift XBox") - .withPopularity(2).build(); + Product xbox = Product.builder().id("id-5").name("XBox").description("Microsift XBox").popularity(2).build(); Query query = new SimpleQuery(where("id").is(xbox.getId())); // add document but delay commit for 3 seconds @@ -138,52 +130,4 @@ public class AdvancedSolrRepositoryTests { Thread.sleep(3010); assertThat(operations.queryForObject(query, Product.class), notNullValue()); } - - static class ProductBuilder { - - private Product product; - - public ProductBuilder() { - this.product = new Product(); - } - - public ProductBuilder withId(String id) { - this.product.setId(id); - return this; - } - - public ProductBuilder named(String name) { - this.product.setName(name); - return this; - } - - public ProductBuilder withDescription(String description) { - this.product.setDescription(description); - return this; - } - - public ProductBuilder withPopularity(Integer popularity) { - this.product.setPopularity(popularity); - return this; - } - - public ProductBuilder inCategory(String category) { - - List categories = new ArrayList<>(); - categories.add(category); - - if (this.product.getCategory() == null) { - categories.addAll(this.product.getCategory()); - } - - this.product.setCategory(categories); - return this; - - } - - public Product build() { - return this.product; - } - - } } diff --git a/solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java b/solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java index 598e62f2..b169a67c 100644 --- a/solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java +++ b/solr/example/src/test/java/example/springdata/solr/BasicSolrRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -33,15 +33,14 @@ public class BasicSolrRepositoryTests { public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost(); - @Autowired ProductRepository repo; + @Autowired ProductRepository repository; /** * Finds all entries using a single request. */ @Test public void findAll() { - repo.findAll()// - .forEach(System.out::println); + repository.findAll().forEach(System.out::println); } /** @@ -49,8 +48,6 @@ public class BasicSolrRepositoryTests { */ @Test public void findAllUsingDeepPagination() { - repo.findAllUsingCursor()// - .forEachRemaining(System.out::println); + repository.findAllUsingCursor().forEachRemaining(System.out::println); } - } diff --git a/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java b/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java index 4e370bf5..17f05999 100644 --- a/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java +++ b/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -59,12 +59,7 @@ public class SolrTestConfiguration { protected void doInitTestData(CrudRepository repository) { for (int i = 0; i < 100; i++) { - - Product p = new Product(); - p.setId("p-" + i); - p.setName("foobar"); - - repository.save(p); + repository.save(Product.builder().id("p-" + i).name("foobar").build()); } } }