DATACMNS-810 - Polishing.

Renamed ExampleSpecification to ExampleMatcher and introduced a matching() factory method, dropping the dedicated override mechanism for types for now. Updated documentation accordingly.

Used Lombok for constructor creation, field defaults, toString() as well as equals(…) and hashCode() implementations.

Tweaked imports in unit tests. Renamed methods to express expected behavior.

Original pull request: #153.
This commit is contained in:
Oliver Gierke
2016-03-02 12:00:37 +01:00
parent b0ce7009e8
commit 26f725be0d
14 changed files with 806 additions and 1869 deletions

View File

@@ -0,0 +1,183 @@
/*
* Copyright 2016 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.domain;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.springframework.data.domain.ExampleMatcher.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.ExampleMatcher.NullHandler;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
/**
* Unit test for {@link ExampleMatcher}.
*
* @author Mark Paluch
* @soundtrack K2 - Der Berg Ruft (Club Mix)
*/
public class ExampleMatcherUnitTests {
ExampleMatcher matcher;
@Before
public void setUp() throws Exception {
matcher = matching();
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnDefault() throws Exception {
assertThat(matcher.getDefaultStringMatcher(), is(StringMatcher.DEFAULT));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnFalseByDefault() throws Exception {
assertThat(matcher.isIgnoreCaseEnabled(), is(false));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsIsEmptyByDefault() throws Exception {
assertThat(matcher.getIgnoredPaths(), is(empty()));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnoreByDefault() throws Exception {
assertThat(matcher.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test(expected = UnsupportedOperationException.class)
public void ignoredPathsIsNotModifiable() throws Exception {
matcher.getIgnoredPaths().add("¯\\_(ツ)_/¯");
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseEnabled() throws Exception {
matcher = matching().withIgnoreCase();
assertThat(matcher.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseSet() throws Exception {
matcher = matching().withIgnoreCase(true);
assertThat(matcher.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnInclude() throws Exception {
matcher = matching().withIncludeNullValues();
assertThat(matcher.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnore() throws Exception {
matcher = matching().withIgnoreNullValues();
assertThat(matcher.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnConfiguredValue() throws Exception {
matcher = matching().withNullHandler(NullHandler.INCLUDE);
assertThat(matcher.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnCorrectProperties() throws Exception {
matcher = matching().withIgnorePaths("foo", "bar", "baz");
assertThat(matcher.getIgnoredPaths(), contains("foo", "bar", "baz"));
assertThat(matcher.getIgnoredPaths(), hasSize(3));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnUniqueProperties() throws Exception {
matcher = matching().withIgnorePaths("foo", "bar", "foo");
assertThat(matcher.getIgnoredPaths(), contains("foo", "bar"));
assertThat(matcher.getIgnoredPaths(), hasSize(2));
}
/**
* @see DATACMNS-810
*/
@Test
public void withCreatesNewInstance() throws Exception {
matcher = matching().withIgnorePaths("foo", "bar", "foo");
ExampleMatcher configuredExampleSpec = matcher.withIgnoreCase();
assertThat(matcher, is(not(sameInstance(configuredExampleSpec))));
assertThat(matcher.getIgnoredPaths(), hasSize(2));
assertThat(matcher.isIgnoreCaseEnabled(), is(false));
assertThat(configuredExampleSpec.getIgnoredPaths(), hasSize(2));
assertThat(configuredExampleSpec.isIgnoreCaseEnabled(), is(true));
}
static class Person {
String firstname;
}
}

View File

@@ -1,220 +0,0 @@
/*
* Copyright 2016 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.domain;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.ExampleSpec.NullHandler;
import org.springframework.data.domain.ExampleSpec.StringMatcher;
/**
* Unit test for {@link ExampleSpec}.
*
* @author Mark Paluch
* @soundtrack K2 - Der Berg Ruft (Club Mix)
*/
public class ExampleSpecUnitTests {
ExampleSpec exampleSpec;
@Before
public void setUp() throws Exception {
exampleSpec = ExampleSpec.untyped();
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnDefault() throws Exception {
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.DEFAULT));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnContainingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.untyped().withStringMatcherContaining();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.CONTAINING));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnStartingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.untyped().withStringMatcherStarting();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.STARTING));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnEndingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.untyped().withStringMatcherEnding();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.ENDING));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnFalseByDefault() throws Exception {
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(false));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsIsEmptyByDefault() throws Exception {
assertThat(exampleSpec.getIgnoredPaths(), is(empty()));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnoreByDefault() throws Exception {
assertThat(exampleSpec.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test(expected = UnsupportedOperationException.class)
public void ignoredPathsIsNotModifiable() throws Exception {
exampleSpec.getIgnoredPaths().add("¯\\_(ツ)_/¯");
}
/**
* @see DATACMNS-810
*/
@Test(expected = IllegalArgumentException.class)
public void defaultExampleSpecWithoutTypeFails() throws Exception {
ExampleSpec.typed(null);
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseEnabled() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnoreCase();
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseSet() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnoreCase(true);
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnInclude() throws Exception {
exampleSpec = ExampleSpec.untyped().withIncludeNullValues();
assertThat(exampleSpec.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnore() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnoreNullValues();
assertThat(exampleSpec.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnConfiguredValue() throws Exception {
exampleSpec = ExampleSpec.untyped().withNullHandler(NullHandler.INCLUDE);
assertThat(exampleSpec.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnCorrectProperties() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnorePaths("foo", "bar", "baz");
assertThat(exampleSpec.getIgnoredPaths(), contains("foo", "bar", "baz"));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(3));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnUniqueProperties() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnorePaths("foo", "bar", "foo");
assertThat(exampleSpec.getIgnoredPaths(), contains("foo", "bar"));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(2));
}
/**
* @see DATACMNS-810
*/
@Test
public void withCreatesNewInstance() throws Exception {
exampleSpec = ExampleSpec.untyped().withIgnorePaths("foo", "bar", "foo");
ExampleSpec configuredExampleSpec = exampleSpec.withIgnoreCase();
assertThat(exampleSpec, is(not(sameInstance(configuredExampleSpec))));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(2));
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(false));
assertThat(configuredExampleSpec.getIgnoredPaths(), hasSize(2));
assertThat(configuredExampleSpec.isIgnoreCaseEnabled(), is(true));
}
static class Person {
String firstname;
}
}

View File

@@ -15,9 +15,8 @@
*/
package org.springframework.data.domain;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.Example.*;
import org.junit.Before;
import org.junit.Test;
@@ -27,11 +26,12 @@ import org.junit.Test;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Oliver Gierke
*/
public class ExampleUnitTests {
private Person person;
private Example<Person> example;
Person person;
Example<Person> example;
@Before
public void setUp() {
@@ -39,14 +39,14 @@ public class ExampleUnitTests {
person = new Person();
person.firstname = "rand";
example = of(person);
example = Example.of(person);
}
/**
* @see DATACMNS-810
*/
@Test(expected = IllegalArgumentException.class)
public void exampleOfNullThrowsException() {
public void rejectsNullProbe() {
Example.of(null);
}
@@ -54,44 +54,11 @@ public class ExampleUnitTests {
* @see DATACMNS-810
*/
@Test
public void getSampleTypeRetunsSampleObjectsClass() {
assertThat(example.getProbeType(), equalTo(Person.class));
}
/**
* @see DATACMNS-810
*/
@Test
public void createTypedExample() {
Example<Person> example = of(new Person(), ExampleSpec.typed(Contact.class));
assertThat(example.getProbeType(), equalTo(Person.class));
assertThat(example.getResultType(), equalTo((Class) Contact.class));
assertThat(example.getProbeType(), equalTo(Person.class));
}
/**
* @see DATACMNS-810
*/
@Test
public void createUntypedExample() {
Example<Person> example = of(new Person(), ExampleSpec.untyped());
assertThat(example.getProbeType(), equalTo(Person.class));
assertThat(example.getResultType(), equalTo((Class) Person.class));
assertThat(example.getProbeType(), equalTo(Person.class));
}
static class Contact {
String label;
public void retunsSampleObjectsClassAsProbeType() {
assertThat(example.getProbeType(), is(equalTo(Person.class)));
}
static class Person {
String firstname;
}
}

View File

@@ -1,231 +0,0 @@
/*
* Copyright 2016 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.domain;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.ExampleSpec.NullHandler;
import org.springframework.data.domain.ExampleSpec.StringMatcher;
/**
* Unit test for {@link ExampleSpec}.
*
* @author Mark Paluch
* @soundtrack Harmonic Rush - The Dark Side of Persia (ahmed romel remix)
*/
public class TypedExampleSpecUnitTests {
TypedExampleSpec<Person> exampleSpec;
@Before
public void setUp() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class);
}
/**
* @see DATACMNS-810
*/
@Test
public void typeShouldReturnConfiguredType() throws Exception {
assertThat(exampleSpec.getType(), equalTo(Person.class));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnDefault() throws Exception {
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.DEFAULT));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnContainingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withStringMatcherContaining();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.CONTAINING));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnStartingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withStringMatcherStarting();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.STARTING));
}
/**
* @see DATACMNS-810
*/
@Test
public void defaultStringMatcherShouldReturnEndingWhenConfigured() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withStringMatcherEnding();
assertThat(exampleSpec.getDefaultStringMatcher(), is(StringMatcher.ENDING));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnFalseByDefault() throws Exception {
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(false));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsIsEmptyByDefault() throws Exception {
assertThat(exampleSpec.getIgnoredPaths(), is(empty()));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnoreByDefault() throws Exception {
assertThat(exampleSpec.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test(expected = UnsupportedOperationException.class)
public void ignoredPathsIsNotModifiable() throws Exception {
exampleSpec.getIgnoredPaths().add("¯\\_(ツ)_/¯");
}
/**
* @see DATACMNS-810
*/
@Test(expected = IllegalArgumentException.class)
public void defaultExampleSpecWithoutTypeFails() throws Exception {
ExampleSpec.typed(null);
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseEnabled() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnoreCase();
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoreCaseShouldReturnTrueWhenIgnoreCaseSet() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnoreCase(true);
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(true));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnInclude() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIncludeNullValues();
assertThat(exampleSpec.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnIgnore() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnoreNullValues();
assertThat(exampleSpec.getNullHandler(), is(NullHandler.IGNORE));
}
/**
* @see DATACMNS-810
*/
@Test
public void nullHandlerShouldReturnConfiguredValue() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withNullHandler(NullHandler.INCLUDE);
assertThat(exampleSpec.getNullHandler(), is(NullHandler.INCLUDE));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnCorrectProperties() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnorePaths("foo", "bar", "baz");
assertThat(exampleSpec.getIgnoredPaths(), contains("foo", "bar", "baz"));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(3));
}
/**
* @see DATACMNS-810
*/
@Test
public void ignoredPathsShouldReturnUniqueProperties() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnorePaths("foo", "bar", "foo");
assertThat(exampleSpec.getIgnoredPaths(), contains("foo", "bar"));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(2));
}
/**
* @see DATACMNS-810
*/
@Test
public void withCreatesNewInstance() throws Exception {
exampleSpec = ExampleSpec.typed(Person.class).withIgnorePaths("foo", "bar", "foo");
TypedExampleSpec<Person> configuredExampleSpec = exampleSpec.withIgnoreCase();
assertThat(exampleSpec, is(not(sameInstance(configuredExampleSpec))));
assertThat(exampleSpec.getIgnoredPaths(), hasSize(2));
assertThat(exampleSpec.isIgnoreCaseEnabled(), is(false));
assertThat(configuredExampleSpec.getIgnoredPaths(), hasSize(2));
assertThat(configuredExampleSpec.isIgnoreCaseEnabled(), is(true));
}
static class Person {
String firstname;
}
}