1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.Locale ; 20 import java.util.Map ; 21 import java.util.MissingResourceException ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import junit.framework.TestCase; 27 28 import org.springframework.beans.factory.BeanIsAbstractException; 29 import org.springframework.core.io.Resource; 30 import org.springframework.mock.web.MockServletContext; 31 import org.springframework.web.context.support.ServletContextResource; 32 import org.springframework.web.context.support.StaticWebApplicationContext; 33 import org.springframework.web.servlet.View; 34 35 39 public class ResourceBundleViewResolverTests extends TestCase { 40 41 42 private static String PROPS_FILE = "org.springframework.web.servlet.view.testviews"; 43 44 private ResourceBundleViewResolver rb; 45 46 private StaticWebApplicationContext wac; 47 48 public ResourceBundleViewResolverTests() { 49 rb = new ResourceBundleViewResolver(); 50 rb.setBasename(PROPS_FILE); 51 rb.setCache(getCache()); 52 rb.setDefaultParentView("testParent"); 53 54 wac = new StaticWebApplicationContext(); 55 wac.setServletContext(new MockServletContext()); 56 wac.refresh(); 57 58 rb.setApplicationContext(wac); 60 } 61 62 66 protected boolean getCache() { 67 return true; 68 } 69 70 public void testParentsAreAbstract() throws Exception { 71 try { 72 View v = rb.resolveViewName("debug.Parent", Locale.ENGLISH); 73 fail("Should have thrown BeanIsAbstractException"); 74 } 75 catch (BeanIsAbstractException ex) { 76 } 78 try { 79 View v = rb.resolveViewName("testParent", Locale.ENGLISH); 80 fail("Should have thrown BeanIsAbstractException"); 81 } 82 catch (BeanIsAbstractException ex) { 83 } 85 } 86 87 public void testDebugViewEnglish() throws Exception { 88 View v = rb.resolveViewName("debugView", Locale.ENGLISH); 89 assertTrue("debugView must be of type InternalResourceView", v instanceof InternalResourceView); 90 InternalResourceView jv = (InternalResourceView) v; 91 assertTrue("debugView must have correct URL", "jsp/debug/debug.jsp".equals(jv.getUrl())); 92 93 Map m = jv.getStaticAttributes(); 94 assertTrue("Must have 2 static attributes, not " + m.size(), m.size() == 2); 95 assertTrue("attribute foo = bar, not '" + m.get("foo") + "'", m.get("foo").equals("bar")); 96 assertTrue("attribute postcode = SE10 9JY", m.get("postcode").equals("SE10 9JY")); 97 98 assertTrue("Correct default content type", jv.getContentType().equals("text/html; charset=ISO-8859-1")); 99 assertTrue("WebAppContext was set on view", jv.getApplicationContext() != null); 100 assertTrue("WebAppContext was correct", jv.getApplicationContext().getParent().equals(wac)); 101 } 102 103 public void testDebugViewFrench() throws Exception { 104 View v = rb.resolveViewName("debugView", Locale.FRENCH); 105 assertTrue("French debugView must be of type InternalResourceView", v instanceof InternalResourceView); 106 InternalResourceView jv = (InternalResourceView) v; 107 assertTrue("French debugView must have correct URL", "jsp/debug/deboug.jsp".equals(jv.getUrl())); 108 assertTrue( 109 "Correct overridden (XML) content type, not '" + jv.getContentType() + "'", 110 jv.getContentType().equals("text/xml; charset=ISO-8859-1")); 111 } 112 113 public void testNoSuchViewEnglish() throws Exception { 114 View v = rb.resolveViewName("xxxxxxweorqiwuopeir", Locale.ENGLISH); 115 assertTrue(v == null); 116 } 117 118 public void testOnSetContextCalledOnce() throws Exception { 119 TestView tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); 120 tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); 121 tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); 122 assertTrue("test has correct name", "test".equals(tv.getBeanName())); 123 assertTrue("test should have been initialized once, not " + tv.initCount + " times", tv.initCount == 1); 124 } 125 126 public void testNoSuchBasename() throws Exception { 127 try { 128 rb.setBasename("weoriwoierqupowiuer"); 129 View v = rb.resolveViewName("debugView", Locale.ENGLISH); 130 fail("No such basename: all requests should fail with exception"); 131 } 132 catch (MissingResourceException ex) { 133 } 135 } 136 137 138 public static class TestView extends AbstractView { 139 140 public int initCount; 141 142 public void setLocation(Resource location) { 143 if (!(location instanceof ServletContextResource)) { 144 throw new IllegalArgumentException ("Expecting ServletContextResource, not " + location.getClass().getName()); 145 } 146 } 147 148 protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) { 149 } 150 151 protected void initApplicationContext() { 152 ++initCount; 153 } 154 } 155 156 } 157 | Popular Tags |