Deprecated as of 4.3.13, in favor of selective use of ClassPathResource.ClassPathResource(String, ClassLoader) vs ClassPathResource.ClassPathResource(String, Class)
@Test public void multipart() throws Exception { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("name 1", "value 1"); parts.add("name 2", "value 2+1"); parts.add("name 2", "value 2+2"); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("logo", logo); HttpEntity<MultiValueMap<String, Object>> requestBody = new HttpEntity<>(parts); Future<URI> future = template.postForLocation(baseUrl + "/multipart", requestBody); future.get(); }
@Test public void getDescriptionWithClassLoaderConstructor() { assertDescriptionContainsExpectedPath( new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()), FQ_RESOURCE_PATH); }
@Test public void getDescriptionWithClassLiteralConstructorAndLeadingSlash() { assertDescriptionContainsExpectedPath( new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass()), FQ_RESOURCE_PATH); }
@Test(expected = ConversionFailedException.class) public void nothingInCommon() throws Exception { List<Object> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(3); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Override protected ObjectNamingStrategy getStrategy() throws Exception { KeyNamingStrategy strat = new KeyNamingStrategy(); strat.setMappingLocation(new ClassPathResource("jmx-names.properties", getClass())); strat.afterPropertiesSet(); return strat; }
@Test public void resolveFromClasspathRoot() { Resource location = new ClassPathResource("/"); String requestPath = "org/springframework/web/servlet/resource/test/bar.css"; Resource actual = this.resolver.resolveResource(null, requestPath, Collections.singletonList(location), null); assertNotNull(actual); }
@Before public void loadBeans() { new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( new ClassPathResource("collections.xml", getClass())); }
@Test public void loadIndexNoMatch() { CandidateComponentsIndex index = CandidateComponentsIndexLoader.loadIndex( CandidateComponentsTestClassLoader.index(getClass().getClassLoader(), new ClassPathResource("spring.components", getClass()))); Set<String> components = index.getCandidateTypes("org.springframework", "none"); assertThat(components, hasSize(0)); }
@Test public void resolveFromClasspath() throws IOException { Resource location = new ClassPathResource("test/", PathResourceResolver.class); String requestPath = "bar.css"; Resource actual = this.resolver.resolveResource(null, requestPath, Collections.singletonList(location), null); assertEquals(location.createRelative(requestPath), actual); }
@Before public void createFilter() { VersionResourceResolver versionResolver = new VersionResourceResolver(); versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy())); PathResourceResolver pathResolver = new PathResourceResolver(); pathResolver.setAllowedLocations(new ClassPathResource("test/", getClass())); List<ResourceResolver> resolvers = new ArrayList<>(); resolvers.add(versionResolver); resolvers.add(pathResolver); this.filter = new ResourceUrlEncodingFilter(); this.urlProvider = createResourceUrlProvider(resolvers); }
@Test public void testStaticAttributesCarriedAcross() throws Exception { XsltView view = getXsltView(HTML_OUTPUT); view.setSourceKey("actualData"); view.addStaticAttribute("title", "Product List"); 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()); assertTrue(this.response.getContentAsString().contains("Product List")); }
@Before public void setup() throws Exception { this.beanFactory.registerScope("session", new SessionScope()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.loadBeanDefinitions(new ClassPathResource("sessionScopeTests.xml", getClass())); }
@Test public void defaultLazyInit() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions( new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass())); assertLazyInits(bf); }
@Before public void setUp() throws Exception { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.setEventListener(this.eventListener); reader.setSourceExtractor(new PassThroughSourceExtractor()); reader.loadBeanDefinitions(new ClassPathResource("beanEvents.xml", getClass())); }
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfiles) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); StandardEnvironment env = new StandardEnvironment(); env.setActiveProfiles(activeProfiles); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass())); return beanFactory; }
@Test public void resolveResourceExisting() { Resource expected = mock(Resource.class); this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/2.3/foo.txt"; given(this.chain.resolveResource(this.request, file, 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); }
@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); }
@Test public void testFactoryMethodForJavaMailSession() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); MailSession session = (MailSession) xbf.getBean("javaMailSession"); assertEquals("someuser", session.getProperty("mail.smtp.user")); assertEquals("somepw", session.getProperty("mail.smtp.password")); } }
@Test public void resolveUrlExistingNotInJarFile() { this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", this.locations); }
@Test public void testSerializableSingletonProxyFactoryBean() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS)); Person p = (Person) bf.getBean("serializableSingleton"); ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton"); ProxyFactoryBean pfb2 = (ProxyFactoryBean) SerializationTestUtils.serializeAndDeserialize(pfb); Person p2 = (Person) pfb2.getObject(); assertEquals(p, p2); assertNotSame(p, p2); assertEquals("serializableSingleton", p2.getName()); }