SGF-583 - Provide InterestBuilder class to appropriately and flexibly express interests in keys/values between client/server.

(cherry picked from commit 734fab75b0a33f3fa80dc95ca3f6dceb132e78a9)
Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2017-01-05 20:54:59 -08:00
parent fb53e13858
commit fa00bd6844
16 changed files with 1831 additions and 1461 deletions

View File

@@ -1,104 +0,0 @@
/*
* Copyright 2010-2013 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 org.springframework.data.gemfire.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.apache.geode.cache.InterestResultPolicy;
import org.junit.Test;
import org.springframework.core.ConstantException;
/**
* The InterestTest class is a test suite of test cases testing the contract and functionality of the Interest class.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.client.Interest
* @since 1.6.0
*/
public class InterestTest {
@Test
public void testConstruct() {
Interest<String> interest = new Interest<String>("aKey", "keys", true, false);
assertEquals("aKey", interest.getKey());
assertEquals(InterestResultPolicy.KEYS, interest.getPolicy());
assertTrue(interest.isDurable());
assertFalse(interest.isReceiveValues());
}
@Test(expected = IllegalArgumentException.class)
public void testConstructWithNullKey() {
try {
new Interest<Object>(null);
}
catch (IllegalArgumentException expected) {
assertEquals("a non-null key is required", expected.getMessage());
throw expected;
}
}
@Test(expected = ConstantException.class)
public void testConstructWithInvalidPolicy() {
new Interest<String>("aKey", "INVALID");
}
@Test
public void testSetAndGetState() {
Interest<String> interest = new Interest();
assertNull(interest.getKey());
assertEquals(InterestResultPolicy.DEFAULT, interest.getPolicy());
assertFalse(interest.isDurable());
assertTrue(interest.isReceiveValues());
interest.setDurable(true);
interest.setKey("testKey");
interest.setPolicy(InterestResultPolicy.KEYS_VALUES);
interest.setReceiveValues(false);
assertEquals("testKey", interest.getKey());
assertEquals(InterestResultPolicy.KEYS_VALUES, interest.getPolicy());
assertTrue(interest.isDurable());
assertFalse(interest.isReceiveValues());
}
@Test
public void testSetAndGetPolicy() {
Interest<Object> interest = new Interest<Object>();
assertEquals(InterestResultPolicy.DEFAULT, interest.getPolicy());
interest.setPolicy(InterestResultPolicy.NONE);
assertEquals(InterestResultPolicy.NONE, interest.getPolicy());
interest.setPolicy("keys");
assertEquals(InterestResultPolicy.KEYS, interest.getPolicy());
}
@Test(expected = ConstantException.class)
public void testSetPolicyWithIllegalValue() {
new Interest<Object>().setPolicy("ILLEGAL");
}
}

View File

