DATAREST-93 - Fixed formatting in Spring Data REST.
Added formatter to be used within Eclipse going forward.
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
/*
|
||||
* Copyright 2012-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.rest.convert;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -16,48 +32,53 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||
|
||||
/**
|
||||
* Tests to ensure the {@link DelegatingConversionService} properly delegates conversions to the {@link
|
||||
* org.springframework.core.convert.ConversionService} that is appropriate for the given source and return types.
|
||||
*
|
||||
* Tests to ensure the {@link DelegatingConversionService} properly delegates conversions to the
|
||||
* {@link org.springframework.core.convert.ConversionService} that is appropriate for the given source and return types.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DelegatingConversionServiceUnitTests {
|
||||
|
||||
private static final UUID RANDOM_UUID = UUID.fromString("9deccfd7-f892-4e26-a4d5-c92893392e78");
|
||||
private static final UUID RANDOM_UUID = UUID.fromString("9deccfd7-f892-4e26-a4d5-c92893392e78");
|
||||
|
||||
@Mock ConversionService conversionService;
|
||||
DelegatingConversionService delegatingConversionService;
|
||||
@Mock ConversionService conversionService;
|
||||
DelegatingConversionService delegatingConversionService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
DefaultFormattingConversionService cs = new DefaultFormattingConversionService(false);
|
||||
cs.addConverter(UUIDConverter.INSTANCE);
|
||||
DefaultFormattingConversionService cs = new DefaultFormattingConversionService(false);
|
||||
cs.addConverter(UUIDConverter.INSTANCE);
|
||||
|
||||
delegatingConversionService = new DelegatingConversionService(conversionService, cs);
|
||||
|
||||
when(conversionService.canConvert(String.class, UUID.class)).thenReturn(false);
|
||||
when(conversionService.canConvert(UUID.class, String.class)).thenReturn(false);
|
||||
}
|
||||
delegatingConversionService = new DelegatingConversionService(conversionService, cs);
|
||||
|
||||
@Test
|
||||
public void shouldDelegateToProperConversionService() throws Exception {
|
||||
assertThat(delegatingConversionService.canConvert(String.class, UUID.class), is(true));
|
||||
assertThat(delegatingConversionService.convert(RANDOM_UUID.toString(), UUID.class), is(RANDOM_UUID));
|
||||
verifyConversionService();
|
||||
}
|
||||
when(conversionService.canConvert(String.class, UUID.class)).thenReturn(false);
|
||||
when(conversionService.canConvert(UUID.class, String.class)).thenReturn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUUIDToString() throws Exception {
|
||||
assertThat(delegatingConversionService.canConvert(UUID.class, String.class), is(true));
|
||||
assertThat(delegatingConversionService.convert(RANDOM_UUID, String.class), is(RANDOM_UUID.toString()));
|
||||
verifyConversionService();
|
||||
}
|
||||
@Test
|
||||
public void shouldDelegateToProperConversionService() {
|
||||
|
||||
private void verifyConversionService() {
|
||||
verify(conversionService, times(0)).convert(Matchers.any(String.class), eq(UUID.class));
|
||||
verify(conversionService, times(0)).convert(Matchers.any(UUID.class), eq(String.class));
|
||||
}
|
||||
assertThat(delegatingConversionService.canConvert(String.class, UUID.class), is(true));
|
||||
assertThat(delegatingConversionService.convert(RANDOM_UUID.toString(), UUID.class), is(RANDOM_UUID));
|
||||
|
||||
verifyConversionService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUUIDToString() {
|
||||
|
||||
assertThat(delegatingConversionService.canConvert(UUID.class, String.class), is(true));
|
||||
assertThat(delegatingConversionService.convert(RANDOM_UUID, String.class), is(RANDOM_UUID.toString()));
|
||||
|
||||
verifyConversionService();
|
||||
}
|
||||
|
||||
private void verifyConversionService() {
|
||||
|
||||
verify(conversionService, times(0)).convert(Matchers.any(String.class), eq(UUID.class));
|
||||
verify(conversionService, times(0)).convert(Matchers.any(UUID.class), eq(String.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,92 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-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.rest.core.util;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests to verify that {@link UriUtils} can manipulate {@link URI}s.
|
||||
*
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class UriUtilsUnitTests {
|
||||
|
||||
private static final String BASE_URI_STR = "http://localhost:8080/data";
|
||||
private static final URI BASE_URI = URI.create(BASE_URI_STR);
|
||||
private static final String BASE_URI_STR = "http://localhost:8080/data";
|
||||
private static final URI BASE_URI = URI.create(BASE_URI_STR);
|
||||
|
||||
private static final String PERSON_2LVL_STR = BASE_URI_STR + "/person/1";
|
||||
private static final URI PERSON_2LVL_URI = URI.create(PERSON_2LVL_STR);
|
||||
private static final String PERSON_2LVL_STR = BASE_URI_STR + "/person/1";
|
||||
private static final URI PERSON_2LVL_URI = URI.create(PERSON_2LVL_STR);
|
||||
|
||||
@Test
|
||||
public void shouldValidateBaseURI() throws Exception {
|
||||
URI uri = new URI(BASE_URI + "/person/1");
|
||||
|
||||
assertThat(UriUtils.validBaseUri(BASE_URI, uri), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldIterateOverPathElements() throws Exception {
|
||||
final List<String> paths = new ArrayList<String>();
|
||||
Function<URI, Void> fn = new Function<URI, Void>() {
|
||||
@Override public Void apply(URI uri) {
|
||||
paths.add(uri.getPath());
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
UriUtils.foreach(BASE_URI, PERSON_2LVL_URI, fn);
|
||||
|
||||
assertThat(paths, hasSize(2));
|
||||
assertThat(paths, contains("person", "1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExplodeRelativeURI() throws Exception {
|
||||
Stack<URI> uris = UriUtils.explode(BASE_URI, PERSON_2LVL_URI);
|
||||
|
||||
assertThat(uris, hasSize(2));
|
||||
assertThat(uris, contains(URI.create("person"), URI.create("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMergeDifferentURIsIntoOne() throws Exception {
|
||||
String qrystr = "?queryParam=testValue";
|
||||
|
||||
URI uriWithQuery = URI.create(qrystr);
|
||||
URI uriWithPath = URI.create("person/1");
|
||||
|
||||
URI uri = UriUtils.merge(BASE_URI, uriWithPath, uriWithQuery);
|
||||
|
||||
assertThat(uri.toString(), is(PERSON_2LVL_STR + qrystr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStripTrailingSlashFromPath() throws Exception {
|
||||
URI uri = URI.create("person/");
|
||||
|
||||
String path = UriUtils.path(uri);
|
||||
|
||||
assertThat(path, is("person"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStripTheLastPathSegmentFromAURI() throws Exception {
|
||||
URI uri = UriUtils.tail(BASE_URI, PERSON_2LVL_URI);
|
||||
|
||||
assertThat(uri, is(URI.create("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBuildURIFromPathSegments() throws Exception {
|
||||
URI uri = UriUtils.buildUri(BASE_URI, "person", "1");
|
||||
|
||||
assertThat(uri, is(PERSON_2LVL_URI));
|
||||
}
|
||||
@Test
|
||||
public void shouldBuildURIFromPathSegments() throws Exception {
|
||||
|
||||
URI uri = UriUtils.buildUri(BASE_URI, "person", "1");
|
||||
assertThat(uri, is(PERSON_2LVL_URI));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user