#98, #97 - Fixed XML element name for PagedResources to allow deserialization.

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:
ericbottard
2013-07-15 11:11:06 +02:00
committed by Oliver Gierke
parent cb6dd51e04
commit 9c4d6029cf
3 changed files with 93 additions and 1 deletions

View File

@@ -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>();

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><pagedEntities xmlns:atom="http://www.w3.org/2005/Atom"></pagedEntities>