Search in sources :

Example 86 with ClassPathResource

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());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 87 with ClassPathResource

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);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 88 with ClassPathResource

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")));
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) TestBean(org.springframework.tests.sample.beans.TestBean) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 89 with ClassPathResource

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 90 with ClassPathResource

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());
}
Also used : HashMap(java.util.HashMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathResource (org.springframework.core.io.ClassPathResource)628 Test (org.junit.Test)404 Resource (org.springframework.core.io.Resource)182 DhisSpringTest (org.hisp.dhis.DhisSpringTest)85 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)80 List (java.util.List)63 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)62 Before (org.junit.Before)55 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)54 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)39 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)36 ArrayList (java.util.ArrayList)35 DataElement (org.hisp.dhis.dataelement.DataElement)32 File (java.io.File)29 UrlResource (org.springframework.core.io.UrlResource)29 TestBean (org.springframework.tests.sample.beans.TestBean)27 IOException (java.io.IOException)26 InputStream (java.io.InputStream)23 FileSystemResource (org.springframework.core.io.FileSystemResource)23 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19