use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class VersionResourceResolverTests method resolveResourceSuccess.
@Test
public void resolveResourceSuccess() throws Exception {
String versionFile = "bar-version.css";
String version = "version";
String file = "bar.css";
Resource expected = new ClassPathResource("test/" + file, getClass());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/resources/bar-version.css");
given(this.chain.resolveResource(request, versionFile, this.locations)).willReturn(null);
given(this.chain.resolveResource(request, file, this.locations)).willReturn(expected);
given(this.versionStrategy.extractVersion(versionFile)).willReturn(version);
given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file);
given(this.versionStrategy.getResourceVersion(expected)).willReturn(version);
this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy));
Resource actual = this.resolver.resolveResourceInternal(request, versionFile, this.locations, this.chain);
assertEquals(expected.getFilename(), actual.getFilename());
verify(this.versionStrategy, times(1)).getResourceVersion(expected);
assertThat(actual, instanceOf(HttpResource.class));
assertEquals("\"" + version + "\"", ((HttpResource) actual).getResponseHeaders().getETag());
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class WebJarsResourceResolverTests method resolveResourceWebJar.
@Test
public void resolveResourceWebJar() {
Resource expected = mock(Resource.class);
String file = "underscorejs/underscore.js";
String expectedPath = "underscorejs/1.8.3/underscore.js";
this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass()));
given(this.chain.resolveResource(this.request, expectedPath, this.locations)).willReturn(expected);
Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain);
assertEquals(expected, actual);
verify(this.chain, times(1)).resolveResource(this.request, file, this.locations);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class ViewResolverTests method testXmlViewResolver.
@Test
public void testXmlViewResolver() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("testBean", TestBean.class);
wac.setServletContext(new MockServletContext());
wac.refresh();
TestBean testBean = (TestBean) wac.getBean("testBean");
XmlViewResolver vr = new XmlViewResolver();
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
vr.setApplicationContext(wac);
View view1 = vr.resolveViewName("example1", Locale.getDefault());
assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
View view2 = vr.resolveViewName("example2", Locale.getDefault());
assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
ServletContext sc = new MockServletContext();
Map model = new HashMap();
TestBean tb = new TestBean();
model.put("tb", tb);
HttpServletRequest request = new MockHttpServletRequest(sc);
HttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view1.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
request = new MockHttpServletRequest(sc);
response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
view2.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method fetchMacro.
private String fetchMacro(String name) throws Exception {
ClassPathResource resource = new ClassPathResource("test.ftl", getClass());
assertTrue(resource.exists());
String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
all = all.replace("\r\n", "\n");
String[] macros = StringUtils.delimitedListToStringArray(all, "\n\n");
for (String macro : macros) {
if (macro.startsWith(name)) {
return macro.substring(macro.indexOf("\n")).trim();
}
}
return null;
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class XsltViewTests method testWithSourceKey.
@Test
public void testWithSourceKey() throws Exception {
XsltView view = getXsltView(HTML_OUTPUT);
view.setSourceKey("actualData");
Map<String, Object> model = new HashMap<>();
model.put("actualData", getProductDataResource());
model.put("otherData", new ClassPathResource("dummyData.xsl", getClass()));
view.render(model, this.request, this.response);
assertHtmlOutput(this.response.getContentAsString());
}
Aggregations