1 16 17 package org.springframework.test.web; 18 19 import java.util.Collections ; 20 import java.util.Comparator ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import junit.framework.TestCase; 28 29 import org.springframework.web.servlet.ModelAndView; 30 31 40 public abstract class AbstractModelAndViewTests extends TestCase { 41 42 45 protected void assertModelAttributeAvailable(ModelAndView mav, Object key) { 46 assertNotNull("Model is null", mav.getModel()); 47 assertTrue("Model attribute with name '" + key + "' is not available", 48 mav.getModel().containsKey(key)); 49 } 50 51 54 protected void assertCompareListModelAttribute(ModelAndView mav, Object key, List assertionList) { 55 List modelList = (List )assertAndReturnModelAttributeOfType(mav, key, List .class); 56 assertEquals("Size of model list is '" + modelList.size() + 57 "' while size of assertion list is '" + assertionList.size() + "'", 58 assertionList.size(), modelList.size()); 59 assertEquals("List in model under key '" + key + "' is not equals to given list", assertionList, modelList); 60 } 61 62 68 protected void assertSortAndCompareListModelAttribute( 69 ModelAndView mav, Object key, List assertionList, Comparator comp) { 70 71 List modelList = (List )assertAndReturnModelAttributeOfType(mav, key, List .class); 72 assertEquals("Size of model list is '" + modelList.size() + 73 "' while size of assertion list is '" + assertionList.size() + "'", 74 assertionList.size(), modelList.size()); 75 76 if (comp != null) { 77 Collections.sort(modelList, comp); 78 Collections.sort(assertionList, comp); 79 } 80 else { 81 Collections.sort(modelList); 82 Collections.sort(assertionList); 83 } 84 assertEquals("List in model under key '" + key + "' is not equals to given list", assertionList, modelList); 85 } 86 87 92 protected Object assertAndReturnModelAttributeOfType(ModelAndView mav, Object key, Class type) { 93 assertNotNull("Model is null", mav.getModel()); 94 assertNotNull("Model attribute with key '" + key + "' is null", mav.getModel().get(key)); 95 Object obj = mav.getModel().get(key); 96 assertTrue("Model attribute is not of type '" + type.getName() + "' but is a '" + 97 obj.getClass().getName() + "'", type.isAssignableFrom(obj.getClass())); 98 return obj; 99 } 100 101 104 protected void assertViewName(ModelAndView mav, String name) { 105 assertEquals("View name is not equal to '" + name + "' but was '" + mav.getViewName() + "'", 106 name, mav.getViewName()); 107 } 108 109 112 protected void assertModelAttributeValue(ModelAndView mav, Object key, Object value) { 113 Object modelValue = assertAndReturnModelAttributeOfType(mav, key, Object .class); 114 assertEquals("Model value with key '" + key + "' is not the same as given value which was '" + value + "'", 115 value, modelValue); 116 } 117 118 121 protected void assertModelAttributeValues(ModelAndView mav, Map assertionModel) { 122 assertNotNull(mav.getModel()); 123 124 if (!mav.getModel().keySet().equals(assertionModel.keySet())) { 125 StringBuffer buf = new StringBuffer ("Keyset of given model does not match.\n"); 126 appendNonMatchingSetsErrorMessage(assertionModel.keySet(), mav.getModel().keySet(), buf); 127 fail(buf.toString()); 128 } 129 130 StringBuffer buf = new StringBuffer (); 131 Iterator it = mav.getModel().keySet().iterator(); 132 while (it.hasNext()) { 133 Object key = (Object ) it.next(); 134 Object assertionValue = assertionModel.get(key); 135 Object mavValue = mav.getModel().get(key); 136 if (!assertionValue.equals(mavValue)) { 137 buf.append("Value under key '" + key + "' differs, should have been '" + 138 assertionValue + "' but was '" + mavValue +"'\n"); 139 } 140 } 141 142 if (buf.length() != 0) { 143 buf.insert(0, "Values of given model do not match.\n"); 144 fail(buf.toString()); 145 } 146 } 147 148 private void appendNonMatchingSetsErrorMessage(Set assertionSet, Set incorrectSet, StringBuffer buf) { 149 Set tempSet = new HashSet (); 150 tempSet.addAll(incorrectSet); 151 tempSet.removeAll(assertionSet); 152 153 if (tempSet.size() > 0) { 154 buf.append("Set has too many elements:\n"); 155 Iterator it = tempSet.iterator(); 156 while (it.hasNext()) { 157 Object o = (Object ) it.next(); 158 buf.append('-'); 159 buf.append(o.toString()); 160 buf.append('\n'); 161 } 162 } 163 164 tempSet = new HashSet (); 165 tempSet.addAll(assertionSet); 166 tempSet.removeAll(incorrectSet); 167 168 if (tempSet.size() > 0) { 169 buf.append("Set is missing elements:\n"); 170 Iterator it = tempSet.iterator(); 171 while (it.hasNext()) { 172 Object o = (Object ) it.next(); 173 buf.append('-'); 174 buf.append(o.toString()); 175 buf.append('\n'); 176 } 177 } 178 } 179 180 } 181 | Popular Tags |