DATACMNS-266 - Use new common Maven build infrastructure.

Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
Oliver Gierke
2013-01-11 12:13:47 +01:00
parent c908d0e023
commit ac256f9921
375 changed files with 215 additions and 3092 deletions

View File

@@ -0,0 +1,27 @@
package org.springframework.data.domain;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
/**
* Unit test for {@link Direction}.
*
* @author Oliver Gierke
*/
public class DirectionUnitTests {
@Test
public void jpaValueMapping() throws Exception {
assertEquals(Direction.ASC, Direction.fromString("asc"));
assertEquals(Direction.DESC, Direction.fromString("desc"));
}
@Test(expected = IllegalArgumentException.class)
public void rejectsInvalidString() throws Exception {
Direction.fromString("foo");
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2008-2010 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.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.UnitTestUtils.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
/**
* Unit test for {@link PageImpl}.
*
* @author Oliver Gierke
*/
public class PageImplUnitTests {
@Test
public void assertEqualsForSimpleSetup() throws Exception {
PageImpl<String> page = new PageImpl<String>(Arrays.asList("Foo"));
assertEqualsAndHashcode(page, page);
assertEqualsAndHashcode(page, new PageImpl<String>(Arrays.asList("Foo")));
}
@Test
public void assertEqualsForComplexSetup() throws Exception {
Pageable pageable = new PageRequest(0, 10);
List<String> content = Arrays.asList("Foo");
PageImpl<String> page = new PageImpl<String>(content, pageable, 100);
assertEqualsAndHashcode(page, page);
assertEqualsAndHashcode(page, new PageImpl<String>(content, pageable, 100));
assertNotEqualsAndHashcode(page, new PageImpl<String>(content, pageable, 90));
assertNotEqualsAndHashcode(page, new PageImpl<String>(content, new PageRequest(1, 10), 100));
assertNotEqualsAndHashcode(page, new PageImpl<String>(content, new PageRequest(0, 15), 100));
}
@Test(expected = IllegalArgumentException.class)
public void preventsNullContentForSimpleSetup() throws Exception {
new PageImpl<Object>(null);
}
@Test(expected = IllegalArgumentException.class)
public void preventsNullContentForAdvancedSetup() throws Exception {
new PageImpl<Object>(null, null, 0);
}
@Test
public void createsPageForEmptyContentCorrectly() {
List<String> list = Collections.emptyList();
Page<String> page = new PageImpl<String>(list);
assertThat(page.getContent(), is(list));
assertThat(page.getNumber(), is(0));
assertThat(page.getNumberOfElements(), is(0));
assertThat(page.getSize(), is(0));
assertThat(page.getSort(), is((Sort) null));
assertThat(page.getTotalElements(), is(0L));
assertThat(page.getTotalPages(), is(0));
assertThat(page.hasNextPage(), is(false));
assertThat(page.hasPreviousPage(), is(false));
assertThat(page.isFirstPage(), is(true));
assertThat(page.isLastPage(), is(true));
assertThat(page.hasContent(), is(false));
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2008-2010 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.springframework.data.domain.UnitTestUtils.*;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
/**
* Unit test for {@link PageRequest}.
*
* @author Oliver Gierke
*/
public class PageRequestUnitTests {
@Test(expected = IllegalArgumentException.class)
public void preventsNegativePage() {
new PageRequest(-1, 10);
}
@Test(expected = IllegalArgumentException.class)
public void preventsNegativeSize() {
new PageRequest(0, -1);
}
@Test(expected = IllegalArgumentException.class)
public void preventsZeroSize() {
new PageRequest(0, 0);
}
@Test
public void equalsRegardsSortCorrectly() {
Sort sort = new Sort(Direction.DESC, "foo");
PageRequest request = new PageRequest(0, 10, sort);
// Equals itself
assertEqualsAndHashcode(request, request);
// Equals another instance with same setup
assertEqualsAndHashcode(request, new PageRequest(0, 10, sort));
// Equals without sort entirely
assertEqualsAndHashcode(new PageRequest(0, 10), new PageRequest(0, 10));
// Is not equal to instance without sort
assertNotEqualsAndHashcode(request, new PageRequest(0, 10));
// Is not equal to instance with another sort
assertNotEqualsAndHashcode(request, new PageRequest(0, 10, Direction.ASC, "foo"));
}
@Test
public void equalsHonoursPageAndSize() {
PageRequest request = new PageRequest(0, 10);
// Equals itself
assertEqualsAndHashcode(request, request);
// Equals same setup
assertEqualsAndHashcode(request, new PageRequest(0, 10));
// Does not equal on different page
assertNotEqualsAndHashcode(request, new PageRequest(1, 10));
// Does not equal on different size
assertNotEqualsAndHashcode(request, new PageRequest(0, 11));
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2008-2010 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.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
/**
* Unit test for {@link Sort}.
*
* @author Oliver Gierke
*/
public class SortUnitTests {
/**
* Asserts that the class applies the default sort order if no order or {@code null} was provided.
*
* @throws Exception
*/
@Test
public void appliesDefaultForOrder() throws Exception {
assertEquals(Sort.DEFAULT_DIRECTION, new Sort("foo").iterator().next().getDirection());
assertEquals(Sort.DEFAULT_DIRECTION, new Sort((Direction) null, "foo").iterator().next().getDirection());
}
/**
* Asserts that the class rejects {@code null} as properties array.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void preventsNullProperties() throws Exception {
new Sort(Direction.ASC, (String[]) null);
}
/**
* Asserts that the class rejects {@code null} values in the properties array.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void preventsNullProperty() throws Exception {
new Sort(Direction.ASC, (String) null);
}
/**
* Asserts that the class rejects empty strings in the properties array.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void preventsEmptyProperty() throws Exception {
new Sort(Direction.ASC, "");
}
/**
* Asserts that the class rejects no properties given at all.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void preventsNoProperties() throws Exception {
new Sort(Direction.ASC);
}
@Test
public void allowsCombiningSorts() {
Sort sort = new Sort("foo").and(new Sort("bar"));
assertThat(sort, hasItems(new Sort.Order("foo"), new Sort.Order("bar")));
}
@Test
public void handlesAdditionalNullSort() {
Sort sort = new Sort("foo").and(null);
assertThat(sort, hasItem(new Sort.Order("foo")));
}
}

View File

@@ -0,0 +1,39 @@
package org.springframework.data.domain;
import static org.junit.Assert.*;
/**
* @author Oliver Gierke
*/
public abstract class UnitTestUtils {
private UnitTestUtils() {
}
/**
* Asserts that delivered objects both equal each other as well as return the same hash code.
*
* @param first
* @param second
*/
public static void assertEqualsAndHashcode(Object first, Object second) {
assertEquals(first, second);
assertEquals(second, first);
assertEquals(first.hashCode(), second.hashCode());
}
/**
* Asserts that both objects are not equal to each other and differ in hash code, too.
*
* @param first
* @param second
*/
public static void assertNotEqualsAndHashcode(Object first, Object second) {
assertFalse(first.equals(second));
assertFalse(second.equals(first));
assertFalse(first.hashCode() == second.hashCode());
}
}

View File

@@ -0,0 +1,160 @@
/*
* Copyright 2012 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.jaxb;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.hateoas.Link;
/**
* Unit test for custom JAXB conversions for Spring Data value objects.
*
* @author Oliver Gierke
*/
public class SpringDataJaxbUnitTests {
Marshaller marshaller;
Unmarshaller unmarshaller;
Sort sort = new Sort(Direction.ASC, "firstname", "lastname");
Pageable reference = new PageRequest(2, 15, sort);
Resource resource = new ClassPathResource("pageable.xml", this.getClass());
Resource schemaFile = new ClassPathResource("spring-data-jaxb.xsd", this.getClass());
Scanner scanner;
@Before
public void setUp() throws Exception {
JAXBContext context = JAXBContext.newInstance("org.springframework.data.domain.jaxb");
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
unmarshaller = context.createUnmarshaller();
scanner = new Scanner(resource.getFile());
}
@After
public void tearDown() {
scanner.close();
}
@Test
public void usesCustomTypeAdapterForPageRequests() throws Exception {
StringWriter writer = new StringWriter();
Wrapper wrapper = new Wrapper();
wrapper.pageable = reference;
wrapper.sort = sort;
wrapper.pageableWithoutSort = new PageRequest(10, 20);
marshaller.marshal(wrapper, writer);
for (String line : writer.toString().split("\n")) {
assertThat(scanner.hasNextLine(), is(true));
assertThat(line, is(scanner.nextLine()));
}
}
@Test
public void readsPageRequest() throws Exception {
Object result = unmarshaller.unmarshal(resource.getFile());
assertThat(result, is(instanceOf(Wrapper.class)));
assertThat(((Wrapper) result).pageable, is(reference));
assertThat(((Wrapper) result).sort, is(sort));
}
@Test
public void writesPlainPage() throws Exception {
PageWrapper wrapper = new PageWrapper();
Content content = new Content();
content.name = "Foo";
wrapper.page = new PageImpl<Content>(Arrays.asList(content));
wrapper.pageWithLinks = new PageImpl<Content>(Arrays.asList(content));
marshaller.marshal(wrapper, new StringWriter());
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class Wrapper {
@XmlElement(name = "page-request", namespace = SpringDataJaxb.NAMESPACE)
Pageable pageable;
@XmlElement(name = "page-request-without-sort", namespace = SpringDataJaxb.NAMESPACE)
Pageable pageableWithoutSort;
@XmlElement(name = "sort", namespace = SpringDataJaxb.NAMESPACE)
Sort sort;
}
@XmlRootElement(name = "wrapper", namespace = SpringDataJaxb.NAMESPACE)
static class PageWrapper {
Page<Content> page;
@XmlElement(name = "page-with-links")
@XmlJavaTypeAdapter(LinkedPageAdapter.class)
Page<Content> pageWithLinks;
}
@XmlRootElement
static class Content {
@XmlAttribute
String name;
}
static class LinkedPageAdapter extends PageAdapter {
@Override
protected List<Link> getLinks(Page<?> source) {
return Arrays.asList(new Link(Link.REL_NEXT, "next"), new Link(Link.REL_PREVIOUS, "previous"));
}
}
}