@@ -0,0 +1,353 @@
/*
* Copyright 2010-2013 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 org.springframework.data.gemfire.client;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.springframework.data.gemfire.client.Interest.Type.KEY;
import static org.springframework.data.gemfire.client.Interest.Type.REGEX;
import static org.springframework.data.gemfire.client.Interest.newInterest;
import java.util.List;
import org.apache.geode.cache.InterestResultPolicy;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.ConstantException;
/**
* Unit tests for {@link Interest}.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.InterestResultPolicy
* @see org.springframework.data.gemfire.client.Interest
* @since 1.6.0
*/
public class InterestUnitTests {
protected static final boolean DURABLE = true;
protected static final boolean DO_NOT_RECEIVE_VALUES = false;
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void constructInterestWithKey() {
Interest<String> interest = new Interest<>("testKey");
assertThat(interest.getKey()).isEqualTo("testKey");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS_VALUES);
assertThat(interest.isDurable()).isFalse();
assertThat(interest.isReceiveValues()).isTrue();
assertThat(interest.getType()).isEqualTo(KEY);
String expectedString = String.format(
"{ @type = %s, key = testKey, durable = false, policy = KEYS_VALUES, receiveValues = true, type = KEY }",
interest.getClass().getName());
assertThat(interest.toString()).isEqualTo(expectedString);
}
@Test
public void constructInterestWithKeyAndPolicy() {
Interest<String> interest = new Interest<>("mockKey", InterestResultPolicy.KEYS);
assertThat(interest.getKey()).isEqualTo("mockKey");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS);
assertThat(interest.isDurable()).isFalse();
assertThat(interest.isReceiveValues()).isTrue();
assertThat(interest.getType()).isEqualTo(KEY);
String expectedString = String.format(
"{ @type = %s, key = mockKey, durable = false, policy = KEYS, receiveValues = true, type = KEY }",
interest.getClass().getName());
assertThat(interest.toString()).isEqualTo(expectedString);
}
@Test
public void constructInterestWithKeyPolicyAndDurability() {
Interest<String> interest = new Interest<>(".*Key", InterestResultPolicy.NONE, DURABLE);
assertThat(interest.getKey()).isEqualTo(".*Key");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.NONE);
assertThat(interest.isDurable()).isTrue();
assertThat(interest.isReceiveValues()).isTrue();
assertThat(interest.getType()).isEqualTo(REGEX);
String expectedString = String.format(
"{ @type = %s, key = .*Key, durable = true, policy = NONE, receiveValues = true, type = REGEX }",
interest.getClass().getName());
assertThat(interest.toString()).isEqualTo(expectedString);
}
@Test
public void constructInterestWithKeyPolicyDurabilityAndReceiveValues() {
List<String> keys = asList("KeyOne", "KeyTwo", "KeyThree");
Interest<Object> interest = new Interest<>(keys, InterestResultPolicy.KEYS_VALUES,
DURABLE, DO_NOT_RECEIVE_VALUES);
assertThat(interest.getKey()).isEqualTo(keys);
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS_VALUES);
assertThat(interest.isDurable()).isTrue();
assertThat(interest.isReceiveValues()).isFalse();
assertThat(interest.getType()).isEqualTo(Interest.Type.KEY);
String expectedString = String.format(
"{ @type = %s, key = [KeyOne, KeyTwo, KeyThree], durable = true, policy = KEYS_VALUES, receiveValues = false, type = KEY }",
interest.getClass().getName());
assertThat(interest.toString()).isEqualTo(expectedString);
}
@Test
public void constructInterestWithNullKey() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Key is required");
new Interest<>(null);
}
@Test
@SuppressWarnings("deprecation")
public void constructInterestWithStringPolicy() {
Interest<String> interest = new Interest<>("mockKey", "nOnE");
assertThat(interest.getKey()).isEqualTo("mockKey");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.NONE);
assertThat(interest.getType()).isEqualTo(KEY);
}
@Test(expected = ConstantException.class)
@SuppressWarnings("deprecation")
public void constructInterestWithInvalidStringPolicy() {
new Interest<>("testKey", "INVALID");
}
@Test
public void isAlphanumericWhitespace() {
Interest<?> interest = newInterest("key");
assertThat(interest.isAlphaNumericWhitespace('a')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('X')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('0')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('1')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('2')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('4')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('8')).isTrue();
assertThat(interest.isAlphaNumericWhitespace('9')).isTrue();
assertThat(interest.isAlphaNumericWhitespace(' ')).isTrue();
}
@Test
public void isNonAlphanumericWhitespace() {
Interest<?> interest = newInterest("key");
assertThat(interest.isNotAlphaNumericWhitespace('@')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('$')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('.')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('_')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('-')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('+')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('*')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('?')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('\\')).isTrue();
assertThat(interest.isNotAlphaNumericWhitespace('[')).isTrue();
}
@Test
public void containsNonAlphanumericWhitespace() {
Interest<?> interest = newInterest("key");
assertThat(interest.containsNonAlphaNumericWhitespace(".*")).isTrue();
assertThat(interest.containsNonAlphaNumericWhitespace(".*Key")).isTrue();
assertThat(interest.containsNonAlphaNumericWhitespace("\\d")).isTrue();
assertThat(interest.containsNonAlphaNumericWhitespace("\\s")).isTrue();
assertThat(interest.containsNonAlphaNumericWhitespace("p\\{Alnum}")).isTrue();
assertThat(interest.containsNonAlphaNumericWhitespace("p\\{Space}")).isTrue();
}
@Test
public void containsOnlyAlphanumericWhitespace() {
Interest<?> interest = newInterest("key");
assertThat(interest.containsNonAlphaNumericWhitespace("0")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("123")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("123 456")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("key")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("keyOne")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("Key One")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("key0")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("key1")).isFalse();
assertThat(interest.containsNonAlphaNumericWhitespace("key123")).isFalse();
}
@Test
public void isRegularExpression() {
Interest<?> interest = newInterest("key");
assertThat(interest.isRegularExpression(".?")).isTrue();
assertThat(interest.isRegularExpression(".+")).isTrue();
assertThat(interest.isRegularExpression(".*")).isTrue();
assertThat(interest.isRegularExpression(".*Key")).isTrue();
assertThat(interest.isRegularExpression("\\d")).isTrue();
assertThat(interest.isRegularExpression("\\n")).isTrue();
assertThat(interest.isRegularExpression("\\s")).isTrue();
assertThat(interest.isRegularExpression("p\\{Alnum}")).isTrue();
assertThat(interest.isRegularExpression("p\\{Space}")).isTrue();
assertThat(interest.isRegularExpression("^abc$")).isTrue();
assertThat(interest.isRegularExpression(" [abc] ")).isTrue();
assertThat(interest.isRegularExpression("a{0,}bc")).isTrue();
assertThat(interest.isRegularExpression("a{1,10} bc*")).isTrue();
}
@Test
public void isNotRegularExpression() {
Interest<?> interest = newInterest("key");
assertThat(interest.isRegularExpression("abc")).isFalse();
assertThat(interest.isRegularExpression("123")).isFalse();
assertThat(interest.isRegularExpression("abc123")).isFalse();
assertThat(interest.isRegularExpression(" abc 123 ")).isFalse();
assertThat(interest.isRegularExpression("lOlO")).isFalse();
assertThat(interest.isRegularExpression((Object) "ALL_KEYS")).isFalse();
assertThat(interest.isRegularExpression(asList("a", "b", "c"))).isFalse();
}
@Test
public void resolveTypeIsCorrect() {
assertThat(newInterest(".*").resolveType(KEY)).isEqualTo(KEY);
assertThat(newInterest("key").resolveType(REGEX)).isEqualTo(REGEX);
assertThat(newInterest("key").resolveType(null)).isEqualTo(KEY);
assertThat(newInterest(asList("a", "b", "c")).resolveType(null)).isEqualTo(KEY);
assertThat(newInterest(".*").resolveType(null)).isEqualTo(REGEX);
}
@Test
@SuppressWarnings("unchecked")
public void setAndGetStateIsCorrect() {
Interest<String> interest = newInterest("key");
assertThat(interest.isDurable()).isFalse();
assertThat(interest.getKey()).isEqualTo("key");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.DEFAULT);
assertThat(interest.isReceiveValues()).isTrue();
assertThat(interest.getType()).isEqualTo(KEY);
interest.setDurable(true);
interest.setKey("testKey");
interest.setPolicy(InterestResultPolicy.KEYS);
interest.setReceiveValues(false);
interest.setType(Interest.Type.REGEX);
assertThat(interest.isDurable()).isTrue();
assertThat(interest.getKey()).isEqualTo("testKey");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS);
assertThat(interest.isReceiveValues()).isFalse();
assertThat(interest.getType()).isEqualTo(REGEX);
}
@Test
public void setAndGetPolicy() {
Interest<?> interest = newInterest("key");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.DEFAULT);
interest.setPolicy(InterestResultPolicy.NONE);
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.NONE);
interest.setPolicy("keys");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS);
}
@Test(expected = ConstantException.class)
public void setPolicyWithIllegalValueThrowsException() {
newInterest("key").setPolicy("ILLEGAL");
}
@Test
public void isKeyTypeIsCorrect() {
Interest<?> interest = newInterest("key");
assertThat(interest.getType()).isEqualTo(KEY);
assertThat(interest.isKeyType()).isTrue();
interest.setType(REGEX);
assertThat(interest.getType()).isEqualTo(REGEX);
assertThat(interest.isKeyType()).isFalse();
interest.setType(KEY);
assertThat(interest.getType()).isEqualTo(KEY);
assertThat(interest.isKeyType()).isTrue();
}
@Test
public void isRegexTypeIsCorrect() {
Interest<?> interest = newInterest("key");
assertThat(interest.getType()).isEqualTo(KEY);
assertThat(interest.isRegexType()).isFalse();
interest.setType(KEY);
assertThat(interest.getType()).isEqualTo(KEY);
assertThat(interest.isRegexType()).isFalse();
interest.setType(REGEX);
assertThat(interest.getType()).isEqualTo(REGEX);
assertThat(interest.isRegexType()).isTrue();
}
@Test
public void newInterestWithBuilder() {
Interest<?> interest = newInterest(".*").makeDurable().receivesValues(false)
.usingPolicy(InterestResultPolicy.KEYS);
assertThat(interest).isNotNull();
assertThat(interest.isDurable()).isTrue();
assertThat(interest.getKey()).isEqualTo(".*");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.KEYS);
assertThat(interest.isReceiveValues()).isFalse();
assertThat(interest.getType()).isEqualTo(REGEX);
}
@Test
@SuppressWarnings("unchecked")
public void newInterestWithBuilderHonorsType() {
Interest<?> interest = newInterest(".*").asType(KEY).withKey("^.+Key\\p{Digit}$")
.usingPolicy(InterestResultPolicy.NONE);
assertThat(interest).isNotNull();
assertThat(interest.isDurable()).isFalse();
assertThat(interest.getKey()).isEqualTo("^.+Key\\p{Digit}$");
assertThat(interest.getPolicy()).isEqualTo(InterestResultPolicy.NONE);
assertThat(interest.isReceiveValues()).isTrue();
assertThat(interest.getType()).isEqualTo(KEY);
}
}

