Consistent Map/Set ordering

Use LinkedHashMaps/Sets wherever exposed to users, and code tests defensively in terms of expected Map/Set ordering. Otherwise, there'll be runtime order differences between JDK 7 and JDK 8 due to internal HashMap/Set implementation differences.

Issue: SPR-9639
(cherry picked from commit 9c09a0a)
This commit is contained in:
Juergen Hoeller
2013-04-23 13:53:09 +02:00
committed by unknown
parent d0513f7521
commit 5bdd2d245c
18 changed files with 145 additions and 195 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-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.
@@ -249,7 +249,7 @@ public class SimpleMappingExceptionResolverTests {
exceptionResolver.setMappedHandlers(Collections.singleton(handler1));
exceptionResolver.setExceptionMappings(props);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException);
assertEquals("error", mav.getViewName());
assertEquals("another-error", mav.getViewName());
}
@Test

View File

@@ -1,79 +0,0 @@
/*
* Copyright 2002-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.web.servlet.view.document;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import junit.framework.TestCase;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
/**
* @author Alef Arendsen
* @author Juergen Hoeller
*/
public class PdfViewTests extends TestCase {
public void testPdf() throws Exception {
final String text = "this should be in the PDF";
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
AbstractPdfView pdfView = new AbstractPdfView() {
@Override
protected void buildPdfDocument(Map model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
document.add(new Paragraph(text));
}
};
pdfView.render(new HashMap(), request, response);
byte[] pdfContent = response.getContentAsByteArray();
assertEquals("correct response content type", "application/pdf", response.getContentType());
assertEquals("correct response content length", pdfContent.length, response.getContentLength());
// rebuild iText document for comparison
Document document = new Document(PageSize.A4);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setViewerPreferences(PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage);
document.open();
document.add(new Paragraph(text));
document.close();
byte[] baosContent = baos.toByteArray();
assertEquals("correct size", pdfContent.length, baosContent.length);
int diffCount = 0;
for (int i = 0; i < pdfContent.length; i++) {
if (pdfContent[i] != baosContent[i]) {
diffCount++;
}
}
assertTrue("difference only in encryption", diffCount < 70);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright ${YEAR} the original author or authors.
* Copyright 2002-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.
@@ -17,8 +17,8 @@
package org.springframework.web.servlet.view.feed;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -27,16 +27,20 @@ import javax.servlet.http.HttpServletResponse;
import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
public class AtomFeedViewTest {
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.custommonkey.xmlunit.XMLUnit.*;
import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
*/
public class AtomFeedViewTests {
private AbstractAtomFeedView view;
@@ -51,9 +55,9 @@ public class AtomFeedViewTest {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
Map<String, String> model = new HashMap<String, String>();
model.put("1", "This is entry 1");
Map<String, String> model = new LinkedHashMap<String, String>();
model.put("2", "This is entry 2");
model.put("1", "This is entry 1");
view.render(model, request, response);
assertEquals("Invalid content-type", "application/atom+xml", response.getContentType());

View File

@@ -17,8 +17,8 @@
package org.springframework.web.servlet.view.feed;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -27,16 +27,20 @@ import javax.servlet.http.HttpServletResponse;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Item;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.custommonkey.xmlunit.XMLUnit.setIgnoreWhitespace;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
public class RssFeedViewTest {
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.custommonkey.xmlunit.XMLUnit.*;
import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
*/
public class RssFeedViewTests {
private AbstractRssFeedView view;
@@ -52,9 +56,9 @@ public class RssFeedViewTest {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
Map<String, String> model = new HashMap<String, String>();
model.put("1", "This is entry 1");
Map<String, String> model = new LinkedHashMap<String, String>();
model.put("2", "This is entry 2");
model.put("1", "This is entry 1");
view.render(model, request, response);
assertEquals("Invalid content-type", "application/rss+xml", response.getContentType());