DATAGEODE-175 - Move away from Spring Data Commons deprecations.
This commit is contained in:
@@ -80,7 +80,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
if (personRepository.count() == 0) {
|
||||
|
||||
if (this.personRepository.count() == 0) {
|
||||
sourDoe = personRepository.save(sourDoe);
|
||||
sandyHandy = personRepository.save(sandyHandy);
|
||||
jonDoe = personRepository.save(jonDoe);
|
||||
@@ -91,10 +92,11 @@ public class PersonRepositoryIntegrationTests {
|
||||
cookieDoe = personRepository.save(cookieDoe);
|
||||
}
|
||||
|
||||
assertThat(personRepository.count()).isEqualTo(8L);
|
||||
assertThat(this.personRepository.count()).isEqualTo(8L);
|
||||
}
|
||||
|
||||
protected <T> List<T> asList(Iterable<T> iterable) {
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
for (T element : iterable) {
|
||||
@@ -117,12 +119,13 @@ public class PersonRepositoryIntegrationTests {
|
||||
}
|
||||
|
||||
protected Sort newSort(Sort.Order... orders) {
|
||||
return new Sort(orders);
|
||||
return Sort.by(orders);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllPeopleSorted() {
|
||||
Iterable<Person> people = personRepository.findAll(newSort(newSortOrder("firstname")));
|
||||
|
||||
Iterable<Person> people = this.personRepository.findAll(newSort(newSortOrder("firstname")));
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
|
||||
@@ -135,7 +138,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleOrderedByLastnameDescendingFirstnameAscending() {
|
||||
List<Person> actualPeople = personRepository.findDistinctPeopleByOrderByLastnameDesc(
|
||||
|
||||
List<Person> actualPeople = this.personRepository.findDistinctPeopleByOrderByLastnameDesc(
|
||||
newSort(newSortOrder("firstname")));
|
||||
|
||||
assertThat(actualPeople).isNotNull();
|
||||
@@ -146,7 +150,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleByLastnameUnordered() {
|
||||
List<Person> actualPeople = personRepository.findDistinctByLastname("Handy", null);
|
||||
|
||||
List<Person> actualPeople = this.personRepository.findDistinctByLastname("Handy", null);
|
||||
|
||||
assertThat(actualPeople).isNotNull();
|
||||
assertThat(actualPeople.size()).isEqualTo(2);
|
||||
@@ -155,7 +160,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleByFirstOrLastNameWithSort() {
|
||||
Collection<Person> people = personRepository.findDistinctByFirstnameOrLastname("Cookie", "Pigg",
|
||||
|
||||
Collection<Person> people = this.personRepository.findDistinctByFirstnameOrLastname("Cookie", "Pigg",
|
||||
newSort(newSortOrder("lastname", Sort.Direction.DESC), newSortOrder("firstname", Sort.Direction.ASC)));
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
@@ -172,7 +178,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findPersonByFirstAndLastNameIgnoringCase() {
|
||||
Collection<Person> people = personRepository.findByFirstnameIgnoreCaseAndLastnameIgnoreCase("jON", "doE");
|
||||
|
||||
Collection<Person> people = this.personRepository.findByFirstnameIgnoreCaseAndLastnameIgnoreCase("jON", "doE");
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -181,7 +188,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findByFirstAndLastNameAllIgnoringCase() {
|
||||
Collection<Person> people = personRepository.findByFirstnameAndLastnameAllIgnoringCase("IMa", "PIGg");
|
||||
|
||||
Collection<Person> people = this.personRepository.findByFirstnameAndLastnameAllIgnoringCase("IMa", "PIGg");
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -195,6 +203,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
public static class GemFireConfiguration {
|
||||
|
||||
Properties gemfireProperties() {
|
||||
|
||||
Properties gemfireProperties = new Properties();
|
||||
|
||||
gemfireProperties.setProperty("name", applicationName());
|
||||
@@ -215,6 +224,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Bean
|
||||
CacheFactoryBean gemfireCache() {
|
||||
|
||||
CacheFactoryBean gemfireCache = new CacheFactoryBean();
|
||||
|
||||
gemfireCache.setClose(true);
|
||||
@@ -225,6 +235,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Bean(name = "simple")
|
||||
LocalRegionFactoryBean simpleRegion(Cache gemfireCache, RegionAttributes<Long, Person> simpleRegionAttributes) {
|
||||
|
||||
LocalRegionFactoryBean<Long, Person> simpleRegion = new LocalRegionFactoryBean<Long, Person>();
|
||||
|
||||
simpleRegion.setAttributes(simpleRegionAttributes);
|
||||
@@ -238,6 +249,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
@Bean
|
||||
@SuppressWarnings("unchecked")
|
||||
RegionAttributesFactoryBean simpleRegionAttributes() {
|
||||
|
||||
RegionAttributesFactoryBean simpleRegionAttributes = new RegionAttributesFactoryBean();
|
||||
|
||||
simpleRegionAttributes.setKeyConstraint(Long.class);
|
||||
|
||||
@@ -37,9 +37,11 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.LocalRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -58,7 +60,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
@SuppressWarnings("unused")
|
||||
public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning";
|
||||
static final String GEMFIRE_LOG_LEVEL = "warning";
|
||||
|
||||
@Autowired
|
||||
private GemfireTemplate template;
|
||||
@@ -71,33 +73,45 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
private SimpleGemfireRepository<Person, Long> repository;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("all")
|
||||
public void setUp() {
|
||||
people.clear();
|
||||
regionClearListener = new RegionClearListener();
|
||||
people.getAttributesMutator().addCacheListener(regionClearListener);
|
||||
EntityInformation<Person, Long> information = new ReflectionEntityInformation<>(Person.class);
|
||||
repository = new SimpleGemfireRepository<>(template, information);
|
||||
|
||||
this.people.clear();
|
||||
this.regionClearListener = new RegionClearListener();
|
||||
this.people.getAttributesMutator().addCacheListener(this.regionClearListener);
|
||||
|
||||
GemfireMappingContext mappingContext = new GemfireMappingContext();
|
||||
|
||||
GemfirePersistentEntity<Person> personEntity =
|
||||
(GemfirePersistentEntity<Person>) mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
EntityInformation<Person, Long> information = new PersistentEntityInformation<>(personEntity);
|
||||
|
||||
this.repository = new SimpleGemfireRepository<>(this.template, information);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteAllFiresClearEvent() {
|
||||
assertThat(regionClearListener.eventFired).isFalse();
|
||||
repository.deleteAll();
|
||||
assertThat(regionClearListener.eventFired).isTrue();
|
||||
|
||||
assertThat(this.regionClearListener.eventFired).isFalse();
|
||||
|
||||
this.repository.deleteAll();
|
||||
|
||||
assertThat(this.regionClearListener.eventFired).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllWithIds() {
|
||||
|
||||
Person dave = new Person(1L, "Dave", "Matthews");
|
||||
Person carter = new Person(2L, "Carter", "Beauford");
|
||||
Person leroi = new Person(3L, "Leroi", "Moore");
|
||||
|
||||
template.put(dave.getId(), dave);
|
||||
template.put(carter.getId(), carter);
|
||||
template.put(leroi.getId(), leroi);
|
||||
this.template.put(dave.getId(), dave);
|
||||
this.template.put(carter.getId(), carter);
|
||||
this.template.put(leroi.getId(), leroi);
|
||||
|
||||
Collection<Person> result = repository.findAllById(Arrays.asList(carter.getId(), leroi.getId()));
|
||||
Collection<Person> result = this.repository.findAllById(Arrays.asList(carter.getId(), leroi.getId()));
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.size()).isEqualTo(2);
|
||||
@@ -106,7 +120,8 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findAllWithIdsReturnsNoMatches() {
|
||||
Collection<Person> results = repository.findAllById(Arrays.asList(1L, 2L));
|
||||
|
||||
Collection<Person> results = this.repository.findAllById(Arrays.asList(1L, 2L));
|
||||
|
||||
assertThat(results).isNotNull();
|
||||
assertThat(results).isEmpty();
|
||||
@@ -114,14 +129,15 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findAllWithIdsReturnsPartialMatches() {
|
||||
|
||||
Person kurt = new Person(1L, "Kurt", "Cobain");
|
||||
Person eddie = new Person(2L, "Eddie", "Veddar");
|
||||
Person michael = new Person(3L, "Michael", "Jackson");
|
||||
|
||||
template.put(kurt.getId(), kurt);
|
||||
template.put(eddie.getId(), eddie);
|
||||
this.template.put(kurt.getId(), kurt);
|
||||
this.template.put(eddie.getId(), eddie);
|
||||
|
||||
Collection<Person> results = repository.findAllById(Arrays.asList(0L, 1L, 2L, 4L));
|
||||
Collection<Person> results = this.repository.findAllById(Arrays.asList(0L, 1L, 2L, 4L));
|
||||
|
||||
assertThat(results).isNotNull();
|
||||
assertThat(results).hasSize(2);
|
||||
@@ -130,12 +146,13 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryRegion() throws Exception {
|
||||
public void queryRegion() {
|
||||
|
||||
Person oliverGierke = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
assertThat(template.put(oliverGierke.getId(), oliverGierke)).isNull();
|
||||
assertThat(this.template.put(oliverGierke.getId(), oliverGierke)).isNull();
|
||||
|
||||
SelectResults<Person> people = template.find("SELECT * FROM /People p WHERE p.firstname = $1",
|
||||
SelectResults<Person> people = this.template.find("SELECT * FROM /People p WHERE p.firstname = $1",
|
||||
oliverGierke.getFirstname());
|
||||
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -144,34 +161,36 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void saveAndDeleteEntity() {
|
||||
|
||||
Person oliverGierke = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
assertThat(repository.save(oliverGierke)).isEqualTo(oliverGierke);
|
||||
assertThat(repository.count()).isEqualTo(1L);
|
||||
assertThat(repository.findById(oliverGierke.getId()).orElse(null)).isEqualTo(oliverGierke);
|
||||
assertThat(repository.findAll()).isEqualTo(Collections.singletonList(oliverGierke));
|
||||
assertThat(this.repository.save(oliverGierke)).isEqualTo(oliverGierke);
|
||||
assertThat(this.repository.count()).isEqualTo(1L);
|
||||
assertThat(this.repository.findById(oliverGierke.getId()).orElse(null)).isEqualTo(oliverGierke);
|
||||
assertThat(this.repository.findAll()).isEqualTo(Collections.singletonList(oliverGierke));
|
||||
|
||||
repository.delete(oliverGierke);
|
||||
this.repository.delete(oliverGierke);
|
||||
|
||||
assertThat(repository.count()).isEqualTo(0L);
|
||||
assertThat(repository.findById(oliverGierke.getId()).orElse(null)).isNull();
|
||||
assertThat(repository.findAll()).isEmpty();
|
||||
assertThat(this.repository.count()).isEqualTo(0L);
|
||||
assertThat(this.repository.findById(oliverGierke.getId()).orElse(null)).isNull();
|
||||
assertThat(this.repository.findAll()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveEntities() {
|
||||
assertThat(template.getRegion()).isEmpty();
|
||||
|
||||
assertThat(this.template.getRegion()).isEmpty();
|
||||
|
||||
Person johnBlum = new Person(1L, "John", "Blum");
|
||||
Person jonBloom = new Person(2L, "Jon", "Bloom");
|
||||
Person juanBlume = new Person(3L, "Juan", "Blume");
|
||||
|
||||
repository.saveAll(Arrays.asList(johnBlum, jonBloom, juanBlume));
|
||||
this.repository.saveAll(Arrays.asList(johnBlum, jonBloom, juanBlume));
|
||||
|
||||
assertThat(template.getRegion().size()).isEqualTo(3);
|
||||
assertThat(template.<Long, Person>get(johnBlum.getId())).isEqualTo(johnBlum);
|
||||
assertThat(template.<Long, Person>get(jonBloom.getId())).isEqualTo(jonBloom);
|
||||
assertThat(template.<Long, Person>get(juanBlume.getId())).isEqualTo(juanBlume);
|
||||
assertThat(this.template.getRegion().size()).isEqualTo(3);
|
||||
assertThat((Person) this.template.get(johnBlum.getId())).isEqualTo(johnBlum);
|
||||
assertThat((Person) this.template.get(jonBloom.getId())).isEqualTo(jonBloom);
|
||||
assertThat((Person) this.template.get(juanBlume.getId())).isEqualTo(juanBlume);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -180,16 +199,17 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
volatile boolean eventFired;
|
||||
|
||||
@Override
|
||||
public void afterRegionClear(RegionEvent ev) {
|
||||
eventFired = true;
|
||||
public void afterRegionClear(RegionEvent event) {
|
||||
this.eventFired = true;
|
||||
}
|
||||
}
|
||||
|
||||
@PeerCacheApplication(name = "SimpleGemfireRepositoryIntegrationTests", logLevel = DEFAULT_GEMFIRE_LOG_LEVEL)
|
||||
@PeerCacheApplication(name = "SimpleGemfireRepositoryIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL)
|
||||
static class SimpleGemfireRepositoryConfiguration {
|
||||
|
||||
@Bean(name = "People")
|
||||
LocalRegionFactoryBean<Object, Object> peopleRegion(GemFireCache gemfireCache) {
|
||||
|
||||
LocalRegionFactoryBean<Object, Object> peopleRegion = new LocalRegionFactoryBean<>();
|
||||
|
||||
peopleRegion.setCache(gemfireCache);
|
||||
|
||||
@@ -35,9 +35,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.repository.GemfireRepository;
|
||||
import org.springframework.data.gemfire.repository.sample.Customer;
|
||||
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
@@ -73,22 +76,26 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
private Region customers;
|
||||
|
||||
static Customer createCustomer(String firstName, String lastName) {
|
||||
|
||||
Customer customer = new SerializableCustomer(firstName, lastName);
|
||||
|
||||
customer.setId(ID_SEQUENCE.incrementAndGet());
|
||||
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
assertNotNull("The 'Customers' GemFire Cache Region was not properly configured and initialized!", customers);
|
||||
assertEquals("Customers", customers.getName());
|
||||
assertEquals("/Customers", customers.getFullPath());
|
||||
assertTrue(customers.isEmpty());
|
||||
|
||||
assertNotNull("The 'Customers' Cache Region was not properly configured and initialized!", this.customers);
|
||||
assertEquals("Customers", this.customers.getName());
|
||||
assertEquals("/Customers", this.customers.getFullPath());
|
||||
assertTrue(this.customers.isEmpty());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
customers.clear();
|
||||
this.customers.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,24 +107,24 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
expectedCustomers.add(createCustomer("Pie", "Doe"));
|
||||
expectedCustomers.add(createCustomer("Cookie", "Doe"));
|
||||
|
||||
customerService.saveAll(expectedCustomers);
|
||||
this.customerService.saveAll(expectedCustomers);
|
||||
|
||||
assertFalse(customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), customers.size());
|
||||
assertFalse(this.customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), this.customers.size());
|
||||
|
||||
try {
|
||||
customerService.removeAllCausingTransactionRollback();
|
||||
this.customerService.removeAllCausingTransactionRollback();
|
||||
}
|
||||
catch (RuntimeException ignore) {
|
||||
// the RuntimeException should cause the Cache Transaction to rollback and avoid the Region modification!
|
||||
}
|
||||
|
||||
assertFalse(customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), customers.size());
|
||||
assertFalse(this.customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), this.customers.size());
|
||||
|
||||
customerService.removeAll();
|
||||
this.customerService.removeAll();
|
||||
|
||||
assertTrue(customers.isEmpty());
|
||||
assertTrue(this.customers.isEmpty());
|
||||
}
|
||||
|
||||
public static class SerializableCustomer extends Customer implements Serializable {
|
||||
@@ -141,34 +148,52 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("all")
|
||||
public CustomerService(GemfireTemplate customersTemplate, PlatformTransactionManager transactionManager) {
|
||||
customerRepository = new SimpleGemfireRepository<Customer, Long>(customersTemplate,
|
||||
new ReflectionEntityInformation<Customer, Long>(Customer.class));
|
||||
|
||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
GemfireMappingContext mappingContext = new GemfireMappingContext();
|
||||
|
||||
GemfirePersistentEntity<Customer> customerEntity =
|
||||
(GemfirePersistentEntity<Customer>) mappingContext.getPersistentEntity(Customer.class);
|
||||
|
||||
EntityInformation<Customer, Long> entityInformation = new PersistentEntityInformation<>(customerEntity);
|
||||
|
||||
this.customerRepository = new SimpleGemfireRepository<Customer, Long>(customersTemplate, entityInformation);
|
||||
this.transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
}
|
||||
|
||||
void saveAll(final Iterable<Customer> customers) {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
customerRepository.saveAll(customers);
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
CustomerService.this.customerRepository.saveAll(customers);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void removeAllCausingTransactionRollback() {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
|
||||
removeAll();
|
||||
|
||||
throw new IllegalStateException("'removeAll' operation not permitted");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
customerRepository.deleteAll();
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
CustomerService.this.customerRepository.deleteAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -52,6 +51,7 @@ import org.springframework.data.gemfire.repository.sample.User;
|
||||
import org.springframework.data.gemfire.support.sample.TestUserDao;
|
||||
import org.springframework.data.gemfire.support.sample.TestUserService;
|
||||
import org.springframework.data.gemfire.test.support.DataSourceAdapter;
|
||||
import org.springframework.data.gemfire.util.SpringUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -77,7 +77,7 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
private static final Object MUTEX_LOCK = new Object();
|
||||
|
||||
protected static final String GEMFIRE_LOCATORS = "localhost[11235]";
|
||||
protected static final String GEMFIRE_LOG_LEVEL = "warning";
|
||||
protected static final String GEMFIRE_LOG_LEVEL = "error";
|
||||
protected static final String GEMFIRE_JMX_MANAGER = "true";
|
||||
protected static final String GEMFIRE_JMX_MANAGER_PORT = "1199";
|
||||
protected static final String GEMFIRE_JMX_MANAGER_START = "true";
|
||||
@@ -87,56 +87,52 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
setupBeforeCacheCreate();
|
||||
}
|
||||
|
||||
private void setupBeforeCacheCreate() {
|
||||
try {
|
||||
long timeout = (System.currentTimeMillis() + CACHE_CLOSE_TIMEOUT);
|
||||
|
||||
long timeout = System.currentTimeMillis() + CACHE_CLOSE_TIMEOUT;
|
||||
|
||||
while (CacheFactory.getAnyInstance() != null && System.currentTimeMillis() < timeout) {
|
||||
synchronized (MUTEX_LOCK) {
|
||||
try {
|
||||
System.out.printf("Waiting in setup...%n");
|
||||
MUTEX_LOCK.wait(500l);
|
||||
}
|
||||
catch (InterruptedException ignore) {
|
||||
MUTEX_LOCK.wait(500L);
|
||||
}
|
||||
catch (InterruptedException ignore) { }
|
||||
}
|
||||
}
|
||||
|
||||
fail(String.format("The Cache instance was not properly closed in the allotted timeout of %1$d seconds!%n",
|
||||
(CACHE_CLOSE_TIMEOUT / 1000)));
|
||||
}
|
||||
catch (CacheClosedException ignore) {
|
||||
fail(String.format("The Cache instance was not properly closed in the allotted timeout of %d seconds%n",
|
||||
TimeUnit.MILLISECONDS.toSeconds(CACHE_CLOSE_TIMEOUT)));
|
||||
}
|
||||
catch (CacheClosedException ignore) { }
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext().close();
|
||||
UserDataStoreCacheLoader.INSTANCE.set(null);
|
||||
tearDownCache();
|
||||
}
|
||||
|
||||
private void tearDownCache() {
|
||||
|
||||
try {
|
||||
|
||||
Cache cache = CacheFactory.getAnyInstance();
|
||||
|
||||
if (cache != null) {
|
||||
System.out.printf("Closing Cache...%n");
|
||||
|
||||
cache.close();
|
||||
|
||||
// Now, wait for the GemFire Hog to shutdown, OIY!
|
||||
// Now, wait for the Apache Geode or Pivotal GemFire Hog to shutdown, OIY!
|
||||
synchronized (MUTEX_LOCK) {
|
||||
while (!cache.isClosed()) {
|
||||
try {
|
||||
System.out.printf("Waiting in tearDown...");
|
||||
MUTEX_LOCK.wait(500l);
|
||||
|
||||
SpringUtils.safeRunOperation(() -> {
|
||||
while (!cache.isClosed()) {
|
||||
MUTEX_LOCK.wait(500L);
|
||||
}
|
||||
catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
MUTEX_LOCK.notifyAll();
|
||||
}
|
||||
@@ -149,13 +145,12 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
protected void doSpringContextBootstrappingInitializationTest(final String cacheXmlFile) {
|
||||
protected void doSpringContextBootstrappingInitializationTest(String cacheXmlFile) {
|
||||
|
||||
Cache gemfireCache = new CacheFactory()
|
||||
.set("name", GEMFIRE_NAME)
|
||||
.set("mcast-port", GEMFIRE_MCAST_PORT)
|
||||
.set("log-level", GEMFIRE_LOG_LEVEL)
|
||||
.set("cache-xml-file", cacheXmlFile)
|
||||
//.set("locators", GEMFIRE_LOCATORS)
|
||||
//.set("start-locator", GEMFIRE_LOCATORS)
|
||||
//.set("jmx-manager", GEMFIRE_JMX_MANAGER)
|
||||
//.set("jmx-manager-port", GEMFIRE_JMX_MANAGER_PORT)
|
||||
@@ -173,12 +168,13 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
assertNotNull(gemfireCache.getRegion("/TestRegion"));
|
||||
assertNotNull(gemfireCache.getRegion("/Users"));
|
||||
|
||||
ConfigurableApplicationContext applicationContext = SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
ConfigurableApplicationContext applicationContext =
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
|
||||
assertNotNull(applicationContext);
|
||||
assertTrue(applicationContext.containsBean(GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
|
||||
assertTrue(applicationContext.containsBean("TestRegion"));
|
||||
assertFalse(applicationContext.containsBean("Users")); // Region 'Users' is defined in GemFire cache.xml
|
||||
assertFalse(applicationContext.containsBean("Users")); // Region 'Users' is defined in Pivotal GemFire cache.xml
|
||||
assertTrue(applicationContext.containsBean("userDataSource"));
|
||||
assertTrue(applicationContext.containsBean("userDao"));
|
||||
assertTrue(applicationContext.containsBean("userService"));
|
||||
@@ -190,7 +186,7 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
assertSame(userDataSource, userDao.getDataSource());
|
||||
assertSame(userDao, userService.getUserDao());
|
||||
|
||||
// NOTE a GemFire declared component initialized by Spring!
|
||||
// NOTE Pivotal GemFire declared component initialized by Spring!
|
||||
UserDataStoreCacheLoader usersCacheLoader = UserDataStoreCacheLoader.getInstance();
|
||||
|
||||
assertSame(userDataSource, usersCacheLoader.getDataSource());
|
||||
@@ -210,9 +206,10 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testSpringContextBootstrappingInitializerUsingAnnotatedClasses() {
|
||||
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfig.class);
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(new Properties());
|
||||
new SpringContextBootstrappingInitializer().init(null, new Properties());
|
||||
|
||||
ConfigurableApplicationContext applicationContext = SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
|
||||
@@ -231,7 +228,8 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testSpringContextBootstrappingInitializerUsingContextConfigLocations() {
|
||||
doSpringContextBootstrappingInitializationTest("cache-with-spring-context-bootstrap-initializer.xml");
|
||||
doSpringContextBootstrappingInitializationTest(
|
||||
"cache-with-spring-context-bootstrap-initializer.xml");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -248,14 +246,14 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
public static final class TestDataSource extends DataSourceAdapter {
|
||||
}
|
||||
public static final class TestDataSource extends DataSourceAdapter { }
|
||||
|
||||
public static final class UserDataStoreCacheLoader extends LazyWiringDeclarableSupport implements CacheLoader<String, User> {
|
||||
public static final class UserDataStoreCacheLoader extends LazyWiringDeclarableSupport
|
||||
implements CacheLoader<String, User> {
|
||||
|
||||
private static final AtomicReference<UserDataStoreCacheLoader> INSTANCE = new AtomicReference<UserDataStoreCacheLoader>();
|
||||
private static final AtomicReference<UserDataStoreCacheLoader> INSTANCE = new AtomicReference<>();
|
||||
|
||||
private static final Map<String, User> USER_DATA = new ConcurrentHashMap<String, User>(3);
|
||||
private static final Map<String, User> USER_DATA = new ConcurrentHashMap<>(3);
|
||||
|
||||
static {
|
||||
USER_DATA.put("jblum", new User("jblum"));
|
||||
@@ -279,10 +277,12 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
|
||||
protected static User createUser(String username, Boolean active, Calendar since, String email) {
|
||||
|
||||
User user = new User(username);
|
||||
user.setActive(active);
|
||||
user.setEmail(email);
|
||||
user.setSince(since);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -291,32 +291,35 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
|
||||
public UserDataStoreCacheLoader() {
|
||||
Assert.state(INSTANCE.compareAndSet(null, this), String.format("An instance of %1$s was already created!",
|
||||
getClass().getName()));
|
||||
Assert.state(INSTANCE.compareAndSet(null, this),
|
||||
String.format("An instance of %1$s was already created!", getClass().getName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertInitialized() {
|
||||
|
||||
super.assertInitialized();
|
||||
Assert.state(userDataSource != null, String.format(
|
||||
"The 'User' Data Source was not properly configured and initialized for use in (%1$s!)",
|
||||
|
||||
Assert.state(this.userDataSource != null,
|
||||
String.format("The 'User' Data Source was not properly configured and initialized for use in (%s)",
|
||||
getClass().getName()));
|
||||
}
|
||||
|
||||
protected DataSource getDataSource() {
|
||||
return userDataSource;
|
||||
return this.userDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
userDataSource = null;
|
||||
this.userDataSource = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User load(final LoaderHelper<String, User> helper) throws CacheLoaderException {
|
||||
public User load(LoaderHelper<String, User> helper) throws CacheLoaderException {
|
||||
|
||||
assertInitialized();
|
||||
|
||||
return USER_DATA.get(helper.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,22 +16,39 @@
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.geode.cache.Cache;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Matchers;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -64,11 +81,27 @@ import org.springframework.util.ObjectUtils;
|
||||
@SuppressWarnings("unused")
|
||||
public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
private static Properties createParameters(String parameter, String value) {
|
||||
|
||||
Properties parameters = new Properties();
|
||||
|
||||
parameters.setProperty(parameter, value);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private static Properties createParameters(Properties parameters, String parameter, String value) {
|
||||
|
||||
parameters.setProperty(parameter, value);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private Cache mockCache = mock(Cache.class);
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
SpringContextBootstrappingInitializer.applicationContext = null;
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = null;
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(null);
|
||||
@@ -76,21 +109,11 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
SpringContextBootstrappingInitializer.unregister(TestAppConfigTwo.class);
|
||||
}
|
||||
|
||||
protected static Properties createParameters(final String parameter, final String value) {
|
||||
Properties parameters = new Properties();
|
||||
parameters.setProperty(parameter, value);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
protected static Properties createParameters(final Properties parameters, final String parameter, final String value) {
|
||||
parameters.setProperty(parameter, value);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInitializedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testGetApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testGetApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
|
||||
@@ -98,25 +121,36 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getUninitializedApplicationContext() {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage("A Spring ApplicationContext was not configured and initialized properly");
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("A Spring ApplicationContext was not configured and initialized properly"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoader() {
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsInactive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(false);
|
||||
|
||||
@@ -126,114 +160,161 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsActive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(true);
|
||||
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("A Spring ApplicationContext has already been initialized");
|
||||
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("A Spring ApplicationContext has already been initialized"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void createApplicationContextWhenAnnotatedClassesBasePackagesAndConfigLocationsAreUnspecified() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createAnnotationBasedApplicationContextWithAnnotatedClasses() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
AnnotationConfigApplicationContext mockAnnotationApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockXmlApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[0]);
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[1]);
|
||||
Arrays.stream(annotatedClasses).forEach(SpringContextBootstrappingInitializer::register);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null, null);
|
||||
/*
|
||||
doAnswer(invocationOnMock ->
|
||||
ObjectUtils.isEmpty(invocationOnMock.getArgument(0))
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext
|
||||
).when(initializer).createApplicationContext(any(String[].class));
|
||||
*/
|
||||
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
doReturn(mockAnnotationApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).register(annotatedClasses[0], annotatedClasses[1]);
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(null, null);
|
||||
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockAnnotationApplicationContext)));
|
||||
|
||||
verify(initializer, times(1))
|
||||
.doRegister(eq(mockAnnotationApplicationContext), eq(annotatedClasses));
|
||||
|
||||
verifyZeroInteractions(mockXmlApplicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithBasePackages() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createAnnotationBasedApplicationContextWithBasePackages() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
AnnotationConfigApplicationContext mockAnnotationApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class,"MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockXmlApplicationContext");
|
||||
|
||||
String[] basePackages = { "org.example.app" };
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(basePackages, null);
|
||||
doReturn(mockAnnotationApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(basePackages, null);
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).scan(eq(basePackages[0]));
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockAnnotationApplicationContext)));
|
||||
|
||||
verify(initializer, times(1))
|
||||
.scanBasePackages(eq(mockAnnotationApplicationContext), eq(basePackages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createXmlApplicationContext() {
|
||||
final ConfigurableApplicationContext mockAnnotationApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createXmlBasedApplicationContext() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
ConfigurableApplicationContext mockAnnotationApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockXmlApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null,
|
||||
new String[] { "/path/to/application/context.xml" });
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(null, new String[] { "/path/to/application/context.xml" });
|
||||
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockXmlApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initApplicationContext() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
@@ -246,19 +327,28 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void initApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("ConfigurableApplicationContext must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("ConfigurableApplicationContext must not be null"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void refreshApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().refreshApplicationContext(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
@@ -266,100 +356,150 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void refreshApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("ConfigurableApplicationContext must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("ConfigurableApplicationContext must not be null"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClasses() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer()
|
||||
.registerAnnotatedClasses(mockApplicationContext, annotatedClasses),
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, annotatedClasses),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).register(annotatedClasses);
|
||||
verify(initializer, times(1))
|
||||
.doRegister(eq(mockApplicationContext), eq(annotatedClasses));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClassesWithEmptyAnnotatedClassesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[0]),
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, new Class<?>[0]),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).register(any(Class[].class));
|
||||
verify(initializer, never()).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClassesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[] { TestAppConfigOne.class }), is(sameInstance(mockApplicationContext)));
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, new Class<?>[] { TestAppConfigOne.class }),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(initializer, never()).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackages() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
String[] basePackages = { "org.example.app", "org.example.plugins" };
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, basePackages),
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, basePackages),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).scan(basePackages);
|
||||
verify(initializer, times(1)).doScan(eq(mockApplicationContext), eq(basePackages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackagesWithEmptyBasePackagesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, null),
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, null),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).scan(any(String[].class));
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackagesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext,
|
||||
new String[] { "org.example.app" }), is(sameInstance(mockApplicationContext)));
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, new String[] { "org.example.app" }),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
verify(mockApplicationContext, times(1))
|
||||
.setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoaderWithNonSettableClassLoaderApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
@@ -369,8 +509,9 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void setClassLoaderWithNullClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(null);
|
||||
|
||||
@@ -380,13 +521,6 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, never()).setClassLoader(any(ClassLoader.class));
|
||||
}
|
||||
|
||||
private Class<?>[] annotatedClasses(final Class<?>... annotatedClasses) {
|
||||
return argThat(argument -> {
|
||||
assertThat(argument instanceof Class<?>[], is(true));
|
||||
return Arrays.equals(annotatedClasses, (Class<?>[]) argument);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNullReference() {
|
||||
assertThat(new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(null), is(nullValue()));
|
||||
@@ -394,6 +528,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNonNullReference() {
|
||||
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("123");
|
||||
@@ -404,14 +539,13 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testInitWithAnnotatedClasses");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "testInitWithAnnotatedClasses");
|
||||
|
||||
doNothing().when(mockApplicationContext).addApplicationListener(any(ApplicationListener.class));
|
||||
doNothing().when(mockApplicationContext).registerShutdownHook();
|
||||
doNothing().when(mockApplicationContext).refresh();
|
||||
doNothing().when(mockApplicationContext).register(Matchers.<Class<?>[]>anyVararg());
|
||||
//doNothing().when(mockApplicationContext).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWithAnnotatedClasses");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(true);
|
||||
@@ -421,28 +555,33 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfigOne.class);
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfigTwo.class);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override protected ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
initializer.init(createParameters("test", "test"));
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
initializer.init(this.mockCache, createParameters("test", "test"));
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
verify(mockApplicationContext, times(1)).register(TestAppConfigOne.class, TestAppConfigTwo.class);
|
||||
//verify(mockApplicationContext, times(1)).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));
|
||||
//verify(mockApplicationContext, times(1)).register(Matchers.<Class<?>[]>anyVararg());
|
||||
verify(mockApplicationContext, never()).scan(any(String[].class));
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
verify(initializer, times(1)).doRegister(eq(mockApplicationContext), eq(new Class[] {
|
||||
TestAppConfigOne.class, TestAppConfigTwo.class }));
|
||||
|
||||
assertEquals(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWithExistingApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWithExistingApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWithExistingApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(true);
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWithExistingApplicationContext");
|
||||
@@ -453,7 +592,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();
|
||||
|
||||
initializer.init(createParameters("test", "test"));
|
||||
initializer.init(this.mockCache, createParameters("test", "test"));
|
||||
|
||||
verify(mockApplicationContext, never()).addApplicationListener(any(SpringContextBootstrappingInitializer.class));
|
||||
verify(mockApplicationContext, never()).registerShutdownHook();
|
||||
@@ -464,24 +603,29 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWhenApplicationContextIsNull() {
|
||||
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
|
||||
final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsNull");
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWhenApplicationContextIsNull");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsNull");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(true);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"/path/to/spring/application/context.xml"));
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"/path/to/spring/application/context.xml");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
@@ -492,8 +636,9 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWhenApplicationContextIsInactive() {
|
||||
ConfigurableApplicationContext mockInactiveApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsInactive.Inactive");
|
||||
|
||||
ConfigurableApplicationContext mockInactiveApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWhenApplicationContextIsInactive.Inactive");
|
||||
|
||||
when(mockInactiveApplicationContext.isActive()).thenReturn(false);
|
||||
|
||||
@@ -508,14 +653,16 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
when(mockNewApplicationContext.isRunning()).thenReturn(true);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockNewApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
initializer.init(this.mockCache, createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app"));
|
||||
|
||||
verify(mockNewApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
@@ -525,51 +672,68 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
assertSame(mockNewApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() throws Throwable {
|
||||
@Test(expected = ApplicationContextException.class)
|
||||
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() {
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
|
||||
expectedException.expect(ApplicationContextException.class);
|
||||
expectedException.expectCause(isA(IllegalArgumentException.class));
|
||||
expectedException.expectMessage(containsString("Failed to bootstrap the Spring ApplicationContext"));
|
||||
try {
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(createParameters(createParameters(
|
||||
Properties parameters = createParameters(createParameters(
|
||||
SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER, ""),
|
||||
SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, " "));
|
||||
SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, " ");
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(this.mockCache, parameters);
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertThat(expected.getMessage(), containsString("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertThat(expected.getCause(), is(instanceOf(IllegalArgumentException.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInitWhenApplicationContextIsNotRunning() {
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
|
||||
final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsNotRunning");
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"testInitWhenApplicationContextIsNotRunning");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsNotRunning");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(false);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app, org.example.plugins"));
|
||||
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app, org.example.plugins");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertTrue(expected.getCause() instanceof IllegalStateException);
|
||||
assertEquals("The Spring ApplicationContext (testInitWhenApplicationContextIsNotRunning) failed to be properly initialized with the context config files ([]) or base packages ([org.example.app, org.example.plugins])!",
|
||||
expected.getCause().getMessage());
|
||||
|
||||
throw (IllegalStateException) expected.getCause();
|
||||
}
|
||||
finally {
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
verify(mockApplicationContext, times(1)).refresh();
|
||||
@@ -578,16 +742,21 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInitLogsErrors() throws Throwable {
|
||||
final Log mockLog = mock(Log.class, "testInitLogsErrors.MockLog");
|
||||
|
||||
Log mockLog = mock(Log.class, "testInitLogsErrors.MockLog");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override protected Log initLogger() {
|
||||
|
||||
@Override
|
||||
protected Log initLogger() {
|
||||
return mockLog;
|
||||
}
|
||||
|
||||
@Override protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
throw new IllegalStateException("TEST");
|
||||
@@ -595,50 +764,60 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
};
|
||||
|
||||
try {
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"classpath/to/spring/application/context.xml"));
|
||||
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"classpath/to/spring/application/context.xml");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertTrue(expected.getCause() instanceof IllegalStateException);
|
||||
assertEquals("TEST", expected.getCause().getMessage());
|
||||
|
||||
throw expected.getCause();
|
||||
}
|
||||
finally {
|
||||
verify(mockLog, times(1)).error(eq("Failed to bootstrap the Spring ApplicationContext"),
|
||||
any(RuntimeException.class));
|
||||
verify(mockLog, times(1))
|
||||
.error(eq("Failed to bootstrap the Spring ApplicationContext"), any(RuntimeException.class));
|
||||
}
|
||||
}
|
||||
|
||||
protected static void assertNotified(TestApplicationListener listener, ApplicationContextEvent expectedEvent) {
|
||||
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(true));
|
||||
assertThat(listener.getActualEvent(), is(sameInstance(expectedEvent)));
|
||||
}
|
||||
|
||||
protected static void assertUnnotified(TestApplicationListener listener) {
|
||||
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(false));
|
||||
assertThat(listener.getActualEvent(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void onContextClosedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnContextClosedApplicationEvent");
|
||||
|
||||
TestApplicationListener testApplicationListener =
|
||||
new TestApplicationListener("testOnContextClosedApplicationEvent");
|
||||
|
||||
try {
|
||||
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent =
|
||||
mock(ContextRefreshedEvent.class,"MockContextRefreshedEvent");
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, isA(ContextRefreshedEvent.class));
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextClosedEvent.class,
|
||||
"MockContextClosedEvent"));
|
||||
new SpringContextBootstrappingInitializer()
|
||||
.onApplicationEvent(mock(ContextClosedEvent.class,"MockContextClosedEvent"));
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -51,18 +50,21 @@ public class SpringServerLauncherCacheProviderIntegrationTest {
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
System.clearProperty(gemfireName());
|
||||
SpringContextBootstrappingInitializer.getApplicationContext().close();
|
||||
GemfireUtils.closeClientCache();
|
||||
}
|
||||
|
||||
String gemfireName() {
|
||||
return (GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME);
|
||||
return GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCacheWithSpring() {
|
||||
|
||||
String springXmlLocation = getClass().getSimpleName() + "-context.xml";
|
||||
|
||||
ServerLauncher.Builder builder = new ServerLauncher.Builder();
|
||||
|
||||
builder.setSpringXmlLocation(springXmlLocation);
|
||||
@@ -70,6 +72,7 @@ public class SpringServerLauncherCacheProviderIntegrationTest {
|
||||
builder.setDisableDefaultServer(true);
|
||||
|
||||
ServerLauncher launcher = builder.build();
|
||||
|
||||
ServerState state = launcher.start();
|
||||
|
||||
assertThat(state.getStatus(), is(equalTo(Status.ONLINE)));
|
||||
|
||||
Reference in New Issue
Block a user