We need to have a different XML element name for PagedResources as it can be used as root element and JAXB cannot tell the difference between Resource and PagedResource otherwise.
This commit is contained in:
committed by
Oliver Gierke
parent
cb6dd51e04
commit
9c4d6029cf
@@ -29,7 +29,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@XmlRootElement(name = "entities")
|
||||
@XmlRootElement(name = "pagedEntities")
|
||||
public class PagedResources<T> extends Resources<T> {
|
||||
|
||||
public static PagedResources<?> NO_PAGE = new PagedResources<Object>();
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 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.hateoas;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* Test for marshalling / unmarshalling of {@link PagedResources}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class PagedResourcesMarshallingTest {
|
||||
|
||||
Marshaller marshaller;
|
||||
Unmarshaller unmarshaller;
|
||||
|
||||
String xmlReference;
|
||||
PagedResources<Inner> pagedResources;
|
||||
|
||||
public PagedResourcesMarshallingTest() throws IOException {
|
||||
xmlReference = readFile(new ClassPathResource("pagedresources.xml"));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
JAXBContext context = JAXBContext.newInstance(PagedResources.class);
|
||||
marshaller = context.createMarshaller();
|
||||
unmarshaller = context.createUnmarshaller();
|
||||
|
||||
pagedResources = new PagedResources<Inner>(new ArrayList<Inner>(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #98
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void jaxbUnMarshalling() throws Exception {
|
||||
|
||||
PagedResources<Inner> actual = (PagedResources<Inner>) unmarshaller.unmarshal(new StringReader(xmlReference));
|
||||
assertThat(actual, is(pagedResources));
|
||||
}
|
||||
|
||||
public static class Inner {
|
||||
|
||||
}
|
||||
|
||||
private static String readFile(org.springframework.core.io.Resource resource) throws IOException {
|
||||
|
||||
FileInputStream stream = new FileInputStream(resource.getFile());
|
||||
|
||||
try {
|
||||
FileChannel fc = stream.getChannel();
|
||||
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
|
||||
return Charset.defaultCharset().decode(bb).toString();
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/test/resources/pagedresources.xml
Normal file
1
src/test/resources/pagedresources.xml
Normal file
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><pagedEntities xmlns:atom="http://www.w3.org/2005/Atom"></pagedEntities>
|
||||
Reference in New Issue
Block a user