View File

@@ -260,11 +260,11 @@ public class ClientRegionNamespaceTest {
public void testClientRegionWithRegisteredInterests() throws Exception {
assertTrue(context.containsBean("client-with-interests"));
ClientRegionFactoryBean factory = context.getBean("&client-with-interests", ClientRegionFactoryBean.class);
ClientRegionFactoryBean factoryBean = context.getBean("&client-with-interests", ClientRegionFactoryBean.class);
assertNotNull(factory);
assertNotNull(factoryBean);
Interest<?>[] interests = TestUtils.readField("interests", factory);
Interest<?>[] interests = TestUtils.readField("interests", factoryBean);
assertNotNull(interests);
assertEquals(2, interests.length);

View File

@@ -275,7 +275,7 @@ public class CollectionUtilsUnitTests {
assertThat(iterable.iterator().hasNext()).isFalse();
}
@Test(expected = UnsupportedOperationException.class)
@Test(expected = IllegalStateException.class)
public void nullSafeIterableIterator() {
Iterable<Object> iterable = CollectionUtils.nullSafeIterable(null);
@@ -290,16 +290,10 @@ public class CollectionUtilsUnitTests {
iterator.next();
}
catch (NoSuchElementException ignore) {
assertThat(ignore.getMessage()).isEqualTo("No more elements");
assertThat(ignore.getCause()).isNull();
try {
iterator.remove();
}
catch (UnsupportedOperationException expected) {
assertThat(expected.getMessage()).isEqualTo("Operation not supported");
assertThat(expected.getCause()).isNull();
catch (IllegalStateException expected) {
throw expected;
}
}