formatting changes

This commit is contained in:
mujah 2016-11-13 23:40:17 +08:00
parent 8ccfd201e6
commit 046b23685e
5 changed files with 150 additions and 151 deletions

View File

@ -1,55 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-data-solr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-data-solr</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.5.RELEASE</spring.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring-data-solr}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-data-solr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-data-solr</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.5.RELEASE</spring.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring-data-solr}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -13,13 +13,13 @@ import org.springframework.data.solr.repository.config.EnableSolrRepositories;
@ComponentScan
public class SolrConfig {
@Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://localhost:8983/solr");
}
@Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://localhost:8983/solr");
}
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
}

View File

@ -6,29 +6,28 @@ import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(solrCoreName = "product")
public class Product {
@Id
@Indexed(name = "id", type = "string")
private String id;
@Indexed(name = "name", type = "string")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -11,12 +11,12 @@ import com.baeldung.spring.data.solr.model.Product;
public interface ProductRepository extends SolrCrudRepository<Product, String> {
public List<Product> findByName(String name);
public List<Product> findByName(String name);
@Query("id:*?0* OR name:*?0*")
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
@Query("id:*?0* OR name:*?0*")
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
@Query(name = "Product.findByNamedQuery")
public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
@Query(name = "Product.findByNamedQuery")
public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
}

View File

@ -22,104 +22,104 @@ import com.baeldung.spring.data.solr.repository.ProductRepository;
@ContextConfiguration(classes = SolrConfig.class)
public class ProductRepositoryIntegrationTest {
@Autowired
private ProductRepository productRepository;
@Autowired
private ProductRepository productRepository;
@Before
public void clearSolrData() {
productRepository.deleteAll();
}
@Before
public void clearSolrData() {
productRepository.deleteAll();
}
@Test
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P000089998");
product.setName("Desk");
productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getId(), retrievedProduct.getId());
}
@Test
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P000089998");
product.setName("Desk");
productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getId(), retrievedProduct.getId());
}
@Test
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P0001");
product.setName("T-Shirt");
@Test
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P0001");
product.setName("T-Shirt");
productRepository.save(product);
productRepository.save(product);
product.setName("Shirt");
productRepository.save(product);
product.setName("Shirt");
productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getName(), retrievedProduct.getName());
}
final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getName(), retrievedProduct.getName());
}
@Test
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P0001");
product.setName("Desk");
productRepository.save(product);
@Test
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
final Product product = new Product();
product.setId("P0001");
product.setName("Desk");
productRepository.save(product);
productRepository.delete(product);
productRepository.delete(product);
Product retrievedProduct = productRepository.findOne(product.getId());
assertNull(retrievedProduct);
Product retrievedProduct = productRepository.findOne(product.getId());
assertNull(retrievedProduct);
}
}
@Test
public void whenFindByName_thenAvailableOnRetrieval() throws Exception {
Product phone = new Product();
phone.setId("P0001");
phone.setName("Phone");
productRepository.save(phone);
@Test
public void whenFindByName_thenAvailableOnRetrieval() throws Exception {
Product phone = new Product();
phone.setId("P0001");
phone.setName("Phone");
productRepository.save(phone);
List<Product> retrievedProducts = productRepository.findByName("Phone");
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
}
List<Product> retrievedProducts = productRepository.findByName("Phone");
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
}
@Test
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
final Product phone = new Product();
phone.setId("P0001");
phone.setName("Smart Phone");
productRepository.save(phone);
@Test
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
final Product phone = new Product();
phone.setId("P0001");
phone.setName("Smart Phone");
productRepository.save(phone);
final Product phoneCover = new Product();
phoneCover.setId("P0002");
phoneCover.setName("Phone Cover");
productRepository.save(phoneCover);
final Product phoneCover = new Product();
phoneCover.setId("P0002");
phoneCover.setName("Phone Cover");
productRepository.save(phoneCover);
final Product wirelessCharger = new Product();
wirelessCharger.setId("P0003");
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
final Product wirelessCharger = new Product();
wirelessCharger.setId("P0003");
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
assertEquals(3, result.getNumberOfElements());
}
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
assertEquals(3, result.getNumberOfElements());
}
@Test
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
final Product phone = new Product();
phone.setId("P0001");
phone.setName("Smart Phone");
productRepository.save(phone);
@Test
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
final Product phone = new Product();
phone.setId("P0001");
phone.setName("Smart Phone");
productRepository.save(phone);
final Product phoneCover = new Product();
phoneCover.setId("P0002");
phoneCover.setName("Phone Cover");
productRepository.save(phoneCover);
final Product phoneCover = new Product();
phoneCover.setId("P0002");
phoneCover.setName("Phone Cover");
productRepository.save(phoneCover);
final Product wirelessCharger = new Product();
wirelessCharger.setId("P0003");
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
final Product wirelessCharger = new Product();
wirelessCharger.setId("P0003");
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
assertEquals(3, result.getNumberOfElements());
}
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
assertEquals(3, result.getNumberOfElements());
}
}