Added showcase for JDK 8's Optional support on repository methods.

Abbreviated JPA sample module folder names. Renamed the Java 8 auditing sample module to Java 8 module as it not only contains samples for auditing anymore.
This commit is contained in:
Oliver Gierke
2014-04-22 14:48:00 +02:00
parent dadaf374c3
commit 0ddb2ba0d6
74 changed files with 95 additions and 19 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2011-2014 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 example.springdata.jpa.showcase;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import example.springdata.jpa.showcase.AbstractShowcaseTest.TestConfig;
/**
* @author Oliver Gierke
*/
@SpringApplicationConfiguration(classes = TestConfig.class)
@Transactional
public abstract class AbstractShowcaseTest extends AbstractTransactionalJUnit4SpringContextTests {
@Configuration
@EnableAutoConfiguration
@ComponentScan
static class TestConfig {
}
@BeforeTransaction
public void setupData() throws Exception {
deleteFromTables("account", "customer");
executeSqlScript("classpath:data.sql", false);
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2011-2014 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 example.springdata.jpa.showcase.after;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
/**
* Integration tests for Spring Data JPA {@link AccountRepository}.
*
* @author Oliver Gierke
*/
public class AccountRepositoryIntegrationTest extends AbstractShowcaseTest {
@Autowired AccountRepository accountRepository;
@Autowired CustomerRepository customerRepository;
@Test
public void savesAccount() {
Account account = accountRepository.save(new Account());
assertThat(account.getId(), is(notNullValue()));
}
@Test
public void findsCustomersAccounts() {
Customer customer = customerRepository.findOne(1L);
List<Account> accounts = accountRepository.findByCustomer(customer);
assertFalse(accounts.isEmpty());
assertThat(accounts.get(0).getCustomer(), is(customer));
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2011-2014 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 example.springdata.jpa.showcase.after;
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.jpa.domain.Specifications.*;
import java.util.List;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
/**
* Integration tests for Spring Data JPA {@link CustomerRepository}.
*
* @author Oliver Gierke
*/
public class CustomerRepositoryIntegrationTest extends AbstractShowcaseTest {
@Autowired CustomerRepository repository;
@Test
public void findsAllCustomers() throws Exception {
Iterable<Customer> result = repository.findAll();
assertThat(result, is(notNullValue()));
assertTrue(result.iterator().hasNext());
}
@Test
public void findsFirstPageOfMatthews() throws Exception {
Page<Customer> customers = repository.findByLastname("Matthews", new PageRequest(0, 2));
assertThat(customers.getContent().size(), is(2));
assertFalse(customers.hasPreviousPage());
}
@Test
public void findsCustomerById() throws Exception {
Customer customer = repository.findOne(2L);
assertThat(customer.getFirstname(), is("Carter"));
assertThat(customer.getLastname(), is("Beauford"));
}
@Test
public void findsCustomersBySpecification() throws Exception {
Customer dave = repository.findOne(1L);
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
List<Customer> result = repository.findAll(where(accountExpiresBefore(expiryLimit)));
assertThat(result.size(), is(1));
assertThat(result, hasItems(dave));
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2011-2014 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 example.springdata.jpa.showcase.before;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
/**
* Integration test for {@link AccountService}.
*
* @author Oliver Gierke
*/
public class AccountServiceIntegrationTest extends AbstractShowcaseTest {
@Autowired AccountService accountService;
@Autowired CustomerService customerService;
@Test
public void savesAccount() {
Account account = accountService.save(new Account());
assertThat(account.getId(), is(notNullValue()));
}
@Test
public void testname() throws Exception {
Customer customer = customerService.findById(1L);
List<Account> accounts = accountService.findByCustomer(customer);
assertThat(accounts, is(not(empty())));
assertThat(accounts.get(0).getCustomer(), is(customer));
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2011-2014 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 example.springdata.jpa.showcase.before;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
/**
* Integration test for {@link CustomerService}.
*
* @author Oliver Gierke
*/
public class CustomerServiceIntegrationTest extends AbstractShowcaseTest {
@Autowired CustomerService repository;
@Test
public void findsAllCustomers() throws Exception {
List<Customer> result = repository.findAll();
assertThat(result, is(notNullValue()));
assertFalse(result.isEmpty());
}
@Test
public void findsPageOfMatthews() throws Exception {
List<Customer> customers = repository.findByLastname("Matthews", 0, 2);
assertThat(customers.size(), is(2));
}
@Test
public void findsCustomerById() throws Exception {
Customer customer = repository.findById(2L);
assertThat(customer.getFirstname(), is("Carter"));
assertThat(customer.getLastname(), is("Beauford"));
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.data" level="debug" />
<root level="info">
<appender-ref ref="console" />
</root>
</configuration>